Python基于Google?Bard實現(xiàn)交互式聊天機(jī)器人
用Python基于Google Bard做一個交互式的聊天機(jī)器人
之前已經(jīng)通過瀏覽器試過了 Google Bard ,更多細(xì)節(jié)請看: Try out Google Bard, Will Google Bard beat the ChatGPT?.
現(xiàn)在我們想實現(xiàn)自動化,所以我用Python做一個交互式的聊天機(jī)器人。
獲取Session ID
通過瀏覽器先拿到SessionID,它是一個cookie,名為 __Secure-1PSID,然后復(fù)制一下對應(yīng)的值:

Python代碼
先做一些初始化,主要是一些請求頭和請求參數(shù):
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()
發(fā)送請求的時候,把之前準(zhǔn)備的參數(shù)和數(shù)據(jù)發(fā)一個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]],
}
運(yùn)行與嘗試
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...
代碼
相關(guān)代碼請查看: GitHUb LarryDpk/pkslow-samples
References:Bard
以上就是Python基于Google Bard實現(xiàn)交互式聊天機(jī)器人的詳細(xì)內(nèi)容,更多關(guān)于Python Google Bard聊天機(jī)器人的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python利用PyVista進(jìn)行mesh的色彩映射的實現(xiàn)
- Python Shiny庫創(chuàng)建交互式Web應(yīng)用及高級功能案例
- Python?Streamlit制作交互式可視化網(wǎng)頁應(yīng)用實例
- Gradio構(gòu)建交互式Python應(yīng)用使用示例詳解
- Python深度學(xué)習(xí)pytorch神經(jīng)網(wǎng)絡(luò)圖像卷積運(yùn)算詳解
- python獲取網(wǎng)絡(luò)圖片方法及整理過程詳解
- 使用Python的networkx繪制精美網(wǎng)絡(luò)圖教程
- Python Pyvis庫創(chuàng)建交互式網(wǎng)絡(luò)圖實例探究
相關(guān)文章
Python虛擬機(jī)棧幀對象及獲取源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python虛擬機(jī)棧幀對象及獲取源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python實現(xiàn)監(jiān)控鍵盤鼠標(biāo)操作示例【基于pyHook與pythoncom模塊】
這篇文章主要介紹了Python實現(xiàn)監(jiān)控鍵盤鼠標(biāo)操作,結(jié)合實例形式分析了Python基于pyHook與pythoncom模塊的鍵盤、鼠標(biāo)事件響應(yīng)及日志文件操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-09-09
Python守護(hù)進(jìn)程實現(xiàn)過程詳解
這篇文章主要介紹了Python守護(hù)進(jìn)程實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
利用python實現(xiàn)簡易版的貪吃蛇游戲(面向python小白)
這篇文章主要給大家介紹了關(guān)于如何利用python實現(xiàn)簡易版的貪吃蛇游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
python 簡單照相機(jī)調(diào)用系統(tǒng)攝像頭實現(xiàn)方法 pygame
今天小編就為大家分享一篇python 簡單照相機(jī)調(diào)用系統(tǒng)攝像頭實現(xiàn)方法 pygame,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
利用python獲取當(dāng)前日期前后N天或N月日期的方法示例
最近在工作中遇到一個需求,查找資料發(fā)現(xiàn)了一個很好的時間組件,所以下面這篇文章主要給大家介紹了關(guān)于利用python獲取當(dāng)前日期前后N天或N月日期的方法示例,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07
Python 使用PIL.Image制作運(yùn)動小人的動態(tài)圖思路詳解
這篇文章主要介紹了Python 使用PIL.Image制作一個運(yùn)動小人的動態(tài)圖,制作過程也很簡單,只需要把圖片拆分成12等分,每幀大?。?7x165;連續(xù)讀取和播放就會形成動態(tài)圖像,需要的朋友可以參考下2021-10-10
Python 機(jī)器學(xué)習(xí)之線性回歸詳解分析
回歸是監(jiān)督學(xué)習(xí)的一個重要問題,回歸用于預(yù)測輸入變量和輸出變量之間的關(guān)系,特別是當(dāng)輸入變量的值發(fā)生變化時,輸出變量的值也隨之發(fā)生變化?;貧w模型正是表示從輸入變量到輸出變量之間映射的函數(shù)2021-11-11
Python解決拋小球問題 求小球下落經(jīng)歷的距離之和示例
這篇文章主要介紹了Python解決拋小球問題 求小球下落經(jīng)歷的距離之和,涉及Python基于遞歸的數(shù)值計算相關(guān)操作技巧,需要的朋友可以參考下2018-02-02

