Skip to content

Commit

Permalink
fix: invalid request on qianwen multi tool-calls (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Nov 13, 2024
1 parent 163ab62 commit ff0ea19
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Argcfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ OPENAI_COMPATIBLE_PLATFORMS=( \
deepseek,deepseek-chat,https://api.deepseek.com \
fireworks,accounts/fireworks/models/llama-v3p1-8b-instruct,https://api.fireworks.ai/inference/v1 \
github,gpt-4o-mini,https://models.inference.ai.azure.com \
groq,llama3-8b-8192,https://api.groq.com/openai/v1 \
groq,llama-3.1-8b-instant,https://api.groq.com/openai/v1 \
hunyuan,hunyuan-large,https://api.hunyuan.cloud.tencent.com/v1 \
lingyiwanwu,yi-large,https://api.lingyiwanwu.com/v1 \
mistral,open-mistral-nemo,https://api.mistral.ai/v1 \
mistral,mistral-small-latest,https://api.mistral.ai/v1 \
moonshot,moonshot-v1-8k,https://api.moonshot.cn/v1 \
openrouter,openai/gpt-4o-mini,https://openrouter.ai/api/v1 \
ollama,llama3.1:latest,http://localhost:11434/v1 \
perplexity,llama-3.1-8b-instruct,https://api.perplexity.ai \
qianwen,qwen-turbo,https://dashscope.aliyuncs.com/compatible-mode/v1 \
qianwen,qwen-turbo-latest,https://dashscope.aliyuncs.com/compatible-mode/v1 \
siliconflow,meta-llama/Meta-Llama-3.1-8B-Instruct,https://api.siliconflow.cn/v1 \
together,meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo,https://api.together.xyz/v1 \
xai,grok-beta,https://api.x.ai/v1 \
Expand Down
5 changes: 4 additions & 1 deletion src/client/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ fn build_chat_completions_body(data: ChatCompletionsData, model: &Model) -> Resu
"content": content,
})]
}
MessageContent::ToolResults((tool_results, text)) => {
MessageContent::ToolResults(results) => {
let ToolResults {
tool_results, text, ..
} = results;
let mut assistant_parts = vec![];
let mut user_parts = vec![];
if !text.is_empty() {
Expand Down
5 changes: 4 additions & 1 deletion src/client/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ pub fn claude_build_chat_completions_body(
"content": content,
})]
}
MessageContent::ToolResults((tool_results, text)) => {
MessageContent::ToolResults(results) => {
let ToolResults {
tool_results, text, ..
} = results;
let mut assistant_parts = vec![];
let mut user_parts = vec![];
if !text.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions src/client/cohere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ fn build_chat_completions_body(data: ChatCompletionsData, model: &Model) -> Resu
.collect();
Some(json!({ "role": role, "message": list.join("\n\n") }))
}
MessageContent::ToolResults((results, _)) => {
tool_results = Some(results);
MessageContent::ToolResults(results) => {
tool_results = Some(results.tool_results);
None
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/ernie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ fn build_chat_completions_body(data: ChatCompletionsData, model: &Model) -> Valu
.flat_map(|message| {
let Message { role, content } = message;
match content {
MessageContent::ToolResults((tool_results, _)) => {
MessageContent::ToolResults(results) => {
let mut list = vec![];
for tool_result in tool_results {
for tool_result in results.tool_results {
list.push(json!({
"role": "assistant",
"content": format!("Action: {}\nAction Input: {}", tool_result.call.name, tool_result.call.arguments)
Expand Down
9 changes: 7 additions & 2 deletions src/client/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@ pub fn openai_build_chat_completions_body(data: ChatCompletionsData, model: &Mod
.flat_map(|message| {
let Message { role, content } = message;
match content {
MessageContent::ToolResults((tool_results, text)) => {
if let Some(true) = tool_results.first().map(|v| v.call.id.is_some()) {
MessageContent::ToolResults(results) => {
let ToolResults {
tool_results,
text,
sequence,
} = results;
if !sequence {
let tool_calls: Vec<_> = tool_results.iter().map(|tool_result| {
json!({
"id": tool_result.call.id,
Expand Down
3 changes: 2 additions & 1 deletion src/client/vertexai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ pub fn gemini_build_chat_completions_body(
.collect();
vec![json!({ "role": role, "parts": parts })]
},
MessageContent::ToolResults((tool_results, _)) => {
MessageContent::ToolResults(results) => {
let tool_results = results.tool_results;
let model_parts: Vec<Value> = tool_results.iter().map(|tool_result| {
json!({
"functionCall": {
Expand Down
5 changes: 2 additions & 3 deletions src/config/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,9 @@ impl Input {
pub fn merge_tool_call(mut self, output: String, tool_results: Vec<ToolResult>) -> Self {
match self.tool_call.as_mut() {
Some(exist_tool_results) => {
exist_tool_results.0.extend(tool_results);
exist_tool_results.1 = output;
exist_tool_results.extend(tool_results, output);
}
None => self.tool_call = Some((tool_results, output)),
None => self.tool_call = Some(ToolResults::new(tool_results, output)),
}
self
}
Expand Down
25 changes: 23 additions & 2 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use std::{
path::{Path, PathBuf},
};

pub type ToolResults = (Vec<ToolResult>, String);

#[cfg(windows)]
const PATH_SEP: &str = ";";
#[cfg(not(windows))]
Expand Down Expand Up @@ -268,6 +266,29 @@ impl ToolCall {
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ToolResults {
pub tool_results: Vec<ToolResult>,
pub text: String,
pub sequence: bool,
}

impl ToolResults {
pub fn new(tool_results: Vec<ToolResult>, text: String) -> Self {
Self {
tool_results,
text,
sequence: false,
}
}

pub fn extend(&mut self, tool_results: Vec<ToolResult>, _text: String) {
self.tool_results.extend(tool_results);
self.text.clear();
self.sequence = true;
}
}

#[cfg(windows)]
fn polyfill_cmd_name<T: AsRef<Path>>(cmd_name: &str, bin_dir: &[T]) -> String {
let cmd_name = cmd_name.to_string();
Expand Down
2 changes: 1 addition & 1 deletion src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ fn parse_messages(message: Vec<Value>) -> Result<Vec<Message>> {
}
output.push(Message::new(
MessageRole::Assistant,
MessageContent::ToolResults((list, text)),
MessageContent::ToolResults(ToolResults::new(list, text)),
));
tool_results = None;
} else {
Expand Down

0 comments on commit ff0ea19

Please sign in to comment.