tinycodinglm / lmstudio /lmstudio-0090.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.33 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How does function/tool calling work in LM Studio with the OpenAI-compatible tools API? Show defining a tool, handling tool_calls, and sending the tool result back. Include code examples.

Assistant

Tool calling requires a tool-capable model loaded (e.g. Hermes 2 Pro family). Define JSON-schema tools, then handle tool_calls and reply with role="tool" including tool_call_id.

python
from openai import OpenAI
import json

client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
MODEL = "model-identifier-of-loaded-tool-model"

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a city",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name"},
                    "units": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["city"],
            },
        },
    }
]

resp = client.chat.completions.create(
    model=MODEL,
    messages=[{"role": "user", "content": "What is the weather in Paris?"}],
    tools=tools,
    tool_choice="auto",
)
msg = resp.choices[0].message

if msg.tool_calls:
    call = msg.tool_calls[0]
    args = json.loads(call.function.arguments or "{}")
    print(f"Called: {call.function.name}({args})")
    result = {"temp": 22, "units": "celsius", "condition": "sunny"}

    followup = client.chat.completions.create(
        model=MODEL,
        messages=[
            {"role": "user", "content": "What is the weather in Paris?"},
            {
                "role": "assistant",
                "content": None,
                "tool_calls": [
                    {
                        "id": call.id,
                        "type": "function",
                        "function": {
                            "name": call.function.name,
                            "arguments": call.function.arguments,
                        },
                    }
                ],
            },
            {
                "role": "tool",
                "tool_call_id": call.id,
                "content": json.dumps(result),
            },
        ],
        tools=tools,
    )
    print(followup.choices[0].message.content)
else:
    print(msg.content)

Always echo back tool_call_id; you must execute the function locally - the model only requests the call.