Slide 15
Slide 15 text
WebSocketでChannelsを使う︓サーバー側
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
async def disconnect(self, close_code):
pass
async def receive(self, text_data):
messages = json.loads(text_data)
last_message = messages[-1]
content = last_message["content"]
role = last_message["role"]
await self.send(
text_data=json.dumps({
"content" : content,
"role" : role,
}))
ollama_response =
await self.get_ollama_response(messages)
await self.send(
text_data=json.dumps({
"content" : ollama_response,
"role" : "assistant",
}))
async def get_ollama_response(self, messages):
system_prompt = [{ 'content': SYSTEM_CONTENT,
'role': 'system’}]
response =
await AsyncClient().chat(
model=MODEL_NAME,
messages=system_prompt + messages)
return response['message']['content']
15