डुअल-प्रॉम्प्ट get_response() फंक्शन बनाना
आगे के अभ्यासों में आप OpenAI API के chat.completions endpoint को दो प्रॉम्प्ट्स (एक system प्रॉम्प्ट और एक user प्रॉम्प्ट) के साथ कॉल करेंगे। इसकी तैयारी के लिए, इस अभ्यास में आप एक डुअल-प्रॉम्प्ट get_response() फंक्शन बनाएँगे जो दो प्रॉम्प्ट्स (system_prompt और user_prompt) इनपुट के रूप में लेता है और आउटपुट में प्रतिक्रिया लौटाता है। इसके बाद, आप इस फंक्शन को अपनी पसंद के किसी भी उदाहरण पर लागू करेंगे.
OpenAI पैकेज आपके लिए पहले से लोड किया गया है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
OpenAI API के साथ Prompt Engineering
अभ्यास निर्देश
messagesलिस्ट में प्रत्येक संदेश का role और content असाइन करें.- अपनी पसंद का
system_promptऔरuser_promptपास करके फंक्शन को आज़माएँ.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
client = OpenAI(api_key="")
def get_response(system_prompt, user_prompt):
# Assign the role and content for each message
messages = [{"role": ____, "content": ____},
{"role": ____, "content": ____}]
response = client.chat.completions.create(
model="gpt-4o-mini", messages= messages, temperature=0)
return response.choices[0].message.content
# Try the function with a system and user prompts of your choice
response = get_response("____", "____")
print(response)