twinnydotdev / twinny

The most no-nonsense, locally or API-hosted AI code completion plugin for Visual Studio Code - like GitHub Copilot but completely free and 100% private.
https://twinny.dev
MIT License
2.3k stars 126 forks source link

Document RAG #262

Open h3x4g0ns opened 3 weeks ago

h3x4g0ns commented 3 weeks ago

Is your feature request related to a problem? Please describe. I understand the the current approach uses some heuristics like document title or something in order to determine with documents are relevant for context. This is obviously a naive approach and doesn't capture the nuances and subtleties when trying to ask questions or perform systems design based on your existing code.

Describe the solution you'd like Let's implement document RAG. We need 2 elements with RAG: 1) knowledge persistency and 2) knowledge retrieval.

1) Knowledge Persistency: We need to treat each file as document containing relevant information. For this we need to store it's embedding (which serves as a document hash) and the actual content. The easiest way to do this is use Ollama's newest embedding feature and register callback for editor activity such once changes have been made a document and we detect a silent period, we feed the entire document through the embedding model and return it's vector. In terms of data persistency, it becomes trivial to maintain a local storage dump (ideally we use Postgres) to main a key value store of embedding to document paths. We would probably maintain an index per workspace, and there would be a delay in building an index from scratch when loading a new workspace.

2) Knowledge Retrieval: Upon prompting a completion query, we can embed the query and use nearest neighbors to determine which documents are the most relevant in the completion of the request. Based on the input context length of the model selected, we can select how many of these chunks we want to incorporate as context. We can use more complex heuristics to rank these chunks down the line.

Describe alternatives you've considered

I'm a huge fan of this extension, it's game changing! Even Copilot is quite irritating to use because it's RAG just uses context based on where the cursor is. Incorporating this feature would be game changing IMHO.

rjmacarthy commented 1 week ago

Hello @h3x4g0ns this feature is has been underway for a while please stay turned for updates, many thanks.