Documentation
¶
Overview ¶
Package xai provides a Genkit plugin for xAI's Grok models.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ModelRef ¶
func ModelRef(id string, config *ChatConfig) ai.ModelRef
ModelRef names a Grok model and carries the config to generate with, so the config is typed at the call site instead of an any the model checks at runtime. A nil config leaves the request's config unset.
ai.WithModel(xai.ModelRef("grok-4.3", &xai.ChatConfig{
ReasoningEffort: "high",
}))
id is the model ID, with or without the provider prefix.
Types ¶
type ChatConfig ¶
type ChatConfig struct {
compat_oai.RequestConfig
// Temperature controls the degree of randomness in token selection, from
// 0 to 2.
Temperature *float64 `` /* 153-byte string literal not displayed */
// TopP is the nucleus sampling threshold. xAI documents no range for it,
// so the schema declares none.
TopP *float64 `` /* 144-byte string literal not displayed */
// MaxOutputTokens is the maximum number of tokens to generate, sent as the
// API's max_completion_tokens; xAI deprecated max_tokens.
MaxOutputTokens int `` /* 159-byte string literal not displayed */
// StopSequences stop generation when produced by the model, up to four.
// Reasoning models do not support them.
StopSequences []string `` /* 183-byte string literal not displayed */
// FrequencyPenalty penalizes tokens by their frequency so far, from -2.0
// to 2.0. Reasoning models do not support it.
FrequencyPenalty *float64 `` /* 190-byte string literal not displayed */
// PresencePenalty penalizes tokens that have appeared at all, from -2.0 to
// 2.0. Reasoning models do not support it.
PresencePenalty *float64 `` /* 189-byte string literal not displayed */
// LogProbs requests log probabilities for the output tokens.
LogProbs *bool `json:"logProbs,omitempty" jsonschema_description:"Requests log probabilities for the output tokens."`
// TopLogProbs is how many of the most likely tokens to return log
// probabilities for at each position, from 0 to 8; it requires LogProbs.
TopLogProbs *int `` /* 203-byte string literal not displayed */
// Seed makes generation reproducible across calls when set.
Seed *int `json:"seed,omitempty" jsonschema_description:"Makes generation reproducible across calls when set, on a best-effort basis."`
// ReasoningEffort adjusts how hard a reasoning-capable Grok model thinks.
ReasoningEffort ReasoningEffort `` /* 243-byte string literal not displayed */
// ParallelToolCalls lets the model request several tool calls in one
// response, which it may do by default. Setting it false caps the model at
// one call per response. It applies to a request that carries tools.
ParallelToolCalls *bool `` /* 186-byte string literal not displayed */
// User identifies the end user a request is made for, which xAI uses to
// monitor and detect abuse.
User string `` /* 137-byte string literal not displayed */
// PromptCacheKey routes requests sharing a prompt prefix to the same
// backend, for best-effort prompt cache hits, and is sent as the API's
// prompt_cache_key. Hits come back as the response usage's
// CachedContentTokens.
PromptCacheKey string `` /* 187-byte string literal not displayed */
// ServiceTier selects how the request is scheduled and billed.
ServiceTier ServiceTier `` /* 195-byte string literal not displayed */
}
ChatConfig is the per-request config for Grok models: the common generation fields plus the xAI-specific controls. See https://docs.x.ai/docs/api-reference.
Two documented request fields are deliberately absent. n asks for several completion choices and bills for all of them, while Genkit reads only the first, so declaring it would only sell tokens that are then thrown away. deferred answers with a request ID to poll rather than a completion, which is not a shape this model action can return.
func (ChatConfig) ApplyToChatCompletion ¶
func (c ChatConfig) ApplyToChatCompletion(params *openai.ChatCompletionNewParams)
ApplyToChatCompletion implements compat_oai.ChatConfig: the generation fields land on their chat completion counterparts, reasoning effort on the SDK's reasoning_effort, and the xAI controls ride as extra request fields.
type ReasoningEffort ¶
type ReasoningEffort string
ReasoningEffort is how hard a reasoning-capable Grok model thinks before it answers.
xAI documents two sets of levels, because it documents two APIs. The chat completions reference this plugin serves lists ReasoningEffortNone through ReasoningEffortHigh with low the default; the model capability guide, which covers the Responses API, lists low through ReasoningEffortXHigh with high the default and no way to turn reasoning off. The schema enumerates the union of both sets; which subset a model takes is the model's to decide, so a declared level a model does not take is an error from xAI rather than one from here.
const ( // ReasoningEffortNone disables reasoning. Models documented only by the // capability guide cannot turn it off and reject this. ReasoningEffortNone ReasoningEffort = "none" // ReasoningEffortLow is fast reasoning, for latency-sensitive work and // simple tool calling. ReasoningEffortLow ReasoningEffort = "low" // ReasoningEffortMedium adds thinking for complex analysis and // long-context tasks. ReasoningEffortMedium ReasoningEffort = "medium" // ReasoningEffortHigh is deeper thinking, for hard problems and multi-step // logic. ReasoningEffortHigh ReasoningEffort = "high" // ReasoningEffortXHigh is the maximum depth, which xAI documents for // grok-4.6 alone. ReasoningEffortXHigh ReasoningEffort = "xhigh" )
type ServiceTier ¶
type ServiceTier string
ServiceTier selects how a request is scheduled and billed.
xAI takes two of the tiers the OpenAI SDK models, so it is declared here rather than reused from openai.ChatCompletionNewParamsServiceTier: a config advertising that type would offer "auto", "flex", and "scale", which xAI rejects.
const ( // ServiceTierDefault is standard scheduling and billing. ServiceTierDefault ServiceTier = "default" // ServiceTierPriority buys faster scheduling at a higher rate. ServiceTierPriority ServiceTier = "priority" )
type XAI ¶
type XAI struct {
// APIKey is the xAI API key. If empty, XAI_API_KEY is consulted.
APIKey string
// Opts contains additional OpenAI client request options, such as
// [option.WithBaseURL] for a different endpoint (XAI_BASE_URL works too).
// Options supplied here are applied after the plugin defaults, so they
// win on overlap.
Opts []option.RequestOption
// Models overrides what the plugin knows about a Grok model, keyed by
// model ID, bare or provider-prefixed. Every Grok model already works
// without an entry: known IDs carry curated capabilities and the rest take
// the Grok defaults. Supply an entry only to correct or extend what the
// plugin resolves, most often for a model released after this version of
// the plugin.
//
// &xai.XAI{Models: map[string]ai.ModelOptions{
// "grok-4.5": {Supports: &ai.ModelSupports{Multiturn: true, Tools: true}},
// }}
//
// Fields left at their zero value keep what the plugin resolves, so an
// entry can pin one capability without restating the label or the
// versions. Entries apply to the models Init registers as well as the
// ones [XAI.ListActions] advertises and [XAI.ResolveAction] builds,
// which is the way to describe a curated model differently: Init has
// already registered those and nothing can re-register them.
Models map[string]ai.ModelOptions
// contains filtered or unexported fields
}
XAI configures the xAI Grok plugin.
func (*XAI) ListActions ¶
func (x *XAI) ListActions(ctx context.Context) []api.ActionDesc
ListActions lists the models the configured xAI endpoint exposes, described by the plugin's config schema and capabilities.
func (*XAI) ResolveAction ¶
ResolveAction dynamically builds a model exposed by the xAI endpoint, described by the plugin's config schema and capabilities.