Python如何對接文心一言
引言
文心一言是百度研發(fā)的Ai機器,能夠與人對話互動,回答問題,協(xié)助創(chuàng)作,高效便捷地幫助人們獲取信息、知識和靈感。
申請Api Key
前往百度智能云
https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application
登錄并創(chuàng)建應(yīng)用,拿到需要的API Key
和Secret Key
:
編輯源代碼
修改參數(shù):
api_key
:替換成自己的
secret_key
:替換自己的
redis
的配置是用于保存access_token
,該token通過接口獲取默認(rèn)有效期為30
天??勺孕袥Q定是否需要redis的配合。
import requests import json import redis # 文心一言配置 api_key = "你的api_key" secret_key = "你的secret_key" # redis配置 redis_host = "127.0.0.1" redis_port = 6379 redis_db = 0 class ChatBot: def __init__(self, api_key, secret_key): self.api_key = api_key self.secret_key = secret_key self.message_history = [] self.redis_client = redis.Redis(host=redis_host, port=redis_port, db=redis_db) self.chat_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token={}" def get_token(self): if self.redis_client.exists('access_token'): return self.redis_client.get('access_token').decode() else: get_access_token_url = ("https://aip.baidubce.com/oauth/2.0/token?" "client_id={}" "&client_secret={}" "&grant_type=client_credentials").format( self.api_key, self.secret_key) response = requests.get(get_access_token_url) self.redis_client.setex('access_token', response.json()['expires_in'], response.json()['access_token']) return response.json()['access_token'] def check_tokens(self, total_tokens): if total_tokens > 4800: self.message_history = self.message_history[len(self.message_history) / 2:] def add_chat_history(self, message): self.message_history.append(message) payload = json.dumps({ "messages": self.message_history }) return payload def send_message(self, message): payload = self.add_chat_history({ "role": "user", "content": message }) headers = {'Content-Type': 'application/json'} response = requests.post(self.chat_url.format(self.get_token()), headers=headers, data=payload) self.add_chat_history({ "role": "assistant", "content": response.json()['result'] }) return response.json()['result'] if __name__ == '__main__': chatbot = ChatBot(api_key, secret_key) while True: message = input("you: ") if message.strip() != "": reply = chatbot.send_message(message) print("bot: ", reply)
思維擴展
通過上面的代碼邏輯,我們是否可以嘗試:通過麥克風(fēng)獲取用戶的語音指令轉(zhuǎn)成文字,然后通過文心一言拿到返回的內(nèi)容再生成語音進行播放。是不是就成了智能語音助手??
以上就是Python如何對接文心一言的詳細(xì)內(nèi)容,更多關(guān)于Python對接文心一言的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 獲取當(dāng)前目錄下的文件目錄和文件名實例代碼詳解
這篇文章主要介紹了python 獲取當(dāng)前目錄下的文件目錄和文件名實例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Python實現(xiàn)簡單線性插值去馬賽克算法代碼示例
去馬賽克是圖像處理中的一項技術(shù),用于從單色彩濾光片陣列(CFA)圖像恢復(fù)全彩圖像,本文介紹了一種基于簡單線性插值的去馬賽克算法,并展示了如何將MATLAB代碼轉(zhuǎn)換為Python代碼,需要的朋友可以參考下2024-10-10使用python腳本自動創(chuàng)建pip.ini配置文件代碼實例
這篇文章主要介紹了使用python腳本自動創(chuàng)建pip.ini配置文件代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09詳解Python 序列化Serialize 和 反序列化Deserialize
這篇文章主要介紹了詳解Python 序列化Serialize 和 反序列化Deserialize的相關(guān)資料,序列化是將對象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^程。與序列化相對的是反序列化,它將流轉(zhuǎn)換為對象。這兩個過程結(jié)合起來,可以輕松地存儲和傳輸數(shù)據(jù),需要的朋友可以參考下2017-08-08python使用selenium登錄QQ郵箱(附帶滑動解鎖)
這篇文章主要為大家詳細(xì)介紹了python使用selenium登錄QQ郵箱,帶滑動解鎖登錄功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Python進階篇之正則表達(dá)式常用語法總結(jié)
正則表達(dá)式是一個特殊的字符序列,它能幫助你方便的檢查一個字符串是否與某種模式匹配。本文為大家總結(jié)了一些正則表達(dá)式常用語法,希望有所幫助2022-08-08