توليد الردود باستخدام واجهة برمجة تطبيقات ChatCompleteletions

مكتمل

نصيحة

راجع علامة التبويب النص والصور لمزيد من التفاصيل!

يستخدم واجهة OpenAI ChatCompletetions API بشكل شائع عبر نماذج ومنصات الذكاء الاصطناعي التوليدي. على الرغم من أن واجهة برمجة تطبيقات الاستجابات موصى بها لتطوير المشاريع الجديدة، من المحتمل أن تواجه سيناريوهات تكون فيها واجهة ChatCompleteions مفيدة لصيانة الكود أو التوافق بين المنصات.

تقديم طلب

تستخدم واجهة برمجة تطبيقات ChatCompletetions مجموعات من كائنات الرسائل بصيغة 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)

الحفاظ على السياق الحواري

على عكس واجهة برمجة تطبيقات الاستجابات ، لا توفر واجهة برمجة تطبيقات ChatCompleteions ميزة تتبع الردود بحالة معينة. للحفاظ على سياق المحادثة، يجب عليك كتابة كود لتتبع المحفزات والردود السابقة يدويا.

# 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!

يضاف كل طلب مستخدم جديد وكل إكمال إلى المحادثة، ويتم إرسال سجل المحادثة بالكامل في كل دور.

على الرغم من أنها ليست متكاملة مثل واجهة برمجة تطبيقات الردود ، إلا أن واجهة برمجة تطبيقات ChatCompleteions راسخة جيدا في منظومة نماذج الذكاء الاصطناعي التوليدي، لذا من المفيد أن تكون على دراية بها.