-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_chatbot_basic.py
More file actions
84 lines (67 loc) · 3.39 KB
/
Copy pathstreamlit_chatbot_basic.py
File metadata and controls
84 lines (67 loc) · 3.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import streamlit as st
from aimu import PROVENANCE_KEY, paths
from aimu.agents import Agent
from aimu.history import ConversationManager
from aimu.models import StreamingContentType, available_text_clients
from aimu.tools import builtin
def stream_reply(client, message):
"""Run one assistant turn as an Agent (chat() is single-turn; the Agent loops it until
the model stops calling tools) and yield the visible answer tokens. Tools live on the
Agent — the model client is a pure provider adapter that no longer executes tools."""
for chunk in Agent(client, tools=builtin.ALL_TOOLS).run(message, stream=True):
if chunk.phase == StreamingContentType.GENERATING:
yield chunk.content
SYSTEM_MESSAGE = """
You are a helpful assistant that can answer questions, provide information, and assist with tasks.
You will always use available tools to help answer questions and complete tasks.
"""
INITIAL_USER_MESSAGE = "Introduce what model you are and share what tools you have access to."
def _new_client(client_cls, model):
return client_cls(model, system_message=SYSTEM_MESSAGE)
if "client" not in st.session_state:
client_cls = available_text_clients()[0]
model = client_cls.TOOL_MODELS[0]
st.session_state.client = _new_client(client_cls, model)
st.session_state.conversation_manager = ConversationManager(
db_path=str(paths.output / "chat_history_basic.json"),
use_last_conversation=True,
)
st.session_state.client.messages = st.session_state.conversation_manager.messages
with st.sidebar:
st.title("AIMU Chatbot")
st.write("Basic AI Assistant")
client_cls = st.selectbox("Model client", options=available_text_clients(), format_func=lambda x: x.__name__)
model = st.selectbox("Model", options=client_cls.TOOL_MODELS, format_func=lambda x: x.name)
current = st.session_state.client
if not isinstance(current, client_cls) or current.model != model:
st.session_state.client = _new_client(client_cls, model)
st.rerun()
if st.button("Reset chat"):
st.session_state.conversation_manager.create_new_conversation()
st.session_state.client = _new_client(client_cls, model)
st.rerun()
for msg in st.session_state.client.messages[2:]:
# Skip framework-injected turns (agent-loop continuation / final-answer prompts) so history
# shows only genuine user input and model replies. See aimu.PROVENANCE_KEY.
if msg.get(PROVENANCE_KEY):
continue
if msg["role"] in ("user", "assistant") and msg.get("content"):
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
if len(st.session_state.client.messages) == 0:
with st.chat_message("assistant"):
placeholder = st.empty()
response = ""
for token in stream_reply(st.session_state.client, INITIAL_USER_MESSAGE):
response += token
placeholder.markdown(response)
st.session_state.conversation_manager.update_conversation(st.session_state.client.messages)
if prompt := st.chat_input("What's up?"):
st.chat_message("user").markdown(prompt)
with st.chat_message("assistant"):
placeholder = st.empty()
response = ""
for token in stream_reply(st.session_state.client, prompt):
response += token
placeholder.markdown(response)
st.session_state.conversation_manager.update_conversation(st.session_state.client.messages)