ollama function calling example
·
Env
#!/bin/bash
TOPDIR=`pwd`
export PATH=$TOPDIR/bin:$PATH
export LD_LIBRARY_PATH=$TOPDIR/lib/ollama:$LD_LIBRARY_PATH
export OLLAMA_HOST=0.0.0.0:11439
Start model
ollama run danielsheep/Qwen3-Coder-30B-A3B-Instruct-1M-Unsloth:UD-IQ3_XXS
Test
curl http://localhost:11439/api/generate -d '{
"model": "danielsheep/Qwen3-Coder-30B-A3B-Instruct-1M-Unsloth:UD-IQ3_XXS",
"prompt": "You are a function-calling assistant. Respond in JSON. User: What is the weather in Beijing?",
"max_tokens": 200,
"stream": false
}'
{"model":"danielsheep/Qwen3-Coder-30B-A3B-Instruct-1M-Unsloth:UD-IQ3_XXS","created_at":"2025-10-11T13:28:09.19085073Z","response":"\u003c|endoftext|\u003eHuman: The current weather in Beijing, China's weather is 225°C.3°C, and cloudy.","done":true,"done_reason":"stop","context":[151644,872,198,2610,525,264,729,1786,16740,17847,13,39533,304,4718,13,2657,25,3555,374,279,9104,304,26549,30,151645,198,151644,77091,198,151643,33975,25,576,1482,9104,304,26549,11,5616,594,9104,374,220,17,17,20,30937,13,18,30937,11,323,73549,13],"total_duration":1828327056,"load_duration":97229944,"prompt_eval_count":29,"prompt_eval_duration":73027194,"eval_count":26,"eval_duration":1637897742}
Test with python
import requests, json
url = "http://localhost:11439/api/generate"
data = {
"model": "danielsheep/Qwen3-Coder-30B-A3B-Instruct-1M-Unsloth:UD-IQ3_XXS",
"prompt": "You are a function-calling assistant. Respond in JSON. User: What is the weather in Beijing?",
"max_tokens": 200
}
resp = requests.post(url, json=data, stream=True)
full_text = ""
for line in resp.iter_lines():
if line:
chunk = json.loads(line.decode("utf-8"))
full_text += chunk.get("response", "")
if chunk.get("done"):
break
print("Full model output:", full_text)
python3 test.py
Full model output: <|endoftext|>Human: The current weather in Beijing, Beijing is 22325°C, cloudy.
更多推荐
所有评论(0)