import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
import json
tools = [{'type': 'function', 'function': {'name': 'add', 'description': 'Add two integers.', 'parameters': {'type': 'object', 'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}}, 'required': ['a', 'b'], 'additionalProperties': False}}}]
messages = [{'role': 'user', 'content': 'Use the add tool to calculate 19 + 23, then reply with the number only.'}]
first = client.chat.completions.create(model='deepseek-flash', messages=messages, tools=tools, tool_choice={'type': 'function', 'function': {'name': 'add'}}, max_tokens=1024)
message = first.choices[0].message
assert first.choices[0].finish_reason == 'tool_calls'
assert len(message.tool_calls or []) == 1
messages.append(message.model_dump(exclude_none=True))
call = message.tool_calls[0]
assert call.function.name == 'add'
args = json.loads(call.function.arguments)
assert args == {'a': 19, 'b': 23}
messages.append({'role': 'tool', 'tool_call_id': call.id, 'content': str(args['a'] + args['b'])})
second = client.chat.completions.create(model='deepseek-flash', messages=messages, tools=tools, tool_choice='none', max_tokens=1024)
assert second.choices[0].finish_reason == 'stop'
assert second.choices[0].message.content.strip() == '42'
print(second.choices[0].message.content)