llm-universal 0.2.0

One async client for Anthropic, OpenAI, Gemini, Cohere, Azure OpenAI and OpenAI-compatible APIs (Groq, Mistral, DeepSeek, xAI, OpenRouter, Ollama), with tool calling and SDK-style errors
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
use async_trait::async_trait;
use serde_json::{json, Value};

use crate::{
    send_json, ChatRequest, ChatResponse, LlmError, LlmProvider, Message, Part, Result, Role,
    StopReason, Usage,
};

/// Client for the OpenAI Chat Completions API (`POST {base_url}/chat/completions`).
///
/// The same wire format is served by many other providers; use the presets
/// ([`groq`](Self::groq), [`mistral`](Self::mistral), [`deepseek`](Self::deepseek), [`xai`](Self::xai),
/// [`openrouter`](Self::openrouter), [`together`](Self::together), [`fireworks`](Self::fireworks),
/// [`perplexity`](Self::perplexity), [`azure`](Self::azure), [`ollama`](Self::ollama),
/// [`lm_studio`](Self::lm_studio)) or [`with_base_url`](Self::with_base_url) for anything else (vLLM, LiteLLM, …).
#[derive(Clone)]
pub struct OpenAiProvider {
    client: reqwest::Client,
    api_key: String,
    base_url: String,
    auth: Auth,
}

/// How the key is sent.
#[derive(Clone, Copy, Debug)]
enum Auth {
    /// `Authorization: Bearer <key>` (OpenAI and most compatible servers).
    Bearer,
    /// `api-key: <key>` (Azure OpenAI).
    ApiKeyHeader,
}

impl std::fmt::Debug for OpenAiProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OpenAiProvider")
            .field("api_key", &"<redacted>")
            .field("base_url", &self.base_url)
            .finish()
    }
}

impl OpenAiProvider {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            client: crate::http_client(),
            api_key: api_key.into(),
            base_url: "https://api.openai.com/v1".to_string(),
            auth: Auth::Bearer,
        }
    }

    fn preset(api_key: impl Into<String>, base_url: &str) -> Self {
        Self::new(api_key).with_base_url(base_url)
    }

    /// Groq: `https://api.groq.com/openai/v1`.
    pub fn groq(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.groq.com/openai/v1")
    }

    /// Mistral AI: `https://api.mistral.ai/v1`.
    pub fn mistral(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.mistral.ai/v1")
    }

    /// DeepSeek: `https://api.deepseek.com/v1`.
    pub fn deepseek(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.deepseek.com/v1")
    }

    /// xAI (Grok): `https://api.x.ai/v1`.
    pub fn xai(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.x.ai/v1")
    }

    /// OpenRouter: `https://openrouter.ai/api/v1`.
    pub fn openrouter(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://openrouter.ai/api/v1")
    }

    /// Together AI: `https://api.together.xyz/v1`.
    pub fn together(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.together.xyz/v1")
    }

    /// Fireworks AI: `https://api.fireworks.ai/inference/v1`.
    pub fn fireworks(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.fireworks.ai/inference/v1")
    }

    /// Perplexity: `https://api.perplexity.ai`.
    pub fn perplexity(api_key: impl Into<String>) -> Self {
        Self::preset(api_key, "https://api.perplexity.ai")
    }

    /// Azure OpenAI v1 API: `https://{resource}.openai.azure.com/openai/v1`, key in the `api-key` header.
    /// Pass the deployment name as the model.
    pub fn azure(resource: &str, api_key: impl Into<String>) -> Self {
        let mut provider = Self::preset(
            api_key,
            &format!("https://{resource}.openai.azure.com/openai/v1"),
        );
        provider.auth = Auth::ApiKeyHeader;
        provider
    }

    /// Ollama on this machine: `http://localhost:11434/v1`, no key.
    pub fn ollama() -> Self {
        Self::preset("", "http://localhost:11434/v1")
    }

    /// LM Studio on this machine: `http://localhost:1234/v1`, no key.
    pub fn lm_studio() -> Self {
        Self::preset("", "http://localhost:1234/v1")
    }

    /// Points the client at another server. The key may be empty if the server does not need one.
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into().trim_end_matches('/').to_string();
        self
    }

    /// Uses your own `reqwest::Client`, for example to share a connection pool or set a proxy.
    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.client = client;
        self
    }

    pub(crate) fn build_body(req: &ChatRequest) -> Value {
        let mut messages = Vec::new();
        if let Some(system) = &req.system {
            messages.push(json!({ "role": "system", "content": system }));
        }
        for message in &req.messages {
            messages.extend(to_openai_messages(message));
        }

        let mut body = json!({ "model": req.model, "messages": messages });

        if !req.tools.is_empty() {
            let tools: Vec<Value> = req
                .tools
                .iter()
                .map(|t| {
                    json!({
                        "type": "function",
                        "function": {
                            "name": t.name,
                            "description": t.description,
                            "parameters": t.parameters,
                        }
                    })
                })
                .collect();
            body["tools"] = json!(tools);
        }
        if let Some(max_tokens) = req.max_tokens {
            body["max_completion_tokens"] = json!(max_tokens);
        }
        if let Some(temperature) = req.temperature {
            body["temperature"] = json!(temperature);
        }
        body
    }
}

pub(crate) fn to_openai_messages(message: &Message) -> Vec<Value> {
    if message.role == Role::Tool {
        // OpenAI expects one `tool` message per result.
        return message
            .parts
            .iter()
            .filter_map(|p| match p {
                Part::ToolResult {
                    call_id, content, ..
                } => Some(json!({
                    "role": "tool",
                    "tool_call_id": call_id,
                    "content": content,
                })),
                _ => None,
            })
            .collect();
    }

    let role = match message.role {
        Role::System => "system",
        Role::User => "user",
        Role::Assistant | Role::Tool => "assistant",
    };
    let text = message.text_content();
    let tool_calls: Vec<Value> = message
        .parts
        .iter()
        .filter_map(|p| match p {
            Part::ToolCall {
                id,
                name,
                arguments,
            } => Some(json!({
                "id": id,
                "type": "function",
                "function": { "name": name, "arguments": arguments.to_string() },
            })),
            _ => None,
        })
        .collect();

    let content = if text.is_empty() && !tool_calls.is_empty() {
        Value::Null
    } else {
        json!(text)
    };
    let mut msg = json!({ "role": role, "content": content });
    if !tool_calls.is_empty() {
        msg["tool_calls"] = json!(tool_calls);
    }
    vec![msg]
}

/// Parses an OpenAI-style `tool_calls` array (also used by Cohere).
pub(crate) fn parse_tool_calls(calls: &Value) -> Vec<Part> {
    let Some(calls) = calls.as_array() else {
        return Vec::new();
    };
    calls
        .iter()
        .map(|call| {
            // Arguments are a JSON string. An empty string means a tool without arguments;
            // invalid JSON (e.g. cut off by max tokens) is kept raw instead of failing the call.
            let raw = call["function"]["arguments"].as_str().unwrap_or_default();
            let arguments = if raw.trim().is_empty() {
                json!({})
            } else {
                serde_json::from_str(raw).unwrap_or_else(|_| Value::String(raw.to_string()))
            };
            Part::ToolCall {
                id: call["id"].as_str().unwrap_or_default().to_string(),
                name: call["function"]["name"]
                    .as_str()
                    .unwrap_or_default()
                    .to_string(),
                arguments,
            }
        })
        .collect()
}

fn parse_response(v: &Value) -> Result<ChatResponse> {
    let choice = v["choices"]
        .get(0)
        .ok_or_else(|| LlmError::InvalidResponse("missing field `choices[0]`".into()))?;
    let message = &choice["message"];

    let mut parts = Vec::new();
    if let Some(text) = message["content"].as_str() {
        if !text.is_empty() {
            parts.push(Part::Text {
                text: text.to_string(),
            });
        }
    }
    // A refusal arrives in its own `refusal` field while `content` is null.
    let refusal = message["refusal"].as_str();
    if let Some(text) = refusal {
        parts.push(Part::Text {
            text: text.to_string(),
        });
    }
    parts.extend(parse_tool_calls(&message["tool_calls"]));

    let stop_reason = match choice["finish_reason"].as_str() {
        _ if refusal.is_some() => StopReason::Refusal,
        Some("stop") => StopReason::EndTurn,
        Some("tool_calls") | Some("function_call") => StopReason::ToolUse,
        Some("length") => StopReason::MaxTokens,
        Some("content_filter") => StopReason::Refusal,
        other => StopReason::Other(other.unwrap_or("unknown").to_string()),
    };

    let tokens =
        |field: &str| u32::try_from(v["usage"][field].as_u64().unwrap_or(0)).unwrap_or(u32::MAX);
    let usage = Usage {
        input_tokens: tokens("prompt_tokens"),
        output_tokens: tokens("completion_tokens"),
    };

    Ok(ChatResponse {
        model: v["model"].as_str().unwrap_or_default().to_string(),
        message: Message::new(Role::Assistant, parts),
        stop_reason,
        usage,
    })
}

#[async_trait]
impl LlmProvider for OpenAiProvider {
    async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse> {
        // OpenAI allows tool names up to 64 characters.
        crate::validate(
            "OpenAiProvider",
            &self.api_key,
            false,
            &self.base_url,
            64,
            req,
        )?;
        let url = format!("{}/chat/completions", self.base_url);
        let mut builder = self.client.post(url).json(&Self::build_body(req));
        if !self.api_key.is_empty() {
            builder = match self.auth {
                Auth::Bearer => builder.header(
                    reqwest::header::AUTHORIZATION,
                    crate::secret_header("OpenAiProvider", &format!("Bearer {}", self.api_key))?,
                ),
                Auth::ApiKeyHeader => builder.header(
                    "api-key",
                    crate::secret_header("OpenAiProvider", &self.api_key)?,
                ),
            };
        }
        let body = send_json(builder).await?;
        parse_response(&body)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Tool;

    #[test]
    fn builds_tool_request() {
        let req = ChatRequest::new("gpt-4o")
            .system("sys")
            .max_tokens(100)
            .tool(Tool::new("f", "d", json!({"type": "object"})))
            .message(Message::user("hi"));
        let body = OpenAiProvider::build_body(&req);
        assert_eq!(body["messages"][0]["role"], "system");
        assert_eq!(body["messages"][1]["content"], "hi");
        assert_eq!(body["tools"][0]["function"]["name"], "f");
        assert_eq!(body["max_completion_tokens"], 100);
    }

    #[test]
    fn parses_tool_call_response() {
        let v = json!({
            "model": "gpt-4o",
            "choices": [{
                "finish_reason": "tool_calls",
                "message": {
                    "content": null,
                    "tool_calls": [{
                        "id": "call_1",
                        "type": "function",
                        "function": { "name": "f", "arguments": "{\"x\":1}" }
                    }]
                }
            }],
            "usage": { "prompt_tokens": 5, "completion_tokens": 7 }
        });
        let resp = parse_response(&v).unwrap();
        assert_eq!(resp.stop_reason, StopReason::ToolUse);
        assert_eq!(resp.tool_calls()[0].2["x"], 1);
        assert_eq!(resp.usage.output_tokens, 7);
    }

    #[test]
    fn tolerates_empty_and_truncated_arguments() {
        let v = json!({
            "choices": [{
                "finish_reason": "length",
                "message": {
                    "content": null,
                    "tool_calls": [
                        { "id": "a", "function": { "name": "f", "arguments": "" } },
                        { "id": "b", "function": { "name": "f", "arguments": "{\"x\":" } }
                    ]
                }
            }]
        });
        let resp = parse_response(&v).unwrap();
        let calls = resp.tool_calls();
        assert_eq!(calls[0].2, json!({}));
        assert_eq!(calls[1].2, json!("{\"x\":"));
        assert_eq!(resp.stop_reason, StopReason::MaxTokens);
    }

    #[test]
    fn maps_refusal_field() {
        let v = json!({
            "choices": [{
                "finish_reason": "stop",
                "message": { "content": null, "refusal": "I can't help with that." }
            }]
        });
        let resp = parse_response(&v).unwrap();
        assert_eq!(resp.stop_reason, StopReason::Refusal);
        assert_eq!(resp.text(), "I can't help with that.");
    }

    #[test]
    fn debug_redacts_api_key() {
        let debug = format!("{:?}", OpenAiProvider::new("sk-secret"));
        assert!(!debug.contains("sk-secret"));
    }
}