Alur Lengkap Function-Calling Tools

Bekerja dengan OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

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)}"
Bekerja dengan OpenAI Responses API

Function-Calling Tools

fct6.jpg

Bekerja dengan OpenAI Responses API

Definisi Tool: Manual untuk LLM

tools = [

{ "type": "function", "name": "convert_currency", "description": "Convert an amount from one currency to another using real-time exchange rates.",
"parameters": { "type": "object",
"properties": {
"amount": {"type": "number", "description": "The amount of money to convert"}, "from_currency": {"type": "string", "description": "The source currency code (e.g., 'USD', 'EUR')"}, "to_currency": {"type": "string", "description": "The target currency code (e.g., 'USD', 'EUR')"}
},
"required": ["amount", "from_currency", "to_currency"], "additionalProperties": False
}
}
]
Bekerja dengan OpenAI Responses API
response = client.responses.create(
    model="gpt-5.4-mini",
    input=[{"role": "user", "content": "How much is $100 in euros?"}],
    tools=tools
)

for item in response.output: print(item)
ResponseReasoningItem(id='rs_0a46aaf8050644c4006924978291a481909d5f50b8b9f7e959',
                      summary=[], type='reasoning', content=None, encrypted_content=None, status=None)

ResponseFunctionToolCall(arguments='{"amount":100,"from_currency":"USD","to_currency":"EUR"}',
                         call_id='call_xTbWJuO6TiuFw84TK0hU9hRT', name='convert_currency',
                         type='function_call',
                         id='fc_0a46aaf8050644c40069249784fa248190874283efa2fc2c46', status='completed')
Bekerja dengan OpenAI Responses API
messages = [{"role": "user", "content": "How much is $100 in euros?"}]

response = client.responses.create( model="gpt-5.5", input=messages, tools=tools )
messages += response.output
Bekerja dengan OpenAI Responses API
for item in response.output:
    if item.type == "function_call":
        if item.name == "convert_currency":

currency_result = convert_currency(**json.loads(item.arguments))
messages.append({ "type": "function_call_output", "call_id": item.call_id, "output": json.dumps({"convert_currency": currency_result}) })
response = client.responses.create( model="gpt-5.4-mini", instructions="Respond to the user input using the tool output.", tools=tools, input=messages, )
Bekerja dengan OpenAI Responses API
print(response.output_text)
100 USD = 86.62 EUR (rate used: 1 USD = 0.86625 EUR).

Catatan: kurs berubah dan jumlah aktual bisa berbeda karena biaya atau
penyedia yang Anda gunakan. Ingin mengonversi jumlah lain atau mata uang berbeda?
Bekerja dengan OpenAI Responses API

Ayo berlatih!

Bekerja dengan OpenAI Responses API

Preparing Video For Download...