全網最詳細中英文ChatGPT介面檔案(二)30分鐘開始使用ChatGPT——快速入門

2023-03-15 12:00:44


Quickstart 快速啟動

OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language.
OpenAI已經訓練出了非常擅長理解和生成文字的尖端語言模型。我們的API提供了對這些模型的存取,並可用於解決幾乎任何涉及處理語言的任務。

In this quickstart tutorial, you’ll build a simple sample application. Along the way, you’ll learn key concepts and techniques that are fundamental to using the API for any task, including:
在本快速入門教學中,您將構建一個簡單的範例應用程式。在此過程中,您將學習使用API執行任何任務所需的關鍵概念和技術,包括:

  • Content generation 內容生成
  • Summarization 總結
  • Classification, categorization, and sentiment analysis
  • 分類、歸類和情感分析
  • Data extraction 資料提取
  • Translation 翻譯
  • Many more! 還有很多!

Introduction 導言

The completions endpoint is the core of our API and provides a simple interface that’s extremely flexible and powerful. You input some text as a prompt, and the API will return a text completion that attempts to match whatever instructions or context you gave it.
完成端點是我們API的核心,它提供了一個非常靈活和強大的簡單介面。您輸入一些文字作為提示,API將返回一個文字完成,嘗試匹配您給出的任何指令或上下文。

Prompt 提示: Write a tagline for an ice cream shop.
為一家冰淇淋店寫一句口號。
Completion 完成: We serve up smiles with every scoop!
每一勺我們都微笑奉上!
You can think of this as a very advanced autocomplete — the model processes your text prompt and tries to predict what’s most likely to come next.
你可以把它看作是一個非常高階的自動完成--模型處理你的文字提示,並試圖預測接下來最有可能發生的事情。

1 Start with an instruction 從說明開始

Imagine you want to create a pet name generator. Coming up with names from scratch is hard!
假設您要建立一個暱稱生成器。從零開始想名字是很難的!

First, you’ll need a prompt that makes it clear what you want. Let’s start with an instruction. Submit this prompt to generate your first completion.
首先,你需要一個提示,讓你清楚地知道你想要什麼。我們先從一個說明開始。提交此提示以生成您的第一個完成。

Suggest one name for a horse. 為一匹馬取名

Not bad! Now, try making your instruction more specific.
不錯!現在,試著讓你的指令更具體一些。

Suggest one name for a black horse. 為一匹黑馬取名

As you can see, adding a simple adjective to our prompt changes the resulting completion. Designing your prompt is essentially how you 「program」 the model.

正如您所看到的,在提示語中新增一個簡單的形容詞會改變最終的完成方式。設計提示符本質上就是如何「程式設計」模型。

2 Add some examples 新增一些範例

Crafting good instructions is important for getting good results, but sometimes they aren’t enough. Let’s try making your instruction even more complex.
精心設計好的指導對於獲得好的結果很重要,但有時候這還不夠。讓我們試著讓你的指令變得更復雜。

Suggest three names for a horse that is a superhero. 給一匹超級英雄馬取三個名字
This completion isn't quite what we want. These names are pretty generic, and it seems like the model didn't pick up on the horse part of our instruction. Let’s see if we can get it to come up with some more relevant suggestions.
這種完整性並不是我們想要的。這些名稱非常通用,而且看起來模型沒有領會我們指令中的馬部分。我們看看能不能讓它提出一些更相關的建議。

In many cases, it’s helpful to both show and tell the model what you want. Adding examples to your prompt can help communicate patterns or nuances. Try submitting this prompt which includes a couple examples.
在許多情況下,向模型顯示並告訴模型您需要什麼是很有幫助的。在提示中新增範例有助於交流模式或細微差別。請嘗試提交此提示,其中包括幾個範例。

Suggest three names for an animal that is a superhero.
————————————————————————————
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: Horse
Names:

Nice! Adding examples of the output we’d expect for a given input helped the model provide the types of names we were looking for.
不錯!新增我們對給定輸入所期望的輸出範例有助於模型提供我們所尋找的名稱型別。

3 Adjust your settings 調整您的設定

Prompt design isn’t the only tool you have at your disposal. You can also control completions by adjusting your settings. One of the most important settings is called temperature.
提示設計並不是你唯一可以使用的工具。您還可以通過調整設定來控制完成。最重要的設定之一叫做溫度。

You may have noticed that if you submitted the same prompt multiple times in the examples above, the model would always return identical or very similar completions. This is because your temperature was set to 0.
您可能已經注意到,如果您在上面的範例中多次提交相同的提示,模型將始終返回相同或非常相似的完成。這是因為您的體溫設定為0。

Try re-submitting the same prompt a few times with temperature set to 1.
嘗試在溫度設定為1的情況下重新提交相同的提示幾次。


See what happened? When temperature is above 0, submitting the same prompt results in different completions each time.
看到發生了什麼嗎?當溫度高於0時,每次提交相同的提示會導致不同的完成。

Remember that the model predicts which text is most likely to follow the text preceding it. Temperature is a value between 0 and 1 that essentially lets you control how confident the model should be when making these predictions. Lowering temperature means it will take fewer risks, and completions will be more accurate and deterministic. Increasing temperature will result in more diverse completions.
請記住,模型預測哪個文字最有可能跟在它前面的文字後面。溫度是一個介於0和1之間的值,它本質上允許您控制模型在進行這些預測時的置信度。降低溫度意味著風險更小,完成更準確、更確定。溫度升高會導致完成更加多樣化。

DEEP DIVE 深入瞭解

Understanding tokens and probabilities 理解記號和概率

Our models process text by breaking it down into smaller units called tokens. Tokens can be words, chunks of words, or single characters. Edit the text below to see how it gets tokenized.
我們的模型通過將文字分解為更小的單元(稱為標記)來處理文字。標記可以是單詞、單詞塊或單個字元。編輯下面的文字,看看它是如何被標記的。


Common words like 「cat」 are a single token, while less common words are often broken down into multiple tokens. For example, 「Butterscotch」 translates to four tokens: 「But」, 「ters」, 「cot」, and 「ch」. Many tokens start with a whitespace, for example 「 hello」 and 「 bye」.
像「cat」這樣的常用詞是單個標記,而不太常用的詞通常被分解為多個標記。例如,「Butterscotch」轉換為四個標記:「but」,「ters」,「cot」和「ch」。許多標記以空格開頭,例如「hello」和「bye」。

Given some text, the model determines which token is most likely to come next. For example, the text 「Horses are my favorite」 is most likely to be followed with the token 「 animal」.
給定一些文字,模型確定哪個標記最有可能出現在下一個文字中。例如,文字「Horses are my favorite」最有可能跟有標記「animal」。


This is where temperature comes into play. If you submit this prompt 4 times with temperature set to 0, the model will always return 「 animal」 next because it has the highest probability. If you increase the temperature, it will take more risks and consider tokens with lower probabilities.
這就是溫度起作用的地方。如果您在溫度設定為0時提交此提示4次,則模型將始終返回「animal」,因為它具有最高的概率。如果你提高溫度,它會冒更大的風險,考慮概率較低的標記。


It’s usually best to set a low temperature for tasks where the desired output is well-defined. Higher temperature may be useful for tasks where variety or creativity are desired, or if you'd like to generate a few variations for your end users or human experts to choose from.
通常最好為所需輸出定義明確的任務設定低溫。較高的溫度可能對需要變化或創造性的任務有用,或者如果您想生成一些變化供終端使用者或人類專家選擇。

For your pet name generator, you probably want to be able to generate a lot of name ideas. A moderate temperature of 0.6 should work well.
對於你的寵物名字生成器,你可能希望能夠生成很多名字的想法。0.6的中等溫度應該可以很好地工作。

4 Build your application 構建應用程式

Now that you’ve found a good prompt and settings, you’re ready to build your pet name generator! We’ve written some code to get you started — follow the instructions below to download the code and run the app.
現在你已經找到了一個好的提示和設定,你已經準備好建立你的暱稱生成器!我們編寫了一些程式碼來幫助你入門-按照下面的說明下載程式碼並執行應用程式。

4.1NODE.JS

Setup

If you don’t have Node.js installed, install it from here. Then download the code by cloning this repository.
如果您沒有安裝Node.js,請從這裡安裝。然後通過克隆這個儲存庫來下載程式碼。

git clone https://github.com/openai/openai-quickstart-node.git

If you prefer not to use git, you can alternatively download the code using this zip file.
如果你不想使用git,你也可以使用這個zip檔案下載程式碼。

Add your API key 新增您的API金鑰

To get the app working, you’ll need an API key. You can get one by signing up for an account and returning to this page.
要使應用正常工作,您需要API金鑰。您可以通過註冊帳戶並返回此頁面來獲取一個。

Run the app 執行應用程式

Run the following commands in the project directory to install the dependencies and run the app.
在專案目錄中執行以下命令以安裝依賴項並執行應用程式。

npm install
npm run dev

Open http://localhost:3000 in your browser and you should see the pet name generator!
在瀏覽器中開啟 http://localhost:3000 ,您應該會看到暱稱生成器!

Understand the code 瞭解程式碼

Open up generate.js in the openai-quickstart-node/pages/api folder. At the bottom, you’ll see the function that generates the prompt that we were using above. Since users will be entering the type of animal their pet is, it dynamically swaps out the part of the prompt that specifies the animal.
開啟 openai-quickstart-node/pages/api 資料夾中的 generate.js 。在底部,您將看到生成我們在上面使用的提示符的函數。由於使用者需要輸入寵物的動物型別,因此它會動態地替換掉提示符中指定動物的部分。

function generatePrompt(animal) {
  const capitalizedAnimal = animal[0].toUpperCase() + animal.slice(1).toLowerCase();
  return `Suggest three names for an animal that is a superhero.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: ${capitalizedAnimal}
Names:`;
}

On line 9 in generate.js, you’ll see the code that sends the actual API request. As mentioned above, it uses the completions endpoint with a temperature of 0.6.
generate.js 的第9行,您將看到傳送實際API請求的程式碼。如上所述,它使用溫度為0.6的完成終點。

const completion = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: generatePrompt(req.body.animal),
  temperature: 0.6,
});

And that’s it! You should now have a full understanding of how your (superhero) pet name generator uses the OpenAI API!
就是這樣!現在您應該已經完全瞭解了您的(超級英雄)暱稱生成器是如何使用OpenAI API的!

4.2 PYTHON (FLASK)

Setup

If you don’t have Python installed, install it from here. Then download the code by cloning this repository.
如果你還沒有安裝Python,可以從這裡安裝。然後通過克隆這個儲存庫來下載程式碼。

git clone https://github.com/openai/openai-quickstart-python.git

If you prefer not to use git, you can alternatively download the code using this zip file.
如果你不想使用git,你也可以使用這個zip檔案下載程式碼

Add your API key 新增您的API金鑰

To get the app working, you’ll need an API key. You can get one by signing up for an account and returning to this page.
要使應用正常工作,您需要API金鑰。您可以通過註冊帳戶並返回此頁面來獲取一個。

Run the app 執行應用程式

Run the following commands in the project directory to install the dependencies and run the app. When running the commands, you may need to type python3/pip3 instead of python/pip depending on your setup.
在專案目錄中執行以下命令以安裝依賴項並執行應用。執行命令時,可能需要鍵入 python3/pip3 而不是 python/pip ,具體取決於您的設定。

 python -m venv venv
 venv/bin/activate
 pip install -r requirements.txt
 flask run

Open http://localhost:5000 in your browser and you should see the pet name generator!
在瀏覽器中開啟 http://localhost:5000 ,您應該會看到暱稱生成器!

Understand the code 瞭解程式碼

Open up app.py in the openai-quickstart-python folder. At the bottom, you’ll see the function that generates the prompt that we were using above. Since users will be entering the type of animal their pet is, it dynamically swaps out the part of the prompt that specifies the animal.
開啟 openai-quickstart-python 資料夾中的 app.py 。在底部,您將看到生成我們在上面使用的提示符的函數。由於使用者需要輸入寵物的動物型別,因此它會動態地替換掉提示符中指定動物的部分。

def generate_prompt(animal):
    return """Suggest three names for an animal that is a superhero.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {}
Names:""".format(animal.capitalize())

On line 14 in app.py, you’ll see the code that sends the actual API request. As mentioned above, it uses the completions endpoint with a temperature of 0.6.
app.py 的第14行,您將看到傳送實際API請求的程式碼。如上所述,它使用溫度為0.6的完成終點。

response = openai.Completion.create(
  model="text-davinci-003",
  prompt=generate_prompt(animal),
  temperature=0.6
)

And that’s it! You should now have a full understanding of how your (superhero) pet name generator uses the OpenAI API!
就是這樣!現在您應該已經完全瞭解了您的(超級英雄)暱稱生成器是如何使用OpenAI API的!

Closing

These concepts and techniques will go a long way in helping you build your own application. That said, this simple example demonstrates just a sliver of what’s possible! The completions endpoint is flexible enough to solve virtually any language processing task, including content generation, summarization, semantic search, topic tagging, sentiment analysis, and so much more.
這些概念和技術將在很大程度上幫助您構建自己的應用程式。也就是說,這個簡單的例子只是展示了可能性的一小部分!補全端點非常靈活,幾乎可以解決任何語言處理任務,包括內容生成、摘要、語意搜尋、主題標記、情感分析等等。

One limitation to keep in mind is that, for most models, a single API request can only process up to 2,048 tokens (roughly 1,500 words) between your prompt and completion.
需要記住的一個限制是,對於大多數模型,在提示符和完成符之間,單個API請求最多隻能處理2,048個令牌(大約1,500個單詞)。

DEEP DIVE 深入瞭解

Models and pricing 型號和定價

We offer a spectrum of models with different capabilities and price points. In this tutorial, we used text-davinci-003, our most capable natural language model. We recommend using this model while experimenting since it will yield the best results. Once you’ve got things working, you can see if the other models can produce the same results with lower latency and costs.
我們提供一系列具有不同功能和價位的模型。在本教學中,我們使用了 text-davinci-003,這是我們最強大的自然語言模型。我們建議在實驗時使用此模型,因為它將產生最佳結果。一旦你讓事情運轉起來,你就可以看看其他模型是否能以更低的延遲和成本產生相同的結果。

The total number of tokens processed in a single request (both prompt and completion) can’t exceed the model's maximum context length. For most models, this is 2,048 tokens or about 1,500 words. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text.
在單個請求(提示和完成)中處理的標記總數不能超過模型的最大上下文長度。對於大多數模型,這是2,048個標記或大約1,500個單詞。根據粗略的經驗,對於英語文字,1個標記大約為4個字元或0.75個單詞。

Pricing is pay-as-you-go per 1,000 tokens, with $5 in free credit that can be used during your first 3 months. Learn more.
定價為每1,000個虛擬幣現收現付,並可在前3個月內使用5美元的免費積分。 瞭解更多資訊

For more advanced tasks, you might find yourself wishing you could provide more examples or context than you can fit in a single prompt. The fine-tuning API is a great option for more advanced tasks like this. Fine-tuning allows you to provide hundreds or even thousands of examples to customize a model for your specific use case.
對於更高階的任務,您可能會發現自己希望提供更多的範例或上下文,而不是一個提示符所能容納的。對於像這樣的高階任務,微調API是一個很好的選擇。微調允許您提供數百甚至數千個範例,以便為您的特定用例客製化模型。

Next steps 下一步步驟

To get inspired and learn more about designing prompts for different tasks:
要獲得靈感並瞭解有關為不同任務設計提示的詳細資訊,請執行以下操作:

Read our completion guide.
閱讀我們的完成指南
Explore our library of example prompts.
探索我們的範例提示庫
Start experimenting in the Playground.
開始在遊樂場體驗吧。
Keep our usage policies in mind as you start building.
開始構建時請牢記我們的使用策略

其它資料下載

如果大家想繼續瞭解人工智慧相關學習路線和知識體系,歡迎大家翻閱我的另外一篇部落格《重磅 | 完備的人工智慧AI 學習——基礎知識學習路線,所有資料免關注免套路直接網路硬碟下載
這篇部落格參考了Github知名開源平臺,AI技術平臺以及相關領域專家:Datawhale,ApacheCN,AI有道和黃海廣博士等約有近100G相關資料,希望能幫助到所有小夥伴們。