glm

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package glm implements llm.Client for Zhipu AI / z.ai GLM models.

GLM is reached over z.ai's Anthropic-compatible endpoint (https://api.z.ai/api/anthropic), so the wire format is identical to the Anthropic Messages API: the request/response shapes, content blocks, image blocks, thinking blocks, and SSE stream are the same as pkg/llm/claude. This package is a self-contained copy of that engine (per evva's one-package-per-provider convention) with three deltas:

  • auth header is "Authorization: Bearer <key>" (z.ai's ANTHROPIC_AUTH_TOKEN scheme) instead of Anthropic's "x-api-key";
  • the provider name / default model are GLM's;
  • effort is documented against GLM-5.2's two thinking-effort tiers, which z.ai derives from the same output_config.effort value.

Because the image-block path is copied verbatim, the read-file tool's image results (pkg/tools/fs/read.go) are forwarded to GLM as base64 image blocks with no extra wiring; image understanding requires a vision-capable GLM model.

Index

Constants

View Source
const (
	DefaultModel     = "glm-5.2"
	DefaultMaxTokens = 4096
)
View Source
const ProviderName = "glm"

ProviderName is the registry key under which this client registers. It matches the Name field of constant.GLM. Registered into pkg/llm.DefaultRegistry() by pkg/llm/builtins.

Variables

This section is empty.

Functions

func Factory

func Factory(cfg llm.APIConfig, model string, opts ...llm.Option) (llm.Client, error)

Factory adapts New into a llm.ClientFactory. Downstream apps that want to register GLM on a non-default registry can call this directly.

Types

type Client

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

Client implements llm.Client backed by z.ai's Anthropic-compatible Messages API.

func New

func New(cfg llm.APIConfig, model string, opts ...llm.Option) *Client

New builds a GLM client from provider config and applies the given options. Options can be re-applied at runtime via Apply.

func (*Client) Apply

func (c *Client) Apply(opts ...llm.Option)

Apply merges further options at runtime. Safe to call between completions.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, messages []llm.Message, toolSet []tools.Tool) (llm.Response, error)

func (*Client) Model

func (c *Client) Model() string

Model returns the model the client is currently bound to.

func (*Client) Name

func (c *Client) Name() string

Name provider name

func (*Client) SetModel

func (c *Client) SetModel(m string)

SetModel swaps the active model id.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, messages []llm.Message, toolSet []tools.Tool, sink llm.ChunkSink) (llm.Response, error)

Stream is the chunked variant of Complete. It opens an SSE connection to z.ai's Anthropic-compatible /v1/messages endpoint with "stream": true, parses each event, emits text/thinking deltas through sink as they arrive, and returns the fully assembled Response when the server emits message_stop.

The stream protocol is the Anthropic per-content-block shape:

  • message_start carries the request id, model, and initial usage stats.
  • content_block_start opens a block of type text | thinking | tool_use.
  • content_block_delta carries incremental data (text/thinking/signature/ input_json deltas).
  • content_block_stop closes the block.
  • message_delta updates the final usage.output_tokens.
  • message_stop is the terminator; ping events are keepalives we ignore; error events abort with the server's reason.

The thinking signature is opaque and never streamed to the chunk sink — it ships back verbatim on the next assistant turn (llm.Message.ThinkingSignature).

func (*Client) SupportsDeferLoading

func (c *Client) SupportsDeferLoading() bool

SupportsDeferLoading currently returns false; mirrors pkg/llm/claude.

Jump to

Keyboard shortcuts

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