a3s_code_core/llm/
zhipu.rs1use super::openai::OpenAiClient;
7use super::structured;
8use super::types::*;
9use super::{LlmClient, ModelGenerationPool};
10use crate::retry::RetryConfig;
11use anyhow::Result;
12use async_trait::async_trait;
13use tokio::sync::mpsc;
14use tokio_util::sync::CancellationToken;
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
20pub 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_logprobs(mut self, enabled: bool) -> Self {
44 self.0 = self.0.with_logprobs(enabled);
45 self
46 }
47
48 pub fn with_top_logprobs(mut self, top_logprobs: usize) -> Self {
49 self.0 = self.0.with_top_logprobs(top_logprobs);
50 self
51 }
52
53 pub fn with_base_url(mut self, base_url: String) -> Self {
54 self.0 = self.0.with_base_url(base_url);
55 self
56 }
57
58 pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
59 self.0 = self.0.with_retry_config(retry_config);
60 self
61 }
62
63 pub fn with_http_client(mut self, http: Arc<dyn HttpClient>) -> Self {
64 self.0 = self.0.with_http_client(http);
65 self
66 }
67}
68
69#[async_trait]
70impl LlmClient for ZhipuClient {
71 fn model_generation_pool(&self) -> Option<ModelGenerationPool> {
72 self.0.model_generation_pool()
73 }
74
75 async fn complete(
76 &self,
77 messages: &[Message],
78 system: Option<&str>,
79 tools: &[ToolDefinition],
80 ) -> Result<LlmResponse> {
81 self.0.complete(messages, system, tools).await
82 }
83
84 async fn complete_streaming(
85 &self,
86 messages: &[Message],
87 system: Option<&str>,
88 tools: &[ToolDefinition],
89 cancel_token: CancellationToken,
90 ) -> Result<mpsc::Receiver<StreamEvent>> {
91 self.0
92 .complete_streaming(messages, system, tools, cancel_token)
93 .await
94 }
95
96 fn native_structured_support(&self) -> structured::NativeStructuredSupport {
97 self.0.native_structured_support()
98 }
99
100 fn has_distinct_non_streaming_transport(&self) -> bool {
101 self.0.has_distinct_non_streaming_transport()
102 }
103
104 async fn complete_structured(
105 &self,
106 messages: &[Message],
107 system: Option<&str>,
108 tools: &[ToolDefinition],
109 directive: &structured::StructuredDirective,
110 ) -> Result<LlmResponse> {
111 self.0
112 .complete_structured(messages, system, tools, directive)
113 .await
114 }
115
116 async fn complete_streaming_structured(
117 &self,
118 messages: &[Message],
119 system: Option<&str>,
120 tools: &[ToolDefinition],
121 directive: &structured::StructuredDirective,
122 cancel_token: CancellationToken,
123 ) -> Result<mpsc::Receiver<StreamEvent>> {
124 self.0
125 .complete_streaming_structured(messages, system, tools, directive, cancel_token)
126 .await
127 }
128}