共用方式為


聊天標記語言 ChatML (預覽)

重要

使用 GPT-3.5-Turbo 模型與完成端點,如本文所述,仍為預覽狀態,且只有在 2024 年 8 月 1 日之前,版本 (0301) 才可能gpt-35-turbo淘汰。 強烈建議使用 GA 聊天完成 API/端點。 聊天完成 API 是與 GPT-3.5-Turbo 模型互動的建議方法。 Chat Completion API 也是存取 GPT-4 模型的唯一方式。

下列代碼段顯示搭配 ChatML 使用 GPT-3.5-Turbo 模型的最基本方式。 如果這是您第一次以程序設計方式使用這些模型,建議您從 GPT-35-Turbo 和 GPT-4 快速入門開始

注意

在 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'])

注意

GPT-35-Turbo 模型無法使用下列參數:logprobsbest_ofecho。 如果您設定其中任何一個參數,您會收到錯誤。

<|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-4gpt-4-32k 模型將會繼續更新。 建立這些模型的部署時,您也需要指定模型版本。

您可以在我們的模型頁面上找到這些模型的模型淘汰日期。

使用聊天標記語言 (ChatML)

注意

OpenAI 會持續改善 GPT-35-Turbo,而與模型搭配使用的聊天標記語言將會在未來持續演進。 我們會使用最新資訊來更新這份文件。

OpenAI 使用區分提示不同部分的特殊權杖,來定型 GPT-35-Turbo。 提示會以系統訊息開頭,用來質化模型,後面接著使用者與助理之間的一系列訊息。

基本 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|> 權杖之間的提示開頭。 此訊息會提供模型的初始指示。 您可以在系統訊息中提供各種資訊,包括:

  • 助理的簡短描述
  • 助理的個人化特徵
  • 您希望助理遵循的指示或規則
  • 模型所需的資料或資訊,例如常見問題中的相關問題

您可以自訂使用案例的系統訊息,或只包含基本系統訊息。 系統訊息是選擇性的,但建議至少包含基本訊息以取得最佳結果。

訊息

系統訊息之後,您可以在使用者助理之間包含一系列訊息。 每個訊息都應該以 <|im_start|> 權杖開頭,後面接著角色 (userassistant) ,並以 <|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]

下一步