函数调用工具:定义函数

使用 OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

函数调用工具

 

  • 反问器调用工具:LLM 调用编程函数
    • 几乎可用于任何任务!
  • convert_currency()
    • amount:金额
    • from_currency:原币种
    • to_currency:目标币种

frankfurter.png

1 https://frankfurter.dev/
使用 OpenAI Responses API

函数调用工具

fct1.jpg

使用 OpenAI Responses API

函数调用工具

fct2.jpg

使用 OpenAI Responses API

函数调用工具

fct3.jpg

使用 OpenAI Responses API

函数调用工具

fct4.jpg

使用 OpenAI Responses API

函数调用工具

fct5.jpg

使用 OpenAI Responses API

函数调用工具

fct6.jpg

使用 OpenAI Responses API
def convert_currency(amount, from_currency, to_currency):

url = f"https://api.frankfurter.dev/v1/latest?base={from_currency}&symbols={to_currency}"
try: response = requests.get(url) response.raise_for_status()
data = response.json() rate = data['rates'].get(to_currency)
if rate is None: return f"Could not find exchange rate for {from_currency} to {to_currency}"
converted_amount = amount * rate return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: {rate})"
except requests.exceptions.RequestException as e: return f"Error converting currency: {str(e)}"
使用 OpenAI Responses API

我们的函数

print(convert_currency(100, "USD", "EUR"))
100 USD = 86.62 EUR (Rate: 0.86625)
使用 OpenAI Responses API

让我们练习!

使用 OpenAI Responses API

Preparing Video For Download...