AI चैटबॉट बनाना
अपने POC को पूरा करने के लिए, आप अपनी मैसेज हिस्ट्री को for लूप के साथ इंटीग्रेट करेंगे, ताकि आप मॉडल को बार-बार प्रॉम्प्ट भेज सकें और हर प्रतिक्रिया को क्रम से मैसेज हिस्ट्री में स्टोर कर सकें.
यह अभ्यास पाठ्यक्रम का हिस्सा है
OpenAI API के साथ काम करना
अभ्यास निर्देश
- यूज़र मैसेज (
user_msgs) पर लूप चलाएँ. - हर iteration में यूज़र मैसेज के लिए एक dictionary बनाएँ और उसे
messagesमें append करें. - चैट रिक्वेस्ट में
messagesको मॉडल को भेजें. - असिस्टेंट मैसेज की dictionary को
messagesमें append करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
client = OpenAI(api_key="")
messages = [{"role": "system", "content": "You are a helpful math tutor that speaks concisely."}]
user_msgs = ["Explain what pi is.", "Summarize this in two bullet points."]
# Loop over the user questions
for q in ____:
print("User: ", q)
# Create a dictionary for the user message from q and append to messages
user_dict = {"role": ____, "content": ____}
messages.append(____)
# Create the API request
response = client.chat.completions.create(
model="gpt-4o-mini",
____,
max_completion_tokens=100
)
# Append the assistant's message to messages
assistant_dict = {"role": "assistant", "content": response.choices[0].message.content}
messages.append(____)
print("Assistant: ", response.choices[0].message.content, "\n")