xai

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

xAI Plugin

This plugin provides Genkit support for xAI's OpenAI-compatible Grok models.

Setup

Set an xAI API key:

export XAI_API_KEY=<your-api-key>

The plugin uses https://api.x.ai/v1 by default. Set XAI_BASE_URL, or pass option.WithBaseURL through the plugin's Opts, to use another compatible endpoint.

import (
    "context"

    "github.com/firebase/genkit/go/ai"
    "github.com/firebase/genkit/go/genkit"
    "github.com/firebase/genkit/go/plugins/compat_oai/xai"
)

ctx := context.Background()
plugin := &xai.XAI{}
g := genkit.Init(ctx,
    genkit.WithPlugins(plugin),
    genkit.WithDefaultModel("xai/grok-4.5"),
)

response, err := genkit.Generate(ctx, g, ai.WithPrompt("Explain reinforcement learning."))

Grok's reasoning_content output is returned as Genkit reasoning parts and is available through response.Reasoning().

Models

Grok 4.6, Grok 4.5, Grok 4.3, the Grok 4.20 line, and the Grok Build coding model are registered. The catalog is not a ceiling: any model ID xAI serves resolves on demand, and the Models field describes or corrects any model, curated or not:

plugin := &xai.XAI{Models: map[string]ai.ModelOptions{
    "grok-5": {Label: "Grok 5", Supports: &compat_oai.Multimodal},
}}

The current model list is at https://docs.x.ai/docs/models, and the API reference is at https://docs.x.ai.

Config

Models take a typed xai.ChatConfig: the generation fields xAI accepts plus its own controls (reasoningEffort, serviceTier, promptCacheKey). xai.ModelRef carries the config with the model ID:

response, err := genkit.Generate(ctx, g,
    ai.WithModel(xai.ModelRef("grok-4.6", &xai.ChatConfig{
        ReasoningEffort: xai.ReasoningEffortHigh,
    })),
    ai.WithPrompt("Work through this step by step."),
)

reasoningEffort runs from none through xhigh. Which levels a model takes is xAI's to decide: none is documented for the chat completions endpoint and xhigh for Grok 4.6, so a level a model does not accept comes back as an error from xAI.

maxOutputTokens reaches xAI as max_completion_tokens, since xAI deprecated max_tokens, and topLogProbs accepts 0 through 8 rather than OpenAI's 20. Streamed generations report token usage, including reasoning tokens, the same way non-streamed ones do.

Every config also carries the settings Genkit owns: version pins the exact model version a request is served by, apiKey (settable only from Go code) serves one request with a different credential, and extra forwards request body fields the config does not declare, keyed by xAI's wire names.

Not supported

xAI's image, video, and voice models are not part of this plugin: it serves the chat completions endpoint only.

Search is not reachable through this plugin. xAI retired the chat completion endpoint's search_parameters (the API answers any request carrying it with 410 Gone), and its successors, the web_search and x_search server-side tools, are Responses API features, while chat completions accepts only function tools.

Two request fields are left out on purpose. n asks for several completion choices and bills for all of them while Genkit reads only the first, and deferred answers with a request ID to poll rather than a completion.

Live tests

Live tests are skipped unless XAI_API_KEY is set:

go test -race ./plugins/compat_oai/xai -run '^TestPluginLive$' -v -count=1

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) Init

func (x *XAI) Init(ctx context.Context) []api.Action

Init implements genkit.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) Name

func (x *XAI) Name() string

Name implements genkit.Plugin.

func (*XAI) ResolveAction

func (x *XAI) ResolveAction(atype api.ActionType, id string) api.Action

ResolveAction dynamically builds a model exposed by the xAI endpoint, described by the plugin's config schema and capabilities.

Jump to

Keyboard shortcuts

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