Skip to main content

a3s_code_core/llm/
zhipu.rs

1//! Zhipu AI (GLM) LLM client
2//!
3//! GLM uses an OpenAI-compatible API but with a different endpoint path.
4//! This client wraps `OpenAiClient` with the correct GLM defaults.
5
6use super::openai::OpenAiClient;
7use super::types::*;
8use super::LlmClient;
9use crate::retry::RetryConfig;
10use anyhow::Result;
11use async_trait::async_trait;
12use tokio::sync::mpsc;
13use tokio_util::sync::CancellationToken;
14#[cfg(test)]
15use {super::http::HttpClient, std::sync::Arc};
16
17const GLM_BASE_URL: &str = "https://open.bigmodel.cn";
18const GLM_CHAT_PATH: &str = "/api/paas/v4/chat/completions";
19
20/// Zhipu AI (GLM) client
21pub struct ZhipuClient(OpenAiClient);
22
23impl ZhipuClient {
24    pub fn new(api_key: String, model: String) -> Self {
25        Self(
26            OpenAiClient::new(api_key, model)
27                .with_provider_name("zhipu")
28                .with_base_url(GLM_BASE_URL.to_string())
29                .with_chat_completions_path(GLM_CHAT_PATH),
30        )
31    }
32
33    pub fn with_temperature(mut self, temperature: f32) -> Self {
34        self.0 = self.0.with_temperature(temperature);
35        self
36    }
37
38    pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
39        self.0 = self.0.with_max_tokens(max_tokens);
40        self
41    }
42
43    pub fn with_base_url(mut self, base_url: String) -> Self {
44        self.0 = self.0.with_base_url(base_url);
45        self
46    }
47
48    pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
49        self.0 = self.0.with_retry_config(retry_config);
50        self
51    }
52
53    #[cfg(test)]
54    pub fn with_http_client(mut self, http: Arc<dyn HttpClient>) -> Self {
55        self.0 = self.0.with_http_client(http);
56        self
57    }
58}
59
60#[async_trait]
61impl LlmClient for ZhipuClient {
62    async fn complete(
63        &self,
64        messages: &[Message],
65        system: Option<&str>,
66        tools: &[ToolDefinition],
67    ) -> Result<LlmResponse> {
68        self.0.complete(messages, system, tools).await
69    }
70
71    async fn complete_streaming(
72        &self,
73        messages: &[Message],
74        system: Option<&str>,
75        tools: &[ToolDefinition],
76        cancel_token: CancellationToken,
77    ) -> Result<mpsc::Receiver<StreamEvent>> {
78        self.0
79            .complete_streaming(messages, system, tools, cancel_token)
80            .await
81    }
82}