开始使用免费开始使用

指定要调用的函数

您收到了一些客户评论需要分析,并被要求从每条评论中提取产品名称、变体,以及客户情感。为了确保模型只提取这些特定信息,您决定使用函数调用,并为模型指定要使用的函数。请使用带有函数调用和 tool_choice 的 Chat Completions 端点来提取这些信息。

本练习中,messagesfunction_definition 已预加载。

本练习是课程的一部分

使用 OpenAI API 构建 AI 系统

查看课程

练习说明

  • 将您的函数定义添加为 tools。
  • 将响应强制调用 extract_review_info 函数。
  • 打印响应。

交互式实操练习

通过完成这段示例代码来试试这个练习。

client = OpenAI(api_key="")

response= client.chat.completions.create(
    model=model,
    messages=messages,
    # Add the function definition
    ____,
    # Specify the function to be called for the response
    tool_choice=____
)

# Print the response
print(____)
编辑并运行代码