使用 Streamlit 搭建简单 GPT 页面

介绍

Streamlit 是一个开源的 Python 框架,用于快速构建和共享用于机器学习和数据科学项目的交互式 Web 应用程序。它简化了应用程序的开发过程,允许开发者通过编写简单的 Python 脚本来创建功能丰富的数据可视化界面 ——by deepseek

示例

运行国内训练过的 LLama3 中文模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import streamlit as st
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.chat_models import ChatOpenAI
from langchain_community.chat_models import ChatOllama

st.title("极简对话")

prompt = ChatPromptTemplate.from_messages([
("system", "请用专业的术语解释下面问题,不超过100个字"),
("human", "{topic}"),
])

# model = ChatOpenAI(
# model='deepseek-chat',
# openai_api_key='sk-442c0bee742143a5bca92ae86a6b2ec6',
# openai_api_base='https://api.deepseek.com',
# max_tokens=1024
# )

model = ChatOllama(model="llamafamily/llama3-chinese-8b-instruct")
chain = prompt | model

with st.form("form"):
text = st.text_area("请输入问题: ")
submit = st.form_submit_button("提交")
if submit:
st.info(chain.invoke({"topic": text}))
chain.get_graph().print_ascii()

效果

Chain 调用情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    +-------------+    
| PromptInput |
+-------------+
*
*
*
+--------------------+
| ChatPromptTemplate |
+--------------------+
*
*
*
+------------+
| ChatOpenAI |
+------------+
*
*
*
+------------------+
| ChatOpenAIOutput |
+------------------+

加载 tools 实现在线查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import streamlit as st
from langchain.agents import initialize_agent, AgentType, load_tools
from langchain_openai import OpenAI, ChatOpenAI

open_api_key = st.sidebar.text_input("OpenAI API Key")

if prompt := st.chat_input():
if not open_api_key:
st.info("请输入 OpenAI Key")
st.stop()
model = ChatOpenAI(
model='deepseek-chat',
openai_api_key=open_api_key,
openai_api_base='https://api.deepseek.com',
max_tokens=1024,
streaming=True
)
tools = load_tools(["ddg-search"])
agent = initialize_agent(tools, model, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
st.chat_message("user").write(prompt)

with st.chat_message("assistant"):
response = agent.run(prompt)
st.write(response)

langchain 执行信息如下