Compare commits
2 Commits
66e8b114b9
...
57aa0aa70e
| Author | SHA1 | Date | |
|---|---|---|---|
| 57aa0aa70e | |||
| 4de457cc5e |
@@ -731,6 +731,8 @@ impl super::Provider for OpenAIProvider {
|
||||
let mut es = es;
|
||||
let mut content_buffer = String::new();
|
||||
let mut has_tool_calls = false;
|
||||
let mut tool_index_map = std::collections::HashMap::<u32, u32>::new();
|
||||
let mut next_tool_index = 0u32;
|
||||
|
||||
while let Some(event) = es.next().await {
|
||||
match event {
|
||||
@@ -765,8 +767,15 @@ impl super::Provider for OpenAIProvider {
|
||||
let call_id = item.get("call_id").and_then(|v| v.as_str());
|
||||
let name = item.get("name").and_then(|v| v.as_str());
|
||||
|
||||
let out_idx = chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
||||
let tc_idx = *tool_index_map.entry(out_idx).or_insert_with(|| {
|
||||
let i = next_tool_index;
|
||||
next_tool_index += 1;
|
||||
i
|
||||
});
|
||||
|
||||
tool_calls = Some(vec![crate::models::ToolCallDelta {
|
||||
index: chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
|
||||
index: tc_idx,
|
||||
id: call_id.map(|s| s.to_string()),
|
||||
call_type: Some("function".to_string()),
|
||||
function: Some(crate::models::FunctionCallDelta {
|
||||
@@ -780,8 +789,16 @@ impl super::Provider for OpenAIProvider {
|
||||
"response.function_call_arguments.delta" => {
|
||||
if let Some(delta) = chunk.get("delta").and_then(|v| v.as_str()) {
|
||||
has_tool_calls = true;
|
||||
|
||||
let out_idx = chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
|
||||
let tc_idx = *tool_index_map.entry(out_idx).or_insert_with(|| {
|
||||
let i = next_tool_index;
|
||||
next_tool_index += 1;
|
||||
i
|
||||
});
|
||||
|
||||
tool_calls = Some(vec![crate::models::ToolCallDelta {
|
||||
index: chunk.get("output_index").and_then(|v| v.as_u64()).unwrap_or(0) as u32,
|
||||
index: tc_idx,
|
||||
id: None,
|
||||
call_type: None,
|
||||
function: Some(crate::models::FunctionCallDelta {
|
||||
@@ -821,9 +838,11 @@ impl super::Provider for OpenAIProvider {
|
||||
|
||||
// Yield the tool calls in two chunks to mimic standard streaming behavior
|
||||
// Chunk 1: Initialization (id, name)
|
||||
let init_deltas: Vec<crate::models::ToolCallDelta> = embedded_calls.iter().enumerate().map(|(idx, tc)| {
|
||||
let init_deltas: Vec<crate::models::ToolCallDelta> = embedded_calls.iter().map(|tc| {
|
||||
let tc_idx = next_tool_index;
|
||||
next_tool_index += 1;
|
||||
crate::models::ToolCallDelta {
|
||||
index: idx as u32,
|
||||
index: tc_idx,
|
||||
id: Some(tc.id.clone()),
|
||||
call_type: Some("function".to_string()),
|
||||
function: Some(crate::models::FunctionCallDelta {
|
||||
@@ -843,9 +862,13 @@ impl super::Provider for OpenAIProvider {
|
||||
};
|
||||
|
||||
// Chunk 2: Payload (arguments)
|
||||
let arg_deltas: Vec<crate::models::ToolCallDelta> = embedded_calls.into_iter().enumerate().map(|(idx, tc)| {
|
||||
// Reset temp index for payload chunk
|
||||
let mut payload_idx = next_tool_index - embedded_calls.len() as u32;
|
||||
let arg_deltas: Vec<crate::models::ToolCallDelta> = embedded_calls.into_iter().map(|tc| {
|
||||
let tc_idx = payload_idx;
|
||||
payload_idx += 1;
|
||||
crate::models::ToolCallDelta {
|
||||
index: idx as u32,
|
||||
index: tc_idx,
|
||||
id: None,
|
||||
call_type: None,
|
||||
function: Some(crate::models::FunctionCallDelta {
|
||||
|
||||
Reference in New Issue
Block a user