openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

acreate method fail when using async

Open menghonghan opened this issue 2 years ago • 3 comments

Describe the bug

image

image

when using async acreate , the API timeout

To Reproduce

async def chat(query, retry_count=0): response = await openai.ChatCompletion.acreate( model="gpt-3.5-turbo",
messages=query, temperature=0.7,
max_tokens=1200,
top_p=1, frequency_penalty=0.0,
presence_penalty=0.0,
)

 out = response.choices[0]['message']['content']

@gpt_app.post("/gpt_go") async def gpt_go(request: Request): json_data = await request.form()

role = json_data["role"]
content = json_data["content"]
output = await chat([{"role": "system", "content":"hello"}])
return output

Code snippets

No response

OS

linux

Python version

Python 3.7

Library version

openai 0.27.0

menghonghan avatar Mar 06 '23 12:03 menghonghan

In my server, upgrade aiohttp to 3.8.4 solved this.

https://github.com/nonebot/nonebot2/issues/1784#issuecomment-1462842717

TheLZY avatar Mar 09 '23 21:03 TheLZY

ClientSession(trust_env=True)

https://stackoverflow.com/a/63364551/2142477

wadecong avatar Mar 12 '23 05:03 wadecong

Workaround: replace the session object:

async with ClientSession(trust_env=True) as session:
    openai.aiosession.set(session)
    ...

frostming avatar Mar 17 '23 08:03 frostming

I met the same problem. I'm wondering if the above solutions work for you? I'm not sure how to add ClientSession in the codes

jinghan23 avatar May 04 '23 07:05 jinghan23

In my case, the issue turned out to be urllib (probably?) failing to locate the CA SSL certificates on the system. The funny thing is that non-async requests worked - apparently because the OpenAI API library uses different HTTP libraries for sync and async.

For me (Python 3.10 installed via MacPorts on macOS), the fix was to add this to .zshrc:

export SSL_CERT_FILE=$(python3 -m certifi)

artmatsak avatar May 04 '23 11:05 artmatsak

This should be fixed in the upcoming v1 beta! Please try it and let us know!

RobertCraigie avatar Nov 03 '23 22:11 RobertCraigie