-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
99 lines (79 loc) · 2.64 KB
/
Copy pathstreamlit_app.py
File metadata and controls
99 lines (79 loc) · 2.64 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
import os
try:
from dotenv import load_dotenv
except ImportError: # pragma: no cover - provide fallback when dotenv not installed
def load_dotenv(*args, **kwargs):
return False
try:
import streamlit as st
except ImportError: # pragma: no cover - provide minimal streamlit stub
class _DummySpinner:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
pass
class _DummyStreamlit:
def error(self, *args, **kwargs):
pass
def stop(self):
pass
def title(self, *args, **kwargs):
pass
def text_input(self, *args, **kwargs):
return ""
def button(self, *args, **kwargs):
return False
def spinner(self, *args, **kwargs):
return _DummySpinner()
def write(self, *args, **kwargs):
pass
st = _DummyStreamlit()
from lib.game_agent import GameAgent, report_run
import io
import contextlib
def check_env() -> list[str]:
"""Return list of missing environment keys required for the agent."""
required = ["OPENAI_API_KEY", "TAVILY_API_KEY"]
return [k for k in required if not os.getenv(k)]
def main():
if not load_dotenv():
load_dotenv("config.env")
missing = check_env()
if missing:
st.error(
"Missing the following API keys: "
+ ", ".join(missing)
+ ". Please update your .env file and restart."
)
st.stop()
st.title("UdaPlay Game Agent")
if "history" not in st.session_state:
st.session_state["history"] = []
for item in st.session_state["history"]:
st.write(f"**You:** {item['question']}")
st.write(f"**Agent:** {item['answer']}")
with st.expander("Debug"):
st.code(item["debug"])
st.write("---")
query = st.text_input("Ask a question about video games")
if st.button("Ask") and query:
agent = GameAgent()
with st.spinner("Thinking..."):
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
run = agent.invoke(query)
report_run(run)
debug_output = buf.getvalue()
final = run.get_final_state()
answer = final.get("answer") if final else None
if answer:
st.session_state["history"].append(
{"question": query, "answer": answer, "debug": debug_output}
)
st.write(answer)
with st.expander("Debug"):
st.code(debug_output)
else:
st.write("No answer available.")
if __name__ == "__main__":
main()