기능 호출을 통해 모델은 외부 도구를 호출하여 기능을 향상시킬 수 있습니다.알아채다#
deepseek-chat Model의 기능 호출 기능 호출의 현재 버전은 불안정하여 루프가 루프 또는 빈 응답을 초래할 수 있습니다. 우리는 수정을 적극적으로 작업하고 있으며 다음 버전에서 해결 될 것으로 예상됩니다.**샘플 코드#
다음은 기능 호출을 사용하여 사용자 위치의 현재 날씨 정보를 얻는 예입니다.특정 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}")
1.
사용자 : 항저우의 현재 날씨에 대해 묻습니다
2.
모델 : 함수를 반환합니다 get_weather({location: 'Hangzhou'})
3.
사용자 : 기능을 호출합니다 get_weather({location: 'Hangzhou'}) 모델에 결과를 제공합니다
4.
모델 : 자연어로 돌아와서 "항저우의 현재 온도는 24 ° C입니다."
참고 : 위 코드에서는 get_weather 함수의 기능을 사용자가 제공해야합니다. 모델 자체는 특정 기능을 실행하지 않습니다. Modified at 2025-02-06 09:55:58