ChatCompletions API के साथ प्रतिक्रियाएं उत्पन्न करें

Complete

OpenAI ChatCompletions API का उपयोग आमतौर पर जनरेटिव AI मॉडल और प्लेटफ़ॉर्म पर किया जाता है। हालांकि नए प्रोजेक्ट के विकास के लिए रिस्पॉन्स एपीआई का सुझाव दिया जाता है, लेकिन संभावना है कि आपको ऐसे परिदृश्य मिलेंगे जहां ChatCompletions API क्रॉस-प्लेटफ़ॉर्म संगतता के कोड रखरखाव के लिए उपयोगी है।

प्रॉम्प्ट सबमिट करना

ChatCompletions API संकेतों को एनकैप्सुलेट करने के लिए JSON प्रारूप में संदेश ऑब्जेक्ट के संग्रह का उपयोग करता है:

completion = openai_client.chat.completions.create(
    model="gpt-4o",  # Your model deployment name
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "When was Microsoft founded?"}
    ]
)

print(completion.choices[0].message.content)

संवादी संदर्भ को बनाए रखना

रिस्पॉन्स एपीआई के विपरीत, चैटकॉम्प्लिटिन्स एपीआई एक स्टेटफुल रिस्पॉन्स ट्रैकिंग सुविधा प्रदान नहीं करता है। संवादात्मक संदर्भ बनाए रखने के लिए, आपको पिछले संकेतों और प्रतिक्रियाओं को मैन्युअल रूप से ट्रैक करने के लिए कोड लिखना होगा।

# Initial messages
conversation_messages=[
    {
        "role": "system",
        "content": "You are a helpful AI assistant that answers questions and provides information."
    }
]

# Add the first user message
conversation_messages.append(
    {"role": "user",
    "content": "When was Microsoft founded?"}
)

# Get a completion
completion = openai_client.chat.completions.create(
    model="gpt-4o",
    messages=conversation_messages
)
assistant_message = completion.choices[0].message.content
print("Assistant:", assistant_text)

# Append the response to the conversation
conversation_messages.append(
    {"role": "assistant", "content": assistant_text}
)

# Add the next user message
conversation_messages.append(
    {"role": "user",
    "content": "Who founded it?"}
)

# Get a completion
completion = openai_client.chat.completions.create(
    model="gpt-4o",
    messages=conversation_messages
)
assistant_message = completion.choices[0].message.content
print("Assistant:", assistant_text)

# and so on...

एक वास्तविक एप्लिकेशन में, बातचीत को एक लूप में लागू किए जाने की संभावना है; इस तरह:

# Initial messages
conversation_messages=[
    {
        "role": "system",
        "content": "You are a helpful AI assistant that answers questions and provides information."
    }
]

# Loop until the user wants to quit
print("Assistant: Enter a prompt (or type 'quit' to exit)")
while True:
    input_text = input('\nYou: ')
    if input_text.lower() == "quit":
        print("Assistant: Goodbye!")
        break

    # Add the user message
    conversation_messages.append(
        {"role": "user",
        "content": input_text}
    )

    # Get a completion
    completion = openai_client.chat.completions.create(
        model="gpt-4o",
        messages=conversation_messages
    )
    assistant_message = completion.choices[0].message.content
    print("\nAssistant:", assistant_message)
    
    # Append the response to the conversation
    conversation_messages.append(
        {"role": "assistant", "content": assistant_message}
    )

इस उदाहरण से आउटपुट इस तरह दिखता है:

Assistant: Enter a prompt (or type 'quit' to exit)

You: When was Microsoft founded?

Assistant: Microsoft was founded on April 4, 1975 in Albuquerque, New Mexico, USA.

You: Who founded it?

Assistant: Microsoft was founded by Bill Gates and Paul Allen.

You: quit

Assistant: Goodbye!

प्रत्येक नया उपयोगकर्ता संकेत और पूर्णता बातचीत में जोड़ दी जाती है, और प्रत्येक मोड़ में संपूर्ण वार्तालाप इतिहास प्रस्तुत किया जाता है।

जबकि रिस्पॉन्स एपीआई के रूप में पूरी तरह से चित्रित नहीं किया गया है, चैटकंप्लीशन एपीआई जनरेटिव एआई मॉडल इकोसिस्टम में अच्छी तरह से स्थापित है, इसलिए इससे परिचित होना उपयोगी है।