A Go client library for the Mistral AI API.
go get github.com/ua1984/mistralpackage main
import (
"context"
"fmt"
"log"
"os"
"github.com/ua1984/mistral"
)
func main() {
client := mistral.NewClient(os.Getenv("MISTRAL_API_KEY"))
resp, err := client.CreateChatCompletion(context.Background(), &mistral.ChatCompletionRequest{
Model: "mistral-large-latest",
Messages: []mistral.ChatMessage{
{
Role: mistral.RoleUser,
Content: "What is the capital of France?",
},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}- Chat Completions: Create chat completions with support for streaming
- Embeddings: Generate embeddings for text inputs
- File Management: Upload, download, list, and delete files
- Model Management: List and retrieve model information
- Streaming Support: Real-time streaming responses for chat completions
- Context Support: Full
context.Contextsupport for all API calls - Custom Configuration: Configurable base URL, HTTP client, and timeouts
client := mistral.NewClient(os.Getenv("MISTRAL_API_KEY"))
resp, err := client.CreateChatCompletion(context.Background(), &mistral.ChatCompletionRequest{
Model: "mistral-large-latest",
Messages: []mistral.ChatMessage{
{
Role: mistral.RoleUser,
Content: "What is the capital of France?",
},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Choices[0].Message.Content)client := mistral.NewClient(os.Getenv("MISTRAL_API_KEY"))
ctx := context.Background()
respChan, errChan := client.CreateChatCompletionStream(ctx, &mistral.ChatCompletionRequest{
Model: "mistral-large-latest",
Messages: []mistral.ChatMessage{
{
Role: mistral.RoleUser,
Content: "Tell me a short story",
},
},
})
for {
select {
case chunk, ok := <-respChan:
if !ok {
return
}
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta != nil {
fmt.Print(chunk.Choices[0].Delta.Content)
}
case err := <-errChan:
if err != nil {
log.Fatal(err)
}
return
case <-ctx.Done():
return
}
}client := mistral.NewClient(os.Getenv("MISTRAL_API_KEY"))
resp, err := client.CreateEmbedding(context.Background(), &mistral.EmbeddingRequest{
Model: "mistral-embed",
Input: []string{
"Hello, world!",
"How are you?",
},
})
if err != nil {
log.Fatal(err)
}
for i, emb := range resp.Data {
fmt.Printf("Embedding %d has %d dimensions\n", i, len(emb.Embedding))
}client := mistral.NewClient(os.Getenv("MISTRAL_API_KEY"))
file, err := os.Open("training_data.jsonl")
if err != nil {
log.Fatal(err)
}
defer file.Close()
uploadedFile, err := client.UploadFile(context.Background(), &mistral.UploadFileRequest{
File: file,
Filename: "training_data.jsonl",
Purpose: mistral.FilePurposeFineTune,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Uploaded file ID: %s\n", uploadedFile.ID)client := mistral.NewClient(os.Getenv("MISTRAL_API_KEY"))
models, err := client.ListModels(context.Background())
if err != nil {
log.Fatal(err)
}
for _, model := range models.Data {
fmt.Printf("Model: %s\n", model.ID)
}The client supports various configuration options:
client := mistral.NewClient(
"your-api-key",
mistral.WithBaseURL("https://custom.api.url"),
mistral.WithTimeout(120 * time.Second),
mistral.WithHTTPClient(customHTTPClient),
)WithBaseURL(url string): Set a custom base URL for the APIWithTimeout(timeout time.Duration): Set the HTTP client timeoutWithHTTPClient(client *http.Client): Use a custom HTTP client
CreateChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)CreateChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamResponse, <-chan error)
CreateEmbedding(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
UploadFile(ctx context.Context, req *UploadFileRequest) (*File, error)ListFiles(ctx context.Context, params *ListFilesParams) (*FileList, error)GetFile(ctx context.Context, fileID string) (*File, error)DeleteFile(ctx context.Context, fileID string) (*DeleteFileResponse, error)DownloadFile(ctx context.Context, fileID string) (io.ReadCloser, error)
ListModels(ctx context.Context) (*ModelList, error)GetModel(ctx context.Context, modelID string) (*Model, error)DeleteModel(ctx context.Context, modelID string) (*DeleteModelResponse, error)
- Go 1.18 or later
See LICENSE file for details.