Repository navigation
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
223 lines (200 loc) Β· 8.14 KB
/
Copy pathstreamlit_app.py
File metadata and controls
223 lines (200 loc) Β· 8.14 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
Streamlit Web UI for Procode Agent Framework
Simple, working alternative to the complex CopilotKit setup
"""
import streamlit as st
import requests
import json
from datetime import datetime
# Page config
st.set_page_config(
page_title="Procode Agent",
page_icon="π¬",
layout="wide"
)
# Custom CSS
st.markdown("""
<style>
.stChatMessage {
padding: 1rem;
border-radius: 0.5rem;
}
.cost-badge {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
display: inline-block;
margin: 0.5rem 0;
}
</style>
""", unsafe_allow_html=True)
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "total_cost" not in st.session_state:
st.session_state.total_cost = 0.0
if "request_count" not in st.session_state:
st.session_state.request_count = 0
# Sidebar
with st.sidebar:
st.title("π° Cost Optimization")
st.markdown("### Session Stats")
st.metric("Total Requests", st.session_state.request_count)
st.metric("Session Cost", f"${st.session_state.total_cost:.4f}")
st.metric("Cost Savings", "98%", delta="vs. baseline")
st.markdown("---")
st.markdown("### About")
st.info("""
This agent uses a multi-LLM strategy:
- π’ Deterministic (Free)
- π΅ Cached (Free)
- π‘ Haiku ($0.0001)
- π΄ Sonnet ($0.001)
Most requests are handled for free!
""")
if st.button("Clear Chat"):
st.session_state.messages = []
st.session_state.total_cost = 0.0
st.session_state.request_count = 0
st.rerun()
# Main content
st.title("Procode Agent π¬")
st.caption("AI Assistant with 98% Cost Savings")
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if "metadata" in message and message["metadata"]:
meta = message["metadata"]
if "intent" in meta:
st.caption(f"π― Intent: {meta['intent']}")
if "model" in meta:
model_emoji = {
"deterministic": "π’",
"cached": "π΅",
"haiku": "π‘",
"sonnet": "π΄"
}.get(meta["model"], "βͺ")
st.caption(f"{model_emoji} Model: {meta['model']}")
# Chat input
if prompt := st.chat_input("What can I help you with?"):
# Add user message
st.session_state.messages.append({"role": "user", "content": prompt, "metadata": {}})
with st.chat_message("user"):
st.markdown(prompt)
# Call backend
with st.chat_message("assistant"):
with st.spinner("π€ Thinking..."):
try:
# Prepare request
request_data = {
"jsonrpc": "2.0",
"method": "message/send",
"params": {
"message": {
"role": "user",
"parts": [{"kind": "text", "text": prompt}],
"messageId": f"msg-{datetime.now().timestamp()}"
}
},
"id": st.session_state.request_count + 1
}
# Call backend
response = requests.post(
"http://localhost:9998/",
json=request_data,
timeout=30
)
if response.status_code == 200:
result = response.json()
# Extract response
agent_response = "I apologize, but I couldn't process that request."
metadata = {}
if "result" in result:
result_data = result["result"]
if isinstance(result_data, dict):
# The response format is: result.parts[0].text
if "parts" in result_data:
parts = result_data["parts"]
if parts and len(parts) > 0:
if isinstance(parts[0], dict):
agent_response = parts[0].get("text", agent_response)
# Fallback: check for message.parts
elif "message" in result_data:
msg = result_data["message"]
if isinstance(msg, dict) and "parts" in msg:
parts = msg["parts"]
if parts and len(parts) > 0:
agent_response = parts[0].get("text", agent_response)
# Fallback: direct content
elif "content" in result_data:
agent_response = result_data["content"]
# Extract metadata (if available)
if "metadata" in result_data:
metadata = result_data["metadata"]
# Display response
st.markdown(agent_response)
# Display metadata
if metadata:
if "intent" in metadata:
st.caption(f"π― Intent: {metadata['intent']}")
if "model" in metadata:
model_emoji = {
"deterministic": "π’",
"cached": "π΅",
"haiku": "π‘",
"sonnet": "π΄"
}.get(metadata.get("model"), "βͺ")
st.caption(f"{model_emoji} Model: {metadata['model']}")
if "cost" in metadata:
st.caption(f"π° Cost: ${metadata['cost']:.6f}")
st.session_state.total_cost += metadata["cost"]
# Update session
st.session_state.messages.append({
"role": "assistant",
"content": agent_response,
"metadata": metadata
})
st.session_state.request_count += 1
else:
error_msg = f"β Backend error: {response.status_code}"
st.error(error_msg)
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"metadata": {}
})
except requests.exceptions.ConnectionError:
error_msg = "β Cannot connect to backend. Make sure the Python backend is running on port 9998.\n\nRun: `make start` or `python __main__.py`"
st.error(error_msg)
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"metadata": {}
})
except Exception as e:
error_msg = f"β Error: {str(e)}"
st.error(error_msg)
st.session_state.messages.append({
"role": "assistant",
"content": error_msg,
"metadata": {}
})
# Footer
st.markdown("---")
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("**Features:**")
st.markdown("β
Real-time chat")
with col2:
st.markdown("**Cost Optimized:**")
st.markdown("β
98% savings")
with col3:
st.markdown("**Backend:**")
if st.button("Check Status"):
try:
response = requests.get("http://localhost:9998/", timeout=2)
st.success("β
Backend is running")
except:
st.error("β Backend not running")