Documentation
¶
Overview ¶
Package mistral is a client for the Mistral API, built on the goloop/ai interface.
The Client implements ai.Client, so Generate and Stream work the same as with any other goloop AI provider. On top of that it exposes the native chat completions endpoint with its full options, embeddings, fill-in-the- middle completions (FIM, for Codestral models) and model listing. The wire format is chat-completions compatible.
c := mistral.New(os.Getenv("MISTRAL_API_KEY"))
resp, err := c.Generate(ctx, &ai.Request{
Model: mistral.ModelLargeLatest,
Messages: []ai.Message{ai.UserText("Say hello in one word.")},
})
Structured output ¶
ai.Request.Format maps onto the provider's response_format, so a request for JSON is enforced rather than merely asked for, and ai.Response.JSON decodes the reply. Plain JSON mode also puts ai.Format.Instruction into the system prompt, because this wire format rejects json_object unless the word "json" appears in the messages.
Hosted capabilities ¶
This provider's hosted search belongs to its conversations and agents endpoints, not to the chat endpoint this package speaks: a chat reply carries no references, and references are what make a searched answer checkable. ai.Hosted is refused with ai.ErrNoHosted.
The refusal is the documented behavior, not a gap waiting to be filled in silence: an answer produced without the search that was asked for looks exactly like one produced with it. A caller who would rather have the answer anyway asks again without ai.Request.Hosted.
Asking what this driver can do ¶
Capabilities describes this driver for the decision taken before a call: whether to offer a feature at all, and whether it needs one request or two.
if ai.SupportsHosted(c, ai.Hosted{Kind: ai.HostedWebSearch}) { ... }
It is a hint and not a permission - support also depends on the model, the account and the region - so ai.ErrNoHosted and ai.ErrNoFormat remain the source of truth and a caller still handles them. What changes is that a refusal the provider only reports as a 400 now arrives as those same sentinels, wrapped around the original ai.APIError, so one errors.Is covers a limitation this driver knew in advance and one it learned over the wire.
It depends only on goloop/ai and the standard library.
Index ¶
- Constants
- type ChatChoice
- type ChatFunctionCall
- type ChatFunctionDef
- type ChatMessage
- type ChatRequest
- type ChatResponse
- type ChatStreamChunk
- type ChatTool
- type ChatToolCall
- type ChatUsage
- type Client
- func (c *Client) Capabilities() ai.Capabilities
- func (c *Client) ChatCompletion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
- func (c *Client) ChatCompletionStream(ctx context.Context, req *ChatRequest) iter.Seq2[ChatStreamChunk, error]
- func (c *Client) Embed(ctx context.Context, model string, input ...string) ([][]float64, error)
- func (c *Client) Embeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
- func (c *Client) FIM(ctx context.Context, req *FIMRequest) (*ChatResponse, error)
- func (c *Client) Generate(ctx context.Context, req *ai.Request) (*ai.Response, error)
- func (c *Client) GetModel(ctx context.Context, id string) (*Model, error)
- func (c *Client) Models(ctx context.Context) ([]Model, error)
- func (c *Client) Stream(ctx context.Context, req *ai.Request) iter.Seq2[ai.Chunk, error]
- type Embedding
- type EmbeddingRequest
- type EmbeddingResponse
- type FIMRequest
- type Model
- type Option
Examples ¶
Constants ¶
const ( ModelLargeLatest = "mistral-large-latest" ModelSmallLatest = "mistral-small-latest" ModelPixtral = "pixtral-12b-2409" ModelEmbed = "mistral-embed" ModelCodestral = "codestral-latest" )
Convenience model identifiers. Any model string is accepted; use Models to discover what the account can call.
const DefaultBaseURL = "https://api.mistral.ai/v1"
DefaultBaseURL is the Mistral API base URL, including the version segment.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChatChoice ¶
type ChatChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
ChatChoice is one completion choice.
type ChatFunctionCall ¶
ChatFunctionCall is the function name and JSON-encoded arguments of a call.
type ChatFunctionDef ¶
type ChatFunctionDef struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
}
ChatFunctionDef is a function's name, description and JSON Schema parameters.
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"`
Content any `json:"content,omitempty"`
Name string `json:"name,omitempty"`
ToolCalls []ChatToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
ChatMessage is one message in a chat request or response. Content is a string or a slice of content parts (for images).
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Tools []ChatTool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Stop []string `json:"stop,omitempty"`
N int `json:"n,omitempty"`
Seed *int `json:"seed,omitempty"`
ResponseFormat json.RawMessage `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
Stream bool `json:"stream,omitempty"`
StreamOptions *streamOptions `json:"stream_options,omitempty"`
}
ChatRequest is the native chat completions request, exposing the full option set (response_format, seed, n and so on). Build one directly for features the shared ai.Request does not model, or let Generate build it.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Model string `json:"model"`
Choices []ChatChoice `json:"choices"`
Usage ChatUsage `json:"usage"`
}
ChatResponse is the native chat completions response.
type ChatStreamChunk ¶
type ChatStreamChunk struct {
ID string `json:"id"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Delta struct {
Content string `json:"content"`
ToolCalls []struct {
Index int `json:"index"`
ID string `json:"id"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
} `json:"tool_calls"`
} `json:"delta"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage *ChatUsage `json:"usage"`
}
ChatStreamChunk is one streamed chat completions event.
type ChatTool ¶
type ChatTool struct {
Type string `json:"type"`
Function ChatFunctionDef `json:"function"`
}
ChatTool declares a callable function.
type ChatToolCall ¶
type ChatToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ChatFunctionCall `json:"function"`
}
ChatToolCall is a tool call the model produced.
type ChatUsage ¶
type ChatUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
ChatUsage reports token usage for a chat completion.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Mistral API client. It implements ai.Client and adds the provider's native endpoints. The wire format is chat-completions compatible.
func New ¶
New returns a Client for the given API key. Shared options (WithBaseURL, WithHTTPClient, WithTimeout, WithMaxRetries, WithHeader) configure it.
Example ¶
package main
import (
"fmt"
"github.com/goloop/mistral"
)
func main() {
c := mistral.New("...")
_ = c // use c.Generate, c.Stream, c.ChatCompletion, ...
fmt.Println(mistral.ModelLargeLatest)
}
Output: mistral-large-latest
func (*Client) Capabilities ¶ added in v1.1.0
func (c *Client) Capabilities() ai.Capabilities
Capabilities describes what this driver can be asked for. It is a hint for the decision taken before a call - whether to offer a feature, and whether it needs one request or two - and never a substitute for handling ai.ErrNoHosted, ai.ErrNoFormat or ai.ErrFormatWithHosted, because support also depends on the model, the account and the region.
func (*Client) ChatCompletion ¶
func (c *Client) ChatCompletion(ctx context.Context, req *ChatRequest) (*ChatResponse, error)
ChatCompletion sends a native chat completions request and returns the whole response. Use it for provider-specific options; use Generate for the shared, provider-agnostic path.
func (*Client) ChatCompletionStream ¶
func (c *Client) ChatCompletionStream(ctx context.Context, req *ChatRequest) iter.Seq2[ChatStreamChunk, error]
ChatCompletionStream sends a native streaming chat request and returns an iterator over the raw chunks.
func (*Client) Embeddings ¶
func (c *Client) Embeddings( ctx context.Context, req *EmbeddingRequest, ) (*EmbeddingResponse, error)
Embeddings sends a native embeddings request.
func (*Client) FIM ¶
func (c *Client) FIM(ctx context.Context, req *FIMRequest) (*ChatResponse, error)
FIM sends a fill-in-the-middle completion request. The response reuses the chat completions shape; read the inserted text from the first choice's message content.
Example ¶
ExampleClient_FIM shows a fill-in-the-middle request. The model completes the gap between Prompt and Suffix; read the inserted text from the first choice.
package main
import (
"fmt"
"github.com/goloop/mistral"
)
func main() {
req := &mistral.FIMRequest{
Model: mistral.ModelCodestral,
Prompt: "def add(a, b):\n ",
Suffix: "\n return result",
}
fmt.Println(req.Model)
}
Output: codestral-latest
func (*Client) Generate ¶
Generate implements ai.Client over chat completions.
Example ¶
ExampleClient_Generate builds a request. Sending it needs a real API key, so this example only shows the shape.
package main
import (
"fmt"
"github.com/goloop/ai"
"github.com/goloop/mistral"
)
func main() {
req := &ai.Request{
Model: mistral.ModelLargeLatest,
Messages: []ai.Message{
ai.UserText("Name the capital of France."),
},
}
fmt.Println(req.Model, len(req.Messages))
}
Output: mistral-large-latest 1
type EmbeddingRequest ¶
EmbeddingRequest is the native embeddings request.
type EmbeddingResponse ¶
type EmbeddingResponse struct {
Model string `json:"model"`
Data []Embedding `json:"data"`
Usage ChatUsage `json:"usage"`
}
EmbeddingResponse is the native embeddings response.
type FIMRequest ¶
type FIMRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Suffix string `json:"suffix,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stop []string `json:"stop,omitempty"`
}
FIMRequest is a fill-in-the-middle completion request. The model (a Codestral model) completes the gap between Prompt and Suffix; leave Suffix empty for a plain prefix completion.
type Model ¶
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
Model describes a model returned by the models endpoint.
type Option ¶
type Option func(*settings)
Option configures a Client in New.
func WithBaseURL ¶
WithBaseURL overrides the API base URL (proxies, gateways, mock servers).
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used for requests.
func WithHeader ¶
WithHeader adds a header sent with every request.
func WithMaxRetries ¶
WithMaxRetries sets how many times a request is retried on 429 and 5xx.
func WithTimeout ¶
WithTimeout sets the per-request timeout when no custom HTTP client is set.