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: Function calling returns invalid tool call as valid chat response #93

Merged
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
12 changes: 4 additions & 8 deletions src/generation/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl crate::Ollama {
return Ok(tool_call_result);
}

let tool_call_content: String = tool_call_result.message.clone().unwrap().content;
let tool_call_content: String = tool_call_result.message.unwrap().content;
let result = parser
.parse(
&tool_call_content,
Expand All @@ -80,8 +80,8 @@ impl crate::Ollama {
Ok(r)
}
Err(e) => {
self.add_assistant_response(id.clone(), e.message.clone().unwrap().content);
Err(OllamaError::from(e.message.unwrap().content))
self.add_assistant_response(id.clone(), e.message.clone());
Err(e)
}
}
}
Expand All @@ -108,12 +108,8 @@ impl crate::Ollama {
}

let response_content: String = result.message.clone().unwrap().content;
let result = parser
return parser
.parse(&response_content, model_name, request.tools)
.await;
match result {
Ok(r) => Ok(r),
Err(e) => Err(OllamaError::from(e.message.unwrap().content)),
}
}
}
33 changes: 10 additions & 23 deletions src/generation/functions/pipelines/meta_llama/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl LlamaFunctionCall {
model_name: String,
tool_params: Value,
tool: Arc<dyn Tool>,
) -> Result<ChatMessageResponse, ChatMessageResponse> {
) -> Result<ChatMessageResponse, OllamaError> {
let result = tool.run(tool_params).await;
match result {
Ok(result) => Ok(ChatMessageResponse {
Expand All @@ -47,7 +47,7 @@ impl LlamaFunctionCall {
done: true,
final_data: None,
}),
Err(e) => Err(self.error_handler(OllamaError::from(e))),
Err(e) => Err(OllamaError::from(e)),
}
}

Expand Down Expand Up @@ -95,32 +95,29 @@ impl RequestParserBase for LlamaFunctionCall {
input: &str,
model_name: String,
tools: Vec<Arc<dyn Tool>>,
) -> Result<ChatMessageResponse, ChatMessageResponse> {
) -> Result<ChatMessageResponse, OllamaError> {
let function_calls = self.parse_tool_response(&self.clean_tool_call(input));

if function_calls.is_empty() {
return Err(self.error_handler(OllamaError::from(
return Err(OllamaError::from(
"No valid function calls found".to_string(),
)));
));
}

let mut results = Vec::new();

for call in function_calls {
if let Some(tool) = tools.iter().find(|t| t.name() == call.function) {
let tool_params = call.arguments;
match self
let result = self
.function_call_with_history(model_name.clone(), tool_params, tool.clone())
.await
{
Ok(result) => results.push(result),
Err(e) => results.push(e),
}
.await?;
results.push(result);
} else {
results.push(self.error_handler(OllamaError::from(format!(
return Err(OllamaError::from(format!(
"Tool '{}' not found",
call.function
))));
)));
}
}

Expand All @@ -145,14 +142,4 @@ impl RequestParserBase for LlamaFunctionCall {
let system_message_content = DEFAULT_SYSTEM_TEMPLATE.replace("{tools}", &tools_json);
ChatMessage::system(system_message_content)
}

fn error_handler(&self, error: OllamaError) -> ChatMessageResponse {
ChatMessageResponse {
model: "".to_string(),
created_at: "".to_string(),
message: Some(ChatMessage::assistant(error.to_string())),
done: true,
final_data: None,
}
}
}
3 changes: 1 addition & 2 deletions src/generation/functions/pipelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ pub trait RequestParserBase: Send + Sync {
input: &str,
model_name: String,
tools: Vec<Arc<dyn Tool>>,
) -> Result<ChatMessageResponse, ChatMessageResponse>;
) -> Result<ChatMessageResponse, OllamaError>;
fn format_query(&self, input: &str) -> String {
input.to_string()
}
fn format_response(&self, response: &str) -> String {
response.to_string()
}
async fn get_system_message(&self, tools: &[Arc<dyn Tool>]) -> ChatMessage;
fn error_handler(&self, error: OllamaError) -> ChatMessageResponse;
}
31 changes: 7 additions & 24 deletions src/generation/functions/pipelines/nous_hermes/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl NousFunctionCall {
model_name: String,
tool_params: Value,
tool: Arc<dyn Tool>,
) -> Result<ChatMessageResponse, ChatMessageResponse> {
) -> Result<ChatMessageResponse, OllamaError> {
let result = tool.run(tool_params).await;
match result {
Ok(result) => Ok(ChatMessageResponse {
Expand All @@ -59,7 +59,7 @@ impl NousFunctionCall {
done: true,
final_data: None,
}),
Err(e) => Err(self.error_handler(OllamaError::from(e))),
Err(e) => Err(OllamaError::from(e)),
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ impl RequestParserBase for NousFunctionCall {
input: &str,
model_name: String,
tools: Vec<Arc<dyn Tool>>,
) -> Result<ChatMessageResponse, ChatMessageResponse> {
) -> Result<ChatMessageResponse, OllamaError> {
//Extract between <tool_call> and </tool_call>
let tool_response = self.extract_tool_call(input);
match tool_response {
Expand All @@ -110,18 +110,16 @@ impl RequestParserBase for NousFunctionCall {
.await?; //Error is also returned as String for LLM feedback
return Ok(result);
} else {
return Err(self.error_handler(OllamaError::from(
"Tool name not found".to_string(),
)));
return Err(OllamaError::from("Tool name not found".to_string()));
}
}
Err(e) => return Err(self.error_handler(OllamaError::from(e))),
Err(e) => return Err(OllamaError::from(e)),
}
}
None => {
return Err(self.error_handler(OllamaError::from(
return Err(OllamaError::from(
"Error while extracting <tool_call> tags.".to_string(),
)))
))
}
}
}
Expand All @@ -143,19 +141,4 @@ impl RequestParserBase for NousFunctionCall {
let system_message_content = DEFAULT_SYSTEM_TEMPLATE.replace("{tools}", &tools_json);
ChatMessage::system(system_message_content)
}

fn error_handler(&self, error: OllamaError) -> ChatMessageResponse {
let error_message = format!(
"<tool_response>\nThere was an error parsing function calls\n Here's the error stack trace: {}\nPlease call the function again with correct syntax</tool_response>",
error
);

ChatMessageResponse {
model: "".to_string(),
created_at: "".to_string(),
message: Some(ChatMessage::assistant(error_message)),
done: true,
final_data: None,
}
}
}
20 changes: 5 additions & 15 deletions src/generation/functions/pipelines/openai/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl OpenAIFunctionCall {
model_name: String,
tool_params: Value,
tool: Arc<dyn Tool>,
) -> Result<ChatMessageResponse, ChatMessageResponse> {
) -> Result<ChatMessageResponse, OllamaError> {
let result = tool.run(tool_params).await;
match result {
Ok(result) => Ok(ChatMessageResponse {
Expand All @@ -48,7 +48,7 @@ impl OpenAIFunctionCall {
done: true,
final_data: None,
}),
Err(e) => Err(self.error_handler(OllamaError::from(e))),
Err(e) => Err(OllamaError::from(e)),
}
}

Expand All @@ -69,7 +69,7 @@ impl RequestParserBase for OpenAIFunctionCall {
input: &str,
model_name: String,
tools: Vec<Arc<dyn Tool>>,
) -> Result<ChatMessageResponse, ChatMessageResponse> {
) -> Result<ChatMessageResponse, OllamaError> {
let response_value: Result<OpenAIFunctionCallSignature, serde_json::Error> =
serde_json::from_str(&self.clean_tool_call(input));
match response_value {
Expand All @@ -85,11 +85,11 @@ impl RequestParserBase for OpenAIFunctionCall {
.await?;
return Ok(result);
} else {
return Err(self.error_handler(OllamaError::from("Tool not found".to_string())));
return Err(OllamaError::from("Tool not found".to_string()));
}
}
Err(e) => {
return Err(self.error_handler(OllamaError::from(e)));
return Err(OllamaError::from(e));
}
}
}
Expand All @@ -100,14 +100,4 @@ impl RequestParserBase for OpenAIFunctionCall {
let system_message_content = DEFAULT_SYSTEM_TEMPLATE.replace("{tools}", &tools_json);
ChatMessage::system(system_message_content)
}

fn error_handler(&self, error: OllamaError) -> ChatMessageResponse {
ChatMessageResponse {
model: "".to_string(),
created_at: "".to_string(),
message: Some(ChatMessage::assistant(error.to_string())),
done: true,
final_data: None,
}
}
}
Loading