|
| 1 | +[Table of contents](README.md#table-of-contents) |
| 2 | + |
| 3 | +# AI for personal data |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +AI can be used for interacting with the personal data of a Cozy. This is |
| 8 | +currently an experimental feature. Retrieval-Augmented Generation (RAG) is |
| 9 | +a classical pattern in the AI world. Here, it is specific to each Cozy. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## Indexation |
| 14 | + |
| 15 | +First of all, the RAG server must be installed with its dependencies. It is |
| 16 | +not mandatory to install them on the same servers as the cozy-stack. And the |
| 17 | +URL of RAG must be filled in cozy-stack configuration file (in `rag`). |
| 18 | + |
| 19 | +For the moment, the feature is experimental, and a trigger must be created |
| 20 | +manually on the Cozy: |
| 21 | + |
| 22 | +```sh |
| 23 | +$ COZY=cozy.localhost:8080 |
| 24 | +$ TOKEN=$(cozy-stack instances token-cli $COZY io.cozy.triggers) |
| 25 | +$ curl "http://${COZY}/jobs/triggers" -H "Authorization: Bearer $TOKEN" -d '{ "data": { "attributes": { "type": "@event", "arguments": "io.cozy.files", "debounce": "1m", "worker": "rag-index", "message": {"doctype": "io.cozy.files"} } } }' |
| 26 | +``` |
| 27 | + |
| 28 | +It can also be a good idea to start a first indexation with: |
| 29 | + |
| 30 | +```sh |
| 31 | +$ cozy-stack triggers launch --domain $COZY $TRIGGER_ID |
| 32 | +``` |
| 33 | + |
| 34 | +In practice, when files are uploaded/modified/deleted, the trigger will create |
| 35 | +a job for the index worker (with debounce). The index worker will look at the |
| 36 | +changed feed, and will call the RAG for each entry in the changes feed. |
| 37 | + |
| 38 | +## Chat |
| 39 | + |
| 40 | +When a user starts a chat, their prompts are sent to the RAG that can use the |
| 41 | +vector database to find relevant documents (technically, only some parts of |
| 42 | +the documents called chunks). Those documents are added to the prompt, so |
| 43 | +that the LLM can use them as a context when answering. |
| 44 | + |
| 45 | +### POST /ai/chat/conversations/:id |
| 46 | + |
| 47 | +This route can be used to ask AI for a chat completion. The id in the path |
| 48 | +must be the identifier of a chat conversation. The client can generate a random |
| 49 | +identifier for a new chat conversation. |
| 50 | + |
| 51 | +The stack will respond after pushing a job for this task, but without the |
| 52 | +response. The client must use the real-time websocket and subscribe to |
| 53 | +`io.cozy.ai.chat.events`. |
| 54 | + |
| 55 | +#### Request |
| 56 | + |
| 57 | +```http |
| 58 | +POST /ai/chat/conversations/e21dce8058b9013d800a18c04daba326 HTTP/1.1 |
| 59 | +Content-Type: application/json |
| 60 | +``` |
| 61 | + |
| 62 | +```json |
| 63 | +{ |
| 64 | + "q": "Why the sky is blue?" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +#### Response |
| 69 | + |
| 70 | +```http |
| 71 | +HTTP/1.1 202 Accepted |
| 72 | +Content-Type: application/vnd.api+json |
| 73 | +``` |
| 74 | + |
| 75 | +```json |
| 76 | +{ |
| 77 | + "data": { |
| 78 | + "type": "io.cozy.ai.chat.conversations", |
| 79 | + "id": "e21dce8058b9013d800a18c04daba326", |
| 80 | + "rev": "1-23456", |
| 81 | + "attributes": { |
| 82 | + "messages": [ |
| 83 | + { |
| 84 | + "id": "eb17c3205bf1013ddea018c04daba326", |
| 85 | + "role": "user", |
| 86 | + "content": "Why the sky is blue?", |
| 87 | + "createdAt": "2024-09-24T13:24:07.576Z" |
| 88 | + } |
| 89 | + ] |
| 90 | + } |
| 91 | + }, |
| 92 | + "cozyMetadata": { |
| 93 | + "createdAt": "2024-09-24T13:24:07.576Z", |
| 94 | + "createdOn": "http://cozy.localhost:8080/", |
| 95 | + "doctypeVersion": "1", |
| 96 | + "metadataVersion": 1, |
| 97 | + "updatedAt": "2024-09-24T13:24:07.576Z" |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Real-time via websockets |
| 103 | + |
| 104 | +``` |
| 105 | +client > {"method": "AUTH", "payload": "token"} |
| 106 | +client > {"method": "SUBSCRIBE", |
| 107 | + "payload": {"type": "io.cozy.ai.chat.events"}} |
| 108 | +server > {"event": "CREATED", |
| 109 | + "payload": {"id": "eb17c3205bf1013ddea018c04daba326", |
| 110 | + "type": "io.cozy.ai.chat.events", |
| 111 | + "doc": {"object": "delta", "content": "The ", "position": 0}}} |
| 112 | +server > {"event": "CREATED", |
| 113 | + "payload": {"id": "eb17c3205bf1013ddea018c04daba326", |
| 114 | + "type": "io.cozy.ai.chat.events", |
| 115 | + "doc": {"object": "delta", "content": "sky ", "position": 1}}} |
| 116 | +[...] |
| 117 | +server > {"event": "CREATED", |
| 118 | + "payload": {"id": "eb17c3205bf1013ddea018c04daba326", |
| 119 | + "type": "io.cozy.ai.chat.events", |
| 120 | + "doc": {"object": "done"}}} |
| 121 | +``` |
0 commit comments