|
|
|
@ -1,10 +1,10 @@
|
|
|
|
|
use super::{
|
|
|
|
|
catch_error, extract_system_message, sse_stream, ClaudeClient, CompletionOutput, ExtraConfig,
|
|
|
|
|
ImageUrl, MessageContent, MessageContentPart, Model, ModelData, PromptAction, PromptKind,
|
|
|
|
|
SendData, SsMmessage, SseHandler,
|
|
|
|
|
catch_error, extract_system_message, message::*, sse_stream, ClaudeClient, CompletionOutput,
|
|
|
|
|
ExtraConfig, ImageUrl, MessageContent, MessageContentPart, Model, ModelData, PromptAction,
|
|
|
|
|
PromptKind, SendData, SsMmessage, SseHandler, ToolCall,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use anyhow::{anyhow, bail, Result};
|
|
|
|
|
use anyhow::{bail, Context, Result};
|
|
|
|
|
use reqwest::{Client as ReqwestClient, RequestBuilder};
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
@ -36,7 +36,9 @@ impl ClaudeClient {
|
|
|
|
|
debug!("Claude Request: {url} {body}");
|
|
|
|
|
|
|
|
|
|
let mut builder = client.post(url).json(&body);
|
|
|
|
|
builder = builder.header("anthropic-version", "2023-06-01");
|
|
|
|
|
builder = builder
|
|
|
|
|
.header("anthropic-version", "2023-06-01")
|
|
|
|
|
.header("anthropic-beta", "tools-2024-05-16");
|
|
|
|
|
if let Some(api_key) = api_key {
|
|
|
|
|
builder = builder.header("x-api-key", api_key)
|
|
|
|
|
}
|
|
|
|
@ -66,14 +68,58 @@ pub async fn claude_send_message_streaming(
|
|
|
|
|
builder: RequestBuilder,
|
|
|
|
|
handler: &mut SseHandler,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let mut function_name = String::new();
|
|
|
|
|
let mut function_arguments = String::new();
|
|
|
|
|
let mut function_id = String::new();
|
|
|
|
|
let handle = |message: SsMmessage| -> Result<bool> {
|
|
|
|
|
let data: Value = serde_json::from_str(&message.data)?;
|
|
|
|
|
debug!("stream-data: {data}");
|
|
|
|
|
if let Some(typ) = data["type"].as_str() {
|
|
|
|
|
if typ == "content_block_delta" {
|
|
|
|
|
if let Some(text) = data["delta"]["text"].as_str() {
|
|
|
|
|
handler.text(text)?;
|
|
|
|
|
match typ {
|
|
|
|
|
"content_block_start" => {
|
|
|
|
|
if let (Some("tool_use"), Some(name), Some(id)) = (
|
|
|
|
|
data["content_block"]["type"].as_str(),
|
|
|
|
|
data["content_block"]["name"].as_str(),
|
|
|
|
|
data["content_block"]["id"].as_str(),
|
|
|
|
|
) {
|
|
|
|
|
if !function_name.is_empty() {
|
|
|
|
|
let arguments: Value = function_arguments
|
|
|
|
|
.parse()
|
|
|
|
|
.context("Invalid call arguments: must be json")?;
|
|
|
|
|
handler.tool_call(ToolCall::new(
|
|
|
|
|
function_name.clone(),
|
|
|
|
|
arguments,
|
|
|
|
|
Some(function_id.clone()),
|
|
|
|
|
))?;
|
|
|
|
|
}
|
|
|
|
|
function_name = name.into();
|
|
|
|
|
function_arguments.clear();
|
|
|
|
|
function_id = id.into();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"content_block_delta" => {
|
|
|
|
|
if let Some(text) = data["delta"]["text"].as_str() {
|
|
|
|
|
handler.text(text)?;
|
|
|
|
|
} else if let (true, Some(partial_json)) = (
|
|
|
|
|
!function_name.is_empty(),
|
|
|
|
|
data["delta"]["partial_json"].as_str(),
|
|
|
|
|
) {
|
|
|
|
|
function_arguments.push_str(partial_json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"content_block_stop" => {
|
|
|
|
|
if !function_name.is_empty() {
|
|
|
|
|
let arguments: Value = function_arguments
|
|
|
|
|
.parse()
|
|
|
|
|
.context("Invalid call arguments: must be json")?;
|
|
|
|
|
handler.tool_call(ToolCall::new(
|
|
|
|
|
function_name.clone(),
|
|
|
|
|
arguments,
|
|
|
|
|
Some(function_id.clone()),
|
|
|
|
|
))?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(false)
|
|
|
|
@ -87,61 +133,94 @@ pub fn claude_build_body(data: SendData, model: &Model) -> Result<Value> {
|
|
|
|
|
mut messages,
|
|
|
|
|
temperature,
|
|
|
|
|
top_p,
|
|
|
|
|
functions: _,
|
|
|
|
|
functions,
|
|
|
|
|
stream,
|
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
|
|
let system_message = extract_system_message(&mut messages);
|
|
|
|
|
|
|
|
|
|
let mut is_tool_call = false;
|
|
|
|
|
let mut network_image_urls = vec![];
|
|
|
|
|
|
|
|
|
|
let messages: Vec<Value> = messages
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|message| {
|
|
|
|
|
let role = message.role;
|
|
|
|
|
let content = match message.content {
|
|
|
|
|
MessageContent::Text(text) => vec![json!({"type": "text", "text": text})],
|
|
|
|
|
MessageContent::Array(list) => list
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|item| match item {
|
|
|
|
|
MessageContentPart::Text { text } => json!({"type": "text", "text": text}),
|
|
|
|
|
MessageContentPart::ImageUrl {
|
|
|
|
|
image_url: ImageUrl { url },
|
|
|
|
|
} => {
|
|
|
|
|
if let Some((mime_type, data)) = url
|
|
|
|
|
.strip_prefix("data:")
|
|
|
|
|
.and_then(|v| v.split_once(";base64,"))
|
|
|
|
|
{
|
|
|
|
|
json!({
|
|
|
|
|
"type": "image",
|
|
|
|
|
"source": {
|
|
|
|
|
"type": "base64",
|
|
|
|
|
"media_type": mime_type,
|
|
|
|
|
"data": data,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
network_image_urls.push(url.clone());
|
|
|
|
|
json!({ "url": url })
|
|
|
|
|
.flat_map(|message| {
|
|
|
|
|
let Message { role, content } = message;
|
|
|
|
|
match content {
|
|
|
|
|
MessageContent::Text(text) => vec![json!({
|
|
|
|
|
"role": role,
|
|
|
|
|
"content": text,
|
|
|
|
|
})],
|
|
|
|
|
MessageContent::Array(list) => {
|
|
|
|
|
let content: Vec<_> = list
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|item| match item {
|
|
|
|
|
MessageContentPart::Text { text } => {
|
|
|
|
|
json!({"type": "text", "text": text})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
MessageContent::ToolCall(_) => {
|
|
|
|
|
is_tool_call = true;
|
|
|
|
|
vec![]
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
json!({ "role": role, "content": content })
|
|
|
|
|
MessageContentPart::ImageUrl {
|
|
|
|
|
image_url: ImageUrl { url },
|
|
|
|
|
} => {
|
|
|
|
|
if let Some((mime_type, data)) = url
|
|
|
|
|
.strip_prefix("data:")
|
|
|
|
|
.and_then(|v| v.split_once(";base64,"))
|
|
|
|
|
{
|
|
|
|
|
json!({
|
|
|
|
|
"type": "image",
|
|
|
|
|
"source": {
|
|
|
|
|
"type": "base64",
|
|
|
|
|
"media_type": mime_type,
|
|
|
|
|
"data": data,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
network_image_urls.push(url.clone());
|
|
|
|
|
json!({ "url": url })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
vec![json!({
|
|
|
|
|
"role": role,
|
|
|
|
|
"content": content,
|
|
|
|
|
})]
|
|
|
|
|
}
|
|
|
|
|
MessageContent::ToolResults((tool_call_results, text)) => {
|
|
|
|
|
let mut tool_call = vec![];
|
|
|
|
|
let mut tool_result = vec![];
|
|
|
|
|
if !text.is_empty() {
|
|
|
|
|
tool_call.push(json!({
|
|
|
|
|
"type": "text",
|
|
|
|
|
"text": text,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
for tool_call_result in tool_call_results {
|
|
|
|
|
tool_call.push(json!({
|
|
|
|
|
"type": "tool_use",
|
|
|
|
|
"id": tool_call_result.call.id,
|
|
|
|
|
"name": tool_call_result.call.name,
|
|
|
|
|
"input": tool_call_result.call.arguments,
|
|
|
|
|
}));
|
|
|
|
|
tool_result.push(json!({
|
|
|
|
|
"type": "tool_result",
|
|
|
|
|
"tool_use_id": tool_call_result.call.id,
|
|
|
|
|
"content": tool_call_result.output.to_string(),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
vec![
|
|
|
|
|
json!({
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": tool_call,
|
|
|
|
|
}),
|
|
|
|
|
json!({
|
|
|
|
|
"role": "user",
|
|
|
|
|
"content": tool_result,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if is_tool_call {
|
|
|
|
|
bail!("The client does not support function calling",);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !network_image_urls.is_empty() {
|
|
|
|
|
bail!(
|
|
|
|
|
"The model does not support network images: {:?}",
|
|
|
|
@ -168,17 +247,58 @@ pub fn claude_build_body(data: SendData, model: &Model) -> Result<Value> {
|
|
|
|
|
if stream {
|
|
|
|
|
body["stream"] = true.into();
|
|
|
|
|
}
|
|
|
|
|
if let Some(functions) = functions {
|
|
|
|
|
body["tools"] = functions
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|v| {
|
|
|
|
|
json!({
|
|
|
|
|
"name": v.name,
|
|
|
|
|
"description": v.description,
|
|
|
|
|
"input_schema": v.parameters,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
}
|
|
|
|
|
Ok(body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn claude_extract_completion(data: &Value) -> Result<CompletionOutput> {
|
|
|
|
|
let text = data["content"][0]["text"]
|
|
|
|
|
.as_str()
|
|
|
|
|
.ok_or_else(|| anyhow!("Invalid response data: {data}"))?;
|
|
|
|
|
let text = data["content"][0]["text"].as_str().unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
let mut tool_calls = vec![];
|
|
|
|
|
if let Some(calls) = data["content"].as_array().map(|content| {
|
|
|
|
|
content
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|content| matches!(content["type"].as_str(), Some("tool_use")))
|
|
|
|
|
.collect::<Vec<&Value>>()
|
|
|
|
|
}) {
|
|
|
|
|
tool_calls = calls
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|call| {
|
|
|
|
|
if let (Some(name), Some(input), Some(id)) = (
|
|
|
|
|
call["name"].as_str(),
|
|
|
|
|
call.get("input"),
|
|
|
|
|
call["id"].as_str(),
|
|
|
|
|
) {
|
|
|
|
|
Some(ToolCall::new(
|
|
|
|
|
name.to_string(),
|
|
|
|
|
input.clone(),
|
|
|
|
|
Some(id.to_string()),
|
|
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if text.is_empty() && tool_calls.is_empty() {
|
|
|
|
|
bail!("Invalid response data: {data}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let output = CompletionOutput {
|
|
|
|
|
text: text.to_string(),
|
|
|
|
|
tool_calls: vec![],
|
|
|
|
|
tool_calls,
|
|
|
|
|
id: data["id"].as_str().map(|v| v.to_string()),
|
|
|
|
|
input_tokens: data["usage"]["input_tokens"].as_u64(),
|
|
|
|
|
output_tokens: data["usage"]["output_tokens"].as_u64(),
|
|
|
|
|