mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
parent
22fd2d45c2
commit
6cbd629481
@ -0,0 +1,315 @@
|
||||
[#]: subject: "Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide]"
|
||||
[#]: via: "https://www.debugpoint.com/openai-chatgpt-api-python"
|
||||
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "ChatGPT"
|
||||
[#]: reviewer: "wxy"
|
||||
[#]: publisher: "wxy"
|
||||
[#]: url: "https://linux.cn/article-15865-1.html"
|
||||
|
||||
使用 OpenAI ChatGPT API 创建你的第一个程序
|
||||
======
|
||||
|
||||
![][0]
|
||||
|
||||
> 以下是如何开发使用 OpenAI ChatGPT AI 创建聊天助手的 Python 程序的方法。
|
||||
|
||||
易于使用的 AI “ChatGPT” 已经以 API [提供][1]。ChatGPT 的创造者 OpenAI 宣布,模型('gpt-3.5-turbo')现在适用于自定义产品和解决方案。而且成本也非常实惠。目前的价格为每 1000 个令牌 0.002 美元。
|
||||
|
||||
该模型目前与 Whisper API 一起提供,后者也用于文本到语音解决方案。该 API 目前具备以下功能:
|
||||
|
||||
- 创建自定义的对话代理和机器人
|
||||
- 为你编写 Python 代码
|
||||
- 起草电子邮件或任何你想要的文档
|
||||
- 你可以将自然语言界面集成到你当前的产品/应用/服务或软件中,为你的消费者提供服务
|
||||
- 语言翻译服务
|
||||
- 成为许多科目的导师
|
||||
- 模拟视频游戏角色
|
||||
|
||||
正如你所见,机会无限。
|
||||
|
||||
如果你计划尝试该 API 并开始使用它,这里有一个简单的指南,为你提供逐步指导。
|
||||
|
||||
### OpenAI ChatGPT API: 入门指南
|
||||
|
||||
#### 先决条件
|
||||
|
||||
确保你拥有一个 OpenAI 账户。如果你没有,[访问此页面][2] 并创建一个账户。你也可以使用你的谷歌或微软账号。
|
||||
|
||||
创建一个账户后,生成一个专属于你的 API 密钥。访问 [此页面][3] 并创建一个新的秘密密钥。
|
||||
|
||||
![创建 OpenAI API 密钥的位置][4]
|
||||
|
||||
记录该密钥或在安全的地方保存它。基于安全原因,它将不会从 OpenAI 账户部分再次可见。而且不要与任何人分享此密钥。如果你计划使用企业解决方案,请向你的组织查询 API 密钥。由于该密钥与你的付费 OpenAI 计划相关,因此请谨慎使用。
|
||||
|
||||
### 设置环境
|
||||
|
||||
#### 安装 Python 和 pip
|
||||
|
||||
本指南使用 Python 编程语言来调用 OpenAI API 密钥。你可以使用 Java 或其他任何语言来调用它。
|
||||
|
||||
首先,请确保你在 Linux 或 Windows 中已经安装了 Python。如果没有,请按照以下指南安装 Python。如果你使用现代 Linux 发行版(例如 Ubuntu),Python 应该已经安装好了。
|
||||
|
||||
- [如何在 Windows 上安装 Python][5]
|
||||
- [如何在 Ubuntu 及其他 Linux 上安装最新版 Python][6]
|
||||
|
||||
在安装 Python 后,确保 `pip` 在 Linux 发行版中可用。运行以下命令进行安装。对于 Windows,你应该已经在 Python 安装的一部分中安装了它。
|
||||
|
||||
Ubuntu、Debian 和其他基于 Debian 的发行版:
|
||||
|
||||
```
|
||||
sudo apt install python3-pip
|
||||
```
|
||||
|
||||
Fedora、RHEL、CentOS 等:
|
||||
|
||||
```
|
||||
sudo dnf install python3-pip
|
||||
```
|
||||
|
||||
Arch Linux:
|
||||
|
||||
```
|
||||
sudo pacman -S python-pip
|
||||
```
|
||||
|
||||
#### 将 OpenAI API 密钥设置为环境变量
|
||||
|
||||
上述步骤中创建的 API 密钥,你可以直接在程序中使用。但这并不是最佳实践。
|
||||
|
||||
最佳实践是从文件或你系统的环境变量中调用它。
|
||||
|
||||
对于 Windows,请设置一个任何名字的环境变量,例如 `API-KEY`。并添加密钥值。
|
||||
|
||||
对于 Linux,请使用超级用户权限打开 `/etc/environment` 文件并添加密钥。例如:
|
||||
|
||||
```
|
||||
API-KEY="<你的密钥>"
|
||||
```
|
||||
|
||||
对于基于文件的密钥访问,请在你的代码中使用以下语句:
|
||||
|
||||
```
|
||||
openai.api_key_path = <你的 API 密钥路径>
|
||||
```
|
||||
|
||||
对于直接在代码中访问(不建议),你可以在你的代码中使用以下语句:
|
||||
|
||||
```
|
||||
openai.api_key = "你的密钥"
|
||||
```
|
||||
|
||||
**注意**:如果验证失败,OpenAI API 将抛出以下错误。你需要验证你的密钥值、路径和其他参数以进行更正:`openai.error.AuthenticationError: No API key provided`。
|
||||
|
||||
#### 安装 OpenAI API
|
||||
|
||||
最后一步是安装 OpenAI 的 Python 库。打开终端或命令窗口,使用以下命令安装 OpenAI API。
|
||||
|
||||
```
|
||||
pip install openai
|
||||
```
|
||||
|
||||
在此阶段,你已经准备好撰写你的第一个程序了!
|
||||
|
||||
### 编写助手程序(逐步)
|
||||
|
||||
OpenAI API 提供了各种接口模式。例如“聊天补完”、“代码补完”、“图像生成”等。在本指南中,我将使用 API 的“聊天补完”功能。使用此功能,我们可以创建一个简单的对话聊天机器人。
|
||||
|
||||
首先,你需要导入 OpenAI 库。你可以使用以下语句在你的 Python 程序中完成:
|
||||
|
||||
```
|
||||
import openai
|
||||
```
|
||||
|
||||
在这个语句之后,你应该确保启用你的 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` 函数以 JSON 格式接受多个参数。这些参数的形式是 “角色”(`role`) 和 “内容”(`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`: 有效的值为 `system`、`user`、`assistant`
|
||||
|
||||
- `system`: 指示 API 如何行动。基本上,它是 OpenAI 的主提示。
|
||||
- `user`: 你要问的问题。这是单个或多个会话中的用户输入。它可以是多行文本。
|
||||
- `assistant`: 当你编写对话时,你需要使用此角色来添加响应。这样,API 就会记住讨论的内容。
|
||||
|
||||
**注意**:在一个单一的消息中,你可以发送多个角色。如上述代码片段所示的行为、你的问题和历史记录。
|
||||
|
||||
让我们定义一个数组来保存 OpenAI 的完整消息。然后向用户展示提示并接受 `system` 指令。
|
||||
|
||||
|
||||
```
|
||||
messages = []
|
||||
system_message = input("What type of chatbot you want me to be?")
|
||||
messages.append({"role":"system","content":system_message})
|
||||
```
|
||||
|
||||
一旦设置好了,再次提示用户进行关于对话的进一步提问。你可以使用 Python 的 `input` 函数(或任何其他文件输入方法),并为角色 `user` 设置 `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})
|
||||
```
|
||||
|
||||
现在,你已经准备好了具有基本 JSON 输入的数组,用于调用“聊天补完”服务的 `create` 函数。
|
||||
|
||||
```
|
||||
response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=messages)
|
||||
```
|
||||
|
||||
现在,你可以对其进行适当的格式化,要么打印响应,要么解析响应。响应是以 JSON 格式提供的。输出响应提供了 `choices` 数组。响应在 `message` JSON 对象下提供,其中包括 `content` 值。
|
||||
|
||||
对于此示例,我们可以读取 `choices` 数组中的第一个对象并读取其 `content`。
|
||||
|
||||
```
|
||||
reply = response["choices"][0]["message"]["content"]
|
||||
print(reply)
|
||||
```
|
||||
|
||||
最后,它将为你提供来自 API 的输出。
|
||||
|
||||
### 运行代码
|
||||
|
||||
你可以从你的 [喜好的 Python IDE][7] 或直接从命令行运行代码。
|
||||
|
||||
```
|
||||
python OpenAIDemo2.py
|
||||
```
|
||||
|
||||
#### 未格式化的 JSON 输出
|
||||
|
||||
以下是使用未格式化的 JSON 输出运行上述程序供你参考。正如你所看到的,响应在 `choices` 数组下具有 `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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 格式化的输出
|
||||
|
||||
这是一个适当的对话式输出。
|
||||
|
||||
```
|
||||
[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)
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
希望这篇简单的指南能让你开始尝试 OpenAI CharGPT API。你可以将上述步骤扩展到更复杂的会话式聊天机器人。此外,你还可以使用 OpenAI 的其他产品。
|
||||
|
||||
请不要错过我后续的教程,我将会实验和分享给大家。最后,请不要忘记关注我们,以便及时获取我们的文章。
|
||||
|
||||
如果上述步骤对你有帮助,请在评论框中告诉我。
|
||||
|
||||
干杯!
|
||||
|
||||
> **[参考资料][10]**
|
||||
|
||||
*(题图:MJ/b206dd48-f698-4800-bccc-19ea11a17ea6)*
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.debugpoint.com/openai-chatgpt-api-python
|
||||
|
||||
作者:[Arindam][a]
|
||||
选题:[lkxed][b]
|
||||
译者:ChatGPT
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.debugpoint.com/author/admin1/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://openai.com/blog/introducing-chatgpt-and-whisper-apis
|
||||
[2]: https://chat.openai.com/
|
||||
[3]: https://platform.openai.com/account/api-keys
|
||||
[4]: https://www.debugpoint.com/wp-content/uploads/2023/03/Where-to-create-openAI-API-key.jpg
|
||||
[5]: https://www.debugpoint.com/install-python-windows/
|
||||
[6]: https://www.debugpoint.com/install-python-3-11-ubuntu/
|
||||
[7]: https://www.debugpoint.com/5-best-python-ide-code-editor/
|
||||
[8]: https://www.debugpoint.com/wp-content/uploads/2023/03/unformatted-JSON-output.jpg
|
||||
[9]: https://www.debugpoint.com/wp-content/uploads/2023/03/OpenAI-ChatGPT-API-output-from-a-Python-prgram.jpg
|
||||
[10]: https://platform.openai.com/docs/guides/chat/introduction
|
||||
[0]: https://img.linux.net.cn/data/attachment/album/202306/01/071320acb1o8okoczg9ok9.jpg
|
@ -1,310 +0,0 @@
|
||||
[#]: subject: "Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide]"
|
||||
[#]: via: "https://www.debugpoint.com/openai-chatgpt-api-python"
|
||||
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: " "
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide]
|
||||
======
|
||||
|
||||
**Here’s how to start your first Python program using OpenAI ChatGPT AI and create a chatbot assistant.**
|
||||
|
||||
The revolutionary, easy-to-use AI “ChatGPT” is now [available][1] as API. OpenAI, the creator of ChatGPT, announced that the model (`gpt-3.5-turbo`) is now available for your custom products and solutions. The cost is super affordable as well. It is currently priced at $0.002 for 1000 tokens.
|
||||
|
||||
The model is currently available alongside Whisper API, which is also used for text-to-speech solutions. The API is currently capable of the following:
|
||||
|
||||
- Create custom conversational agents and bots.
|
||||
- Write Python code for you
|
||||
- Draft emails or any documents you want
|
||||
- You can integrate your current product/app/service or software with a natural language interface for your consumers.
|
||||
- Language translation services
|
||||
- Be a tutor for many subjects
|
||||
- Simulate video game characters
|
||||
|
||||
As you can see, the opportunities are endless.
|
||||
|
||||
If you plan to try the API and get started, here’s a simple guide for you with step-by-step instructions on how to use it.
|
||||
|
||||
### OpenAI ChatGPT API: Getting Started
|
||||
|
||||
#### Pre-requisite
|
||||
|
||||
Make sure you have an OpenAI account. If you don’t[visit this page][2] and create an account. You can also use your Google or Microsoft account.
|
||||
|
||||
After you create an account, generate an API key which is exclusive to your account. Visit [this page][3] and create a new secret key.
|
||||
|
||||
![Where to create openAI API key][4]
|
||||
|
||||
Write down or save the key somewhere safe. It will not be visible from the OpenAI account section a second time for security reasons. And DO NOT share this key with anyone. If you plan to use enterprise solutions, check with your organizations for the API Key. This key is tied to your paid OpenAI plan; hence use it with caution.
|
||||
|
||||
### Setting up environment
|
||||
|
||||
#### Install Python and pip
|
||||
|
||||
This guide uses Python programming language to consume the OpenAI API key. You can use Java or any other language to consume it.
|
||||
|
||||
Firstly, make sure you have Python installed in Linux or Windows. If not, follow the below guides to install Python. If you are using modern Linux distributions such as Ubuntu, Python should already be installed.
|
||||
|
||||
- [How to install Python in Windows][5]
|
||||
- [How to Latest Python in U][6][b][6][untu and other Linux][6]
|
||||
|
||||
After Python is installed, make sure pip is available in Linux distributions. Run the following command to install it. For Windows, you should already have it as part of the Python installation.
|
||||
|
||||
**Ubuntu**, **Debian and others**
|
||||
|
||||
```
|
||||
sudo apt install python3-pip
|
||||
```
|
||||
|
||||
**Fedora, RHEL, CentOS and others**
|
||||
|
||||
```
|
||||
sudo dnf install python3-pip
|
||||
```
|
||||
|
||||
**Arch Linux**
|
||||
|
||||
```
|
||||
sudo pacman -S python-pip
|
||||
```
|
||||
|
||||
#### Set up OpenAI API Key as an environment variable
|
||||
|
||||
The API secret key which you have created in the above steps, you can directly use it in the program. But it is not recommended.
|
||||
|
||||
The best practice is to consume it from a file or your system’s environment variable.
|
||||
|
||||
For Windows, set up a `PATH` variable with any name, for example, “API-KEY”. And add the key value.
|
||||
|
||||
For Linux, open `/etc/environment` file using root privileges and add the key. For example:
|
||||
|
||||
```
|
||||
API-KEY="<your key here>"
|
||||
```
|
||||
|
||||
For file-based key access, use the below statement in your code:
|
||||
|
||||
```
|
||||
openai.api_key_path = <your path to API key>
|
||||
```
|
||||
|
||||
For direct access in code (not recommended), you can use the below statement in your code:
|
||||
|
||||
```
|
||||
openai.api_key="your key here"
|
||||
```
|
||||
|
||||
**Note**: If the authentication fails, OpenAI API throws the below error. You need to verify your key value, path and other parameters for correction: **openai.error.AuthenticationError: No API key provided**
|
||||
|
||||
#### Install OpenAI API
|
||||
|
||||
The final step is to install the Python library for OpenAI. Open a terminal or command window and install OpenAI API using the below.
|
||||
|
||||
```
|
||||
pip install openai
|
||||
```
|
||||
|
||||
At this stage, you are all set to write your first program!
|
||||
|
||||
### Coding the assistant (step-by-step)
|
||||
|
||||
OpenAI API provides various modes of interface. Such as “chat completion”, “code completion”, “image generation”, etc. In this guide, I will use the “chat completion” features of the API. Using this, we can create a simple conversation chatbot.
|
||||
|
||||
Firstly, you need to import the OpenAI library. You can do it using the below statement in your Python program.
|
||||
|
||||
```
|
||||
import openai
|
||||
```
|
||||
|
||||
Following this statement, you should make sure you enable your API key. You can do it in any of the ways explained above.
|
||||
|
||||
```
|
||||
openai.api_key="your key here"
|
||||
```
|
||||
|
||||
```
|
||||
openai.api_key="your environment variable"
|
||||
```
|
||||
|
||||
```
|
||||
openai.api_key_path = <your path to API key>
|
||||
```
|
||||
|
||||
The basic function of OpenAI chat API is below. The `openai.ChatCompletion.create` function takes several arguments in JSON format. The arguments are in the form of `"role"` and `"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?"}
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
**Explanation**
|
||||
|
||||
**role**: Valid values are “system”, “user”, “assistant”
|
||||
|
||||
**system**: Instruct the API on how to behave. Basically, it is the main prompt for OpenAI.
|
||||
|
||||
**user**: The question you want to ask. It is user input in single or multiple conversations. It can be multiple lines of text.
|
||||
|
||||
**assistant**: When you are coding a conversation, you need to use this role to append the response. So that the API remembers what the discussion is about.
|
||||
|
||||
**Note**: In one single message, you can send multiple roles. The behaviour, your question and the history as shown in above code snippet.
|
||||
|
||||
Let’s define an array to hold the entire message for OpenAI. Then show a prompt to the user and accept the `system` instructions.
|
||||
|
||||
```
|
||||
messages = []system_message = input("What type of chatbot you want me to be?")messages.append({"role":"system","content":system_message})
|
||||
```
|
||||
|
||||
Once it has been set, prompt to the user again for further questions about the conversation. You can use the Python input function (or any other file input method) and set the `content` for role `user`.
|
||||
|
||||
```
|
||||
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})
|
||||
```
|
||||
|
||||
At this stage, you have the array ready with basic JSON input to the OpenAI API. Now, all you need to do is call the create function for “chat completion” service using the created JSON.
|
||||
|
||||
```
|
||||
response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=messages)
|
||||
```
|
||||
|
||||
Now, you can either print the response or parse it for proper formatting. The response is in JSON format. The output response provides “`choices`” array. The response is provided under `message` JSON object with `content` value.
|
||||
|
||||
For this example, we can read the first object in the choices array and read the content.
|
||||
|
||||
```
|
||||
reply = response["choices"][0]["message"]["content"]print(reply)
|
||||
```
|
||||
|
||||
And finally, it will give you the output from the API.
|
||||
|
||||
### Running the code
|
||||
|
||||
You can run the code from your [favourite Python IDE][7] or directly from the command line.
|
||||
|
||||
```
|
||||
python OpenAIDemo2.py
|
||||
```
|
||||
|
||||
#### Unformatted JSON output
|
||||
|
||||
Here’s a run of the above program with unformatted JSON output for your reference. As you can see, the response is under `choices` array with `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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
![unformatted JSON output][8]
|
||||
|
||||
#### Formatted output
|
||||
|
||||
Here’s a proper conversational output.
|
||||
|
||||
```
|
||||
[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.
|
||||
```
|
||||
|
||||
![OpenAI ChatGPT API output from a Python prgram][9]
|
||||
|
||||
### Complete Code
|
||||
|
||||
Here’s the complete code which is used in the above demo.
|
||||
|
||||
```
|
||||
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)
|
||||
```
|
||||
|
||||
### Wrapping Up
|
||||
|
||||
I hope this simple guide can get you started with OpenAI CharGPT API. You can extend the above steps to a more complex conversational chatbot. Also, you can use the other offerings of OpenAI.
|
||||
|
||||
Stay tuned for further tutorials as I experiment and share them with you. And finally, don’t forget to follow us, so you don’t miss any articles.
|
||||
|
||||
Do let me know in the comment box if the above steps help you.
|
||||
|
||||
Cheers.
|
||||
|
||||
_[Reference][10]_
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.debugpoint.com/openai-chatgpt-api-python
|
||||
|
||||
作者:[Arindam][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.debugpoint.com/author/admin1/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://openai.com/blog/introducing-chatgpt-and-whisper-apis
|
||||
[2]: https://chat.openai.com/
|
||||
[3]: https://platform.openai.com/account/api-keys
|
||||
[4]: https://www.debugpoint.com/wp-content/uploads/2023/03/Where-to-create-openAI-API-key.jpg
|
||||
[5]: https://www.debugpoint.com/install-python-windows/
|
||||
[6]: https://www.debugpoint.com/install-python-3-11-ubuntu/
|
||||
[7]: https://www.debugpoint.com/5-best-python-ide-code-editor/
|
||||
[8]: https://www.debugpoint.com/wp-content/uploads/2023/03/unformatted-JSON-output.jpg
|
||||
[9]: https://www.debugpoint.com/wp-content/uploads/2023/03/OpenAI-ChatGPT-API-output-from-a-Python-prgram.jpg
|
||||
[10]: https://platform.openai.com/docs/guides/chat/introduction
|
Loading…
Reference in New Issue
Block a user