shim3: native tools, tool_result, SSE tool_calls; anthropic bridge; docs/compat.md
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import json, sys, time, urllib.request, urllib.error
|
||||
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
||||
B='http://127.0.0.1:8833'
|
||||
TOOLS=[{"type":"function","function":{"name":"get_weather","description":"Weather for a city","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}]
|
||||
|
||||
def post(path, body, raw=False):
|
||||
req=urllib.request.Request(B+path, data=json.dumps(body).encode(), headers={'content-type':'application/json'})
|
||||
t0=time.time()
|
||||
try:
|
||||
r=urllib.request.urlopen(req, timeout=280)
|
||||
data=r.read().decode()
|
||||
return r.status, data, time.time()-t0
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, e.read().decode(), time.time()-t0
|
||||
|
||||
def j(d):
|
||||
try: return json.loads(d)
|
||||
except Exception: return None
|
||||
|
||||
print('=== 1 plain')
|
||||
c,d,t=post('/v1/chat/completions', {"model":"claude-haiku-5-5","max_tokens":40,"messages":[{"role":"user","content":"Ответь ровно: OK1"}]})
|
||||
o=j(d); print(c, '%.1fs'%t, '|', (o or {}).get('choices',[{}])[0].get('message',{}).get('content','')[:50], '| usage', (o or {}).get('usage'))
|
||||
|
||||
print('=== 2 stream')
|
||||
c,d,t=post('/v1/chat/completions', {"model":"claude-haiku-5-5","max_tokens":40,"stream":True,"stream_options":{"include_usage":True},"messages":[{"role":"user","content":"Считай до 3"}]})
|
||||
lines=[l for l in d.split('\n') if l.startswith('data: ')]
|
||||
print(c, '%.1fs'%t, 'chunks', len(lines), '| done', 'data: [DONE]' in d, '| usage_in_stream', 'usage' in d)
|
||||
|
||||
print('=== 3 tools')
|
||||
c,d,t=post('/v1/chat/completions', {"model":"claude-haiku-5-5","max_tokens":200,"tools":TOOLS,"tool_choice":"auto",
|
||||
"messages":[{"role":"user","content":"Какая погода в Берлине? Используй инструмент."}]})
|
||||
o=j(d); ch=(o or {}).get('choices',[{}])[0]; msg=ch.get('message',{})
|
||||
tc=msg.get('tool_calls') or []
|
||||
print(c, '%.1fs'%t, 'finish', ch.get('finish_reason'), 'calls', len(tc))
|
||||
for x in tc: print(' ', x['function']['name'], x['function']['arguments'][:80])
|
||||
|
||||
print('=== 4 tools+stream')
|
||||
c,d,t=post('/v1/chat/completions', {"model":"claude-haiku-5-5","max_tokens":200,"stream":True,"tools":TOOLS,
|
||||
"messages":[{"role":"user","content":"Погода в Париже? Вызови инструмент."}]})
|
||||
print(c, '%.1fs'%t, 'tool_calls_in_sse', 'tool_calls' in d, '| finish tool_calls', '"tool_calls"' in d)
|
||||
|
||||
print('=== 5 tool_result roundtrip')
|
||||
msgs=[{"role":"user","content":"Погода в Берлине?"},
|
||||
{"role":"assistant","content":None,"tool_calls":[{"id":"call_1","type":"function","function":{"name":"get_weather","arguments":"{\"city\":\"Berlin\"}"}}]},
|
||||
{"role":"tool","tool_call_id":"call_1","name":"get_weather","content":"{\"temp\":7,\"sky\":\"cloudy\"}"},
|
||||
{"role":"user","content":"Скажи температуру одним словом."}]
|
||||
c,d,t=post('/v1/chat/completions', {"model":"claude-haiku-5-5","max_tokens":60,"tools":TOOLS,"messages":msgs})
|
||||
o=j(d); print(c, '%.1fs'%t, '|', (o or {}).get('choices',[{}])[0].get('message',{}).get('content','')[:70])
|
||||
|
||||
print('=== 6 anthropic messages')
|
||||
c,d,t=post('/v1/messages', {"model":"claude-haiku-5-5","max_tokens":60,"messages":[{"role":"user","content":"Ответь: ANT-OK"}]})
|
||||
o=j(d); print(c, '%.1fs'%t, 'type', (o or {}).get('type'), '|', json.dumps((o or {}).get('content'), ensure_ascii=False)[:90])
|
||||
|
||||
print('=== 7 anthropic tools')
|
||||
AT=[{"name":"get_weather","description":"Weather for a city","input_schema":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}]
|
||||
c,d,t=post('/v1/messages', {"model":"claude-haiku-5-5","max_tokens":200,"tools":AT,
|
||||
"messages":[{"role":"user","content":"Погода в Риме? Вызови инструмент."}]})
|
||||
o=j(d); print(c, '%.1fs'%t, 'stop_reason', (o or {}).get('stop_reason'), '| blocks', [b.get('type') for b in ((o or {}).get('content') or [])])
|
||||
|
||||
print('=== 8 responses api')
|
||||
c,d,t=post('/v1/responses', {"model":"claude-haiku-5-5","input":"Ответь: RESP-OK"})
|
||||
o=j(d); print(c, '%.1fs'%t, 'status', (o or {}).get('status'), '|', json.dumps((o or {}).get('output'), ensure_ascii=False)[:100])
|
||||
|
||||
print('=== 9 unknown model')
|
||||
c,d,t=post('/v1/chat/completions', {"model":"nope-model-xyz","messages":[{"role":"user","content":"hi"}]})
|
||||
print(c, '%.1fs'%t, d[:110])
|
||||
Reference in New Issue
Block a user