ChatGPT提示词工程(七):Chatbot聊天机器人

这篇具有很好参考价值的文章主要介绍了ChatGPT提示词工程(七):Chatbot聊天机器人。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、说明

这是吴恩达 《ChatGPT Prompt Engineering for Developers》 的课程笔记系列。
本文是第八讲的内容:Chatbot

二、安装环境

参考: ChatGPT提示词工程(一):Guidelines准则 的第二节

其中,辅助函数有变化:

1. 辅助函数:get_completion

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message["content"]

ChatGPT提示词工程(七):Chatbot聊天机器人

2. 辅助函数:get_completion_from_messages

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, # this is the degree of randomness of the model's output
    )
	# print(str(response.choices[0].message))
    return response.choices[0].message["content"]

ChatGPT提示词工程(七):Chatbot聊天机器人

这里,可以自定义消息,message里面的role,可以是systemuserassistant
system:系统辅助模型的角色,用户不可知;
user:与模型交互的角色,就是我们;
assistant:指模型

https://blog.csdn.net/Jay_Xio/article/details/130463604



文章来源地址https://www.toymoban.com/news/detail-464594.html

三、聊天机器人(Chatbot)

1. 一般聊天机器人

system角色告诉模型它是什么角色

1.1 简单的例子

messages =  [  
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},    
{'role':'user', 'content':'tell me a joke'},   
{'role':'assistant', 'content':'Why did the chicken cross the road'},   
{'role':'user', 'content':'I don\'t know'}  ]

response = get_completion_from_messages(messages, temperature=1)
print(response)

message
角色system:告诉模型,你是个说话像莎士比亚的助手;
角色user:告诉模型,给我讲个笑话
角色assistant:模型讲了一个笑话:小鸡为什么要过马路?
角色user:告诉模型,我不知道
然后,代码运行结果(即模型输出):
ChatGPT提示词工程(七):Chatbot聊天机器人


1.2 多轮对话

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Hi, my name is Isa'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

message
角色system:告诉模型,你是个友善的机器人;
角色user:告诉模型,嗨,我的名字叫Isa
然后,代码运行结果(即模型输出):
ChatGPT提示词工程(七):Chatbot聊天机器人

接下来,继续对话

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Yes,  can you remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

先看运行结果:
ChatGPT提示词工程(七):Chatbot聊天机器人
上一轮对话中,我告诉模型我叫 Isa,机器人也给我友好地打招呼了,然而现在我问它“你还记得我吗,我叫什么名字?”的时候,机器人已经不知道了。
要怎么解决呢?
要继续之前的对话,再次发起对话时,要把之前的对话内容一起带上,才能让模型知道我们此次对话的上下文。

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \
Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

代码中,message带上了前一轮对话我们问的问题和它回答的结果,后面再加上我们此次要问的问题
运行结果:
ChatGPT提示词工程(七):Chatbot聊天机器人

2. 订单机器人

def collect_messages(_):
    prompt = inp.value_input
    inp.value = ''
    context.append({'role':'user', 'content':f"{prompt}"})
    response = get_completion_from_messages(context) 
    context.append({'role':'assistant', 'content':f"{response}"})
    panels.append(
        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
    panels.append(
        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
 
    return pn.Column(*panels)
import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
The menu includes \
pepperoni pizza  12.95, 10.00, 7.00 \
cheese pizza   10.95, 9.25, 6.50 \
eggplant pizza   11.95, 9.75, 6.75 \
fries 4.50, 3.50 \
greek salad 7.25 \
Toppings: \
extra cheese 2.00, \
mushrooms 1.50 \
sausage 3.00 \
canadian bacon 3.50 \
AI sauce 1.50 \
peppers 1.00 \
Drinks: \
coke 3.00, 2.00, 1.00 \
sprite 3.00, 2.00, 1.00 \
bottled water 5.00 \
"""} ]  # accumulate messages


inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
    inp,
    pn.Row(button_conversation),
    pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

dashboard

代码中,导入了一个GUI,用界面来展示对话,collect_messages函数会收集我们每轮对话,再我要问机模型问题时把前面的对话都发给模型
运行结果:
ChatGPT提示词工程(七):Chatbot聊天机器人

ChatGPT提示词工程(七):Chatbot聊天机器人

下单完成后,订单机器人就可以,把我们下的订单总结成JSON,发给订单系统来结账

messages =  context.copy()
messages.append(
{'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\
 The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size   4) list of sides include size  5)total price '},    
)
 #The fields should be 1) pizza, price 2) list of toppings 3) list of drinks, include size include price  4) list of sides include size include price, 5)total price '},    

response = get_completion_from_messages(messages, temperature=0)
print(response)

ChatGPT提示词工程(七):Chatbot聊天机器人

https://blog.csdn.net/Jay_Xio/article/details/130463604



到了这里,关于ChatGPT提示词工程(七):Chatbot聊天机器人的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 基于Python+百度语音的智能语音ChatGPT聊天机器人(机器学习+深度学习+语义识别)含全部工程源码 适合个人二次开发

    基于Python+百度语音的智能语音ChatGPT聊天机器人(机器学习+深度学习+语义识别)含全部工程源码 适合个人二次开发

    本项目基于机器学习和语义识别技术,让机器人理解文本并进行合适的答复。伙伴们可以通过该工程源码,进行个人二次开发,比如使用语音与机器人交流,实现智能问答、智能音箱及智能机器宠物等等。 当然针对现在最火爆的 ChatGPT等通用大语言模型 ,伙伴们可以直接将其

    2024年02月07日
    浏览(12)
  • 【EAI 006】ChatGPT for Robotics:将 ChatGPT 应用于机器人任务的提示词工程研究

    【EAI 006】ChatGPT for Robotics:将 ChatGPT 应用于机器人任务的提示词工程研究

    论文标题:ChatGPT for Robotics: Design Principles and Model Abilities 论文作者:Sai Vemprala, Rogerio Bonatti, Arthur Bucker, Ashish Kapoor 作者单位:Scaled Foundations, Microsoft Autonomous Systems and Robotics Research 论文原文:https://arxiv.org/abs/2306.17582 论文出处:TMLR 论文被引:148(01/05/2024) 论文代码:https:

    2024年01月21日
    浏览(15)
  • 【人工智能】谷歌的巴德聊天机器人向公众开放 | Google‘s Bard Chatbot Opens to the Public

    【人工智能】谷歌的巴德聊天机器人向公众开放 | Google‘s Bard Chatbot Opens to the Public

      Google is trying to balance AI progress with caution. 谷歌正试图谨慎地平衡人工智能的进展。 目录 https://bard.google.com/

    2024年02月09日
    浏览(35)
  • 【记录】终端如何 进入conda环境,如何退出 conda(base)环境,终端快速进入Jupyter notebook的方法 | 人工智能面试题:什么是聊天机器人(Chatbot)?列举一些常用的

    【记录】终端如何 进入conda环境,如何退出 conda(base)环境,终端快速进入Jupyter notebook的方法 | 人工智能面试题:什么是聊天机器人(Chatbot)?列举一些常用的

      “一个人走向末路往往是因为不遗余力地寻找捷径。”     🎯作者主页: 追光者♂🔥          🌸个人简介:   💖[1] 计算机专业硕士研究生💖   🌿[2] 2023年城市之星领跑者TOP1(哈尔滨)🌿   🌟[3] 2022年度博客之星人工智能领域TOP4🌟   🏅[4] 阿里云社区特邀专家博

    2024年02月07日
    浏览(17)
  • ChatGPT聊天机器人问答实录

    最近ChatGPT爆火,我也在网上找到一个ChatGPT的接口(文末附链接),尝试了一下与人工智能对话的乐趣。我下载的这个应用是基于GPT-3的聊天机器人,我与它的几个问答对话实际记录如下: 答:百度和谷歌是世界上最大的搜索引擎之一,虽然它们在不同的市场中运营,但它们

    2024年02月01日
    浏览(10)
  • 非工程师指南: 训练 LLaMA 2 聊天机器人

    本教程将向你展示在不编写一行代码的情况下,如何构建自己的开源 ChatGPT,这样人人都能构建自己的聊天模型。我们将以 LLaMA 2 基础模型为例,在开源指令数据集上针对聊天场景对其进行微调,并将微调后的模型部署到一个可分享的聊天应用中。全程只需点击鼠标,即可轻

    2024年02月03日
    浏览(11)
  • ChatGPT聊天机器人如何发图片????

    ChatGPT聊天机器人如何发图片????

    问题一、怎么让聊天机器人ChatGPT回复你一张图片? 有问题可以在评论区留言。

    2024年02月11日
    浏览(13)
  • ai聊天机器人chatgpt收费版

    ai聊天机器人chatgpt收费版

        AI聊天机器人的功能通常包括以下几个方面:     自然语言理解。该功能可以识别并理解用户输入的自然语言,如文本、语音等,以便进行后续的处理和回复。     对话管理。该功能可以管理对话的上下文和流程,并根据用户的输入和意图来产生相应的回复和行为。  

    2024年02月03日
    浏览(12)
  • ChatGPT 4.0:AI 聊天机器人

    ChatGPT 4.0:AI 聊天机器人

    当 ChatGPT 问世时,人们对它作为 AI 聊天机器人的自然语言能力印象深刻,人们感到敬畏。但是,当备受期待的 GPT-4 大型语言模型问世时,它揭开了我们认为 AI 可能实现的盖子,有人称其为 AGI(通用人工智能)的早期一瞥。 该模型的创建者OpenAI称其为该公司“最先进的系统

    2024年02月02日
    浏览(15)
  • 中文版ChatGPT:智能中文聊天机器人

    中文版ChatGPT:智能中文聊天机器人

    2017年,AlphaGo在与世界冠军柯洁的人机大战中获胜,引发了人们对人工智能的激烈讨论。 如果说,对于AlphaGo,人们更多是围观者的角色,而最新的人工智能爆款程序ChatGPT,更多人已经参与其中,上线短短两个月内,ChatGPT收获了一亿用户。而且此刻,国内版的ChatGPT也已经正式

    2024年02月12日
    浏览(53)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包