Hi there,
By using Django channels, I want to enable streaming in my chatbot app. Here is my consumers.py code:
from channels.generic.websocket import AsyncWebsocketConsumer
import json
from .models import Chat
import openai
from decouple import config
from asgiref.sync import sync_to_async
import asyncio
openai.api_key = config("OPENAI_API_KEY")
async def generate_answer(question):
messages = [
{"role": "user", "content": question}
]
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
)
return response.choices[0].message.content.strip()
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
async def disconnect(self, close_code):
pass
async def receive(self, text_data):
text_data_json = json.loads(text_data)
question = text_data_json['question']
chat = Chat(question=question)
await sync_to_async(chat.save)()
answer = await generate_answer(question)
chat.answer = answer
await sync_to_async(chat.save)()
response = {
'question': question,
'answer': answer
}
await self.send(text_data=json.dumps(response))
I’ve tried a lot of ways, but none of them works.
Thanks in advance,