-
Notifications
You must be signed in to change notification settings - Fork 139
Add external indexers for RAG #4457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8851c7d
Add external indexers for RAG
nono 95b677d
Skip design_docs in index worker
nono 8660cc6
Remove LibreChat and use a standalone RAG server
nono 392e491
Add a route to ask chat completion to the RAG
nono 036ca1c
Add streaming and realtime events for AI chats
nono 3f0bedd
Improve a few things
nono 97919af
Update doc
nono 6ebb03b
Add auth for RAG server
nono 902130a
Add position to io.cozy.ai.chat.events
nono 1be1469
Add an endpoint to fill the description of a file with AI
nono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
[Table of contents](README.md#table-of-contents) | ||
|
||
# AI for personal data | ||
|
||
## Introduction | ||
|
||
AI can be used for interacting with the personal data of a Cozy. This is | ||
currently an experimental feature. Retrieval-Augmented Generation (RAG) is | ||
a classical pattern in the AI world. Here, it is specific to each Cozy. | ||
|
||
 | ||
|
||
## Indexation | ||
|
||
First of all, the RAG server must be installed with its dependencies. It is | ||
not mandatory to install them on the same servers as the cozy-stack. And the | ||
URL of RAG must be filled in cozy-stack configuration file (in `rag`). | ||
|
||
For the moment, the feature is experimental, and a trigger must be created | ||
manually on the Cozy: | ||
|
||
```sh | ||
$ COZY=cozy.localhost:8080 | ||
$ TOKEN=$(cozy-stack instances token-cli $COZY io.cozy.triggers) | ||
$ 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"} } } }' | ||
``` | ||
|
||
It can also be a good idea to start a first indexation with: | ||
|
||
```sh | ||
$ cozy-stack triggers launch --domain $COZY $TRIGGER_ID | ||
``` | ||
|
||
In practice, when files are uploaded/modified/deleted, the trigger will create | ||
a job for the index worker (with debounce). The index worker will look at the | ||
changed feed, and will call the RAG for each entry in the changes feed. | ||
|
||
## Chat | ||
|
||
When a user starts a chat, their prompts are sent to the RAG that can use the | ||
vector database to find relevant documents (technically, only some parts of | ||
the documents called chunks). Those documents are added to the prompt, so | ||
that the LLM can use them as a context when answering. | ||
|
||
### POST /ai/chat/conversations/:id | ||
|
||
This route can be used to ask AI for a chat completion. The id in the path | ||
must be the identifier of a chat conversation. The client can generate a random | ||
identifier for a new chat conversation. | ||
|
||
The stack will respond after pushing a job for this task, but without the | ||
response. The client must use the real-time websocket and subscribe to | ||
`io.cozy.ai.chat.events`. | ||
|
||
#### Request | ||
|
||
```http | ||
POST /ai/chat/conversations/e21dce8058b9013d800a18c04daba326 HTTP/1.1 | ||
Content-Type: application/json | ||
``` | ||
|
||
```json | ||
{ | ||
"q": "Why the sky is blue?" | ||
} | ||
``` | ||
|
||
#### Response | ||
|
||
```http | ||
HTTP/1.1 202 Accepted | ||
Content-Type: application/vnd.api+json | ||
``` | ||
|
||
```json | ||
{ | ||
"data": { | ||
"type": "io.cozy.ai.chat.conversations", | ||
"id": "e21dce8058b9013d800a18c04daba326", | ||
"rev": "1-23456", | ||
"attributes": { | ||
"messages": [ | ||
{ | ||
"id": "eb17c3205bf1013ddea018c04daba326", | ||
"role": "user", | ||
"content": "Why the sky is blue?", | ||
"createdAt": "2024-09-24T13:24:07.576Z" | ||
} | ||
] | ||
} | ||
}, | ||
"cozyMetadata": { | ||
"createdAt": "2024-09-24T13:24:07.576Z", | ||
"createdOn": "http://cozy.localhost:8080/", | ||
"doctypeVersion": "1", | ||
"metadataVersion": 1, | ||
"updatedAt": "2024-09-24T13:24:07.576Z" | ||
} | ||
} | ||
``` | ||
|
||
### Real-time via websockets | ||
|
||
``` | ||
client > {"method": "AUTH", "payload": "token"} | ||
client > {"method": "SUBSCRIBE", | ||
"payload": {"type": "io.cozy.ai.chat.events"}} | ||
server > {"event": "CREATED", | ||
"payload": {"id": "eb17c3205bf1013ddea018c04daba326", | ||
"type": "io.cozy.ai.chat.events", | ||
"doc": {"object": "delta", "content": "The ", "position": 0}}} | ||
server > {"event": "CREATED", | ||
"payload": {"id": "eb17c3205bf1013ddea018c04daba326", | ||
"type": "io.cozy.ai.chat.events", | ||
"doc": {"object": "delta", "content": "sky ", "position": 1}}} | ||
[...] | ||
server > {"event": "CREATED", | ||
"payload": {"id": "eb17c3205bf1013ddea018c04daba326", | ||
"type": "io.cozy.ai.chat.events", | ||
"doc": {"object": "done"}}} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# https://d2lang.com/ | ||
|
||
stack: {label: "Cozy-Stack"} | ||
rag: {label: "RAG"} | ||
llm: {label: "LLM"; shape: diamond} | ||
embed: {label: "Embeddings model"; shape: diamond} | ||
vector: {label: "Vector DB"; shape: cylinder} | ||
couchdb: {label: "CouchDB"; shape: cylinder} | ||
swift: {label: "Swift"; shape: cylinder} | ||
|
||
stack -> rag | ||
stack -> couchdb | ||
stack -> swift | ||
|
||
rag -> embed | ||
rag -> vector | ||
rag -> llm |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.