mistral is a Go client for the Mistral API. It implements the
github.com/goloop/ai interface, so it looks and works like every other goloop
AI provider, and exposes Mistral's native endpoints with their full options on
top.
- Chat completions:
Generatefor a single response,Streamfor token-by-token output throughiter.Seq2. - Tool use (function calling), multimodal image input and system prompts.
- Native
ChatCompletionandChatCompletionStreamwith the full option set. - Embeddings, fill-in-the-middle completions and model listing.
- Retries on 429 and 5xx with backoff; normalized, typed API errors.
- Depends only on
github.com/goloop/aiand the standard library. - Structured output:
ai.Formatmaps onto the provider'sresponse_format(JSON mode or a JSON Schema); read the reply withresp.JSON(&v). - Hosted capabilities:
ai.Request.Hostedis refused withai.ErrNoHosted, because this provider's search lives on another endpoint.
go get github.com/goloop/mistralpackage main
import (
"context"
"fmt"
"os"
"github.com/goloop/ai"
"github.com/goloop/mistral"
)
func main() {
c := mistral.New(os.Getenv("MISTRAL_API_KEY"))
resp, err := c.Generate(context.Background(), &ai.Request{
Model: mistral.ModelLargeLatest,
Messages: []ai.Message{ai.UserText("Say hello in one word.")},
})
if err != nil {
panic(err)
}
fmt.Println(resp.Text())
}for chunk, err := range c.Stream(ctx, req) {
if err != nil {
break
}
fmt.Print(chunk.Text)
}vecs, err := c.Embed(ctx, mistral.ModelEmbed, "hello", "world")Codestral models can complete the gap between a prefix and a suffix. Read the inserted code from the first choice's message content.
resp, err := c.FIM(ctx, &mistral.FIMRequest{
Model: mistral.ModelCodestral,
Prompt: "def add(a, b):\n ",
Suffix: "\n return result",
})Full reference: DOC.md (Ukrainian: DOC.UK.md).
See CONTRIBUTING.md.
MIT - see LICENSE.
Reject nil native chat and stream requests.