Elffuss BraidHub

kikocis/continue public

main · 8.8 KiBRawPermalink
1---
2title: "How to Build Custom Code RAG"
3description: "Build a custom retrieval-augmented generation (RAG) system for faster and more cost-efficient code search across large codebases. This guide is for advanced users who need to index code a single time across all users or include custom logic."
4keywords: [RAG, vector database, embeddings, code search, MCP]
5---
6
7## Step 1: How to Choose an Embeddings Model
8
9If possible, we recommend using [`voyage-code-3`](https://docs.voyageai.com/docs/embeddings), which will give the most accurate answers of any existing embeddings model for code. You can obtain an API key [here](https://dash.voyageai.com/api-keys). Because their API is [OpenAI-compatible](https://docs.voyageai.com/reference/embeddings-api), you can use any OpenAI client by swapping out the URL.
10
11## Step 2: How to Choose a Vector Database
12
13There are a number of available vector databases, but because most vector databases will be able to performantly handle large codebases, we would recommend choosing one for ease of setup and experimentation.
14
15[LanceDB](https://lancedb.github.io/lancedb/basic/) is a good choice for this because it can run in-memory with libraries for both Python and Node.js. This means that in the beginning you can focus on writing code rather than setting up infrastructure. If you have already chosen a vector database, then using this instead of LanceDB is also a fine choice.
16
17## Step 3: How to Choose a "Chunking" Strategy
18
19Most embeddings models can only handle a limited amount of text at once. To get around this, we "chunk" our code into smaller pieces.
20
21If you use `voyage-code-3`, it has a maximum context length of 16,000 tokens, which is enough to fit most files. This means that in the beginning you can get away with a more naive strategy of truncating files that exceed the limit. In order of easiest to most comprehensive, 3 chunking strategies you can use are:
22
231. Truncate the file when it goes over the context length: in this case you will always have 1 chunk per file.
242. Split the file into chunks of a fixed length: starting at the top of the file, add lines in your current chunk until it reaches the limit, then start a new chunk.
253. Use a recursive, abstract syntax tree (AST)-based strategy: this is the most exact, but most complex. In most cases you can achieve high quality results by using (1) or (2), but if you'd like to try this you can find a reference example in [our code chunker](https://github.com/continuedev/continue/blob/main/core/indexing/chunk/code.ts) or in [LlamaIndex](https://docs.llamaindex.ai/en/stable/api_reference/node_parsers/code/).
26
27As usual in this guide, we recommend starting with the strategy that gives 80% of the benefit with 20% of the effort.
28
29## Step 4: How to Put Together an Indexing Script
30
31Indexing, in which we will insert your code into the vector database in a retrievable format, happens in three steps:
32
331. Chunking
342. Generating embeddings
353. Inserting into the vector database
36
37With LanceDB, we can do steps 2 and 3 simultaneously, as demonstrated [in their docs](https://lancedb.github.io/lancedb/basic/#using-the-embedding-api). If you are using Voyage AI for example, it would be configured like this:
38
39```
40from lancedb.pydantic import LanceModel, Vectorfrom lancedb.embeddings import get_registrydb = lancedb.connect("/tmp/db")func = get_registry().get("openai").create( name="voyage-code-3", base_url="https://api.voyageai.com/v1/", api_key=os.environ["VOYAGE_API_KEY"],)class CodeChunks(LanceModel): filename: str text: str = func.SourceField() # 1024 is the default dimension for `voyage-code-3`: https://docs.voyageai.com/docs/embeddings#model-choices vector: Vector(1024) = func.VectorField()table = db.create_table("code_chunks", schema=CodeChunks, mode="overwrite")table.add([ {"text": "print('hello world!')", filename: "hello.py"}, {"text": "print('goodbye world!')", filename: "goodbye.py"}])query = "greetings"actual = table.search(query).limit(1).to_pydantic(CodeChunks)[0]print(actual.text)
41```
42
43<Check>
44 If you are indexing more than one repository, it is best to store these in
45 separate "tables" (terminology used by LanceDB) or "collections" (terminology
46 used by some other vector DBs). The alternative of adding a "repository" field
47 and then filtering by this is less performant.
48</Check>
49
50Regardless of which database or model you have chosen, your script should iterate over all of the files that you wish to index, chunk them, generate embeddings for each chunk, and then insert all of the chunks into your vector database.
51
52## Step 5: How to Run Your Indexing Script
53
54<Check>
55 In a perfect production version, you would want to build "automatic, incremental indexing", so that you whenever a file changes, that file and nothing else is automatically re-indexed. This has the benefits of perfectly up-to-date embeddings and lower cost.
56
57That said, we highly recommend first building and testing the pipeline before attempting this. Unless your codebase is being entirely rewritten frequently, an incremental refresh of the index is likely to be sufficient and reasonably cheap.
58
59</Check>
60
61At this point, you've written your indexing script and tested that you can make queries from your vector database. Now, you'll want a plan for when to run the indexing script.
62
63In the beginning, you should probably run it by hand. Once you are confident that your custom RAG is providing value and is ready for the long-term, then you can set up a cron job to run it periodically. Because codebases are largely unchanged in short time frames, you won't want to re-index more than once a day. Once per week or month is probably even sufficient.
64
65## Step 6: How to set up an MCP server
66
67To integrate your custom RAG system with Continue, you'll create an MCP (Model Context Protocol) server. MCP provides a standardized way for AI tools to access external resources.
68
69### Create your MCP server
70
71Here's a reference implementation using Python that queries your vector database:
72
73```python
74"""Custom RAG MCP server for code retrieval"""
75import asyncio
76from mcp.server import Server
77from mcp.server.stdio import stdio_server
78from mcp.types import Tool, TextContent
79import lancedb
80
81# Initialize your vector database connection
82db = lancedb.connect("/path/to/your/db")
83table = db.open_table("code_chunks")
84
85app = Server("custom-rag-server")
86
87@app.tool()
88async def search_codebase(query: str, limit: int = 10) -> list[TextContent]:
89 """
90 Search the codebase using vector similarity.
91
92 Args:
93 query: The search query
94 limit: Maximum number of results to return
95 """
96 # Query your vector database
97 results = table.search(query).limit(limit).to_list()
98
99 # Format results for Continue
100 formatted_results = []
101 for result in results:
102 formatted_results.append(TextContent(
103 type="text",
104 text=f"File: {result['filename']}\n\n{result['text']}"
105 ))
106
107 return formatted_results
108
109@app.tool()
110async def get_file_context(filename: str) -> list[TextContent]:
111 """
112 Get all chunks from a specific file.
113
114 Args:
115 filename: The name of the file to retrieve
116 """
117 results = table.where(f"filename = '{filename}'").to_list()
118
119 return [TextContent(
120 type="text",
121 text="\n".join([r['text'] for r in results])
122 )]
123
124if __name__ == "__main__":
125 stdio_server(app).run()
126```
127
128### Configure Continue to use your MCP server
129
130Add your MCP server to Continue's configuration:
131
132**config.yaml:**
133```yaml
134mcpServers:
135 - name: custom-rag
136 command: python
137 args:
138 - /path/to/your/mcp_server.py
139 env:
140 VOYAGE_API_KEY: ${VOYAGE_API_KEY}
141```
142
143**config.json:**
144```json
145{
146 "mcpServers": [
147 {
148 "name": "custom-rag",
149 "command": "python",
150 "args": ["/path/to/your/mcp_server.py"],
151 "env": {
152 "VOYAGE_API_KEY": "${VOYAGE_API_KEY}"
153 }
154 }
155 ]
156}
157```
158
159## Step 7 (Bonus): How to Set Up Reranking
160
161If you'd like to improve the quality of your results, a great first step is to add reranking. This involves retrieving a larger initial pool of results from the vector database, and then using a reranking model to order them from most to least relevant. This works because the reranking model can perform a slightly more expensive calculation on the small set of top results, and so can give a more accurate ordering than similarity search, which has to search over all entries in the database.
162
163If you wish to return 10 total results for each query for example, then you would:
164
1651. Retrieve \~50 results from the vector database using similarity search
1662. Send all of these 50 results to the reranker API along with the query in order to get relevancy scores for each
1673. Sort the results by relevancy score and return the top 10
168
169We recommend using the `rerank-2` model from Voyage AI, which has examples of usage [here](https://docs.voyageai.com/docs/reranker).