调用外部 API
现在您已经有了清晰的函数定义,接下来要改进端点请求。您将使用 Chat Completions 端点,并传入一条 system 消息,确保 AI 助手知道这是货币兑换场景,并需要根据用户输入提取相应的货币代码。
本练习中,get_exchange_rate() 函数已预加载。该函数调用 ExchangeRate API,输入为一个货币代码,并返回所请求的汇率信息。用于打印输出的 print_response() 函数也已预加载。
本练习是课程的一部分
使用 OpenAI API 构建 AI 系统
练习说明
- 调用 Chat Completions 端点,并确保为
system提供处理用户提示的说明。
交互式实操练习
通过完成这段示例代码来试试这个练习。
client = OpenAI(api_key="")
# Call the Chat Completions endpoint
response = ____(
model="gpt-4o-mini",
messages=[
____,
{"role": ____, "content": "I'd like to know the current exchange rates for the Euro."}],
tools=function_definition)
print_response(response)