Skip to content

Commit 817c548

Browse files
committed
Add example, README, & default client constructor
1 parent 39947ae commit 817c548

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Mistral Go Client
2+
3+
The Mistral Go Client is a comprehensive Golang library designed to interface with the Mistral AI API, providing developers with a robust set of tools to integrate advanced AI-powered features into their applications. This client supports a variety of functionalities, including Chat Completions, Chat Completions Streaming, and Embeddings, allowing for seamless interaction with Mistral's powerful language models.
4+
5+
## Features
6+
7+
- **Chat Completions**: Generate conversational responses and complete dialogue prompts using Mistral's language models.
8+
- **Chat Completions Streaming**: Establish a real-time stream of chat completions, ideal for applications requiring continuous interaction.
9+
- **Embeddings**: Obtain numerical vector representations of text, enabling semantic search, clustering, and other machine learning applications.
10+
11+
## Getting Started
12+
13+
To begin using the Mistral Go Client in your project, ensure you have Go installed on your system. This client library is compatible with Go 1.20 and higher.
14+
15+
### Installation
16+
17+
To install the Mistral Go Client, run the following command:
18+
19+
```bash
20+
go get github.com/gage-technologies/mistral-go
21+
```
22+
23+
### Usage
24+
25+
To use the client in your Go application, you need to import the package and initialize a new client instance with your API key.
26+
27+
```go
28+
package main
29+
30+
import (
31+
"log"
32+
33+
"github.com/gage-technologies/mistral-go"
34+
)
35+
36+
func main() {
37+
// If api key is empty it will load from MISTRAL_API_KEY env var
38+
client := mistral.NewMistralClientDefault("your-api-key")
39+
40+
// Example: Using Chat Completions
41+
chatRes, err := client.Chat("mistral-tiny", []mistral.ChatMessage{{Content: "Hello, world!", Role: mistral.RoleUser}}, nil)
42+
if err != nil {
43+
log.Fatalf("Error getting chat completion: %v", err)
44+
}
45+
log.Printf("Chat completion: %+v\n", chatRes)
46+
47+
// Example: Using Chat Completions Stream
48+
chatResChan, err := client.ChatStream("mistral-tiny", []mistral.ChatMessage{{Content: "Hello, world!", Role: mistral.RoleUser}}, nil)
49+
if err != nil {
50+
log.Fatalf("Error getting chat completion stream: %v", err)
51+
}
52+
53+
for chatResChunk := range chatResChan {
54+
if chatResChunk.Error != nil {
55+
log.Fatalf("Error while streaming response: %v", chatResChunk.Error)
56+
}
57+
log.Printf("Chat completion stream part: %+v\n", chatResChunk)
58+
}
59+
60+
// Example: Using Embeddings
61+
embsRes, err := client.Embeddings("mistral-embed", []string{"Embed this sentence.", "As well as this one."})
62+
if err != nil {
63+
log.Fatalf("Error getting embeddings: %v", err)
64+
}
65+
66+
log.Printf("Embeddings response: %+v\n", embsRes)
67+
}
68+
```
69+
70+
## Documentation
71+
72+
For detailed documentation on the Mistral AI API and the available endpoints, please refer to the [Mistral AI API Documentation](https://docs.mistral.ai).
73+
74+
## Contributing
75+
76+
Contributions are welcome! If you would like to contribute to the project, please fork the repository and submit a pull request with your changes.
77+
78+
## License
79+
80+
The Mistral Go Client is open-sourced software licensed under the [MIT license](LICENSE).
81+
82+
## Support
83+
84+
If you encounter any issues or require assistance, please file an issue on the GitHub repository issue tracker.

chat_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package mistral
22

33
import (
44
"testing"
5-
"time"
65

76
"github.com/stretchr/testify/assert"
87
)
98

109
func TestChat(t *testing.T) {
11-
client := NewMistralClient("", "", 3, time.Second*10)
10+
client := NewMistralClientDefault("")
1211
params := DefaultChatRequestParams
1312
params.MaxTokens = 10
1413
params.Temperature = 0
@@ -32,7 +31,7 @@ func TestChat(t *testing.T) {
3231
}
3332

3433
func TestChatStream(t *testing.T) {
35-
client := NewMistralClient("", "", 3, time.Second*10)
34+
client := NewMistralClientDefault("")
3635
params := DefaultChatRequestParams
3736
params.MaxTokens = 50
3837
params.Temperature = 0

client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ func NewMistralClient(apiKey string, endpoint string, maxRetries int, timeout ti
5454
}
5555
}
5656

57+
func NewMistralClientDefault(apiKey string) *MistralClient {
58+
if apiKey == "" {
59+
apiKey = os.Getenv("MISTRAL_API_KEY")
60+
}
61+
62+
return NewMistralClient(apiKey, Endpoint, DefaultMaxRetries, DefaultTimeout)
63+
}
64+
5765
func (c *MistralClient) request(method string, jsonData map[string]interface{}, path string, stream bool, params map[string]string) (interface{}, error) {
5866
uri, err := url.Parse(c.endpoint)
5967
if err != nil {

embeddings_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package mistral
22

33
import (
44
"testing"
5-
"time"
65

76
"github.com/stretchr/testify/assert"
87
)
98

109
func TestEmbeddings(t *testing.T) {
11-
client := NewMistralClient("", "", 3, time.Second*10)
10+
client := NewMistralClientDefault("")
1211
res, err := client.Embeddings("mistral-embed", []string{"Embed this sentence.", "As well as this one."})
1312
assert.NoError(t, err)
1413
assert.NotNil(t, res)

examples/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/gage-technologies/mistral-go"
7+
)
8+
9+
func main() {
10+
// If api key is empty it will load from MISTRAL_API_KEY env var
11+
client := mistral.NewMistralClientDefault("your-api-key")
12+
13+
// Example: Using Chat Completions
14+
chatRes, err := client.Chat("mistral-tiny", []mistral.ChatMessage{{Content: "Hello, world!", Role: mistral.RoleUser}}, nil)
15+
if err != nil {
16+
log.Fatalf("Error getting chat completion: %v", err)
17+
}
18+
log.Printf("Chat completion: %+v\n", chatRes)
19+
20+
// Example: Using Chat Completions Stream
21+
chatResChan, err := client.ChatStream("mistral-tiny", []mistral.ChatMessage{{Content: "Hello, world!", Role: mistral.RoleUser}}, nil)
22+
if err != nil {
23+
log.Fatalf("Error getting chat completion stream: %v", err)
24+
}
25+
26+
for chatResChunk := range chatResChan {
27+
if chatResChunk.Error != nil {
28+
log.Fatalf("Error while streaming response: %v", chatResChunk.Error)
29+
}
30+
log.Printf("Chat completion stream part: %+v\n", chatResChunk)
31+
}
32+
33+
// Example: Using Embeddings
34+
embsRes, err := client.Embeddings("mistral-embed", []string{"Embed this sentence.", "As well as this one."})
35+
if err != nil {
36+
log.Fatalf("Error getting embeddings: %v", err)
37+
}
38+
39+
log.Printf("Embeddings response: %+v\n", embsRes)
40+
}

0 commit comments

Comments
 (0)