如何使用 GPT

您所在的位置:网站首页 gpt35怎么升级成4 如何使用 GPT

如何使用 GPT

2024-01-28 10:28| 来源: 网络整理| 查看: 265

了解如何使用 GPT-35-Turbo 和 GPT-4 模型 發行項 12/12/2023

GPT-35-Turbo 和 GPT-4 模型是針對交談介面最佳化的語言模型。 模型的行為與較舊的 GPT-3 模型不同。 先前的模型是文字輸入和文字輸出,這表示他們接受提示字串,並傳回完成以附加至提示。 不過,GPT-35-Turbo 和 GPT-4 模型是交談輸入和訊息輸出。模型預期輸入格式為特定類似聊天的文字記錄格式,並傳回代表聊天中模型撰寫訊息的完成。 雖然此格式是專為多回合交談所設計,但您也會發現它也適用於非聊天案例。

在 Azure OpenAI 中,有兩種不同的選項可以與這些類型的模型互動:

Chat Completion API。 使用聊天標記語言 (ChatML) 的 Completion API。

Chat Completion API 是新的專用 API,可與 GPT-35-Turbo 和 GPT-4 模型互動。 此 API 是用來存取這些模型的慣用方法。 這也是存取新 GPT-4 模型的唯一方式。

ChatML 會使用您用於 text-davinci-002 等其他模型相同的 Completion API,它需要唯一的權杖型提示格式,稱為聊天標記語言 (ChatML)。 這會提供比專用 Chat Completion API 較低的層級存取,但也需要額外的輸入驗證、僅支援 gpt-35-turbo 模型,而且基礎格式較可能隨著時間變更。

本文將逐步引導您開始使用 GPT-35-Turbo 和 GPT-4 模型。 請務必使用這裡所述的技術來獲得最佳結果。 如果您嘗試與模型互動的方式與舊版模型系列相同,這些模型通常會是詳細資訊,並提供較不實用的回應。

使用 GPT-3.5-Turbo 和 GPT-4 模型

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

OpenAI Python 0.28.1 OpenAI Python 1.x import os import openai openai.api_type = "azure" openai.api_version = "2023-05-15" openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value. openai.api_key = os.getenv("AZURE_OPENAI_KEY") response = openai.ChatCompletion.create( engine="gpt-35-turbo", # The deployment name you chose when you deployed the GPT-3.5-Turbo or GPT-4 model. messages=[ {"role": "system", "content": "Assistant is a large language model trained by OpenAI."}, {"role": "user", "content": "Who were the founders of Microsoft?"} ] ) print(response) # To print only the response content text: # print(response['choices'][0]['message']['content']) 輸出

以人工方式新增 JSON 格式,方便閱讀。

{ "choices": [ { "finish_reason": "stop", "index": 0, "message": { "content": "The founders of Microsoft are Bill Gates and Paul Allen. They co-founded the company in 1975.", "role": "assistant" } } ], "created": 1679014551, "id": "chatcmpl-6usfn2yyjkbmESe3G4jaQR6bsScO1", "model": "gpt-3.5-turbo-0301", "object": "chat.completion", "usage": { "completion_tokens": 86, "prompt_tokens": 37, "total_tokens": 123 } } import os from openai import AzureOpenAI client = AzureOpenAI( api_key = os.getenv("AZURE_OPENAI_KEY"), api_version = "2023-05-15", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") ) response = client.chat.completions.create( model="gpt-35-turbo", # model = "deployment_name". messages=[ {"role": "system", "content": "Assistant is a large language model trained by OpenAI."}, {"role": "user", "content": "Who were the founders of Microsoft?"} ] ) #print(response) print(response.model_dump_json(indent=2)) print(response.choices[0].message.content) { "id": "chatcmpl-8GHoQAJ3zN2DJYqOFiVysrMQJfe1P", "choices": [ { "finish_reason": "stop", "index": 0, "message": { "content": "Microsoft was founded by Bill Gates and Paul Allen. They established the company on April 4, 1975. Bill Gates served as the CEO of Microsoft until 2000 and later as Chairman and Chief Software Architect until his retirement in 2008, while Paul Allen left the company in 1983 but remained on the board of directors until 2000.", "role": "assistant", "function_call": null }, "content_filter_results": { "hate": { "filtered": false, "severity": "safe" }, "self_harm": { "filtered": false, "severity": "safe" }, "sexual": { "filtered": false, "severity": "safe" }, "violence": { "filtered": false, "severity": "safe" } } } ], "created": 1698892410, "model": "gpt-35-turbo", "object": "chat.completion", "usage": { "completion_tokens": 73, "prompt_tokens": 29, "total_tokens": 102 }, "prompt_filter_results": [ { "prompt_index": 0, "content_filter_results": { "hate": { "filtered": false, "severity": "safe" }, "self_harm": { "filtered": false, "severity": "safe" }, "sexual": { "filtered": false, "severity": "safe" }, "violence": { "filtered": false, "severity": "safe" } } } ] } Microsoft was founded by Bill Gates and Paul Allen. They established the company on April 4, 1975. Bill Gates served as the CEO of Microsoft until 2000 and later as Chairman and Chief Software Architect until his retirement in 2008, while Paul Allen left the company in 1983 but remained on the board of directors until 2000.

注意

新的 GPT-35-Turbo 和 GPT-4 模型無法使用下列參數:logprobs、best_of 和 echo。 如果您設定其中任何一個參數,您會收到錯誤。

每個回應都包含 finish_reason。 finish_reason 的可能值為:

stop:API 傳回的完整模型輸出。 length:因為 max_tokens 參數或權杖限制,所以模型輸出不完整。 content_filter:由於內容篩選中的旗標而省略了內容。 null:API 回應仍在進行中或不完整。

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

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

使用 Chat Completion API

OpenAI 已將 GPT-35-Turbo 和 GPT-4 模型定型,以接受格式化為交談的輸入。 messages 參數會採用以角色組織之交談的訊息物件陣列。 使用 Python API 時,會使用字典清單。

基本 Chat Completion 的格式如下所示:

{"role": "system", "content": "Provide some context and/or instructions to the model"}, {"role": "user", "content": "The users messages goes here"}

具有一個範例答案的交談,後面接著問題看起來會像這樣:

{"role": "system", "content": "Provide some context and/or instructions to the model."}, {"role": "user", "content": "Example question goes here."}, {"role": "assistant", "content": "Example answer goes here."}, {"role": "user", "content": "First question/message for the model to actually respond to."} 系統角色

系統角色也稱為系統訊息包含在陣列的開頭。 此訊息會提供模型的初始指示。 您可以在系統角色中提供各種資訊,包括:

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

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

訊息

系統角色之後,您可以在使用者與助理之間包含一系列訊息。

{"role": "user", "content": "What is thermodynamics?"}

若要觸發模型的回應,您應該以使用者訊息結尾,指出輪到助理回應。 您也可以在使用者與助理之間包含一系列範例訊息,以做為執行小樣本學習的方式。

訊息提示範例

下一節顯示您可以搭配 GPT-35-Turbo 和 GPT-4 模型使用之不同提示樣式的範例。 這些範例只是起點,您可以試驗不同的提示來自訂您自己的使用案例行為。

基本範例

如果您想要 GPT-35-Turbo 模型的行為與 chat.openai.com 類似,您可以使用基本系統訊息,例如「助理是 OpenAI 定型的大型語言模型。」

{"role": "system", "content": "Assistant is a large language model trained by OpenAI."}, {"role": "user", "content": "Who were the founders of Microsoft?"} 使用指示的範例

在某些案例中,您可能會想要為模型提供其他指示,以定義模型能夠執行的護欄。

{"role": "system", "content": "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. "}, {"role": "user", "content": "When are my taxes due?"} 使用資料進行基礎

您也可以在系統訊息中包含相關資料或資訊,為交談提供模型額外的內容。 如果您只需要包含少量資訊,您可以在系統訊息中硬式編碼。 如果您有模型應該注意的大量數據,您可以使用內嵌或 Azure AI 搜尋等產品,在查詢時擷取最相關的資訊。

{"role": "system", "content": "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." }, {"role": "user", "content": "What is Azure OpenAI Service?"} 使用 Chat Completion 的小樣本學習

您也可以提供一些範例給模型。 由於新的提示格式,小樣本學習的方法已稍微變更。 您現在可以在提示中包含使用者與助理之間的一系列訊息,做為小樣本範例。 這些範例可用來植入常見問題的解答,以質化模型或教導模型的特定行為。

這只是一個範例,說明如何搭配 GPT-35-Turbo 和 GPT-4 使用小樣本學習。 您可以試驗不同的方法,以查看最適合您的使用案例。

{"role": "system", "content": "Assistant is an intelligent chatbot designed to help users answer their tax related questions. "}, {"role": "user", "content": "When do I need to file my taxes by?"}, {"role": "assistant", "content": "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."}, {"role": "user", "content": "How can I check the status of my tax refund?"}, {"role": "assistant", "content": "You can check the status of your tax refund by visiting https://www.irs.gov/refunds"} 針對非聊天案例使用 Chat Completion

Chat Completion API 的設計目的是使用多回合交談,但也適用於非聊天案例。

例如,針對實體擷取案例,您可以使用下列提示:

{"role": "system", "content": "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": "" }"}, {"role": "user", "content": "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?"} 建立基本交談迴圈

到目前為止,這些範例已示範與 Chat Completion API 互動的基本機制。 此範例示範如何建立執行下列動作的交談迴圈:

持續接受主控台輸入,並將其格式化為訊息清單的一部分做為使用者角色內容。 輸出列印至主控台的回應,並格式化並新增至訊息清單作為助理角色內容。

這表示每次詢問新問題時,到目前為止交談的執行文字記錄都會連同最新的問題一起傳送。 由於模型沒有記憶,因此您必須傳送具有每個新問題的更新文字記錄,否則模型將會失去先前問題和答案的內容。

OpenAI Python 0.28.1 OpenAI Python 1.x import os import openai openai.api_type = "azure" openai.api_version = "2023-05-15" openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value. openai.api_key = os.getenv("AZURE_OPENAI_KEY") conversation=[{"role": "system", "content": "You are a helpful assistant."}] while True: user_input = input() conversation.append({"role": "user", "content": user_input}) response = openai.ChatCompletion.create( engine="gpt-35-turbo", # The deployment name you chose when you deployed the GPT-35-turbo or GPT-4 model. messages=conversation ) conversation.append({"role": "assistant", "content": response["choices"][0]["message"]["content"]}) print("\n" + response['choices'][0]['message']['content'] + "\n") import os from openai import AzureOpenAI client = AzureOpenAI( api_key = os.getenv("AZURE_OPENAI_KEY"), api_version = "2023-05-15", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value. ) conversation=[{"role": "system", "content": "You are a helpful assistant."}] while True: user_input = input("Q:") conversation.append({"role": "user", "content": user_input}) response = client.chat.completions.create( model="gpt-35-turbo", # model = "deployment_name". messages=conversation ) conversation.append({"role": "assistant", "content": response.choices[0].message.content}) print("\n" + response.choices[0].message.content + "\n")

當您執行上述程式碼時,將會取得空白主控台視窗。 在視窗中輸入您的第一個問題,然後按 Enter 鍵。 傳回回應之後,您可以重複此流程並持續詢問問題。

管理交談

上一個範例會執行,直到您達到模型的權杖限制為止。 在收到每個問題並回答時,messages 清單會隨著大小成長。 gpt-35-turbo 的權杖限制是 4096 個權杖,而 gpt-4和 gpt-4-32k 的權杖限制分別為 8192 和 32768。 這些限制包括來自所傳送訊息清單和模型回應的權杖計數。 結合 max_tokens 參數值之訊息清單中的權杖數目必須保持在這些限制之下,否則您會收到錯誤。

您必須負責確保提示和完成落在權杖限制內。 這表示對於較長的交談,您必須追蹤權杖計數,並只傳送屬於限制內的提示模型。

注意

強烈建議您保留所有模型的 記載輸入權杖限制,即使您發現可以超過該限制也一樣。

下列程式碼範例示範簡單的聊天迴圈範例,其中包含使用 OpenAI 的 tiktoken 程式庫處理 4096 個權杖計數的技術。

程式代碼會使用 tiktoken 0.5.1。 如果您有較舊的版本,請執行 pip install tiktoken --upgrade。

OpenAI Python 0.28.1 OpenAI Python 1.x import tiktoken import openai import os openai.api_type = "azure" openai.api_version = "2023-05-15" openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value. openai.api_key = os.getenv("AZURE_OPENAI_KEY") system_message = {"role": "system", "content": "You are a helpful assistant."} max_response_tokens = 250 token_limit = 4096 conversation = [] conversation.append(system_message) def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"): """Return the number of tokens used by a list of messages.""" try: encoding = tiktoken.encoding_for_model(model) except KeyError: print("Warning: model not found. Using cl100k_base encoding.") encoding = tiktoken.get_encoding("cl100k_base") if model in { "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", "gpt-4-0314", "gpt-4-32k-0314", "gpt-4-0613", "gpt-4-32k-0613", }: tokens_per_message = 3 tokens_per_name = 1 elif model == "gpt-3.5-turbo-0301": tokens_per_message = 4 # every message follows {role/name}\n{content}\n tokens_per_name = -1 # if there's a name, the role is omitted elif "gpt-3.5-turbo" in model: print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.") return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613") elif "gpt-4" in model: print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.") return num_tokens_from_messages(messages, model="gpt-4-0613") else: raise NotImplementedError( f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""" ) num_tokens = 0 for message in messages: num_tokens += tokens_per_message for key, value in message.items(): num_tokens += len(encoding.encode(value)) if key == "name": num_tokens += tokens_per_name num_tokens += 3 # every reply is primed with assistant return num_tokens while True: user_input = input("") conversation.append({"role": "user", "content": user_input}) conv_history_tokens = num_tokens_from_messages(conversation) while conv_history_tokens + max_response_tokens >= token_limit: del conversation[1] conv_history_tokens = num_tokens_from_messages(conversation) response = openai.ChatCompletion.create( engine="gpt-35-turbo", # The deployment name you chose when you deployed the GPT-35-Turbo or GPT-4 model. messages=conversation, temperature=0.7, max_tokens=max_response_tokens, ) conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']}) print("\n" + response['choices'][0]['message']['content'] + "\n") import tiktoken import os from openai import AzureOpenAI client = AzureOpenAI( api_key = os.getenv("AZURE_OPENAI_KEY"), api_version = "2023-05-15", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") # Your Azure OpenAI resource's endpoint value. ) system_message = {"role": "system", "content": "You are a helpful assistant."} max_response_tokens = 250 token_limit = 4096 conversation = [] conversation.append(system_message) def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"): """Return the number of tokens used by a list of messages.""" try: encoding = tiktoken.encoding_for_model(model) except KeyError: print("Warning: model not found. Using cl100k_base encoding.") encoding = tiktoken.get_encoding("cl100k_base") if model in { "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", "gpt-4-0314", "gpt-4-32k-0314", "gpt-4-0613", "gpt-4-32k-0613", }: tokens_per_message = 3 tokens_per_name = 1 elif model == "gpt-3.5-turbo-0301": tokens_per_message = 4 # every message follows {role/name}\n{content}\n tokens_per_name = -1 # if there's a name, the role is omitted elif "gpt-3.5-turbo" in model: print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.") return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613") elif "gpt-4" in model: print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.") return num_tokens_from_messages(messages, model="gpt-4-0613") else: raise NotImplementedError( f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""" ) num_tokens = 0 for message in messages: num_tokens += tokens_per_message for key, value in message.items(): num_tokens += len(encoding.encode(value)) if key == "name": num_tokens += tokens_per_name num_tokens += 3 # every reply is primed with assistant return num_tokens while True: user_input = input("Q:") conversation.append({"role": "user", "content": user_input}) conv_history_tokens = num_tokens_from_messages(conversation) while conv_history_tokens + max_response_tokens >= token_limit: del conversation[1] conv_history_tokens = num_tokens_from_messages(conversation) response = client.chat.completions.create( model="gpt-35-turbo", # model = "deployment_name". messages=conversation, temperature=0.7, max_tokens=max_response_tokens ) conversation.append({"role": "assistant", "content": response.choices[0].message.content}) print("\n" + response.choices[0].message.content + "\n")

在此範例中,一旦達到權杖計數,就會移除交談文字記錄中的最舊訊息。 del 是用來取代 pop() 以提升效率,而且我們會從索引 1 開始,以便一律保留系統訊息,並只移除使用者/助理訊息。 經過一段時間後,管理交談的這個方法可能會導致交談品質降低,因為模型會逐漸失去先前交談部分的內容。

替代方法是將交談持續時間限制為最大權杖長度或特定回合數。 一旦達到最大權杖限制,而且如果您允許交談繼續,模型就會遺失內容,您可以提示使用者要求他們開始新的交談,並清除訊息清單,以使用可用的完整權杖限制來啟動全新的交談。

先前所示範程式碼的權杖計數部分是 OpenAI 操作手冊其中一個範例的簡化版本。

下一步 深入瞭解 Azure OpenAI。 使用 GPT-35-Turbo 快速入門開始使用 GPT-35-Turbo模型。 如需更多範例,請參閱 Azure OpenAI 樣本 GitHub 存放庫 使用聊天模型

重要

搭配完成端點使用 GPT-35-Turbo 模型仍保持預覽狀態。 由於基礎 ChatML 語法的變更可能,我們強烈建議使用 Chat Completion API/端點。 Chat Completion API 是與 GPT-35-Turbo 模型互動的建議方法。 Chat Completion API 也是存取 GPT-4 模型的唯一方式。

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

import os import openai openai.api_type = "azure" openai.api_base = "https://{your-resource-name}.openai.azure.com/" openai.api_version = "2023-05-15" 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="system\nAssistant is a large language model trained by OpenAI.\n\nuser\nWho were the founders of Microsoft?\n\nassistant\n", temperature=0, max_tokens=500, top_p=0.5, stop=[""]) print(response['choices'][0]['text'])

注意

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

權杖表示訊息的結尾。 我們建議將 權杖納入為停止序列,以確保模型在到達訊息結尾時停止產生文字。 您可以在聊天標記語言 (ChatML) 一節中深入了解特殊權杖。

請考慮設定 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,而與模型搭配使用的聊天標記語言將會在未來持續演進。 我們會使用最新資訊來更新這份文件。

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

基本 ChatML 提示的格式如下所示:

system Provide some context and/or instructions to the model. user The user’s message goes here assistant 系統訊息

系統訊息會包含在 system 和 權杖之間的提示開頭。 此訊息會提供模型的初始指示。 您可以在系統訊息中提供各種資訊,包括:

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

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

訊息

系統訊息之後,您可以在使用者與助理之間包含一系列訊息。 每個訊息都應該以 權杖開頭,後面接著角色 (user 或 assistant) ,並以 權杖結尾。

user What is thermodynamics?

若要觸發模型的回應,提示應該以 assistant 權杖結尾,指出輪到助理回應。 您也可以在提示中包含使用者與助理之間的訊息,以做為執行小樣本學習的方式。

提示範例

下一節顯示您可以搭配 GPT-35-Turbo 和 GPT-4 模型使用之不同提示樣式的範例。 這些範例只是起點,您可以試驗不同的提示來自訂您自己的使用案例行為。

基本範例

如果您想要 GPT-35-Turbo 和 GPT-4 模型的行為與 chat.openai.com 類似,您可以使用基本系統訊息,例如「助理是 OpenAI 定型的大型語言模型。」

system Assistant is a large language model trained by OpenAI. user Who were the founders of Microsoft? assistant 使用指示的範例

在某些案例中,您可能會想要為模型提供其他指示,以定義模型能夠執行的護欄。

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. user When are my taxes due? assistant 使用資料進行基礎

您也可以在系統訊息中包含相關資料或資訊,為交談提供模型額外的內容。 如果您只需要包含少量資訊,您可以在系統訊息中硬式編碼。 如果您有模型應該注意的大量數據,您可以使用內嵌或 Azure AI 搜尋等產品,在查詢時擷取最相關的資訊。

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 user What is Azure OpenAI Service? assistant 使用 ChatML 的小樣本學習

您也可以提供一些範例給模型。 由於新的提示格式,小樣本學習的方法已稍微變更。 您現在可以在提示中包含使用者與助理之間的一系列訊息,做為小樣本範例。 這些範例可用來植入常見問題的解答,以質化模型或教導模型的特定行為。

這只是一個範例,說明如何搭配 GPT-35-Turbo 使用小樣本學習。 您可以試驗不同的方法,以查看最適合您的使用案例。

system Assistant is an intelligent chatbot designed to help users answer their tax related questions. user When do I need to file my taxes by? 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 user How can I check the status of my tax refund? assistant You can check the status of your tax refund by visiting https://www.irs.gov/refunds 針對非聊天案例使用聊天標記語言

ChatML 的設計目的是讓多回合交談更容易管理,但它也適用於非聊天案例。

例如,針對實體擷取案例,您可以使用下列提示:

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": "" } 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? assistant 防止不安全的使用者輸入

請務必將風險降低功能新增至您的應用程式,以確保安全使用聊天標記語言。

建議您防止終端使用者在其輸入中包含特殊權杖,例如 和 。 我們也建議您包含額外的驗證,以確保您傳送至模型的提示格式正確,並遵循本文件中所述的聊天標記語言格式。

您也可以在系統訊息中提供指示,以引導模型了解如何回應特定類型的使用者輸入。 例如,您可以指示模型只回覆特定主旨的相關訊息。 您也可以使用小樣本範例來強化此行為。

管理交談

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 = "2023-05-15" 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{message['sender']}\n{message['text']}\n" prompt += "\nassistant\n" return prompt # defining the user input and the system message user_input = "" system_message = f"system\n{''}\n" # 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=[''] ) 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, "": 100264, "": 100265 } ) tokens = enc.encode( "user\nHelloassistant", allowed_special={"", ""} ) assert len(tokens) == 7 assert tokens == [100264, 882, 198, 9906, 100265, 100264, 78191] 下一步 深入瞭解 Azure OpenAI。 使用 GPT-35-Turbo 和 GPT-4 快速入門開始使用 GPT-35-Turbo 和 GPT-4 模型。 如需更多範例,請參閱 Azure OpenAI 範例 GitHub 存放庫


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3