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

news/2024/5/20 6:35:25 标签: chatgpt, 机器人, openai, prompt, 人工智能

目录

  • 一、说明
  • 二、安装环境
    • 1. 辅助函数:get_completion
    • 2. 辅助函数:get_completion_from_messages
  • 三、聊天机器人(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"]

在这里插入图片描述

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"]

在这里插入图片描述

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

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



三、聊天机器人(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:告诉模型,我不知道
然后,代码运行结果(即模型输出):
在这里插入图片描述


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
然后,代码运行结果(即模型输出):
在这里插入图片描述

接下来,继续对话

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)

先看运行结果:
在这里插入图片描述
上一轮对话中,我告诉模型我叫 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带上了前一轮对话我们问的问题和它回答的结果,后面再加上我们此次要问的问题
运行结果:
在这里插入图片描述

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函数会收集我们每轮对话,再我要问机模型问题时把前面的对话都发给模型
运行结果:
在这里插入图片描述

在这里插入图片描述

下单完成后,订单机器人就可以,把我们下的订单总结成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)

在这里插入图片描述

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




http://www.niftyadmin.cn/n/282272.html

相关文章

优思学院|受控文件在质量管理体系中的作用?

在质量管理体系中,受控文件是指受到控制和管理的文件,包括政策、程序、指南、规程、说明书、作业指导书、记录等,它们记录了组织内各种活动的要求和实施方法,并规定了文件的创建、审批、发布、变更和废止等流程,以确保…

《企业级Linux高可用负载均衡集群实践真传》目录

第1章 关于负载均衡... 2 1.1 负载均衡定义... 2 1.2 负载均衡在生产环境中的基本要求... 3 1.2.1 在线可扩展性... 3 1.2.2 高可用性... 3 1.2.3 多服务性... 4 1.3 负载均衡基本功能... 4 1.3.1 负载均衡... 4 1.3.2 健康检查... 5 …

业绩稳健增长,公牛集团新老业务如何实现齐头并进?

“插座一哥”公牛集团,正在经历其迈向更高质量发展的自我优化。 4月27日晚,公牛集团(SH:603195)发布了《2022年年度报告》及《2023年第一季度报告》。去年,宏观市场动荡,但公牛集团不仅保持了业绩的稳健增…

E. Multihedgehog(多叉树找root节点)

Problem - E - Codeforces 有人给Ivan一个奇怪的生日礼物,这是一只刺猬 - 一个连通的无向图,其中一个顶点的度至少为3(我们称其为中心),而所有其他顶点的度数均为1。Ivan认为刺猬太无聊了,决定自己制造k-多…

ARM批量数据加载/存储指令

ARM微处理器所支持批量数据加载/存储指令可以一次在一片连续的存储单元和多个寄存器之间传送数据,批量加载指令用于将一片连续的存储器中的数据传送到多个寄存器,批量数据存储指令则完成相反的操作。常用的加载存储指令如下。 LDM:批量数据加…

unity 性能优化之GPU和资源优化

Shader相关优化 众所周知,我们在unity里编写Shader使用的HLSL/CG都是高级语言,这是为了可以书写一套Shader兼容多个平台,在unity打包的时候,它会编译成对应平台可以运行的指令,而变体则是,根据宏生成的&am…

JavaScript学习笔记(三)

文章目录 第7章:迭代器与生成器1. 迭代器模式2. 生成器 第8章:对象、类与面向对象编程1. 理解对象2. 创建对象3. 继承:依靠原型链实现4. 类class 第10章:函数1. 函数定义的方式有:函数声明、函数表达式、箭头函数&…

WIZnet 的 TOE 设计大赛

链接: TOE Design Contest 介绍 欢迎来到 WIZnet 的年度物联网设计大赛! TOE 竞赛是对您的 IoT 技能和创造力的终极考验。 借助强大的 W5300 网络控制器芯片,您将能够以前所未有的方式将您的物联网愿景变为现实。 无论您是经验丰富的专业人士还是刚刚起…