使用gradio搭建聊天ui实现质谱ai智能问答
一、调用智谱 ai api
1、获取api_key
智谱ai开放平台网址:
https://open.bigmodel.cn/overview
2、安装库pip install zhipuai
3、执行一下代码,调用质谱api进行问答
from zhipuai import zhipuai
client = zhipuai(api_key="xxxxx") # 填写您自己的apikey
while true:
prompt = input("user:")
response = client.chat.completions.create(
model="glm-4", # 填写需要调用的模型名称
messages=[
{"role": "user", "content": prompt}
],
)
answer = response.choices[0].message.content
print("zhipuai:", answer)
二、使用gradio搭建聊天ui
import gradio as gr
import random
import time
from langchain_community.chat_models import chatzhipuai
from zhipuai import zhipuai
import configure
llm = configure.chat
client = zhipuai(api_key="xxx") # 填写您自己的apikey
with gr.blocks() as demo:
chatbot = gr.chatbot()
msg = gr.textbox()
clear = gr.button("清除")
def respond(message, chat_history):
response = client.chat.completions.create(
model="glm-4", # 填写需要调用的模型名称
messages=[
{"role": "user", "content": message}
],
)
chat_history.append((message, response.choices[0].message.content))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: none, none, chatbot, queue=false)
demo.launch()
- gradio的textbox模块允许用户输入字符串并显示字符串输出。它创建一个文本区域,用户可以在其中输入文本或显示输出结果。
- button组件是gradio中的一个模块,用于创建一个按钮,并可以为其分配任意的click()事件。按钮的标签(value)可以作为输入使用,或者通过函数的输出来设置。
- chatbot模块是gradio中的一个组件,用于展示聊天机器人的输出,包括用户提交的消息和机器人的回复。它支持一些markdown语法,包括粗体、斜体、代码和图片等。chatbot模块的输入不接受用户输入,而是通过函数返回的列表来设置聊天内容。返回的列表应包含多个内部列表,每个内部列表包含两个元素:用户消息和机器人回复。消息可以是字符串、元组或none。如果消息是字符串,可以包含markdown格式的文本。如果消息是元组,应包含文件路径和可选的替代文本。值为none的消息将不会显示在聊天界面上。
三、将流式处理添加到交互式聊天机器人
import gradio as gr
import time
from zhipuai import zhipuai
from typing import *
client = zhipuai(api_key="your api key") # 填写您自己的apikey
# https://blog.csdn.net/sinat_26917383/article/details/133950480
# https://open.bigmodel.cn/dev/api#glm-4
# https://www.cnblogs.com/ddsuifeng/p/17989484
with gr.blocks(title="智小优") as demo:
gr.html("""<h1 align="center">智小优</h1>""")
gr.markdown("<h1><center>welcome to my personal ai-or assistant (powered by zhipu)</center></h1>")
chatbot = gr.chatbot(render=true)
msg = gr.textbox(placeholder="请输入你的问题")
with gr.row():
submit = gr.button('submit')
clear = gr.button("clear")
def user(user_message: str, history: list[list]) -> tuple:
"""
args:
user_message: 用户输入
history: 历史问答
returns:
"""
return "", history + [[user_message, none]]
def bot(history: list[list]) -> none:
response = client.chat.completions.create(
model="glm-4", # 填写需要调用的模型名称
messages=[
{"role": "user", "content": history[-1][0]}
],
stream=true
)
history[-1][1] = ""
for chunk in response:
for choice in chunk.choices:
# content = choice.delta.content
if content := choice.delta.content:
history[-1][1] += content
time.sleep(0.05)
yield history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=false).then(
bot, chatbot, chatbot
)
# 触发事件监听
submit.click(user, [msg, chatbot], [msg, chatbot], queue=false).then(bot, chatbot, chatbot)
clear.click(lambda: none, none, chatbot, queue=false)
if __name__ == '__main__':
demo.queue().launch()
参考:
- https://blog.csdn.net/sinat_26917383/article/details/133950480
- https://zhuanlan.zhihu.com/p/681207328
- https://blog.csdn.net/alexa_/article/details/134485161
- https://blog.csdn.net/u013558123/article/details/136118024
- https://zhuanlan.zhihu.com/p/678228971
- https://open.bigmodel.cn/dev/api#glm-4
- https://www.cnblogs.com/ddsuifeng/p/17989484
发表评论