関数呼び出しにより、モデルは外部ツールを呼び出してその機能を強化できます。知らせ#
deepseek-chat
モデルの関数呼び出し能力の現在のバージョンは不安定であり、ループされた呼び出しまたは空の応答が発生する可能性があります。私たちは修正に積極的に取り組んでおり、次のバージョンで解決されることが期待されています。サンプルコード#
機能呼び出しを使用して、完全なPythonコードで実証されたユーザーの場 所の現在の天気情報を取得する例を示します。関数呼び出しの特定のAPI形式については、チャット完了ドキュメントを参照してください。from openai import OpenAI
def send_messages(messages):
response = client.chat.completions.create(
model="deepseek-chat",
messages=messages,
tools=tools
)
return response.choices[0].message
client = OpenAI(
api_key="<your api key>",
base_url="https://api.deepseek.com",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather of an location, the user shoud 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?"}]
message = send_messages(messages)
print(f"User>\t {messages[0]['content']}")
tool = message.tool_calls[0]
messages.append(message)
messages.append({"role": "tool", "tool_call_id": tool.id, "content": "24℃"})
message = send_messages(messages)
print(f"Model>\t {message.content}")
2.
モデル:関数を返します get_weather({location: 'Hangzhou'})
3.
ユーザー:関数を呼び出します get_weather({location: 'Hangzhou'})
結果をモデルに提供します
4.
モデル:自然言語では、「杭州の現在の温度は24°Cです。」
注:上記のコードでは、 get_weather
関数の機能をユーザーが提供する必要があります。モデル自体は特定の関数を実行しません。 Modified at 2025-02-06 09:11:05