《镜子大全》《朝华午拾》分享 http://blog.sciencenet.cn/u/liwei999 曾任红小兵,插队修地球,1991年去国离乡,不知行止。

博文

《AI潮流:跟Andrew学如何调用 ChatGPT 做自己的服务前台》

已有 2220 次阅读 2023-5-5 08:45 |个人分类:AI 浪潮|系统分类:教学心得

Andrew Ng 是华裔AI翘楚,不用介绍了。最近,Andrew 亲自参与的这个提示工程的课程,最精华部分是课程最后一节:如何调用 chatGPT 的 API 做一个自己的功能性聊天机器人,例如披萨店订单系统。

ChatGPT刚发布不久,我们就在群里讨论过,想不明白如何驯服这巨大无比的 chat 让它去完成功能性的助理工作。现在看来,非常简单易行。

Andrew 的女搭档一步一步显示了构建全过程,以披萨店菜单为落脚点,用自然语言指令要求调用了 chat 的机器人一步一步与客户周旋,直到所有信息齐全可以匹配菜单,输出订单。

简单到跟玩似的。

看看它的自然语言提示词指令是怎么写的:

您是 orderbot,一个自动化的在线服务,用于收集比萨店的订单。您首先向客户问候,然后收集订单,然后询问它是否为自取或送货。您等待收集整个订单,然后总结并再次检查客户是否要添加其他任何物品。如果是交付,则可以要求提供地址。最后,您收取付款。请确保澄清所有选项、附加项和尺寸,以便从菜单中唯一地识别该项。您以简短、非常友好的方式回复。在此处我们有菜单。

这不就是把订单的流程描述一遍吗?chat 就懂了,然后就工作了?

对,基本就是如此。

大型语言模型的一个令人兴奋的方面是,您可以仅需少量的工作就可以使用它来构建自定义聊天机器人。ChatGPT 是一种让您通过大型语言模型进行对话的方式。其中一个很酷的事情是,您也可以使用大型语言模型来构建自定义的聊天机器人,例如扮演AI客户服务代理或餐厅AI点餐员的角色。自己构建一个聊天机器人,让我们开始吧。首先,我们将像往常一样设置 OpenAI Python 软件包。

像 ChatGPT 这样的聊天模型实际上是经过训练的,可以将一系列消息作为输入,并将模型生成的消息作为输出返回。这是一系列消息的示例。

下面第一段是纯技术性的,一次性开发环境设置,配置 Open AI 的Python库,以便调用 ChatGPT 模型 API 。你先要到 Open AI 那里注册一个账号,获得调用它 API 的 key。

import os
import openai
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion(prompt, model="gpt-3.5-turbo"):
   messages = [{"role": "user", "content": prompt}]
   response = openai.ChatCompletion.create(
      model=model,
      messages=messages,
      temperature=0, # degree of randomness of the model's output
   )
   return response.choices[0].message["content"]

def get_completion_from_messages(messages, model="gpt-3.5-turbo",   temperature=0):
   response = openai.ChatCompletion.create(
      model=model,
      messages=messages,
      temperature=temperature, # degree of randomness of model's output
   )
    # print(str(response.choices[0].message))
   return response.choices[0].message["content"]
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'} ]

第一个 get_completion 的函数是最基础的形式,支持单轮对话,函数的输入是用户的 prompt,确定了调用 ChatGPT 的模型(这里是gpt-3.5.-turbo)后,模型就输出本质上是序列“接龙”(completion)的回应 response,这是生成模型的最基本的功能。

关键是要利用 ChatGPT 丝滑的多轮对话能力,来帮助完成特定场景的交互任务(以前称为“技能”)。目的是克服上一代以 Siri 为代表的智能助理技能开发费时费力、对话不擅长多轮交互的短板。为此,可以利用 ChatGPT API 来定义一个赋能多轮交互的函数 get_completion_from_messages,这个函数利用 ChatGPT messages 对于角色(roles)的环境设置。每个角色和角色的信息构成一个 message,机器人系统有三个角色,除了机器助理(assistant)和用户(user)外,里面还有一个隐身其后的导演角色叫 system。系统消息有助于设置助手的行为和个性,它是对话的高级说明,可以将其视为在助手的耳边耳语并引导其响应,而用户不会意识到系统消息。系统消息的好处在于,它为您作为开发者提供了一种方式来引导助手及其响应。玩 ChatGPT 网络版本比较熟的网友已经意识到可以用提示词给模型设置角色及其行为方式(例如:“你是一位孔子似的教育家,循循善诱,你面对的是你的弟子,现在开始对话,你说:…”),而系统就是扮演这种设置的后台角色(见下图示意)。

自回归生成模型需要模型“记住”前面的对话才能进行丝滑流畅的对话。模型的输入中所提供的早期交流内容称为场景(context)。

现在构建自己的机器助理前台,称为“orderbot”,自动收集用户提示和助手响应作为场景,以构建此 orderbot。这里的具体案例是在比萨饼店接受订单。因此,首先,我们将定义这个辅助函数,收集我们的用户消息,以便我们可以避免手动输入它们。从构建的用户界面中收集提示,并将其附加到名为“context(场景)”的列表中,然后每次都会使用该场景调用模型。然后,模型的响应也会添加到场景中:模型消息会添加到场景中,用户消息也会添加到场景中,以此类推,因此,场景会变得越来越长。这样,模型就拥有了确定下一步要做什么的所需信息。

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 collect the order, and then ask 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

现在,我们将设置并运行此UI以显示orderbot,这是场景,它包含菜单的系统消息,注意每次调用语言模型时,我们将使用相同的场景,场景随着时间的推移不断加长。

让我们看看我们放入系统消息中的内容:

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.

让我们执行这个操作。好的,我要说,嗨,我想订一份比萨。然后助手说,太好了,你要订哪种比萨?我们有意大利辣香肠、芝士和茄子比萨。它们多少钱?好的,我们有了价格。我想我要一个中等的茄子比萨。因此,您可以想象,我们可以继续这个对话,

因此,让我们回到我们的对话,看看助手是否一直遵循指示。太好了,助手问我们是否需要任何配料,我们在助手消息中指定了这一点。因此,我认为我们不需要额外的配料。好的,还有其他东西需要订购吗?嗯,让我们买一些薯条。小的还是大的?这很棒,因为我们在系统消息中要求助手澄清附加项和配菜。

因此,您可以想象并随意自定义它。您可以在自己的笔记本电脑上运行它。

因此,现在我们可以要求模型基于对话创建JSON摘要,并将其发送到订单系统。因此,我们现在附加了另一个系统消息,即指令,并且我们正在创建前一次食品订单的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)

请注意,在这种情况下,我们正在使用较低的温度,因为对于这些任务,我们希望输出相对可预测,降低随机性。因此,这是我们订单的摘要,因此,如果我们想要,我们可以将其提交到订单系统。

因此,您已经构建了自己的订单聊天机器人。随意自定义它,并尝试修改系统消息,以改变聊天机器人的行为,并使其扮演不同角色。

 

AI浪潮博客目录

提示工程课程公开课




https://m.sciencenet.cn/blog-362400-1386835.html

上一篇:《AI潮流:与 ChatGPT4 聊“买房送老公”背后的语言学》
下一篇:《AI浪潮: 辛顿的 AI 威胁论与马斯克如出一辙》

1 杨正瓴

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 05:54

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部