Skip to main content

Enable tool-use ability for any LLM model (DeepSeek V3/R1, etc.)

Project description

tooluser - Enable tool-use ability for any LLM model (DeepSeek V3/R1, etc.)

For some models/providers that doesn't natively support function calling (e.g. DeepSeek V3/R1), you can use this library to transform the tool calls to a user prompt, in Hermes template format by default.

from openai import AsyncOpenAI
from tooluser import make_tool_user

oai = make_tool_user(AsyncOpenAI())

res = await oai.chat.completions.create(
    model="deepseek/deepseek-chat-v3-0324", # From OpenRouter https://openrouter.ai/deepseek/deepseek-chat-v3-0324
    messages=[{"role": "user", "content": "What's the time in Shanghai?"}],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_time",
                "description": "Get the time in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The location to get the time for",
                        },
                    },
                },
            },
        }
    ],
)

Check out the example.py for a runnable example.

What's Hermes template?

Function calling is implicitly a prompt template, to make the model understand how to output the structured response as we want. Hermes template is a widely adopted prompt template for function calling.

What happens under the hood?

As we want to make use of the OpenAI chat completion API, we do not directly use Hermes template to generate the LLM instruction, but we generate the Hermes style system prompt and user prompt.

The actually API call is:

System:

<tool_instruction>
You are a function calling AI model. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions.
<tools>
['{"type": "function", "function": {"name": "get_time", "description": "Get the time in a given location", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "The location to get the time for"}}}}}']
</tools>

For each function call return a json object with function name and arguments within <tool_call> </tool_call> tags with the following schema:
<tool_call>
{"name": <function-name>, "arguments": <args-dict>}
</tool_call>

Here is an example of a tool call:
<tool_call>
{"name": "get_weather", "arguments": {"location": "San Francisco, CA", "unit": "celsius"}}
</tool_call>

</tool_instruction>

User:

What's the time in Shanghai?

Assistant:

<tool_call>
{"name": "get_time", "arguments": {"location": "Shanghai"}}
</tool_call>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tooluser-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tooluser-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file tooluser-0.1.0.tar.gz.

File metadata

  • Download URL: tooluser-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.24.0 CPython/3.13.3 Linux/6.8.0-1021-azure

File hashes

Hashes for tooluser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 30bc8e9514d073c1c039cc3efe0dfb70370c83b88e88b0b88304817368996ba7
MD5 40664f1453dd251680d87aa12583a75d
BLAKE2b-256 03844a89044a5bc5be7af002a6b3434de620fb1a1a04aadf3f19670f8c946bdb

See more details on using hashes here.

File details

Details for the file tooluser-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tooluser-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.24.0 CPython/3.13.3 Linux/6.8.0-1021-azure

File hashes

Hashes for tooluser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 105fc444a5d24bfe2a37dc50ab912e2636bf5f43ec8526ed5f05f2f6a49ca935
MD5 7e71144486dfb8669c1fc738433c4a15
BLAKE2b-256 0b645616f19e6cae429b4907a745c241815076dc51ae201a266a93d714c7228a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page