基于Gradio的GPT聊天程序

网上很多别人写的,要用账号也不放心。就自己写了一个基于gradio的聊天界面,部署后可以本地运行。

特点:

可以用openai的,也可以用api2d,其他api可以自己测试一下。使用了langchain的库

可以更改模型,会的可以自己改代码更新模型

支持修改temperature,对话轮数

公开代码,copy后填了自己的api就能直接运行

python 复制代码
import gradio as gr

from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, AIMessage


api_key = '***'  # openai api, api2d api
api_base ='https://oa.api2d.net/v1'  # api地址
models = ['gpt-3.5-turbo-0613', 'gpt-4-0613']   #模型名称,可以修改

block_css = """.importantButton {
    background: linear-gradient(45deg, #7e0570,#5d1c99, #6e00ff) !important;
    border: none !important;
}
.importantButton:hover {
    background: linear-gradient(45deg, #ff00e0,#8500ff, #6e00ff) !important;
    border: none !important;
}"""


default_theme_args = dict(
    font=["Source Sans Pro", 'ui-sans-serif', 'system-ui', 'sans-serif'],
    font_mono=['IBM Plex Mono', 'ui-monospace', 'Consolas', 'monospace'],
)



init_message = f"""欢迎使用 ChatGPT Gradio UI!"""


def respond(query, model, temperature, history_turns, chat_history):
    global chat_turns
    llm = ChatOpenAI(
        temperature=temperature,
        openai_api_key=api_key,
        openai_api_base=api_base,
        model_name=model
    )

    history=[]
    len_history = min(chat_turns, history_turns)
    if chat_turns > 0:
        for turn in range(len_history):
            history.append(HumanMessage(content=chat_history[len_history-turn][0]));
            history.append(AIMessage(content=chat_history[len_history-turn][1]));

    history.append(HumanMessage(content=query));
    #print(history)

    response = llm(history).content;

    chat_history.append((query, response));
    chat_turns += 1
    return "", chat_history


def clear(chat_history):
    global chat_turns
    chat_history = [(None, "已清除对话历史")]
    chat_turns = 0
    return chat_history

def setting_change(model ,temperature, history_turns ,chat_history):
    global chat_turns
    chat_history = [(None, f"设置更新:\n 模型名称:{model} \n 温度:{temperature} \n 记忆历史对话轮数:{history_turns}\n")]
    chat_turns =0
    return chat_history





with gr.Blocks(css=block_css, theme=gr.themes.Default(**default_theme_args)) as demo:

    gr.Markdown('ChatGPT Gradio')
    chat_turns = 0
    with gr.Row():
        with gr.Column(scale=10):
            chatbot = gr.Chatbot([[None, init_message]],
                                 elem_id="chat-box",
                                 label="聊天历史")
            query = gr.Textbox(label="输入问题",
                               placeholder="请输入提问内容,按回车进行提交")
            clear_button = gr.Button("重新对话", visible=True)

        with gr.Column(scale=5):
            model = gr.Radio(models,
                            label="请选择使用模型",
                            value=models[0], interactive=True)


            temperature = gr.Slider(0,1,
                                    value=0.8,
                                    step=0.1,
                                    label="Temperature",
                                    interactive=True)

            history_turns = gr.Slider(1, 20,
                                 value=5,
                                 step=1,
                                 label="对话轮数",
                                 info='记录历史对话轮数',
                                 interactive=True)

            settings_button = gr.Button("更新设置", visible=True)

            settings_button.click(fn=setting_change, inputs=[model, temperature, history_turns, chatbot], outputs=[chatbot])

    query.submit(respond, [query, model, temperature, history_turns, chatbot], [query, chatbot])

    clear_button.click(fn=clear,
                    inputs=[chatbot],
                    outputs=[chatbot])

demo.launch()

需要的库,requirement.txt 文件

python 复制代码
langchain
openai
gradio
相关推荐
TomEval6 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill7 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
Thanks_ks8 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
CyrusYiVPP8 小时前
实测 uv 0.13.7:同一套包,pip 装 94 秒,它只用 19 秒
人工智能·python
yiangL_9 小时前
Python 学习笔记:递归、turtle绘图、字符串与函数
笔记·python·学习
回眸&啤酒鸭9 小时前
【回眸】GPT-6.1 Sol 新手快速上手指南
gpt
lula88810 小时前
PVC-U、PVC-C、PVC-O 差一个字母,差的是三种增强路线
python
数字工作台11 小时前
给 LangGraph 加一道可审计记忆:替换默认 Checkpointer,实现防篡改 + 崩溃不丢数据
python·agent
DKPT11 小时前
Python 的 Web 框架:选型选错的人,都踩了同一个坑
python
2401_8685347812 小时前
逻辑网络设计
网络·python