githubEdit

Tool Use/Function Calling

Crynux Bridge supports the standard OpenAI Tool Use (Function Calling) API. This allows you to describe functions to the model, and have the model intelligently choose to output a JSON object containing arguments to call one or many of those functions.

circle-exclamation

The following examples demonstrate how to use the tool calling feature with the OpenAI SDK, the dedicated langchain-crynux library, and the standard langchain-openai library.

When using the official openai Python SDK, you define tools as dictionaries and pass them to the tools parameter. The vram_limit is passed via extra_body.

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://bridge.crynux.io/v1/llm",
    api_key="your-api-key",
)

# 1. Define the tool
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        },
    }
]

# 2. Call the model
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
completion = client.chat.completions.create(
    model="Qwen/Qwen2.5-7B-Instruct",
    messages=messages,
    tools=tools,
    tool_choice="auto",
    extra_body={
        "vram_limit": 24
    }
)

response_message = completion.choices[0].message
tool_calls = response_message.tool_calls

if tool_calls:
    print("Tool calls detected:")
    for tool_call in tool_calls:
        print(f"Function: {tool_call.function.name}")
        print(f"Arguments: {tool_call.function.arguments}")

Last updated