自建wap网站,分类信息网站如何优化,手机app怎么制作流程图,佛山企业网站建站网上很多别人写的#xff0c;要用账号也不放心。就自己写了一个基于gradio的聊天界面#xff0c;部署后可以本地运行。 特点#xff1a;
可以用openai的#xff0c;也可以用api2d#xff0c;其他api可以自己测试一下。使用了langchain的库
可以更改模型#xff0c;会的…网上很多别人写的要用账号也不放心。就自己写了一个基于gradio的聊天界面部署后可以本地运行。 特点
可以用openai的也可以用api2d其他api可以自己测试一下。使用了langchain的库
可以更改模型会的可以自己改代码更新模型
支持修改temperature对话轮数 公开代码copy后填了自己的api就能直接运行 import gradio as grfrom langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, AIMessageapi_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 UIdef respond(query, model, temperature, history_turns, chat_history):global chat_turnsllm ChatOpenAI(temperaturetemperature,openai_api_keyapi_key,openai_api_baseapi_base,model_namemodel)history[]len_history min(chat_turns, history_turns)if chat_turns 0:for turn in range(len_history):history.append(HumanMessage(contentchat_history[len_history-turn][0]));history.append(AIMessage(contentchat_history[len_history-turn][1]));history.append(HumanMessage(contentquery));#print(history)response llm(history).content;chat_history.append((query, response));chat_turns 1return , chat_historydef clear(chat_history):global chat_turnschat_history [(None, 已清除对话历史)]chat_turns 0return chat_historydef setting_change(model ,temperature, history_turns ,chat_history):global chat_turnschat_history [(None, f设置更新\n 模型名称{model} \n 温度{temperature} \n 记忆历史对话轮数{history_turns}\n)]chat_turns 0return chat_historywith gr.Blocks(cssblock_css, themegr.themes.Default(**default_theme_args)) as demo:gr.Markdown(ChatGPT Gradio)chat_turns 0with gr.Row():with gr.Column(scale10):chatbot gr.Chatbot([[None, init_message]],elem_idchat-box,label聊天历史)query gr.Textbox(label输入问题,placeholder请输入提问内容按回车进行提交)clear_button gr.Button(重新对话, visibleTrue)with gr.Column(scale5):model gr.Radio(models,label请选择使用模型,valuemodels[0], interactiveTrue)temperature gr.Slider(0,1,value0.8,step0.1,labelTemperature,interactiveTrue)history_turns gr.Slider(1, 20,value5,step1,label对话轮数,info记录历史对话轮数,interactiveTrue)settings_button gr.Button(更新设置, visibleTrue)settings_button.click(fnsetting_change, inputs[model, temperature, history_turns, chatbot], outputs[chatbot])query.submit(respond, [query, model, temperature, history_turns, chatbot], [query, chatbot])clear_button.click(fnclear,inputs[chatbot],outputs[chatbot])demo.launch()需要的库requirement.txt 文件
langchain
openai
gradio