OpenAI API 提供了各种接口模式。例如“聊天补完”、“代码补完”、“图像生成”等。在本指南中,我将使用 API 的“聊天补完”功能。使用此功能,我们可以创建一个简单的对话聊天机器人。
首先,你需要导入 OpenAI 库。你可以使用以下语句在你的 Python 程序中完成:
import openai
在这个语句之后,你应该确保启用你的 API 密钥。你可以使用上面解释的任何方法来完成。
openai.api_key="your key here"openai.api_key="your environment variable"openai.api_key_path =
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数组。响应在messageJSON 对象下提供,其中包括content值。
对于此示例,我们可以读取 choices数组中的第一个对象并读取其content。
reply = response["choices"][0]["message"]["content"]print(reply)
最后,它将为你提供来自 API 的输出。
以下是使用未格式化的 JSON 输出运行上述程序供你参考。正如你所看到的,响应在 choices数组下具有content。
[debugpoint@fedora python]$ python OpenAIDemo2.pyWhat type of chatbot you want me to be?a friendly friendAlright! I am ready to be your friendly chatbotYou 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":
格式化的输出
这是一个适当的对话式输出。
[debugpoint@fedora python]$ python OpenAIDemo2.pyWhat type of chatbot you want me to be?a friendly friendAlright! I am ready to be your friendly chatbotYou 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.