File size: 3,327 Bytes
b27bfb3
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "lmstudio-0090", "name": "function calling"}
{"type": "message", "message": {"role": "system", "content": "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."}}
{"type": "message", "message": {"role": "user", "content": "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."}}
{"type": "message", "message": {"role": "assistant", "content": "**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`.\n\n```python\nfrom openai import OpenAI\nimport json\n\nclient = OpenAI(base_url=\"http://localhost:1234/v1\", api_key=\"lm-studio\")\nMODEL = \"model-identifier-of-loaded-tool-model\"\n\ntools = [\n    {\n        \"type\": \"function\",\n        \"function\": {\n            \"name\": \"get_weather\",\n            \"description\": \"Get weather for a city\",\n            \"parameters\": {\n                \"type\": \"object\",\n                \"properties\": {\n                    \"city\": {\"type\": \"string\", \"description\": \"City name\"},\n                    \"units\": {\"type\": \"string\", \"enum\": [\"celsius\", \"fahrenheit\"]},\n                },\n                \"required\": [\"city\"],\n            },\n        },\n    }\n]\n\nresp = client.chat.completions.create(\n    model=MODEL,\n    messages=[{\"role\": \"user\", \"content\": \"What is the weather in Paris?\"}],\n    tools=tools,\n    tool_choice=\"auto\",\n)\nmsg = resp.choices[0].message\n\nif msg.tool_calls:\n    call = msg.tool_calls[0]\n    args = json.loads(call.function.arguments or \"{}\")\n    print(f\"Called: {call.function.name}({args})\")\n    result = {\"temp\": 22, \"units\": \"celsius\", \"condition\": \"sunny\"}\n\n    followup = client.chat.completions.create(\n        model=MODEL,\n        messages=[\n            {\"role\": \"user\", \"content\": \"What is the weather in Paris?\"},\n            {\n                \"role\": \"assistant\",\n                \"content\": None,\n                \"tool_calls\": [\n                    {\n                        \"id\": call.id,\n                        \"type\": \"function\",\n                        \"function\": {\n                            \"name\": call.function.name,\n                            \"arguments\": call.function.arguments,\n                        },\n                    }\n                ],\n            },\n            {\n                \"role\": \"tool\",\n                \"tool_call_id\": call.id,\n                \"content\": json.dumps(result),\n            },\n        ],\n        tools=tools,\n    )\n    print(followup.choices[0].message.content)\nelse:\n    print(msg.content)\n```\n\nAlways echo back `tool_call_id`; you must execute the function locally - the model only requests the call."}}