文心一言(英文名:ERNIE Bot)是百度全新一代知識增強大語言模型,文心大模型家族的新成員,能夠與人對話互動、回答問題、協(xié)助創(chuàng)作,高效便捷地幫助人們獲取信息、知識和靈感。文心一言從數(shù)萬億數(shù)據(jù)和數(shù)千億知識中融合學習,得到預訓練大模型,在此基礎上采用有監(jiān)督精調、人類反饋強化學習、提示等技術,具備知識增強、檢索增強和對話增強的技術優(yōu)勢。
本文使用gradio開發(fā)一個簡單的對話頁面,使用的大模型是文心一言。
一、獲取token
百度的aistudio平臺提供免費的文心一言調用token,每個用戶100W個,點擊token:
在我的令牌可以看到token:
后續(xù)會用到該token,請將代碼中的token更換成自己的token。
二、編寫代碼
創(chuàng)建文件erniebot_test.py,代碼如下,請將erniebot.access_token替換為自己的token:
import gradio as gr
import random
import time
import os
import erniebot
import gradio as gr
def predict(content, his):
if len(his)>0 and isinstance(his[0], list):
his = his[0]
erniebot.api_type = "aistudio"
erniebot.access_token ="xxx" # 替換為自己的token
message = []
for idx, msg in enumerate(his):
if idx % 2 == 0:
message.append(
{'role': 'user',
'content': msg,}
)
else:
message.append(
{'role': 'assistant',
'content': msg,}
)
message.append(
{'role': 'user',
'content': content,}
)
response = erniebot.ChatCompletion.create(model="ernie-bot", messages=message)
return response.result
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history):
bot_message = predict(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
if __name__ == "__main__":
demo.launch(inbrowser=True, server_port=80, share=True, server_name="0.0.0.0")
三、創(chuàng)建環(huán)境
您可以參考【深度學習實踐】換臉應用dofaker本地部署中的anaconda安裝教程完成anaconda的安裝。
創(chuàng)建虛擬環(huán)境erniebot_test?:
conda create -n erniebot_test python=3.10
如下圖,提示輸入Y/N時輸入y:
進入該虛擬環(huán)境:
conda activate erniebot_test
如下圖:
安裝依賴:
pip install -U erniebot -i https://mirrors.aliyun.com/pypi/simple/
pip install -U gradio -i https://mirrors.aliyun.com/pypi/simple/
運行代碼:
python erniebot_test.py
如下圖:
瀏覽器打開127.0.0.1即可:
在下方的文本框輸入,按回車開啟對話:文章來源:http://www.zghlxwxcb.cn/news/detail-768017.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-768017.html
到了這里,關于【大模型實踐】基于文心一言的對話模型設計的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!