Files
tianxuan/scripts/test_tool_loop.py
T

44 lines
2.0 KiB
Python

"""Test tool calling loop with DeepSeek"""
import json, urllib.request
tools = [{"type": "function", "function": {
"name": "profile_data", "description": "Profile dataset",
"parameters": {"type": "object", "properties": {"dataset_id": {"type": "string"}}, "required": ["dataset_id"]}
}}]
def call(messages):
payload = json.dumps({"model": "deepseek-v4-flash", "messages": messages, "tools": tools,
"tool_choice": "auto", "max_tokens": 200}).encode()
req = urllib.request.Request('https://api.deepseek.com/chat/completions', data=payload,
headers={'Content-Type': 'application/json',
'Authorization': 'Bearer sk-360ef76d59674d6b8bc2eb160327dd39'},
method='POST')
return json.loads(urllib.request.urlopen(req, timeout=10).read())
# Call 1: Get tool call
msgs = [{"role": "user", "content": "Call profile_data with ds"}]
r1 = call(msgs)
msg1 = r1["choices"][0]["message"]
tc = msg1["tool_calls"][0]
print(f'Call 1: id={tc["id"][:20]} tool={tc["function"]["name"]}')
# Call 2: Send result back (with tools still in payload)
msgs2 = msgs + [msg1, {"role": "tool", "tool_call_id": tc["id"], "content": '{"status": "ok"}'}]
try:
r2 = call(msgs2)
print(f'Call 2: {r2["choices"][0]["message"]["content"][:100]}')
except urllib.error.HTTPError as e:
print(f'Call 2 400: body={e.read().decode()[:300]}')
# Call 3: Without tools in payload
try:
payload3 = json.dumps({"model": "deepseek-v4-flash", "messages": msgs2, "max_tokens": 200}).encode()
req3 = urllib.request.Request('https://api.deepseek.com/chat/completions', data=payload3,
headers={'Content-Type': 'application/json',
'Authorization': 'Bearer sk-360ef76d59674d6b8bc2eb160327dd39'},
method='POST')
r3 = json.loads(urllib.request.urlopen(req3, timeout=10).read())
print(f'Call 3 (no tools): {r3["choices"][0]["message"]["content"][:100]}')
except urllib.error.HTTPError as e:
print(f'Call 3 400: body={e.read().decode()[:300]}')