-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.rs
More file actions
429 lines (402 loc) · 17.3 KB
/
Copy pathtranslate.rs
File metadata and controls
429 lines (402 loc) · 17.3 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! OpenAI ⇄ Anthropic 协议互转:文本 + 工具调用(function calling)+ 多模态图片。
use serde_json::{json, Value};
/// OpenAI chat/completions 请求 → Anthropic messages 请求。
pub fn openai_to_anthropic(body: &Value, upstream_model: &str, stream: bool) -> Value {
let mut out = serde_json::Map::new();
out.insert("model".into(), json!(upstream_model));
let max_tokens = body.get("max_tokens").and_then(|v| v.as_u64()).unwrap_or(4096);
out.insert("max_tokens".into(), json!(max_tokens));
let (system, messages) = convert_messages(body.get("messages").and_then(|m| m.as_array()));
if !system.is_empty() {
out.insert("system".into(), json!(system));
}
out.insert("messages".into(), json!(messages));
for (src, dst) in [("temperature", "temperature"), ("top_p", "top_p")] {
if let Some(v) = body.get(src) {
out.insert(dst.into(), v.clone());
}
}
match body.get("stop") {
Some(Value::String(s)) => { out.insert("stop_sequences".into(), json!([s])); }
Some(Value::Array(a)) => { out.insert("stop_sequences".into(), json!(a)); }
_ => {}
}
if let Some(tools) = body.get("tools").and_then(|t| t.as_array()) {
out.insert("tools".into(), json!(convert_tools(tools)));
}
if let Some(tc) = body.get("tool_choice") {
if let Some(v) = convert_tool_choice(tc) {
out.insert("tool_choice".into(), v);
}
}
if stream {
out.insert("stream".into(), json!(true));
}
Value::Object(out)
}
/// OpenAI messages → (system, Anthropic messages)。处理工具调用/结果与多模态。
fn convert_messages(msgs: Option<&Vec<Value>>) -> (String, Vec<Value>) {
let mut system = String::new();
let mut out: Vec<Value> = Vec::new();
let Some(msgs) = msgs else { return (system, out) };
let mut i = 0;
while i < msgs.len() {
let m = &msgs[i];
let role = m.get("role").and_then(|r| r.as_str()).unwrap_or("user");
match role {
"system" => {
let t = content_to_text(m.get("content"));
if !t.is_empty() {
if !system.is_empty() { system.push_str("\n\n"); }
system.push_str(&t);
}
i += 1;
}
// 连续的 tool 结果合并到一个 user 回合(Anthropic 要求 tool_result 在 user 消息里)。
"tool" => {
let mut blocks = Vec::new();
while i < msgs.len() && msgs[i].get("role").and_then(|r| r.as_str()) == Some("tool") {
let tm = &msgs[i];
let id = tm.get("tool_call_id").and_then(|v| v.as_str()).unwrap_or("");
blocks.push(json!({
"type": "tool_result",
"tool_use_id": id,
"content": content_to_text(tm.get("content")),
}));
i += 1;
}
out.push(json!({ "role": "user", "content": blocks }));
}
"assistant" => {
let mut blocks = Vec::new();
let text = content_to_text(m.get("content"));
if !text.is_empty() {
blocks.push(json!({ "type": "text", "text": text }));
}
if let Some(tcs) = m.get("tool_calls").and_then(|v| v.as_array()) {
for tc in tcs {
let id = tc.get("id").and_then(|v| v.as_str()).unwrap_or("");
let name = tc.pointer("/function/name").and_then(|v| v.as_str()).unwrap_or("");
let args = tc.pointer("/function/arguments").and_then(|v| v.as_str()).unwrap_or("{}");
let input: Value = serde_json::from_str(args).unwrap_or_else(|_| json!({}));
blocks.push(json!({ "type": "tool_use", "id": id, "name": name, "input": input }));
}
}
let content = if blocks.is_empty() { json!("") } else { json!(blocks) };
out.push(json!({ "role": "assistant", "content": content }));
i += 1;
}
_ => {
out.push(json!({ "role": "user", "content": convert_user_content(m.get("content")) }));
i += 1;
}
}
}
(system, out)
}
/// user content → Anthropic content(字符串或含图片的 block 数组)。
fn convert_user_content(content: Option<&Value>) -> Value {
match content {
Some(Value::String(s)) => json!(s),
Some(Value::Array(parts)) => {
let blocks: Vec<Value> = parts
.iter()
.filter_map(|p| match p.get("type").and_then(|t| t.as_str()) {
Some("image_url") => p.pointer("/image_url/url").and_then(|v| v.as_str()).map(image_block),
_ => p.get("text").and_then(|t| t.as_str()).map(|s| json!({ "type": "text", "text": s })),
})
.collect();
if blocks.is_empty() { json!("") } else { json!(blocks) }
}
_ => json!(""),
}
}
/// OpenAI image_url(data URI 或 http)→ Anthropic image block。
fn image_block(url: &str) -> Value {
if let Some(rest) = url.strip_prefix("data:") {
if let Some((meta, data)) = rest.split_once(',') {
let media_type = meta.split(';').next().unwrap_or("image/png");
return json!({ "type": "image", "source": { "type": "base64", "media_type": media_type, "data": data } });
}
}
json!({ "type": "image", "source": { "type": "url", "url": url } })
}
fn convert_tools(tools: &[Value]) -> Vec<Value> {
tools
.iter()
.filter_map(|t| {
let f = t.get("function")?;
Some(json!({
"name": f.get("name").cloned().unwrap_or(json!("")),
"description": f.get("description").cloned().unwrap_or(json!("")),
"input_schema": f.get("parameters").cloned().unwrap_or(json!({ "type": "object" })),
}))
})
.collect()
}
fn convert_tool_choice(tc: &Value) -> Option<Value> {
match tc {
Value::String(s) => match s.as_str() {
"required" => Some(json!({ "type": "any" })),
"none" => None, // Anthropic 无显式 none;省略即可
_ => Some(json!({ "type": "auto" })),
},
Value::Object(_) => {
let name = tc.pointer("/function/name").and_then(|v| v.as_str())?;
Some(json!({ "type": "tool", "name": name }))
}
_ => Some(json!({ "type": "auto" })),
}
}
/// content(字符串或多段)→ 纯文本(用于 system / tool_result)。
fn content_to_text(content: Option<&Value>) -> String {
match content {
Some(Value::String(s)) => s.clone(),
Some(Value::Array(parts)) => parts
.iter()
.filter_map(|p| p.get("text").and_then(|t| t.as_str()).map(|s| s.to_string()))
.collect::<Vec<_>>()
.join(""),
_ => String::new(),
}
}
/// Anthropic messages 响应(非流式)→ OpenAI chat.completion(含 tool_calls)。
pub fn anthropic_to_openai(aresp: &Value, public_model: &str) -> Value {
let mut text = String::new();
let mut tool_calls: Vec<Value> = Vec::new();
if let Some(blocks) = aresp.get("content").and_then(|c| c.as_array()) {
for b in blocks {
match b.get("type").and_then(|t| t.as_str()) {
Some("text") => {
if let Some(t) = b.get("text").and_then(|t| t.as_str()) {
text.push_str(t);
}
}
Some("tool_use") => {
let args = serde_json::to_string(b.get("input").unwrap_or(&json!({}))).unwrap_or_else(|_| "{}".into());
tool_calls.push(json!({
"id": b.get("id").cloned().unwrap_or(json!("")),
"type": "function",
"function": { "name": b.get("name").cloned().unwrap_or(json!("")), "arguments": args },
}));
}
_ => {}
}
}
}
let finish = map_stop_reason(aresp.get("stop_reason").and_then(|s| s.as_str()));
let input = aresp.pointer("/usage/input_tokens").and_then(|v| v.as_u64()).unwrap_or(0);
let output = aresp.pointer("/usage/output_tokens").and_then(|v| v.as_u64()).unwrap_or(0);
let id = aresp.get("id").and_then(|v| v.as_str()).unwrap_or("chatcmpl-runapi");
let mut message = json!({ "role": "assistant" });
message["content"] = if text.is_empty() && !tool_calls.is_empty() { Value::Null } else { json!(text) };
if !tool_calls.is_empty() {
message["tool_calls"] = json!(tool_calls);
}
json!({
"id": id,
"object": "chat.completion",
"model": public_model,
"choices": [{ "index": 0, "message": message, "finish_reason": finish }],
"usage": { "prompt_tokens": input, "completion_tokens": output, "total_tokens": input + output }
})
}
pub fn map_stop_reason(reason: Option<&str>) -> &'static str {
match reason {
Some("max_tokens") => "length",
Some("tool_use") => "tool_calls",
_ => "stop",
}
}
// ================== 反向:Anthropic 入站 → OpenAI 上游 ==================
/// Anthropic Messages 请求 → OpenAI chat/completions 请求。
pub fn anthropic_to_openai_request(areq: &Value, upstream_model: &str, stream: bool) -> Value {
let mut out = serde_json::Map::new();
out.insert("model".into(), json!(upstream_model));
if let Some(mt) = areq.get("max_tokens") {
out.insert("max_tokens".into(), mt.clone());
}
let mut messages: Vec<Value> = Vec::new();
if let Some(sys) = areq.get("system") {
let t = anth_text(sys);
if !t.is_empty() {
messages.push(json!({ "role": "system", "content": t }));
}
}
if let Some(arr) = areq.get("messages").and_then(|m| m.as_array()) {
for m in arr {
let role = m.get("role").and_then(|r| r.as_str()).unwrap_or("user");
let content = m.get("content");
if role == "assistant" {
messages.push(anth_assistant_to_openai(content));
} else {
// user:可能含 text/image/tool_result
anth_user_to_openai(content, &mut messages);
}
}
}
out.insert("messages".into(), json!(messages));
for k in ["temperature", "top_p"] {
if let Some(v) = areq.get(k) {
out.insert(k.into(), v.clone());
}
}
if let Some(s) = areq.get("stop_sequences") {
out.insert("stop".into(), s.clone());
}
if let Some(tools) = areq.get("tools").and_then(|t| t.as_array()) {
let mapped: Vec<Value> = tools
.iter()
.map(|t| json!({
"type": "function",
"function": {
"name": t.get("name").cloned().unwrap_or(json!("")),
"description": t.get("description").cloned().unwrap_or(json!("")),
"parameters": t.get("input_schema").cloned().unwrap_or(json!({"type":"object"})),
}
}))
.collect();
out.insert("tools".into(), json!(mapped));
}
if let Some(tc) = areq.get("tool_choice") {
let mapped = match tc.get("type").and_then(|t| t.as_str()) {
Some("any") => json!("required"),
Some("tool") => json!({ "type": "function", "function": { "name": tc.get("name").cloned().unwrap_or(json!("")) } }),
_ => json!("auto"),
};
out.insert("tool_choice".into(), mapped);
}
if stream {
out.insert("stream".into(), json!(true));
}
Value::Object(out)
}
fn anth_assistant_to_openai(content: Option<&Value>) -> Value {
match content {
Some(Value::String(s)) => json!({ "role": "assistant", "content": s }),
Some(Value::Array(blocks)) => {
let mut text = String::new();
let mut tool_calls = Vec::new();
for b in blocks {
match b.get("type").and_then(|t| t.as_str()) {
Some("text") => {
if let Some(t) = b.get("text").and_then(|t| t.as_str()) { text.push_str(t); }
}
Some("tool_use") => {
let args = serde_json::to_string(b.get("input").unwrap_or(&json!({}))).unwrap_or_else(|_| "{}".into());
tool_calls.push(json!({
"id": b.get("id").cloned().unwrap_or(json!("")),
"type": "function",
"function": { "name": b.get("name").cloned().unwrap_or(json!("")), "arguments": args },
}));
}
_ => {}
}
}
let mut msg = json!({ "role": "assistant" });
msg["content"] = if text.is_empty() && !tool_calls.is_empty() { Value::Null } else { json!(text) };
if !tool_calls.is_empty() { msg["tool_calls"] = json!(tool_calls); }
msg
}
_ => json!({ "role": "assistant", "content": "" }),
}
}
/// Anthropic user 消息 → OpenAI 消息(tool_result 拆成 role:tool;text/image 合成 user)。
fn anth_user_to_openai(content: Option<&Value>, out: &mut Vec<Value>) {
match content {
Some(Value::String(s)) => out.push(json!({ "role": "user", "content": s })),
Some(Value::Array(blocks)) => {
let mut parts: Vec<Value> = Vec::new();
for b in blocks {
match b.get("type").and_then(|t| t.as_str()) {
Some("tool_result") => {
out.push(json!({
"role": "tool",
"tool_call_id": b.get("tool_use_id").cloned().unwrap_or(json!("")),
"content": anth_text(b.get("content").unwrap_or(&json!(""))),
}));
}
Some("image") => {
if let Some(p) = anth_image_to_openai(b) { parts.push(p); }
}
_ => {
if let Some(t) = b.get("text").and_then(|t| t.as_str()) {
parts.push(json!({ "type": "text", "text": t }));
}
}
}
}
if !parts.is_empty() {
out.push(json!({ "role": "user", "content": parts }));
}
}
_ => {}
}
}
/// Anthropic image block → OpenAI image_url part。
fn anth_image_to_openai(b: &Value) -> Option<Value> {
let src = b.get("source")?;
let url = match src.get("type").and_then(|t| t.as_str()) {
Some("base64") => {
let mt = src.get("media_type").and_then(|v| v.as_str()).unwrap_or("image/png");
let data = src.get("data").and_then(|v| v.as_str()).unwrap_or("");
format!("data:{mt};base64,{data}")
}
_ => src.get("url").and_then(|v| v.as_str()).unwrap_or("").to_string(),
};
Some(json!({ "type": "image_url", "image_url": { "url": url } }))
}
/// Anthropic 文本(string 或 text-block 数组)→ 纯文本。
fn anth_text(v: &Value) -> String {
match v {
Value::String(s) => s.clone(),
Value::Array(arr) => arr
.iter()
.filter_map(|b| b.get("text").and_then(|t| t.as_str()).map(|s| s.to_string()))
.collect::<Vec<_>>()
.join(""),
_ => String::new(),
}
}
/// OpenAI chat.completion(非流式)→ Anthropic Messages 响应。
pub fn openai_to_anthropic_response(oai: &Value, public_model: &str) -> Value {
let msg = oai.pointer("/choices/0/message").cloned().unwrap_or(json!({}));
let mut content: Vec<Value> = Vec::new();
if let Some(text) = msg.get("content").and_then(|c| c.as_str()) {
if !text.is_empty() {
content.push(json!({ "type": "text", "text": text }));
}
}
if let Some(tcs) = msg.get("tool_calls").and_then(|v| v.as_array()) {
for tc in tcs {
let args = tc.pointer("/function/arguments").and_then(|v| v.as_str()).unwrap_or("{}");
let input: Value = serde_json::from_str(args).unwrap_or_else(|_| json!({}));
content.push(json!({
"type": "tool_use",
"id": tc.get("id").cloned().unwrap_or(json!("")),
"name": tc.pointer("/function/name").cloned().unwrap_or(json!("")),
"input": input,
}));
}
}
let finish = oai.pointer("/choices/0/finish_reason").and_then(|v| v.as_str());
let input = oai.pointer("/usage/prompt_tokens").and_then(|v| v.as_u64()).unwrap_or(0);
let output = oai.pointer("/usage/completion_tokens").and_then(|v| v.as_u64()).unwrap_or(0);
json!({
"id": oai.get("id").cloned().unwrap_or(json!("msg_runapi")),
"type": "message",
"role": "assistant",
"model": public_model,
"content": content,
"stop_reason": map_finish_to_stop(finish),
"stop_sequence": Value::Null,
"usage": { "input_tokens": input, "output_tokens": output }
})
}
pub fn map_finish_to_stop(finish: Option<&str>) -> &'static str {
match finish {
Some("length") => "max_tokens",
Some("tool_calls") => "tool_use",
_ => "end_turn",
}
}