zai

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Z.ai Plugin

This plugin provides Genkit support for Z.ai's OpenAI-compatible GLM text and vision models.

Setup

Set a Z.ai API key:

export ZAI_API_KEY=<your-api-key>

The plugin uses https://api.z.ai/api/paas/v4 by default. Set ZAI_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/zai"
)

ctx := context.Background()
plugin := &zai.ZAI{}
g := genkit.Init(ctx,
    genkit.WithPlugins(plugin),
    genkit.WithDefaultModel("zai/glm-5.1"),
)

response, err := genkit.Generate(ctx, g, ai.WithPrompt("Explain mixture-of-experts models."))

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

Models

The registered catalog spans the GLM text line (glm-5.1, glm-5-turbo, glm-5, the glm-4.7 and glm-4.5 families) and the GLM vision line (glm-5v-turbo, glm-4.6v, glm-4.5v and variants). The catalog is not a ceiling: any model ID Z.ai serves resolves on demand, and the Models field describes or corrects any model, curated or not:

plugin := &zai.ZAI{Models: map[string]ai.ModelOptions{
    "glm-6": {Label: "GLM 6", Supports: &compat_oai.Multimodal},
}}

Z.ai's API documentation is at https://docs.z.ai, and the current model list is at https://docs.z.ai/guides/overview/pricing.

Config

Models take a typed zai.ChatConfig: the generation fields Z.ai accepts plus its own controls (thinking, doSample). zai.ModelRef carries the config with the model ID:

response, err := genkit.Generate(ctx, g,
    ai.WithModel(zai.ModelRef("glm-5.1", &zai.ChatConfig{
        Thinking: &zai.ThinkingConfig{Type: zai.ThinkingTypeDisabled},
    })),
    ai.WithPrompt("Answer concisely."),
)

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 Z.ai's wire names (for example user_id).

Tool choice

Z.ai currently documents only automatic tool choice. Tool calling is supported, but forced required and none modes are not advertised by this plugin.

Live tests

Live tests are skipped unless ZAI_API_KEY is set:

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

Documentation

Overview

Package zai provides a Genkit plugin for Z.ai's GLM 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 GLM 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(zai.ModelRef("glm-5", &zai.ChatConfig{
	Thinking: &zai.ThinkingConfig{Type: "enabled"},
}))

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 1; the default varies by model.
	Temperature *float64 `` /* 182-byte string literal not displayed */
	// TopP is the nucleus sampling threshold, from 0.01 to 1.
	TopP *float64 `` /* 126-byte string literal not displayed */
	// MaxOutputTokens is the maximum number of tokens to generate, sent as the
	// API's max_tokens; Z.ai documents up to 131072.
	MaxOutputTokens int `` /* 181-byte string literal not displayed */
	// StopSequences stop generation when produced by the model, up to four.
	StopSequences []string `` /* 145-byte string literal not displayed */
	// Thinking controls the chain-of-thought mode of GLM 4.5 and later
	// models, sent as the API's thinking field.
	Thinking *ThinkingConfig `` /* 140-byte string literal not displayed */
	// DoSample turns sampling off when set to false, making temperature and
	// TopP inert; sent as the API's do_sample.
	DoSample *bool `` /* 145-byte string literal not displayed */
}

ChatConfig is the per-request config for GLM models: the generation fields Z.ai accepts plus the Z.ai-specific controls. See https://docs.z.ai/api-reference/llm/chat-completion.

Z.ai documents no penalties, log probabilities, or seed, so those are deliberately absent, and its temperature range stops at 1 rather than the 2 OpenAI allows.

func (ChatConfig) ApplyToChatCompletion

func (c ChatConfig) ApplyToChatCompletion(params *openai.ChatCompletionNewParams)

ApplyToChatCompletion implements compat_oai.ChatConfig: the generation fields land on their chat completion counterparts and the Z.ai controls ride as extra request fields.

type ThinkingConfig

type ThinkingConfig struct {
	// Type turns thinking [ThinkingTypeEnabled] (the default) or
	// [ThinkingTypeDisabled].
	Type ThinkingType `` /* 136-byte string literal not displayed */
	// ClearThinking controls whether the reasoning content is cleared from
	// the response, sent as the API's clear_thinking; Z.ai defaults it to
	// true.
	ClearThinking *bool `` /* 167-byte string literal not displayed */
}

ThinkingConfig configures the chain-of-thought mode of GLM models.

type ThinkingType

type ThinkingType string

ThinkingType turns the chain-of-thought of GLM models on or off.

const (
	// ThinkingTypeEnabled turns thinking on, which is Z.ai's default.
	ThinkingTypeEnabled ThinkingType = "enabled"
	// ThinkingTypeDisabled turns thinking off.
	ThinkingTypeDisabled ThinkingType = "disabled"
)

type ZAI

type ZAI struct {
	// APIKey is the Z.ai API key. If empty, ZAI_API_KEY is consulted.
	APIKey string
	// Opts contains additional OpenAI client request options, such as
	// [option.WithBaseURL] for a different endpoint (ZAI_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 GLM model, keyed by
	// model ID, bare or provider-prefixed. Every GLM model already works
	// without an entry: known IDs carry curated capabilities and the rest take
	// the GLM 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.
	//
	//	&zai.ZAI{Models: map[string]ai.ModelOptions{
	//		"glm-5.1": {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 [ZAI.ListActions] advertises and [ZAI.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
}

ZAI configures the Z.ai GLM plugin.

func (*ZAI) Init

func (z *ZAI) Init(ctx context.Context) []api.Action

Init implements genkit.Plugin.

func (*ZAI) ListActions

func (z *ZAI) ListActions(ctx context.Context) []api.ActionDesc

ListActions lists the models the configured Z.ai endpoint exposes, described by the plugin's config schema and capabilities.

func (*ZAI) Name

func (z *ZAI) Name() string

Name implements genkit.Plugin.

func (*ZAI) ResolveAction

func (z *ZAI) ResolveAction(atype api.ActionType, id string) api.Action

ResolveAction dynamically builds a model exposed by the Z.ai 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