用 Streamlit + Ciyuano 搭建 DeepSeek 聊天机器人
·2 分钟阅读·5 次阅读
目标
30 分钟搭建一个完整的 Web 聊天界面,支持流式输出、对话历史、模型切换。
技术栈
- 前端:Streamlit(纯 Python,无需写 HTML/JS)
- 后端:Ciyuano API(DeepSeek V4)
- 语言:Python 3.10+
第一步:安装依赖
bashpip install streamlit openai
第二步:创建 app.py
pythonimport streamlit as st
from openai import OpenAI
st.set_page_config(page_title="DeepSeek Chat", page_icon="🤖")
st.title("🤖 DeepSeek 聊天助手")
with st.sidebar:
st.header("⚙️ 设置")
api_key = st.text_input("API Key", type="password")
model = st.selectbox("模型", ["deepseek-v4", "glm-5", "qwen-plus", "auto"])
st.caption("Powered by Ciyuano")
if "messages" not in st.session_state:
st.session_state.messages = []
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
if prompt := st.chat_input("输入你的问题..."):
if not api_key:
st.error("请先输入 API Key")
st.stop()
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
client = OpenAI(
base_url="https://www.ciyuano.com/v1",
api_key=api_key
)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
stream = client.chat.completions.create(
model=model,
messages=st.session_state.messages,
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
full_response += chunk.choices[0].delta.content
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append(
{"role": "assistant", "content": full_response}
)
第三步:运行
bashstreamlit run app.py
浏览器自动打开 http://localhost:8501,一个完整的聊天界面就出现了!
效果预览
- ✅ 流式逐字输出(打字机效果)
- ✅ 多轮对话记忆
- ✅ 模型实时切换
- ✅ 纯 Python 实现,零前端代码
部署上线
bashnohup streamlit run app.py --server.port 8501 &
配置 Nginx 反向代理 + SSL 即可对外提供服务。
总结
Streamlit + Ciyuano 的组合让你在 30 分钟内拥有一个生产可用的 AI 聊天应用。没有复杂的前端框架,没有服务端 API 开发,全部用 Python 完成。
💬 评论功能暂未开放,敬请期待