I am working with the following code to create a streamlit app and I am running with the issue of RuntimeError: The event loop is already running. So what I want to do is call this module in a streamlit app that takes text_area and applies it to the ‘x’ variable in the async function webster within the streamlit app →
import asyncio
from langchain import hub
from langchain_community.tools.playwright.utils import create_async_playwright_browser
from langchain_openai.chat_models import ChatOpenAI
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
from langchain.agents import initialize_agent, AgentType, load_tools, AgentExecutor, create_structured_chat_agent
llm = ChatOpenAI(
temperature=0.9,
model="gpt-4-32k"
)
prompt = hub.pull("hwchase17/structured-chat-agent")
async def webster(x) -> Union[str, None, dict]:
async with create_async_playwright_browser() as k:
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=k)
tools = toolkit.get_tools()
agent = create_structured_chat_agent(llm=llm,
tools=tools,
prompt=prompt)
second_agent_chain = AgentExecutor(agent=agent,
tools=tools,
verbose=True,
return_intermediate_steps=True,
handle_parsing_errors=True,
intermediate_steps=['action', 'final_answer', 'Observation', 'Thought']
)
net = await second_agent_chain.ainvoke({'input': x}, return_only_outputs=True)
return net['output']
if __name__ == "__main__":
query = f'what is the weather today at any three places in the world?'
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
result = loop.run_until_complete(webster(query))
RuntimeError: This event loop is already running
Traceback:
File "C:\Users\PycharmProjects\test\venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script
exec(code, module.__dict__)
File "C:\Users\PycharmProjects\test\pages\webscrape.py", line 71, in <module>
result = loop.run_until_complete(webster(prompt))
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\futures.py", line 201, in result
raise self._exception.with_traceback(self._exception_tb)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", line 232, in __step
result = coro.send(None)
File "C:\Users\PycharmProjects\test\.\scraped.py", line 45, in webster
async with create_async_playwright_browser() as k:
File "C:\Users\PycharmProjects\test\venv\lib\site-packages\langchain_community\tools\playwright\utils.py", line 68, in create_async_playwright_browser
browser = run_async(async_playwright().start())
File "C:\Users\PycharmProjects\test\venv\lib\site-packages\langchain_community\tools\playwright\utils.py", line 104, in run_async
return event_loop.run_until_complete(coro)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 625, in run_until_complete
self._check_running()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 584, in _check_running
raise RuntimeError('This event loop is already running')