Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stream function calling for openai-compatible client #1017

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async fn chat_completions_streaming(
if !function_name.is_empty() {
let arguments: Value =
function_arguments.parse().with_context(|| {
format!("Tool call '{function_name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{function_name}' have non-JSON arguments '{function_arguments}'")
})?;
handler.tool_call(ToolCall::new(
function_name.clone(),
Expand All @@ -257,7 +257,7 @@ async fn chat_completions_streaming(
"contentBlockStop" => {
if !function_name.is_empty() {
let arguments: Value = function_arguments.parse().with_context(|| {
format!("Tool call '{function_name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{function_name}' have non-JSON arguments '{function_arguments}'")
})?;
handler.tool_call(ToolCall::new(
function_name.clone(),
Expand Down
4 changes: 2 additions & 2 deletions src/client/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub async fn claude_chat_completions_streaming(
if !function_name.is_empty() {
let arguments: Value =
function_arguments.parse().with_context(|| {
format!("Tool call '{function_name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{function_name}' have non-JSON arguments '{function_arguments}'")
})?;
handler.tool_call(ToolCall::new(
function_name.clone(),
Expand Down Expand Up @@ -124,7 +124,7 @@ pub async fn claude_chat_completions_streaming(
json!({})
} else {
function_arguments.parse().with_context(|| {
format!("Tool call '{function_name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{function_name}' have non-JSON arguments '{function_arguments}'")
})?
};
handler.tool_call(ToolCall::new(
Expand Down
4 changes: 2 additions & 2 deletions src/client/ernie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async fn chat_completions_streaming(
function.get("arguments").and_then(|v| v.as_str()),
) {
let arguments: Value = arguments.parse().with_context(|| {
format!("Tool call '{name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{name}' have non-JSON arguments '{arguments}'")
})?;
handler.tool_call(ToolCall::new(name.to_string(), arguments, None))?;
}
Expand Down Expand Up @@ -292,7 +292,7 @@ fn extract_chat_completions_text(data: &Value) -> Result<ChatCompletionsOutput>
call.get("arguments").and_then(|v| v.as_str()),
) {
let arguments: Value = arguments.parse().with_context(|| {
format!("Tool call '{name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{name}' have non-JSON arguments '{arguments}'")
})?;
tool_calls.push(ToolCall::new(name.to_string(), arguments, None));
}
Expand Down
25 changes: 15 additions & 10 deletions src/client/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ pub async fn openai_chat_completions_streaming(
handler: &mut SseHandler,
_model: &Model,
) -> Result<()> {
let mut function_index = 0;
let mut call_id = String::new();
let mut function_name = String::new();
let mut function_arguments = String::new();
let mut function_id = String::new();
let handle = |message: SseMmessage| -> Result<bool> {
if message.data == "[DONE]" {
if !function_name.is_empty() {
let arguments: Value = function_arguments.parse().with_context(|| {
format!("Tool call '{function_name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{function_name}' have non-JSON arguments '{function_arguments}'")
})?;
handler.tool_call(ToolCall::new(
function_name.clone(),
Expand All @@ -121,18 +121,23 @@ pub async fn openai_chat_completions_streaming(
}
let data: Value = serde_json::from_str(&message.data)?;
debug!("stream-data: {data}");
if let Some(text) = data["choices"][0]["delta"]["content"].as_str() {
if let Some(text) = data["choices"][0]["delta"]["content"]
.as_str()
.filter(|v| !v.is_empty())
{
handler.text(text)?;
} else if let (Some(function), index, id) = (
data["choices"][0]["delta"]["tool_calls"][0]["function"].as_object(),
data["choices"][0]["delta"]["tool_calls"][0]["index"].as_u64(),
data["choices"][0]["delta"]["tool_calls"][0]["id"].as_str(),
data["choices"][0]["delta"]["tool_calls"][0]["id"]
.as_str()
.filter(|v| !v.is_empty()),
) {
let index = index.unwrap_or_default();
if index != function_index {
let maybe_call_id = format!("{}/{}", id.unwrap_or_default(), index.unwrap_or_default());
if maybe_call_id != call_id && maybe_call_id.len() >= call_id.len() {
if !function_name.is_empty() {
let arguments: Value = function_arguments.parse().with_context(|| {
format!("Tool call '{function_name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{function_name}' have non-JSON arguments '{function_arguments}'")
})?;
handler.tool_call(ToolCall::new(
function_name.clone(),
Expand All @@ -143,7 +148,7 @@ pub async fn openai_chat_completions_streaming(
function_name.clear();
function_arguments.clear();
function_id.clear();
function_index = index;
call_id = maybe_call_id;
}
if let Some(name) = function.get("name").and_then(|v| v.as_str()) {
if name.starts_with(&function_name) {
Expand Down Expand Up @@ -240,7 +245,7 @@ pub fn openai_build_chat_completions_body(data: ChatCompletionsData, model: &Mod
vec![
json!({
"role": MessageRole::Assistant,
"content": null,
"content": "",
"tool_calls": [
{
"id": tool_result.call.id,
Expand Down Expand Up @@ -319,7 +324,7 @@ pub fn openai_extract_chat_completions(data: &Value) -> Result<ChatCompletionsOu
call["id"].as_str(),
) {
let arguments: Value = arguments.parse().with_context(|| {
format!("Tool call '{name}' is invalid: arguments must be in valid JSON format")
format!("Tool call '{name}' have non-JSON arguments '{arguments}'")
})?;
tool_calls.push(ToolCall::new(
name.to_string(),
Expand Down