DeepSeek-Coder icon indicating copy to clipboard operation
DeepSeek-Coder copied to clipboard

Function call sample code 需要更新下

Open markshao opened this issue 5 months ago • 0 comments

当前的 function call 官方文档 https://platform.deepseek.com/api-docs/guides/function_calling

最新的 openai python sdk openai==1.42.0

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)

直接访问 message.tool_calls 会报错 AttributeError

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ilya/.cache/pypoetry/virtualenvs/buffett-4Wtakw1Y-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 828, in __getattr__
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'ChatCompletion' object has no attribute 'tool_calls'

看了下 openai 的 doc https://platform.openai.com/docs/guides/function-calling , 协议有些变化, function call 是在 Choice 里

Choice(
    finish_reason='tool_calls', 
    index=0, 
    logprobs=None, 
    message=chat.completionsMessage(
        content=None, 
        role='assistant', 
        function_call=None, 
        tool_calls=[
            chat.completionsMessageToolCall(
                id='call_62136354', 
                function=Function(
                    arguments='{"order_id":"order_12345"}', 
                    name='get_delivery_date'), 
                type='function')
        ])
)

markshao avatar Aug 30 '24 05:08 markshao