构建一个能教我们人工智能的聊天机器人ChatGPT。
简介:
使用ChatGPT的帮助,开发人员可以轻松构建对话式人工智能应用程序。开发人员可以使用ChatGPT创建聊天机器人、虚拟助手和其他对话式人工智能系统,这些系统可以与人类自然地、友好地互动。
一个比text-davinci-003便宜的选择是ChatGPT的gpt-3.5-turbo变种,它仍然提供可比较的性能。这使它成为大多数用例的最佳选择,因为它提供了可观的成本降低而不牺牲质量。
让我们使用ChatGPT来制作聊天机器人:
为了开始使用ChatGPT,你需要安装OpenAI Python包。你可以通过执行以下命令来完成安装:
pip install openai -q
import openai
openai.api_key = "YOUR_OPENAI_API_KEY"
您可以从官方openAI网站的个人资料中获取您的api密钥。
构建一个对话型AI聊天机器人
您必须接着定义您的对话。对话的定义是一个消息对象的集合,每个对象都有一个角色(系统,用户或助手)和内容(消息的内容)。如果需要,一个消息可以作为整个对话的全部。通常,讨论是这样设计的:系统消息首先出现,然后是交替的用户和助手消息。
以下是一个对话的示例:
messages = [
{"role": "system", "content": "You are a helpful AI Tutor."},
{"role": "user", "content": "I am Tejpal, I want to learn AI"},
{"role": "assistant", "content": "Hello, Tejpal. That's awesome! What do you want to know about AI?"},
{"role": "user", "content": "What is NLP?"}
]
使用openai.ChatCompletion.create()方法可以获取ChatGPT API的响应。此方法需要两个参数:要使用的模型的名称和之前定义的对话。
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful AI Tutor."},
{"role": "user", "content": "I am Pradip, I want to learn AI"},
{"role": "assistant", "content": "Hello, Pradip Thats awesome, what do you want to know aboout AI"},
{"role": "user", "content": "What is NLP?"}
]
)
openai.ChatCompletion.create() 方法返回一个包含AI响应的响应对象。您可以通过访问choices列表,然后访问message对象来获得AI的响应。以下是一个示例:
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "NLP stands for Natural Language Processing. It is a branch of AI that focuses on enabling machines to understand, interpret, and generate human language. NLP algorithms use various techniques to analyze and derive meaning from human language, including but not limited to, syntax, semantics, and pragmatics. Some of the common applications of NLP include chatbots, sentiment analysis, machine translation, and text summarization.",
"role": "assistant"
}
}
]
你可以使用以下基本原则创建一个简单的会话式人工智能应用程序。这是一个简单的基于控制台的聊天机器人示例,你可以使用ChatGPT创建它:
def update_chat(messages, role, content):
messages.append({"role": role, "content": content})
return messages
让我们将这个辅助函数与chatgpt的输出集成。
def get_chatgpt_response(messages):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
return response['choices'][0]['message']['content']
import pprint
messages=[
{"role": "system", "content": "You are a helpful AI Tutor."},
{"role": "user", "content": "I am Tejpal, I want to learn AI"},
{"role": "assistant", "content": "Hello, TejpalThats awesome, what do you want to know aboout AI"},
]
while True:
pprint.pprint(messages)
user_input = input()
messages = update_chat(messages, "user", user_input)
model_response = get_chatgpt_response(messages)
messages = update_chat(messages, "assistant", model_response)
使用OpenAI GPT-3模型,此代码创建了一个用户与AI助手之间的交互式聊天。它首先定义一个名为messages的列表,其中包含三个消息,每个消息表示对话的开始。
while循环会使用pprint.pprint(messages)反复显示对话的当前状态,并通过将user_input设置为input()来请求用户的反馈。
update_chat函数用于将用户输入添加到消息列表中,并将角色设置为“user”。然后AI助理使用get_chatgpt_response方法将当前消息列表提交给OpenAI GPT-3模型以生成对话的响应。
然后将模型生成的回应添加到消息列表中,角色设置为“助手”。然后循环重复,显示更新后的会话状态,并等待用户输入另一条消息。该过程将持续进行,直到循环被手动中断或程序停止。
最终对话
[{'content': 'You are a helpful AI Tutor.', 'role': 'system'},
{'content': 'I am Tejpal, I want to learn AI', 'role': 'user'},
{'content': 'Hello, Tejpal Thats awesome, what do you want to know aboout AI',
'role': 'assistant'},
{'content': 'What is NLP?', 'role': 'user'},
{'content': 'NLP stands for Natural Language Processing. It is a branch of '
'Artificial Intelligence that is focused on enabling machines to '
'understand, interpret, and generate human language. This '
'technology can be used to analyze large amounts of text data, '
'understand sentiment, language translation, and more. NLP is a '
'rapidly growing field that has many applications in industries '
'such as customer service, marketing, healthcare, and finance.',
'role': 'assistant'},
{'content': 'can you tell me three use cases of NLP?', 'role': 'user'},
{'content': 'Sure! Here are three use cases of NLP:\n'
'\n'
'1. Sentiment Analysis: NLP can be used to analyze large amounts '
'of text data, such as social media posts or customer reviews, to '
'determine the sentiment behind them. This can help companies '
'understand how customers feel about their brand, products, or '
'services, and can inform decision-making around marketing, '
'customer service, and product development.\n'
'\n'
'2. Language Translation: NLP can be used to translate text from '
'one language to another. This is a useful application in many '
'contexts, such as global business and cross-cultural '
'communication.\n'
'\n'
'3. Chatbots: NLP can also be used to develop chatbots that can '
'interact with customers in a natural, conversational way. '
'Chatbots can be used for customer service, sales, and other '
'applications, and can be integrated into websites, messaging '
'apps, and other platforms.',
'role': 'assistant'},
{'content': 'Can you repeat my name?', 'role': 'user'},
{'content': 'Yes, your name is Pradip.', 'role': 'assistant'},
{'content': 'can you talk more about the second usecase?', 'role': 'user'},
{'content': 'Sure! Language translation is an important use case of NLP. With '
'the help of machine translation systems, applications can now '
'translate text from one language to another. This use can be '
'helpful for people who need to translate documents, websites, or '
'other types of content from one language to another. \n'
'\n'
'NLP-powered machine translation engines learn to use a '
'combination of statistical models, neural networks, and '
'rule-based systems to automatically translate text from one '
'language to another. These models learn from vast amounts of '
'text data in multiple languages and can handle complex language '
'structures and nuances. \n'
'\n'
'One challenge with machine translation is maintaining accuracy '
'and context during translation. The quality of translation '
'depends on several factors, such as the language pair being '
'translated and the domain of the content being translated. '
'Despite these challenges, machine translation has made great '
'strides in recent years, and continues to be a promising field '
'in NLP.',
'role': 'assistant'}]
你可以根据特定主题自定义这个聊天机器人,也可以提供一些特定任务的例子,让聊天机器人按照我们的需求工作。
参考:
https://chat.openai.com/