我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Python基于Google Bard实现交互式聊天机器人

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Python基于Google Bard实现交互式聊天机器人

用Python基于Google Bard做一个交互式的聊天机器人

之前已经通过浏览器试过了 Google Bard ,更多细节请看: Try out Google Bard, Will Google Bard beat the ChatGPT?.

现在我们想实现自动化,所以我用Python做一个交互式的聊天机器人。

获取Session ID

通过浏览器先拿到SessionID,它是一个cookie,名为 __Secure-1PSID,然后复制一下对应的值:

Python代码

先做一些初始化,主要是一些请求头和请求参数:

def __init__(self, session_id):
  headers = {
    "Host": "bard.google.com",
    "X-Same-Domain": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
    "Origin": "https://bard.google.com",
    "Referer": "https://bard.google.com/",
  }
  self._reqid = int("".join(random.choices(string.digits, k=4)))
  self.conversation_id = ""
  self.response_id = ""
  self.choice_id = ""
  self.session = requests.Session()
  self.session.headers = headers
  self.session.cookies.set("__Secure-1PSID", session_id)
  self.SNlM0e = self.__get_snlm0e()

发送请求的时候,把之前准备的参数和数据发一个POST请求到 bard.google.com

resp = self.session.post(
  "https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate",
  params=params,
  data=data,
  timeout=120,
)

返回了之后,需要解析消息体,获取我们所需要的信息:

chat_data = json.loads(resp.content.splitlines()[3])[0][2]
if not chat_data:
  return {"content": f"Google Bard encountered an error: {resp.content}."}
json_chat_data = json.loads(chat_data)
results = {
  "content": json_chat_data[0][0],
  "conversation_id": json_chat_data[1][0],
  "response_id": json_chat_data[1][1],
  "factualityQueries": json_chat_data[3],
  "textQuery": json_chat_data[2][0] if json_chat_data[2] is not None else "",
  "choices": [{"id": i[0], "content": i[1]} for i in json_chat_data[4]],
}

运行与尝试

python Bard.py --session 'UgiXYPjpaIYuE9K_3BSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 

它一个交互式的终端,我随便问了几个问题:

ChatGPT - A command-line interface to Google's Bard (https://bard.google.com/)
You:
What is terrible-two?
Google Bard:
Terrible-two is a term used to describe the behavior of a child around the age of 2. It is a normal developmental phase that all children go through. During this time, children  
are learning to assert their independence and explore the world around them. This can lead to tantrums, defiance, and other challenging behavior.                                 
There are a few things that parents can do to help manage the terrible twos:                                                                                                      
 • Be patient and understanding. Remember that this is a normal phase and that your child is not trying to be difficult.                                                          
 • Set clear boundaries and expectations. Children need to know what is expected of them.                                                                                         
 • Be consistent with your discipline. If you are not consistent, your child will not learn what is right and wrong.                                                              
 • Offer positive reinforcement. Praise your child for good behavior and help them feel good about themselves.                                                                    
 • Talk to your child's doctor if you are concerned about their behavior. Your doctor can offer advice and support.                                                               
The terrible twos can be a challenging time for parents, but it is important to remember that this is a normal phase and that your child will eventually grow out of it.          
You:
how far is the moon?
Google Bard:
The Moon is an average of 238,855 miles (384,400 km) away from Earth. This distance varies slightly because the Moon's orbit is not a perfect circle. The Moon is closest to Earth
at perigee, which is when it is about 225,623 miles (363,104 km) away. It is farthest from Earth at apogee, which is when it is about 252,088 miles (405,696 km) away.            
You:
how far is the sun?
Google Bard:
The Sun is about 93 million miles (150 million kilometers) away from Earth. That's about 1 Astronomical Unit (AU).                                                                
The Sun is the center of our solar system. All of the planets and other objects in our solar system orbit around the Sun. The Sun is a very large star. It is about 109 times the 
diameter of Earth and about 330,000 times more massive than Earth. The Sun is made up of mostly hydrogen and helium. It is so hot that the hydrogen and helium are constantly     
fusing together to create heavier elements. This process is called nuclear fusion. Nuclear fusion releases a lot of energy, which is what powers the Sun.                         
You:
how to cure a flu?
Google Bard:
There is no cure for the flu, but there are treatments that can help relieve symptoms and prevent complications. The most effective way to prevent the flu is to get a flu vaccine
every year.                                                                                                                                                                       
If you get the flu, there are a few things you can do to feel better:                                                                                                             
 • Rest. Get plenty of sleep and avoid strenuous activity.                                                                                                                        
 • Drink plenty of fluids. Water, juice, and soup are all good choices.                                                                                                           
 • Take over-the-counter pain relievers like acetaminophen or ibuprofen.                                                                                                          
 • Use a humidifier. The moisture can help soothe your throat and make it easier to breathe.                                                                                      
 • See a doctor if you have severe symptoms or are at high risk of complications.                                                                                                 
Here are some tips to help you prevent the flu:                                                                                                                                   
 • Get a flu vaccine every year. The flu vaccine is the best way to prevent the flu.                                                                                              
 • Wash your hands often with soap and water. This helps to prevent the spread of germs.                                                                                          
 • Avoid close contact with people who are sick. If you must be around someone who is sick, wear a mask.                                                                          
 • Clean and disinfect surfaces that may be contaminated with germs.                                                                                                              
 • Stay home from work or school if you are sick. This will help to prevent the spread of the flu.                                                                                
 • Eat a healthy diet and exercise regularly. A healthy immune system is better able to fight off infection.                                                                      
You:
Exiting...

代码

相关代码请查看: GitHUb LarryDpk/pkslow-samples

References:Bard

以上就是Python基于Google Bard实现交互式聊天机器人的详细内容,更多关于Python Google Bard聊天机器人的资料请关注编程网其它相关文章!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

Python基于Google Bard实现交互式聊天机器人

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

Python基于Google Bard实现交互式聊天机器人

这篇文章主要为大家介绍了Python基于Google Bard实现交互式聊天机器人示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
2023-03-24

Python基于Google Bard怎么实现交互式聊天机器人

这篇文章主要介绍“Python基于Google Bard怎么实现交互式聊天机器人”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Python基于Google Bard怎么实现交互式聊天机器人”文章能帮
2023-07-05

基于Python实现交互式文件浏览器

这篇文章主要为大家详细介绍了如何基于Python实现简单的交互式文件浏览器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
2023-05-18

快速实现基于Python的微信聊天机器人示例代码

最近听说一个很好玩的图灵机器人api,正好可以用它做一个微信聊天机器人,下面是实现# test.pyimport requests import itchat #这是一个用于微信回复的库KEY = '8edce3ce905a4c1dbb96
2022-06-04

基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器的方法

ChatGPT是一款基于自然语言处理技术的聊天机器人,ChatGPT可以模拟真实的人类对话,并能够更贴近用户的需求,提供更有价值的服务,这篇文章主要介绍了基于ChatGPT+SpringBoot实现智能聊天AI机器人接口并上线至服务器,需要的朋友可以参考下
2023-02-16

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录