Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error RuntimeError: Cannot run the event loop while another loop is running on notebook #141

Closed
tokestermw opened this issue Jun 9, 2023 · 3 comments · Fixed by #357
Labels

Comments

@tokestermw
Copy link

tokestermw commented Jun 9, 2023

Hi, I am getting an error when running on a jupyterlab notebook, the python shell works fine:

  • python 3.9
  • outlines 0.0.4
import os

import outlines.text as text
import outlines.models as models

examples = [
    ("The food was digusting", "Negative"),
    ("We had a fantastic night", "Positive"),
    ("Recommended", "Positive"),
    ("The waiter was rude", "Negative")
]

@text.prompt
def labelling(to_label, examples):
    """You are a sentiment-labelling assistant.

    {% for example in examples %}
    {{ example[0] }} // {{ example[1] }}
    {% endfor %}
    {{ to_label }} //
    """

complete = models.text_completion.openai("text-davinci-003")
prompt = labelling("Just awesome", examples)
answer = complete(prompt)

Gets the error

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[3], line 20
     18 complete = models.text_completion.openai("text-davinci-003")
     19 prompt = labelling("Just awesome", examples)
---> 20 answer = complete(prompt)

File ~/venv3.9/lib/python3.9/site-packages/outlines/models/openai.py:84, in OpenAICompletion.<locals>.generate(prompt, samples, stop_at, is_in, type)
     82     return generate_choice(prompt, is_in, samples)
     83 else:
---> 84     return generate_base(prompt, stop_at, samples, mask)

File ~/venv3.9/lib/python3.9/site-packages/outlines/base.py:48, in vectorize.__call__(self, *args, **kwargs)
     46     return self.call_thunk()
     47 elif self.signature is not None:
---> 48     return self.call_with_signature(*args, **kwargs)
     49 else:
     50     return self.call_no_signature(*args, **kwargs)

File ~/venv3.9/lib/python3.9/site-packages/outlines/base.py:153, in vectorize.call_with_signature(self, *args, **kwargs)
    150 n_out = len(output_core_dims)
    152 if self.is_coroutine_fn:
--> 153     outputs = self.vectorize_call_coroutine(broadcast_shape, args, kwargs)
    154 else:
    155     outputs = self.vectorize_call(broadcast_shape, args, kwargs)

File ~/venv3.9/lib/python3.9/site-packages/outlines/base.py:242, in vectorize.vectorize_call_coroutine(self, broadcast_shape, args, kwargs)
    240 loop = asyncio.new_event_loop()
    241 try:
--> 242     outputs = loop.run_until_complete(create_and_gather_tasks())
    243 finally:
    244     loop.close()

File /usr/lib/python3.9/asyncio/base_events.py:623, in BaseEventLoop.run_until_complete(self, future)
    612 """Run until the Future is done.
    613 
    614 If the argument is a coroutine, it is wrapped in a Task.
   (...)
    620 Return the Future's result, or raise its exception.
    621 """
    622 self._check_closed()
--> 623 self._check_running()
    625 new_task = not futures.isfuture(future)
    626 future = tasks.ensure_future(future, loop=self)

File /usr/lib/python3.9/asyncio/base_events.py:585, in BaseEventLoop._check_running(self)
    583     raise RuntimeError('This event loop is already running')
    584 if events._get_running_loop() is not None:
--> 585     raise RuntimeError(
    586         'Cannot run the event loop while another loop is running')

RuntimeError: Cannot run the event loop while another loop is running
@tokestermw
Copy link
Author

tokestermw commented Jun 9, 2023

ah this workaround works: slackapi/python-slack-sdk#429 (comment)

adding this to the top of the notebook:

# you need to do this for jupyter notebooks
# https://github.com/slackapi/python-slack-sdk/issues/429#issuecomment-525402667
import nest_asyncio
nest_asyncio.apply()

@rlouf
Copy link
Member

rlouf commented Jun 10, 2023

I think this is due to outlines running async tasks in an event loop instead of exposing the async API. The correct solution would be to expose it, but glad you found a workaround.

@rlouf rlouf added the bug label Jun 20, 2023
@rlouf
Copy link
Member

rlouf commented Jul 13, 2023

Re-opening as this was mentioned again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants