Files
2026-09-23 21:27:55 +08:00

55 lines
1.6 KiB
Python

from openai import OpenAI
client = OpenAI(
api_key='sk-83c3ebe6b1b04bdf93c3c4be9b17736d',
base_url="https://api.deepseek.com",
)
def send_messages(messages,model="deepseek-flash"):
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools
)
return response.choices[0].message
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of a location, the user should supply a location first.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"]
},
}
},
]
messages = [{"role": "user", "content": "How's the weather in Hangzhou, Zhejiang?"}]
AI_message = send_messages(messages)
print(AI_message,type(AI_message))
print(f"User>\t {messages[0]['content']}")
print(f"AI>\t {AI_message.content}")
# AI_message_dict={"role": "assitant", "content": "I'll check the current weather in Hangzhou, Zhejiang for you."}
tool = AI_message.tool_calls[0]
messages.append(AI_message)
# messages.append(AI_message_dict)
# =================================
# 调用函数返回结果
tool_result={"role": "tool", "tool_call_id": tool.id, "content": "24℃"}
# =================================
messages.append(tool_result)
print(messages)
message = send_messages(messages)
print(f"Model>\t {message.content}")