LLM大语言模型(五):用streamlit开发LLM应用

目录

背景

Streamlit是一个开源Python库,可以轻松创建和共享用于机器学习数据科学的漂亮的自定义web应用程序,用户可以在几分钟内构建一个强大的数据App。

其最大的特色是直接用Python写前端页面。

对于数据分析场景,其强大的数据可视化能力和极方便简单的开发流程,极大的方便了demo展示、方案验证等工作。

随着ChatGPT的兴起,LLM方向变得炙手可热,Streamlit也顺势推出了支持LLM的新特性。

准备工作

安装

python 复制代码
pip install streamlit

python版本支持3.8~3.12.

切记

页面的每次渲染,其实都是from top to down重新执行了一次后端的py文件。

记住这个,方便理解下文的例子。

streamlit开发LLM demo

开一个新页面

python 复制代码
import streamlit as st
import random
import time

st.title("来啦,老铁")

这一行生成一个新页面,且页面顶部的标题是大大的:来啦,老铁。

初始化session

python 复制代码
# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []
    st.session_state.messages.append({"role": "assistant", "content": "老铁,有什么需要帮忙的?"})

session_state是会话保持,你在页面上的各种操作,对会对应到后端的同一个session上。

要是新开了一个网页,即使是同一个用户,也会开启一个新的session,因为它并没有用户体系。

messages会存储对话的历史消息,对同一个session来讲,初始化只会执行一次。

在初始化的时候,助手会先发一条消息,跟用户打个招呼。

先渲染历史消息

python 复制代码
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"]) 

st.chat_message 会返回一个message container,这个container可以放streamlit的任何元素。这里放的markdown元素,也就是消息的内容。

st.chat_message 的第一个参数是消息的作者,例如userassistant ,针对不同类型的作者,streamlit会显示内置的头像和风格,当然也可以自定义。

第一次渲染时,这里会把助手说的第一句话渲染出来。

接收用户输入

python 复制代码
if prompt := st.chat_input("What is up?"):
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})

st.chat_input 会在页面展示一个输入框,用户可在里面输入对话内容。

后端收到用户的输入内容后,首先要在页面的历史消息里渲染出来,这一步st.chat_message会自动帮你做。

然后将用户输入再追加到session里的的历史消息中,后续要拿着整个对话历史去调LLM,或者页面重新渲染时展示所有的历史消息。

模拟调用LLM

python 复制代码
# Accept user input
if prompt := st.chat_input("What is up?"):
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Display assistant response in chat message container
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""
        assistant_response = random.choice(
            [
                "st.chat_input lets you display a chat input widget so the user can type in a message. The returned value is the user's input, which is None if the user hasn't sent a message yet. You can also pass in a default prompt to display in the input widget. Here's an example of how to use st.chat_input to display a chat input widget and show the user's input:",
            ]
        )
        # Simulate stream of response with milliseconds delay
        for chunk in assistant_response.split():
            full_response += chunk + " "
            time.sleep(0.1)
            # Add a blinking cursor to simulate typing
            message_placeholder.markdown(full_response + "▌")
        message_placeholder.markdown(full_response)
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": full_response})
  

上述demo并没有真正的去调用LLM,而是做了个效果模拟。

message_placeholder = st.empty() 创建了一个可变内容的对象,顾名思义,它的内容可以被修改,且修改完后会自动渲染到页面上。

上述demo会给出一个较长的回答。然后模拟LLM的效果,把回答渲染到页面上。

首先将回答split分词,每个split相当于LLM调用返回结果里的chunk。

每个chunk处理时,先sleep 0.1s,模拟LLM在generate,然后修改message_placeholder的内容,为了效果更逼真,每次渲染还在末尾加了指针符号。

最后将整个答案再渲染了一次。

最最后,把assistant的回答追加到session里的历史消息里。

视频演示:

llm_demo · Streamlit

效果图:

参考

LLM大语言模型(一):ChatGLM3-6B本地部署
LLM大语言模型(二):Streamlit 无需前端经验也能画web页面
LLM大语言模型(三):使用ChatGLM3-6B的函数调用功能前先学会Python的装饰器
LLM大语言模型(四):在ChatGLM3-6B中使用langchain

相关推荐
whaosoft-1433 分钟前
大模型~合集3
人工智能
Dream-Y.ocean4 分钟前
文心智能体平台AgenBuilder | 搭建智能体:情感顾问叶晴
人工智能·智能体
丶21366 分钟前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
春末的南方城市41 分钟前
FLUX的ID保持项目也来了! 字节开源PuLID-FLUX-v0.9.0,开启一致性风格写真新纪元!
人工智能·计算机视觉·stable diffusion·aigc·图像生成
zmjia11143 分钟前
AI大语言模型进阶应用及模型优化、本地化部署、从0-1搭建、智能体构建技术
人工智能·语言模型·自然语言处理
jndingxin1 小时前
OpenCV视频I/O(14)创建和写入视频文件的类:VideoWriter介绍
人工智能·opencv·音视频
_.Switch1 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一个闪现必杀技1 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
AI完全体1 小时前
【AI知识点】偏差-方差权衡(Bias-Variance Tradeoff)
人工智能·深度学习·神经网络·机器学习·过拟合·模型复杂度·偏差-方差
GZ_TOGOGO2 小时前
【2024最新】华为HCIE认证考试流程
大数据·人工智能·网络协议·网络安全·华为