Function-Calling Tools का इंटीग्रेशन
आपने convert_timezone() फंक्शन का उपयोग करके एक timezone conversion टूल बना लिया है और उसे OpenAI के tool फ़ॉर्मैट में परिभाषित कर दिया है. अब आपको पूरा function-calling वर्कफ़्लो लागू करना है. client पहले से इनिशियलाइज़ है, और tools लिस्ट में आपके timezone conversion टूल की परिभाषा मौजूद है. convert_timezone() फंक्शन भी उपयोग के लिए तैयार है.
एक messages लिस्ट शुरू की गई है जिसमें एक user इनपुट है, जिसे आपके convert_timezone टूल से timezone जानकारी चाहिए.
यह अभ्यास पाठ्यक्रम का हिस्सा है
OpenAI Responses API के साथ काम करना
अभ्यास निर्देश
- पहली Responses रिक्वेस्ट के response output items पर लूप चलाइए ताकि यह जाँच सकें कि उनमें
'function_call'to'convert_timezone'शामिल है; फिर item से unpack किए गए arguments परconvert_timezone()कॉल कीजिए और परिणामtimezone_resultमें स्टोर कीजिए. messagesलिस्ट में'function_call_output'टाइप का एक संदेश जोड़िए जिसमेंconvert_timezone()का परिणाम हो.- अंतिम Responses रिक्वेस्ट बनाइए जिसमें function result वाले messages हों और फिर से
toolsलिस्ट पास कीजिए.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
messages = [{"role": "user", "content": "What time is 2:30pm on January 20th in New York in Tokyo time?"}]
response = client.responses.create(model="gpt-5.4-mini", input=messages, tools=tools)
messages += response.output
# Process function calls and execute the timezone conversion
for item in response.output:
if item.type == "____":
if item.name == "____":
timezone_result = ____(**json.loads(item.arguments))
# Append function output to messages
messages.append({"type": "____", "call_id": item.call_id, "output": json.dumps({"convert_timezone": ____})})
# Make second API request with function results
response = client.responses.create(model="gpt-5.4-mini", input=____, tools=____)
print(response.output_text)