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通過接口獲取默認有效期為30天。可自行決定是否需要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如何對接文心一言的詳細內(nèi)容,更多關(guān)于Python對接文心一言的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
通過python讀取txt文件和繪制柱形圖的實現(xiàn)代碼
這篇文章主要介紹了通過python讀取txt文件和繪制柱形圖的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Python如何使用struct.unpack處理二進制文件
這篇文章主要介紹了Python如何使用struct.unpack處理二進制文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
python如何調(diào)用php文件中的函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于python如何調(diào)用php文件中函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python實現(xiàn)添加或移除PowerPoint中的背景圖和背景顏色
在處理 PowerPoint 演示文稿時,我們常常會遇到需要為幻燈片添加或移除背景的情況,本文將介紹如何使用 Python 在 PowerPoint 中添加或移除背景,并提供詳細步驟和代碼示例,幫助你快速上手2026-05-05
Python實現(xiàn)定時監(jiān)測網(wǎng)站運行狀態(tài)的示例代碼
這篇文章主要介紹了Python實現(xiàn)定時監(jiān)測網(wǎng)站狀態(tài)的示例代碼,幫助大家更好的管理自己的網(wǎng)站,感興趣的朋友可以了解下2020-09-09
torchxrayvision包安裝過程(附pytorch1.6cpu版安裝)
這篇文章主要介紹了torchxrayvision包安裝過程(附pytorch1.6cpu版安裝),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

