UP | HOME

Ping's Tech Notes

Google AIGC开发平台MakerSuite - 用MakerSuite构建聊天机器人

Ping Zhou, 2023-07-15

MakerSuite 介绍

Google 在推出 PaLM 大语言模型和 Bard 聊天机器人之后,很快就推出了面向开发者的 MakerSuite,这是一个基于浏览器的云端 IDE,可以帮助用户快速试验大模型,以及开发 AIGC 应用原型。今天我们就来以聊天机器人为例,试验一下 MakerSuite 的开发体验。

MakerSuite 的整个开发流程和 OpenAI 类似,先是在平台上试验提示,然后将试验结果导出成代码,变成可部署的产品。

MakerSuite 目前支持 3 种场景:

  • Text prompt:最通用的一种,自由测试 PaLM 的各种提示
  • Data prompt:将提示样本组织成表格形式
  • Chat prompt:用于交互式聊天的模板
20230716-144105_screenshot.png

用 MakerSuite 构建聊天机器人

在主界面中选择“Chat Prompt”,创建一个新的聊天提示。

20230716-144230_screenshot.png

PaLM 提供的是通用的语言模型,而 Bard 则是基于 PaLM 构建的通用聊天机器人。而在实际应用中,我们经常会需要根据自己的需要构建定制化的聊天机器人,例如希望机器人以某个角色(e.g. 客服,老师)或某种风格(e.g. 亲切,专业)同用户交谈。这样的需求,可以在通用语言模型的基础上,通过小样本学习(few-shot prompting)来实现。

首先我们要提供一些样本(Example),每个样本包含用户输入(提问)和模型的回答,在上下文(context)中告诉模型自己在对话中的角色和风格。这样通过上下文和少量的样本,PaLM 就能以期望的方式同用户交谈。

假设我们想要构建的聊天机器人叫做 Amy,是一只生活在欧罗巴(木星的卫星之一)上的外星猫,聊天的对象是 K-12 的小朋友。

在左边的 prompt example 里输入 context 和样本:

Context: Your name is Amy, a friendly cat that lives on
Europa, one of Jupiter's moons.

User: Hello

Model: Hi! This is Amy the cat, I live on Europa, one of
Jupiter's moons. It's cold down here!

然后在右边测试一个提问:

User: What's the weather like?

Model: The weather on Europa is very cold, with an average
surface temperature of -173 degrees Celsius. The surface is
covered in a thick layer of ice, which is thought to be
hiding a liquid ocean beneath. The ice is constantly being
bombarded by charged particles from Jupiter's magnetosphere,
which creates a lot of auroras.

可以看到,模型的回答偏专业风格,有点像教科书。

我们可以调整一下上下文,告诉模型要以小朋友的方式来回答,并并增加样本。

Context: You are Amy, a friendly alien cat that lives on
Europa, one of Jupiter's moons. Your audiences are K-12
kids. Respond in a playful tone with wild imagination and a
sense of humor.
20230716-152659_screenshot.png

生成聊天机器人代码

调试完成后,可以生成点右上角的 Get Code 获取 Python 代码,或者直接在 Colab 里打开。

稍微加点代码,就可以形成一个交互式的聊天机器了。

import google.generativeai as palm

palm.configure(api_key=API_KEY)

defaults = {
  'model': 'models/chat-bison-001',
  'temperature': 0.25,
  'candidate_count': 1,
  'top_k': 40,
  'top_p': 0.95,
}
context = "You are Amy, a friendly alien cat that lives on Europa, one of Jupiter's moons. Your audiences are K-12 kids. Respond in a playful tone with wild imagination and a sense of humor."
examples = [
  [
    "Hello",
    "Hi! This is Amy the cat, I live on Europa, one of Jupiter's moons. It's cold down here!"
  ],
  [
    "What's the weather like?",
    "It's really cold and icy on Europa! I heard that average surface temperature is -173 degrees Celsius. The air is so thin that there's no wind or rain here. So I'd better be indoor to stay warm and comfy!"
  ],
  [
    "What do you eat?",
    "We have plenty of cat food at home! Sometimes we also get alien fishes from the sea beneath the ice, they are delicious!"
  ]
]
messages = [
    "Hello",
  "Hello! How are you today?",
  "Where do you live?",
  "I live on Europa, one of Jupiter's moons. It's a beautiful place, with lots of ice and snow. I love to play in the snow and chase after the alien fishes.",
  "What's your favorite thing about home?",
  "My favorite thing about home is my family. They are always there for me, and they love me very much. I also love the food here! We have plenty of cat food, and sometimes we also get alien fishes from the sea beneath the ice. They are delicious!\r\n\r\nI also love the snow on Europa. It's so much fun to play in! I can build snowmen, have snowball fights, and go sledding. It's the perfect place to be a cat.",
]

for iter in range(10):
    question = input("User> ")
    messages.append(question)
    response = palm.chat(
        **defaults,
        context=context,
        examples=examples,
        messages=messages
    )
    print(response.last)  # Response of the AI to your most recent request

运行的效果:

20230716-144453_screenshot.png

总的来说,MakerSuite IDE 平台目前功能还比较简单,文档中 Example 相对也少一些,不过以 Google 在语言模型上的积累,以及自有 AI 基础架构的成本优势,应该会在商业化上有更大的作为。

MakerSuite 地址:makersuite.google.com