WebSocket如何同时接收两个请求chatgpt

tpgth1q7  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(241)

我有一个WebSocket(在Django中)来接收来自客户端的请求(reactjs)。请求调用chatgpt API来将其响应流传输到客户端。

  1. class GPTAPIConsumer(AsyncWebsocketConsumer):
  2. async def connect(self):
  3. await self.accept()
  4. async def receive(self, text_data):
  5. data = json.loads(text_data)
  6. print('Start', data['requestID'])
  7. asyncio.create_task(self.streamToClient(data['requestID']))
  8. async def streamToClient(self, requestID):
  9. completion = openai.ChatCompletion.create(...)
  10. content = ''
  11. for chunk in completion:
  12. if chunk['choices'][0].get('delta', {}).get('function_call'):
  13. chunkContent = chunk['choices'][0]['delta']['function_call']['arguments']
  14. if chunkContent is not None:
  15. content += chunkContent
  16. await self.send(text_data=json.dumps({'text': content}))
  17. print('End', requestID)

字符串
从客户端,我发送了两条消息,请求ID为1和2。在服务器端,请求1花了大约10秒才完成,所以日志是:

  1. Start 1
  2. End 1
  3. Start 2
  4. End 2


我想要的是:

  1. Start 1
  2. Start 2
  3. End 1 or End 2 (depends which one ends first)


帮帮我!谢谢!

quhf5bfb

quhf5bfb1#

我通过openai API调用aprc来实现它:

  1. completion = await openai.ChatCompletion.acreate(...)
  2. content = ''
  3. async for chunk in completion:

字符串

bjp0bcyl

bjp0bcyl2#

您面临的问题是,streamToClient函数在移动到下一个请求之前等待每个请求的完成。为了实现多个请求的并发处理和交错响应,您可以利用Pencio的gather并发运行任务。下面是您的代码的更新版本:

  1. class GPTAPIConsumer(AsyncWebsocketConsumer):
  2. async def connect(self):
  3. await self.accept()
  4. async def receive(self, text_data):
  5. data = json.loads(text_data)
  6. print('Start', data['requestID'])
  7. asyncio.create_task(self.streamToClient(data['requestID']))
  8. async def streamToClient(self, requestID):
  9. completion = openai.ChatCompletion.create(...)
  10. content = ''
  11. async for chunk in completion:
  12. if chunk['choices'][0].get('delta', {}).get('function_call'):
  13. chunkContent = chunk['choices'][0]['delta']['function_call']['arguments']
  14. if chunkContent is not None:
  15. content += chunkContent
  16. await self.send(text_data=json.dumps({'text': content}))
  17. print('End', requestID)
  18. # You can use asyncio.gather to run tasks concurrently
  19. async def run_multiple_requests(request_ids):
  20. tasks = [GPTAPIConsumer().streamToClient(request_id) for request_id in request_ids]
  21. await asyncio.gather(*tasks)
  22. # Example usage:
  23. request_ids = [1, 2] # List of request IDs
  24. await run_multiple_requests(request_ids)

字符串
这段代码将使用asyncio.gather并发地为每个请求执行streamToClient方法。它应该允许响应交错,而不是等待一个请求完成才处理下一个请求。调整它以适应Django设置并使用必要的组件。

展开查看全部

相关问题