codeinterpreter-api
codeinterpreter-api copied to clipboard
RuntimeError: Event loop is closed
this is my code:
from codeinterpreterapi import CodeInterpreterSession, File
import os, dotenv
import openai
### Lade Variabeln ###
os.environ["OPENAI_API_KEY"] = "sk-XXXXXXXXXXX-Its my key:)YYYYYY"
async def main():
# context manager for auto start/stop of the session
async with CodeInterpreterSession(openai_api_key="OPENAI_API_KEY", model="gpt-3.5-turbo") as session:
# define the user request
user_request = "Analyze this dataset and plot something interesting about it."
files = [
File.from_path("./bitcoin_csv.csv"),
]
# generate the response
response = await session.generate_response(
user_request, files=files
)
# output to the user
print("AI: ", response.content)
for i, file in enumerate(response.files):
with open(f'image_{i}.png', 'wb') as f:
f.write(file)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
this is my traceback:
`PS C:\Users\zaind\Documents\Hookmaker> & C:/Users/zaind/AppData/Local/Programs/Python/Python311/python.exe c:/Users/zaind/Documents/Hookmaker/tester.py
Traceback (most recent call last):
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 655, in __await_impl_timeout__
return await self.__await_impl__()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 662, in __await_impl__
await protocol.handshake(
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 323, in handshake
status_code, response_headers = await self.read_http_response()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 139, in read_http_response
status_code, reason, headers = await read_response(self.reader)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\http.py", line 120, in read_response
status_line = await read_line(stream)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\http.py", line 194, in read_line
line = await stream.readline()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\streams.py", line 545, in readline
line = await self.readuntil(sep)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\streams.py", line 637, in readuntil
await self._wait_for_data('readuntil')
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\streams.py", line 522, in _wait_for_data
await self._waiter
asyncio.exceptions.CancelledError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\zaind\Documents\Hookmaker\tester.py", line 33, in <module>
asyncio.run(main())
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "c:\Users\zaind\Documents\Hookmaker\tester.py", line 11, in main
async with CodeInterpreterSession(openai_api_key="OPENAI_API_KEY", model="gpt-3.5-turbo") as session:
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\codeinterpreterapi\session.py", line 194, in __aenter__
await self.astart()
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\codeinterpreterapi\session.py", line 32, in astart
await self.codebox.astart()
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\codeboxapi\box\localbox.py", line 139, in astart
self.ws = await ws_connect(
^^^^^^^^^^^^^^^^^
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\site-packages\websockets\legacy\client.py", line 654, in __await_impl_timeout__
async with asyncio_timeout(self.open_timeout):
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\timeouts.py", line 111, in __aexit__
raise TimeoutError from exc_val
TimeoutError
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001ABFF0F8D50>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000001ABFF15F1C0>, 58040.218)]']
connector: <aiohttp.connector.TCPConnector object at 0x000001ABFEB21D10>
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x000001ABE4D47920>
Traceback (most recent call last):
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_subprocess.py", line 126, in __del__
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_subprocess.py", line 104, in close
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\proactor_events.py", line 109, in close
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 761, in call_soon
File "C:\Users\zaind\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 519, in _check_closed
RuntimeError: Event loop is closed
Anyone know what the problem is?
Thanks :)
I am also facing the same issue.
I encounter this issue when try to run on Windows, my solution is to use docker as an alternative
Maybe the LocalBox which is fully isolated. When I asked question next time,there showed the info as below:
INFO: Using a LocalBox which is not fully isolated
and not scalable across multiple users.
Make sure to use a CODEBOX_API_KEY in production.
Set envar SHOW_INFO=False to not see this again.