Add Function Calling support and OpenAI Style Conversion#908
Add Function Calling support and OpenAI Style Conversion#908MarcusDunn merged 8 commits intoutilityai:mainfrom
Conversation
Streaming Example with Toolscargo run --features cuda --release --example openai_stream -- hf-model TheBloke/Llama-2-7B-GGUF llama-2-7b.Q4_K_M.ggufOutputStreaming deltas:
{"tool_calls":[{"function":{"arguments":"","name":"get_weather"},"id":"cyrkrUxsxp2KRhcU5y0UP5yunfiFwK1f","index":0,"type":"function"}]}
{"tool_calls":[{"function":{"arguments":"{"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\""},"index":0}]}
{"tool_calls":[{"function":{"arguments":"city"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\":"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\""},"index":0}]}
{"tool_calls":[{"function":{"arguments":"Par"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"is"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\","},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\""},"index":0}]}
{"tool_calls":[{"function":{"arguments":"unit"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\":"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\""},"index":0}]}
{"tool_calls":[{"function":{"arguments":"c"},"index":0}]}
{"tool_calls":[{"function":{"arguments":"\""},"index":0}]}
{"tool_calls":[{"function":{"arguments":"}"},"index":0}]}
Final message:
{
"content": null,
"role": "assistant",
"tool_calls": [
{
"function": {
"arguments": "{\"city\":\"Paris\",\"unit\":\"c\"}",
"name": "get_weather"
},
"id": "6jYW4BsvCRuduDwghsKLzgiyTrWsv49Q",
"type": "function"
}
]
} |
|
I'll try to get around to reviewing this this weekend. Thanks for the PR. |
|
I'll look into the failures and conflicts, Also planning on some small refactoring. Thanks |
|
Refactored with minial API policy and also added OpenAI style Chat Completions server example curl --location 'localhost:8080/v1/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model": "dummy-model",
"messages": [
{
"role": "system",
"content": "You are an helpful assistant Tess"
},
{
"role": "user",
"content": "What is your name?"
}
]
}'---- OUTPUT --- {"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hi there! My name is Tess, and I'm here to assist you with any questions or tasks you may have. I'm a helpful assistant, and I'm always happy to lend a hand. What can I help you with today?","role":"assistant"}}],"created":1769269333,"id":"chatcmpl-1769269333","model":"dummy-model","object":"chat.completion","usage":{"completion_tokens":48,"prompt_tokens":26,"total_tokens":74}} |
|
Hey @MarcusDunn , Hope you are doing great. Wanted to know, when can we review this pr and get it into the next version. Thnks |
llama-cpp-2/src/lib.rs
Outdated
| llama_cpp_sys_2::llama_rs_json_schema_to_grammar(schema_cstr.as_ptr(), false, &mut out) | ||
| }; | ||
|
|
||
| let result = (|| { |
There was a problem hiding this comment.
is this a lambda for a reason? can it just be a block?
There was a problem hiding this comment.
No Strong reason to use IIFE here, Block would work. The only intent was to scope the Result while ensuring the FFI Free happens. I can swith to block to avoid IIFE confusion.
llama-cpp-2/src/openai.rs
Outdated
| ) | ||
| }; | ||
|
|
||
| let result = (|| { |
There was a problem hiding this comment.
again, unsure about iife.
There was a problem hiding this comment.
Same as above, Can change to block.
llama-cpp-2/src/sampling.rs
Outdated
|
|
||
| /// Try accepting a token from the sampler. Returns an error if the sampler throws. | ||
| pub fn try_accept(&mut self, token: LlamaToken) -> Result<(), SamplerAcceptError> { | ||
| let rc = unsafe { llama_cpp_sys_2::llama_rs_sampler_accept(self.sampler, token.0) }; |
There was a problem hiding this comment.
is this reference counted? if no, please change name.
There was a problem hiding this comment.
Naming rc was ResultCheck, Yes it seems to confusing the Rust nomenclature. WIll change it to sampler_result instead.
MarcusDunn
left a comment
There was a problem hiding this comment.
I'm not qualified to review the cpp unfortunately. See comments. Otherwise looks good.
reviewed. If tests. pass I'll merge. |
|
Hi @MarcusDunn , Checking the review comments on C++, and will revert back. Thanks |
|
long as linux and Mac pass, I'm happy, ideally windows stays functional, but It's very much a best-effort-basis. |
Okay, Sorry for the to and fro, Updated the fix for review comments. |
Uh oh!
There was an error while loading. Please reload this page.