Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit d6b8670

Browse files
committed
add missing connections logic
1 parent 4bbb14a commit d6b8670

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

docs/guides/python/llama-rag.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,33 +276,41 @@ uv run model_utilities.py
276276
Let's create our resources in a common file so that it can be imported to the subscriber and chat modules. We'll create a websocket which will interface with the user for prompts and create a topic to handle the backend query engine. The websocket will trigger the topic on a prompt message, which will trigger the subscriber to handle the prompt. Once the subscriber is finished it will send a response to the socket. It is done this way with the topic so that the websocket doesn't time out after 30 seconds, as most queries will take longer than that to process.
277277

278278
```python title:common/resources.py
279-
from nitric.resources import websocket, topic
279+
from nitric.resources import websocket, topic, kv
280280

281281
socket = websocket("socket")
282282
chat_topic = topic("chat")
283+
connections = kv("connections")
283284
```
284285

285286
## Use the resources for querying the model
286287

287288
With our LLM downloaded and given the context documentation for querying, we can use our websocket to handle prompts. The main piece of logic here is publishing to the chat topic
288289

289290
```python title:services/chat.py
290-
from common.resources import socket, chat_topic
291+
from common.resources import socket, chat_topic, connections
291292

292293
from nitric.context import WebsocketContext
293294
from nitric.application import Nitric
294295

295296
publishable_chat_topic = chat_topic.allow("publish")
297+
write_delete_connections = connections.allow("set", "delete")
296298

297299
@socket.on("connect")
298300
async def on_connect(ctx):
299301
# handle connections
302+
await write_delete_connections.set(ctx.req.connection_id, {
303+
"context": []
304+
})
305+
300306
print(f"socket connected with {ctx.req.connection_id}")
301307
return ctx
302308

303309
@socket.on("disconnect")
304310
async def on_disconnect(ctx):
305311
# handle disconnections
312+
await write_delete_connections.delete(ctx.req.connection_id)
313+
306314
print(f"socket disconnected with {ctx.req.connection_id}")
307315
return ctx
308316

0 commit comments

Comments
 (0)