mistral

package module
v0.61.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package mistral wraps Mistral AI's API.

Mistral exposes:

  • /chat/completions — native structured content and thinking chunks, used via NewChat;
  • /embeddings — native output_dimension and float-vector projection, used via NewEmbeddingModel;
  • /moderations — Mistral-native shape that doesn't match OpenAI's moderation response; NewModerationModel handles it directly.

Additional Mistral surfaces not exposed here:

  • /agents (stateful agent runs);
  • /fim (code completion endpoint; it is not modeled by the chat facade).

Stream termination. A chunk carrying finish_reason is the only claim that the answer is whole; the closing [DONE] marker is not, and neither is a body that simply ends. A stream that stops without one fails with [chat.ErrInvalidResponse] rather than handing back the fragment it did deliver. finish_reason model_length is the model's own context filling rather than the caller's budget, and both report as a truncation. The native reason owns completion; Core receives it only on the last delta after the stream ends normally, so trailing usage is included and a tail failure cannot follow a published terminal delta. Content and Tool calls after the native finish_reason are invalid. Each stream binds the first reported response ID and model; later omissions preserve them, while conflicting values fail with [chat.ErrInvalidResponse].

Finish reasons. Mistral's own client types the field as stop, length, model_length, error or an unrecognized string. error and an unrecognized value both map to [chat.FinishReasonOther] — a known terminal state with no portable match — and the provider's own word for it rides along on the output, so an errored generation stays distinguishable from a provider limit.

See https://docs.mistral.ai/ for the full API reference. Core Options.ReasoningEffort is the only request control for reasoning intensity. Native request extensions reject that field and other unknown fields instead of accepting a second configuration source.

Reasoning replay. Each native thinking child has its own Core reasoning Part. Part.Text owns visible text; ReasoningState retains native fields, references, signatures, and block boundaries without text copies. Replay reconstructs the native thinking content from current Core parts. Previously stored full thinking chunks are rejected. Preserve signed content and native fields when replaying.

Index

Constants

View Source
const (
	// ModelMedium is the current frontier-class model with adjustable reasoning.
	ModelMedium = "mistral-medium-latest"

	// ModelSmall is the current hybrid instruct, reasoning, and coding model.
	ModelSmall = "mistral-small-2603"

	// ModelLarge is Mistral Large 3.
	ModelLarge = "mistral-large-2512"

	// ModelCodestral targets code generation and fill-in-the-middle.
	ModelCodestral = "codestral-latest"

	ModelMinistral3B = "ministral-3b-latest"
	ModelMinistral8B = "ministral-8b-latest"

	// ModelPixtralLarge remains the supported Pixtral Large model.
	ModelPixtralLarge = "pixtral-large-latest"
)

Current chat model ids. See https://docs.mistral.ai/models/.

View Source
const (
	// ModelEmbed (mistral-embed) is the general-purpose embedding
	// model. 1024-dim by default; pass [embedding.Options.Dimensions]
	// to truncate.
	ModelEmbed = "mistral-embed"

	// ModelCodestralEmbed is the code-tuned
	// embedding model.
	ModelCodestralEmbed = "codestral-embed-2505"
)
View Source
const (
	DefaultBaseURL = "https://api.mistral.ai/v1"
)

Exported defaults keep constructor behavior visible and overridable.

View Source
const (
	EmbeddingRequestExtensionKey = Provider + "/embedding_request"
)
View Source
const (
	// ModelModeration is the current moderation
	// classifier reachable via [NewModerationModel].
	ModelModeration = "mistral-moderation-2603"
)
View Source
const (
	Provider = "mistral"
)

Provider is the stable backend name for host-side attribution.

View Source
const (
	RequestExtensionKey = Provider + "/request"
)

Namespacing preserves provider-specific data without promoting it into the shared Core protocol or colliding with another provider.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chat

type Chat struct {
	// contains filtered or unexported fields
}

Chat implements Mistral's native Chat Completions protocol, including structured thinking chunks and their multi-turn replay semantics.

func NewChat

func NewChat(ctx context.Context, config ChatConfig) (*Chat, error)

func (*Chat) Call

func (c *Chat) Call(ctx context.Context, request *corechat.Request) (*corechat.Response, error)

func (*Chat) Stream

func (c *Chat) Stream(ctx context.Context, request *corechat.Request) iter.Seq2[*corechat.ResponseDelta, error]

type ChatConfig

type ChatConfig struct {
	APIKey         string
	DefaultOptions corechat.Options
	BaseURL        string
	HTTPClient     *http.Client
}

func (ChatConfig) Validate

func (c ChatConfig) Validate() error

type ChatRequestOptions

type ChatRequestOptions struct {
	RandomSeed     *int64 `json:"random_seed,omitzero"`
	SafePrompt     *bool  `json:"safe_prompt,omitzero"`
	PromptCacheKey string `json:"prompt_cache_key,omitempty"`
	// Metadata is an open JSON subtree; metadata.Map keeps each value as raw JSON so
	// an identifier beyond float64 precision survives the round trip to the wire.
	Metadata   metadata.Map      `json:"metadata,omitempty"`
	Guardrails []json.RawMessage `json:"guardrails,omitempty"`
}

ChatRequestOptions exposes Mistral-specific Chat Completions parameters that have no provider-neutral Core equivalent. Store it under RequestExtensionKey.

func (*ChatRequestOptions) UnmarshalJSON

func (c *ChatRequestOptions) UnmarshalJSON(data []byte) error

func (ChatRequestOptions) Validate

func (c ChatRequestOptions) Validate() error

type EmbeddingModel

type EmbeddingModel struct {
	// contains filtered or unexported fields
}

Mistral owns its embedding wire vocabulary: Core dimensions project to output_dimension, while the float representation stays fixed for Core vectors.

func NewEmbeddingModel

func NewEmbeddingModel(ctx context.Context, config EmbeddingModelConfig) (*EmbeddingModel, error)

func (*EmbeddingModel) Call added in v0.58.0

type EmbeddingModelConfig

type EmbeddingModelConfig struct {
	APIKey         string
	DefaultOptions embedding.Options
	BaseURL        string
	HTTPClient     *http.Client
}

func (EmbeddingModelConfig) Validate

func (e EmbeddingModelConfig) Validate() error

type EmbeddingRequestOptions added in v0.58.0

type EmbeddingRequestOptions struct {
	Metadata metadata.Map `json:"metadata,omitzero"`
}

func (*EmbeddingRequestOptions) UnmarshalJSON added in v0.58.0

func (e *EmbeddingRequestOptions) UnmarshalJSON(data []byte) error

type ModerationModel

type ModerationModel struct {
	// contains filtered or unexported fields
}

ModerationModel wraps Mistral's /moderations endpoint. Mistral reports a custom category set (sexual / hate_and_discrimination / violence_and_threats / dangerous_and_criminal_content / selfharm / health / financial / law / pii). Category names are preserved exactly.

func NewModerationModel

func NewModerationModel(ctx context.Context, config ModerationModelConfig) (*ModerationModel, error)

func (*ModerationModel) Call

type ModerationModelConfig

type ModerationModelConfig struct {
	APIKey         string
	DefaultOptions moderation.Options
	BaseURL        string
	HTTPClient     *http.Client
}

func (ModerationModelConfig) Validate

func (m ModerationModelConfig) Validate() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL