自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

創(chuàng)建你的第一個使用 OpenAI ChatGPT API 的程序

開發(fā)
以下是如何使用 OpenAI ChatGPT AI 創(chuàng)建聊天助手的 Python 程序的方法。

易于使用的 AI “ChatGPT” 已經(jīng)以 API 提供。ChatGPT 的創(chuàng)造者 OpenAI 宣布,模型('gpt-3.5-turbo')現(xiàn)在適用于自定義產(chǎn)品和解決方案。而且成本也非常實(shí)惠。目前的價格為每 1000 個令牌 0.002 美元。

該模型目前與 Whisper API 一起提供,后者也用于文本到語音解決方案。該 API 目前具備以下功能:

  • 創(chuàng)建自定義的對話代理和機(jī)器人
  • 為你編寫 Python 代碼
  • 起草電子郵件或任何你想要的文檔
  • 你可以將自然語言界面集成到你當(dāng)前的產(chǎn)品/應(yīng)用/服務(wù)或軟件中,為你的消費(fèi)者提供服務(wù)
  • 語言翻譯服務(wù)
  • 成為許多科目的導(dǎo)師
  • 模擬視頻游戲角色

正如你所見,機(jī)會無限。

如果你計(jì)劃嘗試該 API 并開始使用它,這里有一個簡單的指南,為你提供逐步指導(dǎo)。

OpenAI ChatGPT API: 入門指南

先決條件

確保你擁有一個 OpenAI 賬戶。如果你沒有,訪問此頁面 并創(chuàng)建一個賬戶。你也可以使用你的谷歌或微軟賬號。

創(chuàng)建一個賬戶后,生成一個專屬于你的 API 密鑰。訪問 此頁面 并創(chuàng)建一個新的秘密密鑰。

創(chuàng)建 OpenAI API 密鑰的位置

創(chuàng)建 OpenAI API 密鑰的位置

記錄該密鑰或在安全的地方保存它?;诎踩?,它將不會從 OpenAI 賬戶部分再次可見。而且不要與任何人分享此密鑰。如果你計(jì)劃使用企業(yè)解決方案,請向你的組織查詢 API 密鑰。由于該密鑰與你的付費(fèi) OpenAI 計(jì)劃相關(guān),因此請謹(jǐn)慎使用。

設(shè)置環(huán)境

安裝 Python 和 pip

本指南使用 Python 編程語言來調(diào)用 OpenAI API 密鑰。你可以使用 Java 或其他任何語言來調(diào)用它。

首先,請確保你在 Linux 或 Windows 中已經(jīng)安裝了 Python。如果沒有,請按照以下指南安裝 Python。如果你使用現(xiàn)代 Linux 發(fā)行版(例如 Ubuntu),Python 應(yīng)該已經(jīng)安裝好了。

在安裝 Python 后,確保 pip 在 Linux 發(fā)行版中可用。運(yùn)行以下命令進(jìn)行安裝。對于 Windows,你應(yīng)該已經(jīng)在 Python 安裝的一部分中安裝了它。

Ubuntu、Debian 和其他基于 Debian 的發(fā)行版:

sudo apt install python3-pip

Fedora、RHEL、CentOS 等:

sudo dnf install python3-pip

Arch Linux:

sudo pacman -S python-pip

將 OpenAI API 密鑰設(shè)置為環(huán)境變量

上述步驟中創(chuàng)建的 API 密鑰,你可以直接在程序中使用。但這并不是最佳實(shí)踐。

最佳實(shí)踐是從文件或你系統(tǒng)的環(huán)境變量中調(diào)用它。

對于 Windows,請?jiān)O(shè)置一個任何名字的環(huán)境變量,例如 API-KEY。并添加密鑰值。

對于 Linux,請使用超級用戶權(quán)限打開 /etc/environment 文件并添加密鑰。例如:

API-KEY="<你的密鑰>"

對于基于文件的密鑰訪問,請?jiān)谀愕拇a中使用以下語句:

openai.api_key_path = <你的 API 密鑰路徑>

對于直接在代碼中訪問(不建議),你可以在你的代碼中使用以下語句:

openai.api_key = "你的密鑰"

注意:如果驗(yàn)證失敗,OpenAI API 將拋出以下錯誤。你需要驗(yàn)證你的密鑰值、路徑和其他參數(shù)以進(jìn)行更正:openai.error.AuthenticationError: No API key provided

安裝 OpenAI API

最后一步是安裝 OpenAI 的 Python 庫。打開終端或命令窗口,使用以下命令安裝 OpenAI API。

pip install openai

在此階段,你已經(jīng)準(zhǔn)備好撰寫你的第一個程序了!

編寫助手程序(逐步)

OpenAI API 提供了各種接口模式。例如“聊天補(bǔ)完”、“代碼補(bǔ)完”、“圖像生成”等。在本指南中,我將使用 API 的“聊天補(bǔ)完”功能。使用此功能,我們可以創(chuàng)建一個簡單的對話聊天機(jī)器人。

首先,你需要導(dǎo)入 OpenAI 庫。你可以使用以下語句在你的 Python 程序中完成:

import openai

在這個語句之后,你應(yīng)該確保啟用你的 API 密鑰。你可以使用上面解釋的任何方法來完成。

openai.api_key="your key here"
openai.api_key="your environment variable"
openai.api_key_path = <your path to API key>

OpenAI 聊天 API 的基本功能如下所示。openai.ChatCompletion.create 函數(shù)以 JSON 格式接受多個參數(shù)。這些參數(shù)的形式是 “角色”(role) 和 “內(nèi)容”(content):

openai.ChatCompletion.create(   
  model = "gpt-3.5-turbo",   
  messages = [         
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}     
  ]
)

說明:

role: 有效的值為 systemuser、assistant

  • system: 指示 API 如何行動?;旧希?OpenAI 的主提示。
  • user: 你要問的問題。這是單個或多個會話中的用戶輸入。它可以是多行文本。
  • assistant: 當(dāng)你編寫對話時,你需要使用此角色來添加響應(yīng)。這樣,API 就會記住討論的內(nèi)容。

注意:在一個單一的消息中,你可以發(fā)送多個角色。如上述代碼片段所示的行為、你的問題和歷史記錄。

讓我們定義一個數(shù)組來保存 OpenAI 的完整消息。然后向用戶展示提示并接受 system 指令。

messages = []
system_message = input("What type of chatbot you want me to be?")
messages.append({"role":"system","content":system_message})

一旦設(shè)置好了,再次提示用戶進(jìn)行關(guān)于對話的進(jìn)一步提問。你可以使用 Python 的 input 函數(shù)(或任何其他文件輸入方法),并為角色 user 設(shè)置 content

print("Alright! I am ready to be your friendly chatbot" + "\n" + "You can now type your messages.")
message = input("")
messages.append({"role":"user","content": message})

現(xiàn)在,你已經(jīng)準(zhǔn)備好了具有基本 JSON 輸入的數(shù)組,用于調(diào)用“聊天補(bǔ)完”服務(wù)的 create 函數(shù)。

response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=messages)

現(xiàn)在,你可以對其進(jìn)行適當(dāng)?shù)母袷交?,要么打印響?yīng),要么解析響應(yīng)。響應(yīng)是以 JSON 格式提供的。輸出響應(yīng)提供了 choices 數(shù)組。響應(yīng)在 message JSON 對象下提供,其中包括 content 值。

對于此示例,我們可以讀取 choices 數(shù)組中的第一個對象并讀取其 content。

reply = response["choices"][0]["message"]["content"]
print(reply)

最后,它將為你提供來自 API 的輸出。

運(yùn)行代碼

你可以從你的 喜好的 Python IDE 或直接從命令行運(yùn)行代碼。

python OpenAIDemo2.py

未格式化的 JSON 輸出

以下是使用未格式化的 JSON 輸出運(yùn)行上述程序供你參考。正如你所看到的,響應(yīng)在 choices 數(shù)組下具有 content。

[debugpoint@fedora python]$ python OpenAIDemo2.py
What type of chatbot you want me to be?a friendly friend
Alright! I am ready to be your friendly chatbot
You can now type your messages.
what do you think about kindness?
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "As an AI language model, I don't have personal opinions, but I can tell you that kindness is a very positive and essential trait to have. Kindness is about being considerate and compassionate towards others, which creates positive emotions and reduces negativity. People who are kind towards others are more likely to inspire kindness and compassion in return. It is an important quality that helps to build positive relationships, promote cooperation, and create a more peaceful world.",
        "role": "assistant"
      }
    }
  ],
  "created": <removed>,
  "id": "chatcmpl-<removed>",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 91,
    "prompt_tokens": 22,
    "total_tokens": 113
  }
}

格式化的輸出

這是一個適當(dāng)?shù)膶υ捠捷敵觥?/p>

[debugpoint@fedora python]$ python OpenAIDemo2.py
What type of chatbot you want me to be?a friendly friend
Alright! I am ready to be your friendly chatbot
You can now type your messages.
what do you think about artificial general intelligence?
As an AI language model, I am programmed to be neutral and not have personal opinions. However, artificial general intelligence (AGI) is a fascinating field of study. AGI refers to the development of machines and algorithms that can perform any intellectual task that a human being can. The potential benefits and risks of AGI are still widely debated, with some experts worried about the implications of machines reaching human-like intelligence. However, many believe that AGI has the potential to revolutionize fields such as healthcare, education, and transportation. The key is to ensure that AGI is developed in a responsible and ethical manner.

完整代碼

這是上面演示中使用的完整代碼。

import openai
openai.api_key = "<your key>"
messages = []
system_message = input("What type of chatbot you want me to be?")
messages.append({"role":"system","content":system_message})
print("Alright! I am ready to be your friendly chatbot" + "\n" + "You can now type your messages.")
message = input("")
messages.append({"role":"user","content": message})
response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=messages
)
reply = response["choices"][0]["message"]["content"]
print(reply)

總結(jié)

希望這篇簡單的指南能讓你開始嘗試 OpenAI CharGPT API。你可以將上述步驟擴(kuò)展到更復(fù)雜的會話式聊天機(jī)器人。此外,你還可以使用 OpenAI 的其他產(chǎn)品。

請不要錯過我后續(xù)的教程,我將會實(shí)驗(yàn)和分享給大家。最后,請不要忘記關(guān)注我們,以便及時獲取我們的文章。

如果上述步驟對你有幫助,請?jiān)谠u論框中告訴我。

干杯!

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2012-05-25 15:20:38

XNA

2019-12-31 08:00:00

DebianLinuxApple Swift

2022-10-17 10:28:05

Web 組件代碼

2009-06-26 16:07:43

MyEclipse開發(fā)Hibernate程序

2021-03-24 08:00:44

項(xiàng)目Vue 3Typescript

2013-10-30 22:10:28

Clouda程序

2021-11-02 08:00:00

機(jī)器學(xué)習(xí)API技術(shù)

2011-06-08 10:24:38

Windows Pho 應(yīng)用程序

2011-06-08 10:01:36

Windows Pho 應(yīng)用程序

2025-04-27 03:33:00

2013-01-14 09:44:58

JavaScriptJSJS框架

2023-05-04 07:16:50

ChatGPT開源

2023-02-08 19:46:25

ChatGPTOpenAI生產(chǎn)力

2018-10-15 10:10:41

Linux內(nèi)核補(bǔ)丁

2013-12-19 09:46:04

垃圾收集器

2024-06-07 08:59:35

2022-11-01 07:23:55

Dockernetcore程序

2010-03-25 16:04:56

Python程序執(zhí)行

2011-04-02 10:08:21

webOS開發(fā)應(yīng)用程序

2010-08-04 13:16:23

Flex項(xiàng)目
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號