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
//! Anthropic Claude协议实现 - V2架构
//!
//! 这个模块实现了Anthropic Claude API协议规范。
use crate::core::Protocol;
use crate::types::{ChatRequest, ChatResponse, Message, Role, Choice, Usage};
use crate::error::LlmConnectorError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
/// Anthropic Claude协议实现
#[derive(Clone, Debug)]
pub struct AnthropicProtocol {
api_key: String,
}
impl AnthropicProtocol {
/// 创建新的Anthropic协议实例
pub fn new(api_key: &str) -> Self {
Self {
api_key: api_key.to_string(),
}
}
/// 获取API密钥
pub fn api_key(&self) -> &str {
&self.api_key
}
}
#[async_trait]
impl Protocol for AnthropicProtocol {
type Request = AnthropicRequest;
type Response = AnthropicResponse;
fn name(&self) -> &str {
"anthropic"
}
fn chat_endpoint(&self, base_url: &str) -> String {
format!("{}/v1/messages", base_url.trim_end_matches('/'))
}
fn build_request(&self, request: &ChatRequest) -> Result<Self::Request, LlmConnectorError> {
// Anthropic API 需要分离 system 消息
let mut system_message = None;
let mut messages = Vec::new();
for msg in &request.messages {
match msg.role {
Role::System => {
// Anthropic 只支持一个 system 消息,放在单独的字段中
if system_message.is_none() {
system_message = Some(msg.content.clone());
} else {
// 如果有多个 system 消息,合并它们
let existing = system_message.take().unwrap_or_default();
system_message = Some(format!("{}\n\n{}", existing, msg.content));
}
}
Role::User => {
messages.push(AnthropicMessage {
role: "user".to_string(),
content: msg.content.clone(),
});
}
Role::Assistant => {
messages.push(AnthropicMessage {
role: "assistant".to_string(),
content: msg.content.clone(),
});
}
Role::Tool => {
// Anthropic 暂不支持 tool 角色,转换为 user
messages.push(AnthropicMessage {
role: "user".to_string(),
content: format!("Tool result: {}", msg.content),
});
}
}
}
Ok(AnthropicRequest {
model: request.model.clone(),
max_tokens: request.max_tokens.unwrap_or(1024), // Anthropic 要求必须设置
messages,
system: system_message,
temperature: request.temperature,
top_p: request.top_p,
stream: request.stream,
})
}
fn parse_response(&self, response: &str) -> Result<ChatResponse, LlmConnectorError> {
let anthropic_response: AnthropicResponse = serde_json::from_str(response)
.map_err(|e| LlmConnectorError::ParseError(format!("Failed to parse Anthropic response: {}", e)))?;
// Anthropic 返回单个内容块
let content = anthropic_response.content.first()
.map(|c| c.text.clone())
.unwrap_or_default();
let choices = vec![Choice {
index: 0,
message: Message {
role: Role::Assistant,
content: content.clone(),
name: None,
tool_calls: None,
tool_call_id: None,
reasoning_content: None,
reasoning: None,
thought: None,
thinking: None,
},
finish_reason: Some(anthropic_response.stop_reason.unwrap_or_else(|| "stop".to_string())),
logprobs: None,
}];
let usage = Some(Usage {
prompt_tokens: anthropic_response.usage.input_tokens,
completion_tokens: anthropic_response.usage.output_tokens,
total_tokens: anthropic_response.usage.input_tokens + anthropic_response.usage.output_tokens,
completion_tokens_details: None,
prompt_cache_hit_tokens: None,
prompt_cache_miss_tokens: None,
prompt_tokens_details: None,
});
Ok(ChatResponse {
id: anthropic_response.id,
object: "chat.completion".to_string(),
created: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
model: anthropic_response.model,
choices,
content,
usage,
system_fingerprint: None,
})
}
fn map_error(&self, status: u16, body: &str) -> LlmConnectorError {
let error_info = serde_json::from_str::<serde_json::Value>(body)
.ok()
.and_then(|v| v.get("error").cloned())
.unwrap_or_else(|| serde_json::json!({"message": body}));
let message = error_info.get("message")
.and_then(|m| m.as_str())
.unwrap_or("Unknown Anthropic error");
match status {
400 => LlmConnectorError::InvalidRequest(format!("Anthropic: {}", message)),
401 => LlmConnectorError::AuthenticationError(format!("Anthropic: {}", message)),
403 => LlmConnectorError::PermissionError(format!("Anthropic: {}", message)),
429 => LlmConnectorError::RateLimitError(format!("Anthropic: {}", message)),
500..=599 => LlmConnectorError::ServerError(format!("Anthropic: {}", message)),
_ => LlmConnectorError::ApiError(format!("Anthropic HTTP {}: {}", status, message)),
}
}
fn auth_headers(&self) -> Vec<(String, String)> {
vec![
("x-api-key".to_string(), self.api_key.clone()),
("Content-Type".to_string(), "application/json".to_string()),
("anthropic-version".to_string(), "2023-06-01".to_string()),
]
}
/// 解析 Anthropic 流式响应
///
/// Anthropic 使用不同的流式格式:
/// - message_start: 包含 message 对象(有 id)
/// - content_block_start: 开始内容块
/// - content_block_delta: 内容增量(包含 text)
/// - content_block_stop: 结束内容块
/// - message_delta: 消息增量(包含 usage)
/// - message_stop: 消息结束
#[cfg(feature = "streaming")]
async fn parse_stream_response(&self, response: reqwest::Response) -> Result<crate::types::ChatStream, LlmConnectorError> {
use crate::types::{StreamingResponse, StreamingChoice, Delta, Usage};
use futures_util::StreamExt;
use std::sync::{Arc, Mutex};
// 使用标准 SSE 解析器
let events_stream = crate::sse::sse_events(response);
// 共享状态:保存 message_id
let message_id = Arc::new(Mutex::new(String::new()));
// 转换事件流
let response_stream = events_stream.filter_map(move |result| {
let message_id = message_id.clone();
async move {
match result {
Ok(json_str) => {
// 解析 Anthropic 流式事件
match serde_json::from_str::<serde_json::Value>(&json_str) {
Ok(event) => {
let event_type = event.get("type").and_then(|t| t.as_str()).unwrap_or("");
match event_type {
"message_start" => {
// 提取并保存 message id
if let Some(msg_id) = event.get("message")
.and_then(|m| m.get("id"))
.and_then(|id| id.as_str()) {
if let Ok(mut id) = message_id.lock() {
*id = msg_id.to_string();
}
}
// message_start 不返回内容
None
}
"content_block_delta" => {
// 提取文本增量
if let Some(text) = event.get("delta")
.and_then(|d| d.get("text"))
.and_then(|t| t.as_str()) {
let id = message_id.lock().ok()
.map(|id| id.clone())
.unwrap_or_default();
// 构造 StreamingResponse
Some(Ok(StreamingResponse {
id,
object: "chat.completion.chunk".to_string(),
created: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
model: "anthropic".to_string(),
choices: vec![StreamingChoice {
index: 0,
delta: Delta {
role: Some(crate::types::Role::Assistant),
content: Some(text.to_string()),
tool_calls: None,
reasoning_content: None,
reasoning: None,
thought: None,
thinking: None,
},
finish_reason: None,
logprobs: None,
}],
content: text.to_string(),
reasoning_content: None,
usage: None,
system_fingerprint: None,
}))
} else {
None
}
}
"message_delta" => {
// 提取 usage 和 stop_reason
let stop_reason = event.get("delta")
.and_then(|d| d.get("stop_reason"))
.and_then(|s| s.as_str())
.map(|s| s.to_string());
let usage = event.get("usage").and_then(|u| {
let input_tokens = u.get("input_tokens").and_then(|t| t.as_u64()).unwrap_or(0) as u32;
let output_tokens = u.get("output_tokens").and_then(|t| t.as_u64()).unwrap_or(0) as u32;
Some(Usage {
prompt_tokens: input_tokens,
completion_tokens: output_tokens,
total_tokens: input_tokens + output_tokens,
completion_tokens_details: None,
prompt_cache_hit_tokens: None,
prompt_cache_miss_tokens: None,
prompt_tokens_details: None,
})
});
let id = message_id.lock().ok()
.map(|id| id.clone())
.unwrap_or_default();
// 返回最终的响应(包含 finish_reason 和 usage)
Some(Ok(StreamingResponse {
id,
object: "chat.completion.chunk".to_string(),
created: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
model: "anthropic".to_string(),
choices: vec![StreamingChoice {
index: 0,
delta: Delta {
role: None,
content: None,
tool_calls: None,
reasoning_content: None,
reasoning: None,
thought: None,
thinking: None,
},
finish_reason: stop_reason,
logprobs: None,
}],
content: String::new(),
reasoning_content: None,
usage,
system_fingerprint: None,
}))
}
_ => {
// 忽略其他事件类型
None
}
}
}
Err(e) => {
Some(Err(LlmConnectorError::ParseError(format!(
"Failed to parse Anthropic streaming event: {}. JSON: {}",
e, json_str
))))
}
}
}
Err(e) => Some(Err(e)),
}
}
});
Ok(Box::pin(response_stream))
}
}
// Anthropic请求类型
#[derive(Serialize, Debug)]
pub struct AnthropicRequest {
pub model: String,
pub max_tokens: u32,
pub messages: Vec<AnthropicMessage>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stream: Option<bool>,
}
#[derive(Serialize, Debug)]
pub struct AnthropicMessage {
pub role: String,
pub content: String,
}
// Anthropic响应类型
#[derive(Deserialize, Debug)]
pub struct AnthropicResponse {
pub id: String,
pub model: String,
pub content: Vec<AnthropicContent>,
pub stop_reason: Option<String>,
pub usage: AnthropicUsage,
}
#[derive(Deserialize, Debug)]
pub struct AnthropicContent {
#[serde(rename = "type")]
pub content_type: String,
pub text: String,
}
#[derive(Deserialize, Debug)]
pub struct AnthropicUsage {
pub input_tokens: u32,
pub output_tokens: u32,
}