ollama / ollama-python

Ollama Python library
https://ollama.com
MIT License
4.09k stars 344 forks source link

asyncio.run() cannot be called from a running event loop #35

Closed chenxizhang closed 8 months ago

chenxizhang commented 8 months ago

I am using python 3.11.7 and the latest version of the Ollama pythons SDKs to run the sample code about AsyncClient

from ollama import AsyncClient
import asyncio

async def chat():
    message ={ "role":"user","content":"why people smile?"}
    response = await AsyncClient().chat(model="llama2",message=[message])

asyncio.run(chat())

I got an error as below.

RuntimeError Traceback (most recent call last) Cell In[7], line 8 5 message ={ "role":"user","content":"why people smile?"} 6 response = await AsyncClient().chat(model="llama2",message=[message]) ----> 8 asyncio.run(chat())

File ~/anaconda3/envs/tedia/lib/python3.11/asyncio/runners.py:186, in run(main, debug) 161 """Execute the coroutine and return the result. 162 163 This function runs the passed coroutine, taking care of (...) 182 asyncio.run(main()) 183 """ 184 if events._get_running_loop() is not None: 185 # fail fast with short traceback --> 186 raise RuntimeError( 187 "asyncio.run() cannot be called from a running event loop") 189 with Runner(debug=debug) as runner: 190 return runner.run(main)

RuntimeError: asyncio.run() cannot be called from a running event loop

chenxizhang commented 8 months ago

fixed it, and added some sample code in this pr #36