Share via


チャット マークアップ言語 ChatML (プレビュー)

重要

この記事で説明されている GPT-3.5-Turbo モデルと入力候補エンドポイントの使用はプレビューであり、2024 年 8 月 1 日にも廃止予定となっている gpt-35-turbo バージョン (0301) でのみ可能です。 GA Chat Completion API/エンドポイントを使用することを強くお勧めします。 Chat Completion API は、GPT-3.5-Turbo モデルと対話するための推奨される方法です。 Chat Completion API は、GPT-4 モデルにアクセスする唯一の方法でもあります。

次のコード スニペットは、ChatML で GPT-3.5-Turbo モデルを使用する最も基本的な方法を示しています。 これらのモデルをプログラムで初めて使う場合は、GPT-35-Turbo と GPT-4 のクイックスタートから始めることをお勧めします。

Note

Azure OpenAI のドキュメントでは、GPT-3.5-Turbo と GPT-35-Turbo を同じ意味で言及しています。 OpenAI のモデルの正式な名前は gpt-3.5-turbo ですが、Azure OpenAI の場合、Azure 固有の文字制約により基になるモデル名は gpt-35-turbo です。

import os
import openai
openai.api_type = "azure"
openai.api_base = "https://{your-resource-name}.openai.azure.com/"
openai.api_version = "2024-02-01"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  engine="gpt-35-turbo", # The deployment name you chose when you deployed the GPT-35-Turbo model
  prompt="<|im_start|>system\nAssistant is a large language model trained by OpenAI.\n<|im_end|>\n<|im_start|>user\nWho were the founders of Microsoft?\n<|im_end|>\n<|im_start|>assistant\n",
  temperature=0,
  max_tokens=500,
  top_p=0.5,
  stop=["<|im_end|>"])

print(response['choices'][0]['text'])

Note

パラメーター logprobsbest_ofecho は、gpt-35-turbo モデルでは使用できません。 これらのパラメーターのいずれかを設定すると、エラーが発生します。

<|im_end|> トークンはメッセージの末尾を示します。 ChatML を使用する場合は、メッセージの末尾に達したときにモデルがテキストの生成を停止するように、<|im_end|> トークンを停止シーケンスとして含めることをお勧めします。

max_tokens は、300 や 500 など、通常より少し高い値に設定することを検討してください。 こうすると、メッセージの末尾に達する前にモデルがテキストの生成を停止することがありません。

モデルのバージョン管理

注意

gpt-35-turbo は、OpenAI の gpt-3.5-turbo モデルに相当します。

以前の GPT-3 および GPT-3.5 モデルとは異なり、gpt-35-turbo モデルだけでなく、gpt-4 および gpt-4-32k モデルも引き続き更新されます。 これらのモデルのデプロイを作成する場合は、モデル バージョンも指定する必要があります。

これらのモデルで、モデルの提供終了日がいつになるかは、モデル ページで確認できます。

チャット マークアップ言語 (ChatML) を操作する

注意

OpenAI では GPT-35-Turbo の改善を続け、そのモデルで使用されるチャット マークアップ言語は今後も進化し続けます。 このドキュメントは、常に最新の情報で更新されます。

GPT-35-Turbo は、プロンプトのさまざまな部分を表す特殊なトークンで OpenAI によってトレーニングされました。 プロンプトは、モデルの事前処理に使用されるシステム メッセージで始まり、その後にユーザーとアシスタントの間の一連のメッセージが続きます。

基本的な ChatML プロンプトの形式は次のとおりです。

<|im_start|>system 
Provide some context and/or instructions to the model.
<|im_end|> 
<|im_start|>user 
The user’s message goes here
<|im_end|> 
<|im_start|>assistant 

システム メッセージ

システム メッセージは、プロンプトの先頭の <|im_start|>system<|im_end|> トークンの間に含まれます。 このメッセージは、モデルに最初の指示を与えます。 システム メッセージには、次のような、さまざまな情報を指定できます。

  • アシスタントの簡単な説明
  • アシスタントの性格的な特性
  • アシスタントに従ってもらいたい手順またはルール
  • FAQ からの関連する質問など、モデルに必要なデータまたは情報

ユース ケースに合わせてシステム メッセージをカスタマイズすることも、単に基本的なシステム メッセージを含めることもできます。 システム メッセージはオプションですが、最適な結果を得るために、少なくとも基本的なものを含めることをお勧めします。

メッセージ

システム メッセージの後に、ユーザーアシスタントの間の一連のメッセージを含めることができます。 各メッセージは、<|im_start|> トークンで始めて、その後にロール (user または assistant) を続け、<|im_end|> トークンで終わる必要があります。

<|im_start|>user
What is thermodynamics?
<|im_end|>

モデルからの応答をトリガーするには、プロンプトの最後に、アシスタントが応答する番であることを示す <|im_start|>assistant トークンを指定する必要があります。 また、少数ショット学習を行う方法として、プロンプトにユーザーとアシスタントの間のメッセージを含めることもできます。

プロンプトの例

次のセクションでは、GPT-35-Turbo および GPT-4 モデルで使用できるさまざまなスタイルのプロンプトの例を示します。 これらの例は単に出発点であり、さまざまなプロンプトを試してご自身のユース ケースの動作をカスタマイズできます。

基本的な例

GPT-35-Turbo および GPT-4 モデルを chat.openai.com と同様に動作させる場合、"アシスタントは OpenAI によってトレーニングされた大規模な言語モデルです" のような基本的なシステム メッセージを使用できます。

<|im_start|>system
Assistant is a large language model trained by OpenAI.
<|im_end|>
<|im_start|>user
Who were the founders of Microsoft?
<|im_end|>
<|im_start|>assistant

指示の例

一部のシナリオでは、モデルに追加の指示を与えて、モデルが実行できる内容に対するガードレールを定義できます。

<|im_start|>system
Assistant is an intelligent chatbot designed to help users answer their tax related questions. 

Instructions:
- Only answer questions related to taxes. 
- If you're unsure of an answer, you can say "I don't know" or "I'm not sure" and recommend users go to the IRS website for more information.
<|im_end|>
<|im_start|>user
When are my taxes due?
<|im_end|>
<|im_start|>assistant

基礎知識のデータを使用する

関連するデータや情報をシステム メッセージに含めて、モデルに会話の追加コンテキストを与えることもできます。 含める必要がある情報の量が少ない場合は、システム メッセージの中にハード コーディングできます。 モデルで認識する必要があるデータの量が多い場合は、埋め込み、または Azure AI 検索などの製品を使用して、クエリ時に最も関連性の高い情報を取得できます。

<|im_start|>system
Assistant is an intelligent chatbot designed to help users answer technical questions about Azure OpenAI Serivce. Only answer questions using the context below and if you're not sure of an answer, you can say "I don't know".

Context:
- Azure OpenAI Service provides REST API access to OpenAI's powerful language models including the GPT-3, Codex and Embeddings model series.
- Azure OpenAI Service gives customers advanced language AI with OpenAI GPT-3, Codex, and DALL-E models with the security and enterprise promise of Azure. Azure OpenAI co-develops the APIs with OpenAI, ensuring compatibility and a smooth transition from one to the other.
- At Microsoft, we're committed to the advancement of AI driven by principles that put people first. Microsoft has made significant investments to help guard against abuse and unintended harm, which includes requiring applicants to show well-defined use cases, incorporating Microsoft’s principles for responsible AI use
<|im_end|>
<|im_start|>user
What is Azure OpenAI Service?
<|im_end|>
<|im_start|>assistant

ChatML を使用した少数ショット学習

モデルに少数ショットの例を与えることもできます。 少数ショット学習の手法は、プロンプト形式が新しくなったため、若干変更されています。 ユーザーとアシスタントの間の一連のメッセージを、少数ショットの例としてプロンプトに含められるようになりました。 これらの例を使用すると、一般的な質問に対する回答をシード処理してモデルを事前処理したり、特定の動作をモデルに教えたりすることができます。

これは、GPT-35-Turbo で少数ショット学習を使用する方法を示す単なる一例です。 さまざまな手法を試して、ご自身のユース ケースに最適なものを確認できます。

<|im_start|>system
Assistant is an intelligent chatbot designed to help users answer their tax related questions. 
<|im_end|>
<|im_start|>user
When do I need to file my taxes by?
<|im_end|>
<|im_start|>assistant
In 2023, you will need to file your taxes by April 18th. The date falls after the usual April 15th deadline because April 15th falls on a Saturday in 2023. For more details, see https://www.irs.gov/filing/individuals/when-to-file
<|im_end|>
<|im_start|>user
How can I check the status of my tax refund?
<|im_end|>
<|im_start|>assistant
You can check the status of your tax refund by visiting https://www.irs.gov/refunds
<|im_end|>

チャット以外のシナリオにチャット マークアップ言語を使用する

ChatML は、マルチターン会話を管理しやすくするように設計されていますが、チャット以外のシナリオにも適しています。

たとえば、エンティティ抽出シナリオでは、次のプロンプトを使用できます。

<|im_start|>system
You are an assistant designed to extract entities from text. Users will paste in a string of text and you will respond with entities you've extracted from the text as a JSON object. Here's an example of your output format:
{
   "name": "",
   "company": "",
   "phone_number": ""
}
<|im_end|>
<|im_start|>user
Hello. My name is Robert Smith. I’m calling from Contoso Insurance, Delaware. My colleague mentioned that you are interested in learning about our comprehensive benefits policy. Could you give me a call back at (555) 346-9322 when you get a chance so we can go over the benefits?
<|im_end|>
<|im_start|>assistant

ユーザーによる安全でない入力を防ぐ

チャット マークアップ言語を安全に使用できるように、アプリケーションに軽減策を追加することが重要です。

エンド ユーザーが <|im_start|><|im_end|> などの特殊なトークンを入力に含めないように図ることをお勧めします。 また、追加の検証を含めて、モデルに送信するプロンプトが適切な形式であり、このドキュメントで説明されているチャット マークアップ言語の形式に従っていることを確認するようにお勧めします。

さらに、ユーザーによる特定の種類の入力への応答方法をモデルにガイドするための指示を、システム メッセージに指定することもできます。 たとえば、特定の主題に関するメッセージにのみ返信するようにモデルに指示できます。 また、少数ショットの例を使用して、この動作を強化することもできます。

会話の管理

gpt-35-turbo でのトークンの制限数は 4096 です。 この制限には、プロンプトと入力候補の両方のトークン数が含まれます。 プロンプト内のトークン数を max_tokens パラメーターの値と組み合わせた値は、4096 未満でなければなりません。そうでないとエラーが発生します。

ご自身の責任で、プロンプトと入力候補がトークンの制限内に収まるようにしてください。 つまり、会話が長い場合は、トークン数を追跡して、トークンの制限内に収まるプロンプトのみをモデルに送信することが必要です。

次に示すコード サンプルは、会話内の個別のメッセージを追跡する方法を示した簡単な例です。

import os
import openai
openai.api_type = "azure"
openai.api_base = "https://{your-resource-name}.openai.azure.com/" #This corresponds to your Azure OpenAI resource's endpoint value
openai.api_version = "2024-02-01" 
openai.api_key = os.getenv("OPENAI_API_KEY")

# defining a function to create the prompt from the system message and the conversation messages
def create_prompt(system_message, messages):
    prompt = system_message
    for message in messages:
        prompt += f"\n<|im_start|>{message['sender']}\n{message['text']}\n<|im_end|>"
    prompt += "\n<|im_start|>assistant\n"
    return prompt

# defining the user input and the system message
user_input = "<your user input>" 
system_message = f"<|im_start|>system\n{'<your system message>'}\n<|im_end|>"

# creating a list of messages to track the conversation
messages = [{"sender": "user", "text": user_input}]

response = openai.Completion.create(
    engine="gpt-35-turbo", # The deployment name you chose when you deployed the GPT-35-Turbo model.
    prompt=create_prompt(system_message, messages),
    temperature=0.5,
    max_tokens=250,
    top_p=0.9,
    frequency_penalty=0,
    presence_penalty=0,
    stop=['<|im_end|>']
)

messages.append({"sender": "assistant", "text": response['choices'][0]['text']})
print(response['choices'][0]['text'])

トークンの制限内に収める

トークンの制限内に収める最も簡単な方法は、トークンの制限に達したときに、会話内の最も古いメッセージを削除することです。

制限内に収めながら常にできるだけ多くのトークンを含めることも、メッセージが制限の範囲内に収まっていると想定して、設定された数の以前のメッセージを常に含めることもできます。 プロンプトが長い場合は応答の生成に長い時間がかかり、短いプロンプトよりコストが高くなることに留意することが重要です。

文字列内のトークンの数を見積もるには、次に示すように tiktoken Python ライブラリを使用します。

import tiktoken 

cl100k_base = tiktoken.get_encoding("cl100k_base") 

enc = tiktoken.Encoding( 
    name="gpt-35-turbo",  
    pat_str=cl100k_base._pat_str, 
    mergeable_ranks=cl100k_base._mergeable_ranks, 
    special_tokens={ 
        **cl100k_base._special_tokens, 
        "<|im_start|>": 100264, 
        "<|im_end|>": 100265
    } 
) 

tokens = enc.encode( 
    "<|im_start|>user\nHello<|im_end|><|im_start|>assistant",  
    allowed_special={"<|im_start|>", "<|im_end|>"} 
) 

assert len(tokens) == 7 
assert tokens == [100264, 882, 198, 9906, 100265, 100264, 78191]

次の手順