-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
61 lines (47 loc) · 2.39 KB
/
streamlit_app.py
File metadata and controls
61 lines (47 loc) · 2.39 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
import uuid
from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
from src.graph import app
st.set_page_config(page_title="Qwen RAG Agent", page_icon="🤖")
st.title("Демо RAG-агент")
if "thread_id" not in st.session_state:
st.session_state.thread_id = str(uuid.uuid4())
st.session_state.messages = []
for msg in st.session_state.messages:
if msg["role"] == "user":
with st.chat_message("user"):
st.markdown(msg["content"])
else:
with st.chat_message("assistant"):
st.markdown(msg["content"])
if prompt := st.chat_input("Введите ваш вопрос"):
with st.chat_message("user"):
st.markdown(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
status_placeholder = st.status("Думаю...", expanded=True)
config = {"configurable": {"thread_id": st.session_state.thread_id}}
try:
events = app.stream(
{"messages": [HumanMessage(content=prompt)]},
config,
stream_mode="values"
)
for event in events:
messages = event.get("messages")
if not messages: continue
last_msg = messages[-1]
if isinstance(last_msg, AIMessage) and last_msg.tool_calls:
tool_name = last_msg.tool_calls[0]['name']
status_placeholder.write(f"Вызываю инструмент: **{tool_name}**")
elif isinstance(last_msg, ToolMessage):
status_placeholder.write(f"Получены данные. Проверка релевантности...")
elif isinstance(last_msg, AIMessage) and last_msg.content:
full_response = last_msg.content
message_placeholder.markdown(full_response)
status_placeholder.update(label="Готово!", state="complete", expanded=False)
st.session_state.messages.append({"role": "assistant", "content": full_response})
except Exception as e:
st.error(f"Произошла ошибка: {e}")