from arkruntime import Ark
from arkruntime.types.chat import ChatCompletion
base_url="https://ark.cn-beijing.volces.com/api/v3",
{"role": "user", "content": "北京和上海今天的天气如何?"}
"name": "get_current_weather",
"description": "获取指定地点的天气信息",
"description": "地点的位置信息,例如北京、上海"
"required": ["location"]
def get_current_weather(location: str, unit="摄氏度"):
return f"{location}今天天气晴朗,温度 25 {unit}。"
# 步骤2: 发起模型请求,由于模型在收到工具执行结果后仍然可能有工具调用意愿,因此需要多次请求
completion: ChatCompletion = client.chat.completions.create(
model="doubao-seed-2-1-pro-260628",
resp_msg = completion.choices[0].message
if completion.choices[0].finish_reason != "tool_calls":
messages.append(completion.choices[0].message.model_dump())
tool_calls = completion.choices[0].message.tool_calls
for tool_call in tool_calls:
tool_name = tool_call.function.name
if tool_name == "get_current_weather":
args = json.loads(tool_call.function.arguments)
tool_result = get_current_weather(**args)
{"role": "tool", "content": tool_result, "tool_call_id": tool_call.id}