[Large Model Practice] Dialogue model design based on Wen Xinyiyan

Wenxin Yiyan (English name: ERNIE Bot) is Baidu 's new generation of knowledge-enhanced large language model and a new member of the Wenxin large model family. It can interact with people, answer questions, assist in creation, and help people obtain information efficiently and conveniently . Knowledge and inspiration . Wen Xinyiyan fuses learning from trillions of data and hundreds of billions of knowledge to obtain a large pre-trained model. On this basis, it uses supervised fine-tuning, human feedback reinforcement learning, prompts and other technologies, with knowledge enhancement, retrieval enhancement and Technical advantages of conversation enhancement.

This article uses Gradio to develop a simple dialogue page, and the large model used is Wen Xin Yi Yan.

1. Obtain token

Baidu's aistudio platform provides free Wen Xinyiyan calling tokens, 1 million for each user, click on the token:

You can see the token in my token:

This token will be used later, please replace the token in the code with your own token.

2. Write code

Create the file erniebot_test.py with the following code. Please replace erniebot.access_token with your own 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")

3. Create an environment

You can complete the installation of anaconda by referring to the anaconda installation tutorial in the local deployment of the face-changing application dofaker in [Deep Learning Practice] .

Create virtual environment erniebot_test:

conda create -n erniebot_test python=3.10

As shown below, enter y when prompted to enter Y/N:

Enter the virtual environment:

conda activate erniebot_test

As shown below:

Install dependencies:

pip install -U erniebot -i https://mirrors.aliyun.com/pypi/simple/
pip install -U gradio -i https://mirrors.aliyun.com/pypi/simple/

Run the code:

python erniebot_test.py

As shown below:

Just open 127.0.0.1 in your browser:

Enter in the text box below and press Enter to start the conversation:

Guess you like

Origin blog.csdn.net/qq_40035462/article/details/135153476