-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support tools #1587
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
Support tools #1587
Conversation
This PR adds basic support for tools/functions. The api follows the same format as openai and can be used with the note current differences between TGI functions and openai:
example use (with TGI running locally) from openai import OpenAI
# Initialize the client, pointing it to one of the available models
client = OpenAI(
base_url="http://localhost:3000/v1",
api_key="_",
)
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
},
"required": ["location", "format"],
},
},
},
{
"type": "function",
"function": {
"name": "get_n_day_weather_forecast",
"description": "Get an N-day weather forecast",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
"num_days": {
"type": "integer",
"description": "The number of days to forecast",
},
},
"required": ["location", "format", "num_days"],
},
},
}
] Use one of the provided toolschat_completion = client.chat.completions.create(
model="tgi",
messages=[
{
"role": "system",
"content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.",
},
{
"role": "user",
"content": "What's the weather like the next 3 days in San Francisco, CA?",
},
],
tools=tools,
tool_choice="auto", # tool selected by model
max_tokens=500,
)
called = chat_completion.choices[0].message.tool_calls
print(called)
# {
# "id": 0,
# "type": "function",
# "function": {
# "description": None,
# "name": "tools",
# "parameters": {
# "format": "celsius",
# "location": "San Francisco, CA",
# "num_days": 3,
# },
# },
# } Use specific toolchat_completion = client.chat.completions.create(
model="tgi",
messages=[
{
"role": "system",
"content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.",
},
{
"role": "user",
"content": "What's the weather like the next 3 days in San Francisco, CA?",
},
],
tools=tools,
tool_choice="get_current_weather", # tool selected by caller
max_tokens=500,
)
called = chat_completion.choices[0].message.tool_calls
print(called)
# {
# "id": 0,
# "type": "function",
# "function": {
# "description": None,
# "name": "tools",
# "parameters": {"format": "celsius", "location": "San Francisco, CA"},
# },
# }
|
Looking good overall. We definitely need some docs ! |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Thanks @drbh ! A few thoughts/points:
|
Hi, nice Jobs, I find a inconsistent with the output of the selected tool accord to the open ai api specification, I create the issue in #1624 The PR only modify the server.rs and the lib.rs, and basically the improvement is on the grammar generated for the tool selection inference, the grammar of the PR complains the json schema standar and allows the llm to indicate the name of the selected function, also the struct og the grammar fits the open ai schema now the ouput for:
code output: open ai specs from docs outputs: llm raw output from TGI debug tracing:
I did not touch the streaming methods and the objects ChatCompletionChunk and ChatCompletionDelta, my rust uderstanding is quiet pretty basic. |
@drbh @Narsil Is there any documentation (or examples) on
This would be very helpful for everyone training models to increase reliability of Tool/calls and structured output. Unfortunately there is no single standard for all large inference libraries and almost all models use different formats (requiring custom wrappers) as well - it would be a huge win for the whole OSS LLM community if you add a detailed documentation of formats which hopefully could then also be adopted by other inference libs and guarantee OpenAI compatibility.... many thanks! |
+1 I couldn't have put this better myself |
have solved but these days I have not had time to investigate how to create the tests and the pull request, I am too oversold with my work, but if someone can support us, I can gladly deliver the changes made in the few files that were necessary to modify so that some member can be generated the pull (I've never made a contribution with Git, it's my first time, and unfortunately I haven't had the space for the learning curve) |
This work in progress PR begins to add support for tools. Tools relies on grammar support and still has some unsolved challenges. Opening the PR for visibility and feedback
This work in progress PR begins to add support for tools. Tools relies on grammar support and still has some unsolved challenges. Opening the PR for visibility and feedback