Files
deepai-code-autoreg/tushim.py
T

52 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
"""tool use + round-trip против локального шима: python3 tushim.py <base> [models...]"""
import json
import sys
import urllib.request
BASE = sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:8840/v1/chat/completions"
MODELS = sys.argv[2:] or ["claude-opus-5-5", "glm-5.3-flash"]
TOOLS = [{"type": "function", "function": {
"name": "get_weather", "description": "Get weather for a city",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}}]
def post(body, timeout=120):
req = urllib.request.Request(BASE, data=json.dumps(body).encode(),
headers={"Content-Type": "application/json",
"Authorization": "Bearer none"})
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.loads(r.read().decode())
for m in MODELS:
base = {"model": m, "tools": TOOLS, "tool_choice": "auto", "max_tokens": 300,
"messages": [{"role": "user", "content": "Weather in Paris? Use the tool."}]}
try:
d = post(base)
except Exception as e:
print("%-20s ERR %s" % (m, str(e)[:70]), flush=True)
continue
ch = (d.get("choices") or [{}])[0]
msg = ch.get("message") or {}
tc = msg.get("tool_calls") or []
if not tc:
print("%-20s NO_TOOL finish=%s" % (m, ch.get("finish_reason")), flush=True)
continue
fn = tc[0].get("function") or {}
rt = "rt_fail"
try:
body2 = dict(base)
body2.pop("tools")
body2.pop("tool_choice")
body2["messages"] = base["messages"] + [
{"role": "assistant", "content": None, "tool_calls": tc},
{"role": "tool", "tool_call_id": tc[0].get("id") or "call_1",
"content": json.dumps({"temp_c": 11, "sky": "cloudy"})}]
d2 = post(body2)
txt = ((d2.get("choices") or [{}])[0].get("message") or {}).get("content") or ""
rt = "rt_ok:%r" % txt[:50]
except Exception as e:
rt = "rt_err %s" % str(e)[:50]
print("%-20s TOOL %s(%s) %s" % (m, fn.get("name"), fn.get("arguments"), rt), flush=True)