Công cụ gọi hàm: Định nghĩa hàm

Làm việc với OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

Công cụ gọi hàm

 

  • Công cụ gọi hàm: LLM gọi các hàm đã lập trình
    • Có thể tạo cho hầu như mọi việc!
  • convert_currency()
    • amount: số tiền
    • from_currency: tiền tệ gốc
    • to_currency: tiền tệ đích

frankfurter.png

1 https://frankfurter.dev/
Làm việc với OpenAI Responses API

Công cụ gọi hàm

fct1.jpg

Làm việc với OpenAI Responses API

Công cụ gọi hàm

fct2.jpg

Làm việc với OpenAI Responses API

Công cụ gọi hàm

fct3.jpg

Làm việc với OpenAI Responses API

Công cụ gọi hàm

fct4.jpg

Làm việc với OpenAI Responses API

Công cụ gọi hàm

fct5.jpg

Làm việc với OpenAI Responses API

Công cụ gọi hàm

fct6.jpg

Làm việc với 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)}"
Làm việc với OpenAI Responses API

Hàm của chúng ta

print(convert_currency(100, "USD", "EUR"))
100 USD = 86.62 EUR (Rate: 0.86625)
Làm việc với OpenAI Responses API

Ayo berlatih!

Làm việc với OpenAI Responses API

Preparing Video For Download...