用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(
"

最低0.47元/天 解锁文章
114

被折叠的 条评论
为什么被折叠?



