shan gao commited on
Commit
aff4b96
·
1 Parent(s): efb3827

modify streamlit_app.py

Browse files
Files changed (2) hide show
  1. src/.DS_Store +0 -0
  2. src/streamlit_app.py +14 -3
src/.DS_Store ADDED
Binary file (6.15 kB). View file
 
src/streamlit_app.py CHANGED
@@ -167,10 +167,21 @@ def build_agent():
167
  return agent
168
 
169
 
170
- def run_agent(agent: FunctionAgent, prompt: str) -> str:
171
- """Run the agent synchronously inside Streamlit."""
172
  handler = agent.run(prompt)
173
- return str(asyncio.run(handler))
 
 
 
 
 
 
 
 
 
 
 
174
 
175
 
176
  def _require_env(var_name: str) -> bool:
 
167
  return agent
168
 
169
 
170
+ async def _arun_agent(agent: FunctionAgent, prompt: str) -> str:
171
+ """Await the agent workflow and return the stringified response."""
172
  handler = agent.run(prompt)
173
+ return str(await handler)
174
+
175
+
176
+ def run_agent(agent: FunctionAgent, prompt: str) -> str:
177
+ """Run the agent from Streamlit, whether or not an event loop is already running."""
178
+ try:
179
+ loop = asyncio.get_running_loop()
180
+ except RuntimeError:
181
+ return asyncio.run(_arun_agent(agent, prompt))
182
+ else:
183
+ return loop.run_until_complete(_arun_agent(agent, prompt))
184
+
185
 
186
 
187
  def _require_env(var_name: str) -> bool: