-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
149 lines (120 loc) · 4.55 KB
/
app.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Streamlit app for the Netdata LLM Agent Chat.
"""
import streamlit as st
from netdata_llm_agent.agent import NetdataLLMAgent
DEFAULT_NETDATA_URLS = [
"http://localhost:19999/",
"https://london3.my-netdata.io/",
"https://bangalore.my-netdata.io/",
"https://newyork.my-netdata.io/",
"https://sanfrancisco.my-netdata.io/",
"https://singapore.my-netdata.io/",
"https://toronto.my-netdata.io/",
]
def init_session_state():
"""Initialize session state variables if not already set."""
if "netdata_urls" not in st.session_state:
st.session_state.netdata_urls = DEFAULT_NETDATA_URLS
if "agent" not in st.session_state:
st.session_state.agent = NetdataLLMAgent(st.session_state.netdata_urls)
if "conversation" not in st.session_state:
st.session_state.conversation = []
if "reverse_chat" not in st.session_state:
st.session_state.reverse_chat = False
if "verbose_mode" not in st.session_state:
st.session_state.verbose_mode = False
def sidebar_config():
"""Render and handle sidebar configuration."""
st.sidebar.header("Configuration")
netdata_url_input = st.sidebar.text_area(
"Enter Netdata URLs (one per line):",
value="\n".join(st.session_state.netdata_urls),
height=150,
)
if st.sidebar.button("Update Netdata URLs"):
new_urls = [
url.strip() for url in netdata_url_input.splitlines() if url.strip()
]
st.session_state.netdata_urls = new_urls
st.session_state.agent = NetdataLLMAgent(new_urls)
st.session_state.conversation = []
st.rerun()
st.sidebar.checkbox(
"Reverse Chat Order",
key="reverse_chat",
help="Reverse the order of the chat messages.",
)
st.sidebar.checkbox(
"Verbose Mode", key="verbose_mode", help="Toggle verbose output for debugging."
)
if st.sidebar.button("Clear Chat"):
st.session_state.agent = NetdataLLMAgent(st.session_state.netdata_urls)
st.session_state.conversation = []
st.rerun()
def render_conversation():
"""Display the conversation history."""
conv = st.session_state.conversation.copy()
if st.session_state.reverse_chat:
conv.reverse()
for chat in conv:
if chat["role"] == "user":
with st.chat_message("user"):
st.markdown(chat["content"])
else:
with st.chat_message("assistant"):
st.markdown(chat["content"])
def chat_input():
"""Render the chat input form and return user input."""
with st.form("chat_form", clear_on_submit=True):
user_input = st.text_input("Enter your message:", key="input")
send_pressed = st.form_submit_button("Send")
return send_pressed, user_input
def main():
"""Main function to run the Streamlit app."""
st.set_page_config(
page_title="Netdata LLM Agent Chat",
page_icon="./netdata_llm_agent/static/i-love-trouble.jpg",
layout="wide",
)
st.logo("./netdata_llm_agent/static/i-love-trouble.jpg", link="https://www.netdata.cloud/")
st.markdown(
"<h1 style='color: #00AB44;'>Netdata LLM Agent Chat</h1>",
unsafe_allow_html=True,
)
init_session_state()
sidebar_config()
send_pressed, user_input = chat_input()
if send_pressed and user_input:
st.session_state.conversation.append({"role": "user", "content": user_input})
continue_chat = len(st.session_state.conversation) > 1
with st.spinner("Agent is thinking..."):
try:
if st.session_state.verbose_mode:
response = st.session_state.agent.chat(
user_input,
continue_chat=continue_chat,
return_thinking=True,
return_last=False,
)
else:
response = st.session_state.agent.chat(
user_input,
continue_chat=continue_chat,
return_last=True,
)
except Exception as e:
response = f"Error: {e}"
st.session_state.conversation.append({"role": "agent", "content": response})
st.rerun()
render_conversation()
def run_app():
"""Entry point for the Streamlit app when run as a module."""
import streamlit.web.cli as stcli
import sys
sys.argv = ["streamlit", "run", __file__]
sys.exit(stcli.main())
if __name__ == "__main__":
main()