GLMProvider#
Connect a provider and configure retries, credentials, and transport. See the guide.
- class zett_agent.providers.glm.GLMProvider(model, api_key, transport=None, *, base_url=None, temperature=None, retry=RetryOptions(base_delay=0.25, max_delay=8.0, max_retries=3), timeout=TimeoutOptions(connect=5.0, first_byte=60.0, idle=600.0), clear_thinking=False, tool_stream=False, send_prompt_cache_key=False, extra_headers=None, extra_query=None, extra_body=None)[源代码]#
Stream GLM through its OpenAI-compatible protocol.
- 参数:
model (str) -- GLM model identifier, such as
glm-5.1orglm-4.6.api_key (str) -- Z.ai API key.
transport (httpx.AsyncBaseTransport | None) -- Optional HTTPX transport for custom routing or tests.
base_url (str | None) -- API root; None uses
https://api.z.ai/api/paas/v4. Accounts on the China platform usehttps://open.bigmodel.cn/api/paas/v4.temperature (float | None) -- Optional sampling temperature.
retry (RetryOptions) -- Model-owned retry/backoff policy; never restarts an emitted stream.
timeout (TimeoutOptions) -- How long the provider may stay silent before the request is given up on. See
TimeoutOptions.clear_thinking (bool) -- Let the server drop the reasoning of earlier turns. Off by default: an agent that calls tools is continuing one line of thought, and the server is sent the earlier reasoning in full to use it. Turn it on to spend fewer tokens on a plain chat.
tool_stream (bool) -- Stream the arguments of a tool call as they are generated. Only the GLM-4.6 series and later support it.
send_prompt_cache_key (bool) -- Declare the run's prompt cache key on every request. Off by default: the GLM reference does not document the field.
extra_headers (Mapping[str, str] | None) -- HTTP headers sent on every request after the adapter's own.
extra_query (Mapping[str, str] | None) -- URL query parameters sent on every request.
extra_body (Mapping[str, JsonValue] | None) -- JSON fields merged into every request body last, so they add an option the adapter does not model and may override one it does, such as
thinking.
备注
thinkingis sent only for GLM-4.6 and later,reasoning_effortonly for GLM-5.2 and later; an older model gets neither. GLM-5.3 always thinks, so anOFFeffort is not sent to it. The levels GLM takes arelow,highandmax; the others map to the nearest.示例
Stream a GLM model:
model = GLMProvider(model="glm-5.1", api_key=os.environ["ZAI_API_KEY"]) try: client = await create_agent(model) print((await client.run("Explain this function")).content) finally: await model.aclose()
- provider_name = 'glm'#
Name stored on messages this provider produced and compared before reasoning is replayed.
- request_fields(request)[源代码]#
Switch thinking on or off, give the effort to the models that take one, and stream tools.
- 参数:
request (ModelRequest)
- 返回类型:
dict[str, JsonValue]
- async aclose()#
Close the SDK's owned HTTP connection pool.
- 返回类型:
None
- chat_stop(finish_reason, *, has_tool_calls, refusal_text, stream_error, native_finish_reason)#
Say why the response ended. Override it for a service with finish reasons of its own.
- merge_replay(blocks, fragment)#
Add one streamed opaque block (
ChatDelta.replay) to the ones collected so far.The default appends. A service that streams one block in pieces overrides this to join them, and
blocksis what finally becomesreplay_blocks.
- native_deferred_tools = False#
See
zett_agent.model.NativeDeferredToolsModel.native_deferred_tools. A provider opts in by overriding this on its class; the default keeps Responses, Chat Completions, Google, and Ollama on the withhold-and-search path.
- read_delta(delta)#
Read one streamed
delta.The default reads
content,refusal,tool_calls, and the first ofreasoning_keysthat has text. A service with fields of its own returns aChatDeltathat also carries them, usually by extendingsuper().read_delta.
- read_usage(usage)#
Read the
usageobject of a chunk.The default accepts every spelling of the cache and reasoning counters that the compatible services use.
- 参数:
usage (Mapping[str, JsonValue])
- 返回类型:
- reasoning_keys = ('reasoning_content', 'reasoning')#
Fields of a streamed delta that carry reasoning text, in order of preference. Services differ on the name (
reasoning_contentandreasoningare the two in use), and reading both is harmless because only the first one that has text is used.
- replay_reasoning(message, payload)#
Add what this service needs of an earlier assistant turn to its rendered
payload.The default returns the reasoning as
reasoning_content, which reasoning services expect to see again on later requests. A message from another provider is skipped, because reasoning from one model is not context for another.- 参数:
message (AssistantMessage)
payload (dict[str, JsonValue])
- 返回类型:
None
- response = False#
Whether this adapter uses a Responses-style endpoint instead of its provider's traditional message or chat-completions endpoint. Only the providers read it; the runtime does not.
- status_error(error)#
Turn an HTTP failure into the typed error the runtime reports.
The default reports a refusal the service sent as an HTTP error, a rejected credential, and everything else as
ProviderHTTPError. A service that explains failures in a body of its own overrides this and falls back tosuper().- 参数:
error (APIStatusError)
- 返回类型:
- sync(*, runtime=None)#
Return a context-managed synchronous view; optionally share a runtime.
Omitting runtime creates a new background thread for this view. Passing an existing runtime reuses its loop and leaves its shutdown to the caller. This API is for service entry points, not Extension lifecycle hooks.
示例
Use a model or tool from a plain function:
with read_file.sync() as read: result = read({"path": "README.md"})
- 参数:
runtime (SyncRuntime | None)
- 返回类型:
SyncObject[Self]
- vendor_headers()#
Return the headers this service wants on every request.
They are sent before
extra_headers, so the application can still replace one.- 返回类型:
dict[str, str]