Issue
I have a websocket (in Django) to receive request from client (reactjs). The request calls chatgpt api to stream its response to client.
class GPTAPIConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
async def receive(self, text_data):
data = json.loads(text_data)
print('Start', data['requestID'])
asyncio.create_task(self.streamToClient(data['requestID']))
async def streamToClient(self, requestID):
completion = openai.ChatCompletion.create(...)
content = ''
for chunk in completion:
if chunk['choices'][0].get('delta', {}).get('function_call'):
chunkContent = chunk['choices'][0]['delta']['function_call']['arguments']
if chunkContent is not None:
content += chunkContent
await self.send(text_data=json.dumps({'text': content}))
print('End', requestID)
From client, I sent two messages with requestID 1 and 2. In server, the request 1 took about 10 seconds to finish so the log is:
Start 1
End 1
Start 2
End 2
What I want is:
Start 1
Start 2
End 1 or End 2 (depends which one ends first)
Please help me! Thank you!
Solution
I get it work by making the openai api call async:
completion = await openai.ChatCompletion.acreate(...)
content = ''
async for chunk in completion:
Answered By - Trong Lam Phan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.