lmkit 0.1.0

Multi-provider AI API client (OpenAI, Anthropic, Google Gemini, Aliyun, Ollama, Zhipu; chat, embed incl. Gemini, rerank, image, audio stubs)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! **Google Gemini**(Generative Language API):`POST …/models/{model}:generateContent` / `streamGenerateContent`,`key` query 鉴权。
//!
//! 多轮、`systemInstruction`、`tools`(`functionDeclarations`)与 `functionCall` / `functionResponse` 映射见实现与 [Gemini API](https://ai.google.dev/api/rest)。

use async_trait::async_trait;
use futures::future::ready;
use futures::StreamExt;
use serde_json::{json, Value};
use std::time::Duration;

use crate::client::HttpClient;
use crate::config::ProviderConfig;
use crate::error::{Error, Result};
use crate::sse::SseEvent;

use super::{
    ChatChunk, ChatMessage, ChatProvider, ChatRequest, ChatResponse, ChatStream, FinishReason,
    FunctionCallResult, Role, ToolCall, ToolCallDelta, ToolChoice,
};

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
const DEFAULT_TEMPERATURE: f32 = 0.2;

pub(crate) struct GoogleGeminiChat {
    client: HttpClient,
    api_key: String,
    model: String,
    base_url: String,
}

impl GoogleGeminiChat {
    pub fn new(config: &ProviderConfig) -> Result<Self> {
        let timeout = config.timeout.unwrap_or(DEFAULT_TIMEOUT);
        let client = HttpClient::new(timeout)?;
        Ok(Self {
            client,
            api_key: config.api_key.clone(),
            model: config.model.clone(),
            base_url: config.base_url.clone(),
        })
    }

    fn build_request_json(&self, request: &ChatRequest) -> Result<Value> {
        let (system_instruction, contents) = gemini_contents_from_chat(request)?;
        let mut generation_config = json!({
            "temperature": request.temperature.unwrap_or(DEFAULT_TEMPERATURE),
        });
        if let Some(m) = request.max_tokens {
            generation_config["maxOutputTokens"] = json!(m);
        }
        if let Some(p) = request.top_p {
            generation_config["topP"] = json!(p);
        }
        let mut body = json!({
            "contents": contents,
            "generationConfig": generation_config,
        });
        if let Some(si) = system_instruction {
            body["systemInstruction"] = si;
        }
        if let Some(tools) = request.tools.as_ref().filter(|t| !t.is_empty()) {
            let decls: Vec<Value> = tools
                .iter()
                .map(|t| {
                    json!({
                        "name": t.function.name,
                        "description": t.function.description,
                        "parameters": t.function.parameters.clone(),
                    })
                })
                .collect();
            body["tools"] = json!([{ "functionDeclarations": decls }]);
            if let Some(tc) = &request.tool_choice {
                body["toolConfig"] = json!({
                    "functionCallingConfig": gemini_function_calling_config(tc)
                });
            }
        }
        Ok(body)
    }
}

fn gemini_function_calling_config(tc: &ToolChoice) -> Value {
    match tc {
        ToolChoice::None => json!({ "mode": "NONE" }),
        ToolChoice::Auto => json!({ "mode": "AUTO" }),
        ToolChoice::Required => json!({ "mode": "ANY" }),
        ToolChoice::Tool(name) => json!({
            "mode": "ANY",
            "allowedFunctionNames": [name]
        }),
    }
}

fn gemini_contents_from_chat(request: &ChatRequest) -> Result<(Option<Value>, Vec<Value>)> {
    let mut system_parts: Vec<String> = Vec::new();
    let mut contents: Vec<Value> = Vec::new();
    for m in &request.messages {
        match m.role {
            Role::System => {
                if let Some(c) = &m.content {
                    if !c.is_empty() {
                        system_parts.push(c.clone());
                    }
                }
            }
            Role::User => {
                let text = m
                    .content
                    .clone()
                    .ok_or(Error::MissingField("user.content"))?;
                contents.push(json!({
                    "role": "user",
                    "parts": [{ "text": text }]
                }));
            }
            Role::Assistant => {
                let parts = gemini_model_parts(m)?;
                contents.push(json!({
                    "role": "model",
                    "parts": parts
                }));
            }
            Role::Tool => {
                let name = m
                    .name
                    .as_ref()
                    .filter(|s| !s.is_empty())
                    .cloned()
                    .ok_or(Error::MissingField("tool.name"))?;
                let response = tool_message_to_gemini_response(m)?;
                contents.push(json!({
                    "role": "user",
                    "parts": [{
                        "functionResponse": {
                            "name": name,
                            "response": response
                        }
                    }]
                }));
            }
        }
    }
    let system_instruction = if system_parts.is_empty() {
        None
    } else {
        Some(json!({
            "parts": [{ "text": system_parts.join("\n\n") }]
        }))
    };
    Ok((system_instruction, contents))
}

fn gemini_model_parts(m: &ChatMessage) -> Result<Vec<Value>> {
    let mut parts: Vec<Value> = Vec::new();
    if let Some(t) = &m.content {
        if !t.is_empty() {
            parts.push(json!({ "text": t }));
        }
    }
    if let Some(calls) = &m.tool_calls {
        for c in calls {
            let args: Value =
                serde_json::from_str(&c.function.arguments).unwrap_or_else(|_| json!({}));
            parts.push(json!({
                "functionCall": {
                    "name": c.function.name,
                    "args": args
                }
            }));
        }
    }
    if parts.is_empty() {
        parts.push(json!({ "text": "" }));
    }
    Ok(parts)
}

fn tool_message_to_gemini_response(m: &ChatMessage) -> Result<Value> {
    let raw = m.content.as_deref().unwrap_or("{}");
    serde_json::from_str(raw).or_else(|_| Ok(json!({ "result": raw })))
}

fn parse_gemini_generate_response(v: &Value) -> Result<ChatResponse> {
    let candidates = v
        .get("candidates")
        .and_then(|c| c.as_array())
        .ok_or(Error::MissingField("candidates"))?;
    if candidates.is_empty() {
        let hint = v
            .get("promptFeedback")
            .map(Value::to_string)
            .unwrap_or_else(|| "empty candidates, no promptFeedback".to_string());
        return Err(Error::Parse(format!(
            "Gemini generateContent returned no candidates (check promptFeedback): {hint}"
        )));
    }
    let c0 = &candidates[0];
    let mut text = String::new();
    let mut tool_calls: Vec<ToolCall> = Vec::new();
    if let Some(parts) = c0
        .get("content")
        .and_then(|x| x.get("parts"))
        .and_then(|p| p.as_array())
    {
        for (i, p) in parts.iter().enumerate() {
            if let Some(t) = p.get("text").and_then(|x| x.as_str()) {
                text.push_str(t);
            }
            if let Some(fc) = p.get("functionCall") {
                let name = fc
                    .get("name")
                    .and_then(|x| x.as_str())
                    .unwrap_or("")
                    .to_string();
                let args = fc.get("args").cloned().unwrap_or(json!({}));
                let arguments = serde_json::to_string(&args).unwrap_or_else(|_| "{}".to_string());
                // Gemini 响应无 OpenAI 式 tool_call id;用占位 id 便于在统一类型中承载多轮(下游可自管与 `functionResponse` 的对应关系)。
                tool_calls.push(ToolCall {
                    id: format!("gemini_fc_{i}"),
                    function: FunctionCallResult { name, arguments },
                });
            }
        }
    }
    let finish_reason = if !tool_calls.is_empty() {
        Some(FinishReason::ToolCalls)
    } else {
        c0.get("finishReason")
            .and_then(|f| f.as_str())
            .and_then(map_gemini_finish_reason)
    };
    Ok(ChatResponse {
        content: if text.is_empty() { None } else { Some(text) },
        tool_calls: if tool_calls.is_empty() {
            None
        } else {
            Some(tool_calls)
        },
        finish_reason,
    })
}

#[async_trait]
impl ChatProvider for GoogleGeminiChat {
    async fn complete(&self, request: &ChatRequest) -> Result<ChatResponse> {
        let body = self.build_request_json(request)?;
        let base = self.base_url.trim_end_matches('/');
        let url = format!("{}/models/{}:generateContent", base, self.model);
        let query = [("key", self.api_key.as_str())];
        let v: Value = self
            .client
            .post_json_query(&url, &query, &body, |s| s)
            .await?;
        parse_gemini_generate_response(&v)
    }

    async fn complete_stream(&self, request: &ChatRequest) -> Result<ChatStream> {
        let body = self.build_request_json(request)?;
        let base = self.base_url.trim_end_matches('/');
        let url = format!("{}/models/{}:streamGenerateContent", base, self.model);
        let query = [("key", self.api_key.as_str())];
        let sse = self
            .client
            .post_json_query_sse(&url, &query, &body, |s| s)
            .await?;
        Ok(Box::pin(
            sse.filter_map(|item| ready(google_sse_item_to_chunk(item))),
        ))
    }
}

fn google_sse_item_to_chunk(item: Result<SseEvent>) -> Option<Result<ChatChunk>> {
    match item {
        Err(e) => Some(Err(e)),
        Ok(ev) => google_parse_sse_event(ev),
    }
}

fn google_parse_sse_event(ev: SseEvent) -> Option<Result<ChatChunk>> {
    let data = ev.data.trim();
    if data.is_empty() {
        return None;
    }
    let v: Value = match serde_json::from_str(data) {
        Ok(v) => v,
        Err(e) => return Some(Err(Error::Parse(e.to_string()))),
    };

    let candidates = v.get("candidates").and_then(|c| c.as_array())?;

    if candidates.is_empty() {
        if let Some(pf) = v.get("promptFeedback") {
            let hint = pf.to_string();
            return Some(Err(Error::Parse(format!(
                "Gemini streamGenerateContent returned no candidates: {hint}"
            ))));
        }
        return None;
    }

    let c0 = &candidates[0];
    let mut text = String::new();
    let mut tool_deltas: Vec<ToolCallDelta> = Vec::new();
    if let Some(parts) = c0
        .get("content")
        .and_then(|x| x.get("parts"))
        .and_then(|p| p.as_array())
    {
        for (i, p) in parts.iter().enumerate() {
            if let Some(t) = p.get("text").and_then(|t| t.as_str()) {
                text.push_str(t);
            }
            if let Some(fc) = p.get("functionCall") {
                let name = fc.get("name").and_then(|x| x.as_str()).map(str::to_string);
                let args_str = fc
                    .get("args")
                    .map(|a| serde_json::to_string(a).unwrap_or_else(|_| "{}".to_string()));
                // 流式单条事件常含完整 `functionCall`;`id` 仍为 None(与 OpenAI 流式 index 语义不同,见模块文档)。
                tool_deltas.push(ToolCallDelta {
                    index: i as u32,
                    id: None,
                    function_name: name,
                    function_arguments: args_str,
                });
            }
        }
    }

    let finish = c0
        .get("finishReason")
        .and_then(|f| f.as_str())
        .and_then(map_gemini_finish_reason);

    // 与非流式 `parse_gemini_generate_response` 一致:含 `functionCall` 且本帧已带结束信号时统一为 `ToolCalls`
    //(Gemini 常在此时仍给 `finishReason: STOP`)。
    let finish_reason = if !tool_deltas.is_empty() {
        finish.map(|_| FinishReason::ToolCalls)
    } else {
        finish
    };

    if text.is_empty() && tool_deltas.is_empty() && finish_reason.is_none() {
        return None;
    }

    let delta = if text.is_empty() { None } else { Some(text) };
    let tool_call_deltas = if tool_deltas.is_empty() {
        None
    } else {
        Some(tool_deltas)
    };

    Some(Ok(ChatChunk {
        delta,
        tool_call_deltas,
        finish_reason,
    }))
}

fn map_gemini_finish_reason(s: &str) -> Option<FinishReason> {
    match s {
        "STOP" | "FINISH_REASON_STOP" => Some(FinishReason::Stop),
        "MAX_TOKENS" | "FINISH_REASON_MAX_TOKENS" => Some(FinishReason::Length),
        "SAFETY" | "RECITATION" | "OTHER" => Some(FinishReason::ContentFilter),
        _ => None,
    }
}

#[cfg(test)]
mod json_shape_tests {
    use super::*;
    use crate::chat::{ToolChoice, ToolDefinition};
    use crate::config::{Provider, ProviderConfig};

    #[test]
    fn build_request_json_includes_tool_config_for_tool_choice() {
        let cfg = ProviderConfig::new(
            Provider::Google,
            "k",
            "https://example.invalid/v1beta".to_string(),
            "gemini-2.0-flash",
        );
        let chat = GoogleGeminiChat::new(&cfg).unwrap();
        let req = ChatRequest {
            messages: vec![ChatMessage::user("hi")],
            tools: Some(vec![ToolDefinition::function(
                "get_weather",
                serde_json::json!({}),
            )]),
            tool_choice: Some(ToolChoice::Tool("get_weather".into())),
            temperature: Some(0.7),
            max_tokens: Some(512),
            top_p: None,
        };
        let v = chat.build_request_json(&req).unwrap();
        let t = v["generationConfig"]["temperature"].as_f64().unwrap();
        assert!((t - 0.7f64).abs() < 1e-5);
        assert_eq!(v["generationConfig"]["maxOutputTokens"], 512);
        assert!(v.get("toolConfig").is_some());
        assert_eq!(v["toolConfig"]["functionCallingConfig"]["mode"], "ANY");
        assert_eq!(
            v["toolConfig"]["functionCallingConfig"]["allowedFunctionNames"][0],
            "get_weather"
        );
    }
}

#[cfg(test)]
mod tests;