ChatCompletions API を使用して応答を生成する

完了

OpenAI ChatCompletions API は、一般的に生成 AI モデルとプラットフォーム間で使用されます。 新しいプロジェクト開発には Responses API が推奨されますが、 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)

会話コンテキストの保持

Responses API とは異なり、ChatCompletins API にはステートフル応答追跡機能はありません。 会話コンテキストを保持するには、以前のプロンプトと応答を手動で追跡するコードを記述する必要があります。

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

新しいユーザープロンプトと完了が会話に追加され、会話履歴全体が各ターンで送信されます。

Responses API ほど完全には機能しませんが、ChatCompletions API は生成型 AI モデル エコシステムで十分に確立されているため、理解すると便利です。