-
Notifications
You must be signed in to change notification settings - Fork 719
feat(rig-1197): handle llama.cpp tool call (#1408) #1409
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,8 @@ use crate::one_or_many::string_or_one_or_many; | |
| use crate::telemetry::{ProviderResponseExt, SpanCombinator}; | ||
| use crate::wasm_compat::{WasmCompatSend, WasmCompatSync}; | ||
| use crate::{OneOrMany, completion, json_utils, message}; | ||
| use serde::{Deserialize, Serialize, Serializer}; | ||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
| use serde_json::Value; | ||
| use std::convert::Infallible; | ||
| use std::fmt; | ||
| use tracing::{Instrument, Level, enabled, info_span}; | ||
|
|
@@ -402,10 +403,28 @@ impl TryFrom<crate::message::ToolChoice> for ToolChoice { | |
| #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] | ||
| pub struct Function { | ||
| pub name: String, | ||
| #[serde(with = "json_utils::stringified_json")] | ||
| #[serde( | ||
| serialize_with = "json_utils::stringified_json::serialize", | ||
| deserialize_with = "deserialize_arguments" | ||
| )] | ||
| pub arguments: serde_json::Value, | ||
| } | ||
|
|
||
| /// the arguments for tool calls in the openai API are an JSON object encoded as a string | ||
| /// the arguments for tool calls in llama.cpp are already an JSON object (that is not stringified) | ||
| /// therefore we handle both cases here | ||
| fn deserialize_arguments<'de, D>(deserializer: D) -> Result<Value, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| let value = Value::deserialize(deserializer)?; | ||
|
|
||
| match value { | ||
| Value::String(s) => serde_json::from_str(&s).map_err(serde::de::Error::custom), | ||
| other => Ok(other), | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
+416
to
+427
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add docstring to explain why this is required (it's not immediately clear to anyone who reads this in the future if they don't refer back to this PR)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea. Added a comment for that. |
||
| impl TryFrom<message::ToolResult> for Message { | ||
| type Error = message::MessageError; | ||
|
|
||
|
|
@@ -1520,4 +1539,38 @@ mod tests { | |
|
|
||
| assert!(matches!(result, Err(CompletionError::RequestError(_)))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserialize_llama_cpp_tool_call() { | ||
| let request = r#"{ | ||
| "choices": [{ | ||
| "finish_reason": "tool_calls", | ||
| "index": 0, | ||
| "message": { | ||
| "role": "assistant", | ||
| "content": "", | ||
| "tool_calls": [{ "type": "function", "function": { "name": "hello_world", "arguments": {} }, "id": "xxx" }] | ||
| } | ||
| }], | ||
| "created": 0, | ||
| "model": "unsloth/Qwen3-Coder-Next-GGUF:Q8_0", | ||
| "system_fingerprint": "b8113-xxxx", | ||
| "object": "chat.completion", | ||
| "usage": { "completion_tokens": 13, "prompt_tokens": 255, "total_tokens": 268 }, | ||
| "id": "xxx", | ||
| "timings": { | ||
| "cache_n": 0, | ||
| "prompt_n": 255, | ||
| "prompt_ms": 670, | ||
| "prompt_per_token_ms": 2.63, | ||
| "prompt_per_second": 380, | ||
| "predicted_n": 13, | ||
| "predicted_ms": 367, | ||
| "predicted_per_token_ms": 28, | ||
| "predicted_per_second": 35 | ||
| } | ||
| } | ||
| "#; | ||
| serde_json::from_str::<ApiResponse<CompletionResponse>>(&request).unwrap(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct? It seems to have been perfectly fine before to my knowledge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For openai its fine. But with llama.cpp there is a problem. See here #1408