函式呼叫工具:定義函式

使用 OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

函式呼叫工具

 

  • 函式呼叫工具:LLM 呼叫已編寫的函式
    • 幾乎可做任何事!
  • convert_currency()
    • amount:金額
    • from_currency:原始幣別
    • to_currency:目標幣別

Frankfurter 標誌

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...