Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +40 -40
src/streamlit_app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
|
|
|
| 4 |
from llama_index.llms.openai import OpenAI
|
| 5 |
from llama_index.core.agent import ReActAgent
|
|
|
|
| 6 |
from llama_index.core.tools import FunctionTool
|
| 7 |
-
from llama_index.core.llms import ChatMessage
|
| 8 |
|
| 9 |
# --- Import tools class ---
|
| 10 |
from tools import PokemonAdvisorTools
|
|
@@ -25,58 +26,57 @@ def initialize_agent():
|
|
| 25 |
advisor.get_data_shape
|
| 26 |
]
|
| 27 |
tools = [FunctionTool.from_defaults(fn=func) for func in tool_methods]
|
| 28 |
-
llm = OpenAI(model="gpt-4o-mini"
|
| 29 |
|
| 30 |
-
system_prompt = """
|
| 31 |
-
|
| 32 |
-
You
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
agent = ReActAgent(
|
| 45 |
tools=tools,
|
| 46 |
llm=llm,
|
| 47 |
-
verbose=True,
|
| 48 |
-
|
|
|
|
| 49 |
)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
agent
|
| 53 |
-
|
| 54 |
-
return agent
|
| 55 |
|
| 56 |
-
agent = initialize_agent()
|
|
|
|
| 57 |
|
| 58 |
# --- 2. Page Configuration ---
|
| 59 |
st.set_page_config(page_title="cAsh Robo-Advisor", page_icon="🤖", layout="wide")
|
| 60 |
|
| 61 |
# --- 3. Chat Logic Function ---
|
| 62 |
-
def process_chat(user_message):
|
| 63 |
-
"""Handles the
|
| 64 |
st.session_state.messages.append({"role": "user", "content": user_message})
|
| 65 |
|
| 66 |
# Placeholder for the assistant's response while it thinks
|
| 67 |
with st.chat_message("assistant"):
|
| 68 |
response_placeholder = st.empty()
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
response_placeholder.markdown(error_msg)
|
| 79 |
-
st.error(f"Full error: {repr(e)}")
|
| 80 |
|
| 81 |
# --- 4. Sidebar & UI Styling ---
|
| 82 |
st.title("🤖 cAsh Robo-Advisor")
|
|
@@ -90,7 +90,6 @@ if "messages" not in st.session_state:
|
|
| 90 |
st.sidebar.header("Example Queries")
|
| 91 |
examples = [
|
| 92 |
"What are the top 3 grading opportunities right now?",
|
| 93 |
-
"Is investing in Charizard VMAX risky?",
|
| 94 |
"What cards are trending down?",
|
| 95 |
"Show me profitable cards by Tomokazu Komiya."
|
| 96 |
]
|
|
@@ -98,8 +97,7 @@ examples = [
|
|
| 98 |
# Sidebar helper: clicking an example sends it immediately
|
| 99 |
for ex in examples:
|
| 100 |
if st.sidebar.button(ex):
|
| 101 |
-
process_chat(ex)
|
| 102 |
-
st.rerun()
|
| 103 |
|
| 104 |
# --- 5. Main Chat Interface ---
|
| 105 |
# Display existing messages
|
|
@@ -109,6 +107,8 @@ for message in st.session_state.messages:
|
|
| 109 |
|
| 110 |
# User Input
|
| 111 |
if prompt := st.chat_input("Ask your Pokemon Quants advisor..."):
|
|
|
|
| 112 |
with st.chat_message("user"):
|
| 113 |
st.markdown(prompt)
|
| 114 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
import asyncio
|
| 5 |
from llama_index.llms.openai import OpenAI
|
| 6 |
from llama_index.core.agent import ReActAgent
|
| 7 |
+
from llama_index.core.workflow import Context
|
| 8 |
from llama_index.core.tools import FunctionTool
|
|
|
|
| 9 |
|
| 10 |
# --- Import tools class ---
|
| 11 |
from tools import PokemonAdvisorTools
|
|
|
|
| 26 |
advisor.get_data_shape
|
| 27 |
]
|
| 28 |
tools = [FunctionTool.from_defaults(fn=func) for func in tool_methods]
|
| 29 |
+
llm = OpenAI(model="gpt-4o-mini")
|
| 30 |
|
| 31 |
+
system_prompt = """
|
| 32 |
+
### ROLE
|
| 33 |
+
You are the **cAsh, Pokemon Investment Advisor**, an expert algorithmic trading assistant for the Pokémon TCG market.
|
| 34 |
+
You rely **strictly** on data. You do not guess. You do not hallucinate prices.
|
| 35 |
+
|
| 36 |
+
### TOOL USAGE PROTOCOL
|
| 37 |
+
1. **Verify First:** If a user asks about a card, ALWAYS use `get_card_info` first to ensure it exists.
|
| 38 |
+
2. **Safety Check:** BEFORE recommending ANY card, you MUST call `assess_risk_volatility` (default to 6 months).
|
| 39 |
+
3. **Refining Search:** If the user corrects you (e.g., "Not that card"), use `get_card_info` again with the corrected name or use `analyze_set_performance` to broaden the scope.
|
| 40 |
+
4. **Profit Hunting:** If a user asks "What should I buy?", use `find_grading_opportunities` or `get_market_movers`.
|
| 41 |
+
|
| 42 |
+
### TONE
|
| 43 |
+
Professional, objective, concise, and user-friendly.
|
| 44 |
+
"""
|
| 45 |
|
| 46 |
agent = ReActAgent(
|
| 47 |
tools=tools,
|
| 48 |
llm=llm,
|
| 49 |
+
verbose=True,
|
| 50 |
+
system_prompt=system_prompt,
|
| 51 |
+
timeout=120
|
| 52 |
)
|
| 53 |
+
# Context should be initialized once
|
| 54 |
+
ctx = Context()
|
| 55 |
+
return agent, ctx
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
agent, global_ctx = initialize_agent()
|
| 58 |
+
print("agent has initialized")
|
| 59 |
|
| 60 |
# --- 2. Page Configuration ---
|
| 61 |
st.set_page_config(page_title="cAsh Robo-Advisor", page_icon="🤖", layout="wide")
|
| 62 |
|
| 63 |
# --- 3. Chat Logic Function ---
|
| 64 |
+
async def process_chat(user_message):
|
| 65 |
+
"""Handles the async call to the agent and updates session state."""
|
| 66 |
st.session_state.messages.append({"role": "user", "content": user_message})
|
| 67 |
|
| 68 |
# Placeholder for the assistant's response while it thinks
|
| 69 |
with st.chat_message("assistant"):
|
| 70 |
response_placeholder = st.empty()
|
| 71 |
+
try:
|
| 72 |
+
# Execute the agent workflow
|
| 73 |
+
response = await agent.run(user_msg=user_message, ctx=global_ctx)
|
| 74 |
+
final_text = str(response)
|
| 75 |
+
response_placeholder.markdown(final_text)
|
| 76 |
+
st.session_state.messages.append({"role": "assistant", "content": final_text})
|
| 77 |
+
except Exception as e:
|
| 78 |
+
error_msg = f"⚠️ **Agent Error:** {str(e)}"
|
| 79 |
+
response_placeholder.markdown(error_msg)
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# --- 4. Sidebar & UI Styling ---
|
| 82 |
st.title("🤖 cAsh Robo-Advisor")
|
|
|
|
| 90 |
st.sidebar.header("Example Queries")
|
| 91 |
examples = [
|
| 92 |
"What are the top 3 grading opportunities right now?",
|
|
|
|
| 93 |
"What cards are trending down?",
|
| 94 |
"Show me profitable cards by Tomokazu Komiya."
|
| 95 |
]
|
|
|
|
| 97 |
# Sidebar helper: clicking an example sends it immediately
|
| 98 |
for ex in examples:
|
| 99 |
if st.sidebar.button(ex):
|
| 100 |
+
asyncio.run(process_chat(ex))
|
|
|
|
| 101 |
|
| 102 |
# --- 5. Main Chat Interface ---
|
| 103 |
# Display existing messages
|
|
|
|
| 107 |
|
| 108 |
# User Input
|
| 109 |
if prompt := st.chat_input("Ask your Pokemon Quants advisor..."):
|
| 110 |
+
# Clear the "user" UI immediately then run logic
|
| 111 |
with st.chat_message("user"):
|
| 112 |
st.markdown(prompt)
|
| 113 |
+
|
| 114 |
+
asyncio.run(process_chat(prompt))
|