设为首页 加入收藏

TOP

用Python基于Google Bard做一个交互式的聊天机器人(一)
2023-07-25 21:36:03 】 浏览:49
Tags: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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇我的第一个项目(五):(前后端)注.. 下一篇记录--从AI到美颜全流程讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目