From ed62f18e7d5c8e29324f866791f9a4f564169f25 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 13:06:54 -0700 Subject: [PATCH 1/3] update docs to explain chromium --- docs/docs/features/talk-to-your-docs.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/docs/docs/features/talk-to-your-docs.md b/docs/docs/features/talk-to-your-docs.md index d1088b4ea..01596e800 100644 --- a/docs/docs/features/talk-to-your-docs.md +++ b/docs/docs/features/talk-to-your-docs.md @@ -37,7 +37,7 @@ We offer a selection of pre-indexed documentation sites for popular frameworks a ### Through the `@docs` Context Provider -To add a single documentation site, we recommend going through the `@docs` context provider. +To add a single documentation site, we recommend using the `@docs` context provider. 1. Type `@docs` in the chat panel, hit enter 2. Type "add" and select the "Add Docs" option @@ -58,7 +58,6 @@ The configuration schema is as follows: "startUrl": "https://docs.continue.dev/intro", "rootUrl": "https://docs.continue.dev", "faviconUrl": "https://docs.continue.dev/favicon.ico", - "maxDepth": 3 } ] ``` @@ -67,18 +66,21 @@ The configuration schema is as follows: - `startUrl`: The URL where the indexing process should begin. - `rootUrl`: The base URL of the documentation site, used to determine which pages to index. - `faviconUrl`: The URL of the site's favicon, used for visual identification in the UI. -- `maxDepth`: The maximum number of levels deep the indexer should crawl from the start URL. + +## Crawling dynamically generated sites with `useChromiumForDocsCrawling` + +By default, we use a lighter weight tool to crawl documentation sites that cannot render sites that are dynamically generated using JavaScript. + +If you want to crawl a site that is dynamically generated, or you get an error while attempting to crawl a site, you can enable the experimental `useChromiumForDocsCrawling` feature in your `config.json`. This will download and install Chromium to `~/.continue/.utils`. + +```json title=config.json +"experimental": { + "useChromiumForDocsCrawling": true +} +``` ## FAQ -### Why did my documentation site fail to index? - -Our current crawler is designed to work with static websites and may encounter issues with dynamic or JavaScript-heavy sites. If your documentation site failed to index, it's possible that it contains non-static elements that our crawler couldn't process. - -If you're experiencing issues with indexing a documentation site, please let us know. We're continuously improving our indexing capabilities and would appreciate your feedback to help us enhance the feature. - -Report Indexing Issues on Discord - ### How often is the indexed content updated? Currently we do not automatically re-index your docs. If you would like to force a re-index, you can use the following command: `Continue: Docs Force Re-Index`. From 2040f86899bf6c6cb7a8fcc6c758456b828656c2 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 19:42:42 -0700 Subject: [PATCH 2/3] hotfix: CRLF bug in diff streaming --- .changes/extensions/vscode/0.8.51.md | 3 +++ core/util/verticalEdit.ts | 3 +++ extensions/vscode/CHANGELOG.md | 4 ++++ extensions/vscode/package.json | 2 +- 4 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changes/extensions/vscode/0.8.51.md diff --git a/.changes/extensions/vscode/0.8.51.md b/.changes/extensions/vscode/0.8.51.md new file mode 100644 index 000000000..2fbbbeb96 --- /dev/null +++ b/.changes/extensions/vscode/0.8.51.md @@ -0,0 +1,3 @@ +## 0.8.51 - 2024-09-05 +### Fixed +* Fixed CRLF bug causing diff streams to treat every line as changed on Windows diff --git a/core/util/verticalEdit.ts b/core/util/verticalEdit.ts index 0a32a6a5b..34a17ebd4 100644 --- a/core/util/verticalEdit.ts +++ b/core/util/verticalEdit.ts @@ -81,6 +81,9 @@ export async function* streamDiffLines( oldLines = []; } + // Trim end of oldLines, otherwise we have trailing \r on every line for CRLF files + oldLines = oldLines.map((line) => line.trimEnd()); + const prompt = constructPrompt( prefix, highlighted, diff --git a/extensions/vscode/CHANGELOG.md b/extensions/vscode/CHANGELOG.md index 70a854996..8ec740e4c 100644 --- a/extensions/vscode/CHANGELOG.md +++ b/extensions/vscode/CHANGELOG.md @@ -5,6 +5,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), and is generated by [Changie](https://github.com/miniscruff/changie). +## 0.8.51 - 2024-09-05 +### Fixed +* Fixed CRLF bug causing diff streams to treat every line as changed on Windows + ## 0.8.50 - 2024-09-02 ### Fixed * Hotfix for ability to use more than one inline context provider diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 5335c91b6..51c2eebc7 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -2,7 +2,7 @@ "name": "continue", "icon": "media/icon.png", "author": "Continue Dev, Inc", - "version": "0.9.206", + "version": "0.8.51", "repository": { "type": "git", "url": "https://github.com/continuedev/continue" From 3791774e45ae87821f6539f38da5c0752d63fe4f Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 9 Sep 2024 17:46:38 -0700 Subject: [PATCH 3/3] custom code rag docs --- docs/docs/walkthroughs/custom-code-rag.md | 154 ++++++++++++++++++++++ docs/sidebars.js | 6 +- 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 docs/docs/walkthroughs/custom-code-rag.md diff --git a/docs/docs/walkthroughs/custom-code-rag.md b/docs/docs/walkthroughs/custom-code-rag.md new file mode 100644 index 000000000..cf81a38ca --- /dev/null +++ b/docs/docs/walkthroughs/custom-code-rag.md @@ -0,0 +1,154 @@ +--- +title: Custom code RAG +description: Learn how to set up your own vector database and build a custom retrieval-augmented generation (RAG) system for code. +keywords: [codebase, rag, vector database, custom] +--- + +# Custom code RAG + +While Continue comes with [@codebase](../features/codebase-embeddings.md) out of the box, you might wish to set up your own vector database and build a custom retrieval-augmented generation (RAG) system. This can allow you to access code that is not available locally, to index code a single time across all users, or to include custom logic. In this guide, we'll walk you through the steps it takes to build this. + +## Step 1: Choose an embeddings model + +If possible, we recommend using [`voyage-code-2`](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. + +## Step 2: Choose a vector database + +There 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. + +[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. + +## Step 3: Choose a "chunking" strategy + +Most embeddings models can only handle a limited amount of text at once. To get around this, we "chunk" our code into smaller pieces. + +If you use `voyage-code-2`, 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: + +1. Truncate the file when it goes over the context length: in this case you will always have 1 chunk per file. +2. Split the file into chunks of a fixed length: starting at the top of the file, add lines you your current chunk until it reaches the limit, then start a new chunk. +3. 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/). + +As usual in this guide, we recommend starting with the strategy that gives 80% of the benefit with 20% of the effort. + +## Step 4: Put together an indexing script + +Indexing, in which we will insert your code into the vector database in a retrievable format, happens in three steps: + +1. Chunking +2. Generating embeddings +3. Inserting into the vector database + +With 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: + +```python +from lancedb.pydantic import LanceModel, Vector +from lancedb.embeddings import get_registry + +db = lancedb.connect("/tmp/db") +func = get_registry().get("openai").create( + name="voyage-code-2", + base_url="https://api.voyageai.com/v1/", + api_key=os.environ["VOYAGE_API_KEY"], +) + +class CodeChunks(LanceModel): + filename: str + text: str = func.SourceField() + vector: Vector(func.ndims()) = 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) +``` + +:::tip + +If you are indexing more than one repository, it is best to store these in separate "tables" (terminology used by LanceDB) or "collections" (terminology used by some other vector DBs). The alternative of adding a "repository" field and then filtering by this is less performant. + +::: + +Regardless 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. + +## Step 5: Run your indexing script + +:::tip + +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. + +That said, we highly recommend first building and testing the pipeline before attempting this. Unless your codebase is being entirely rewritten frequently, a full refresh of the index is likely to be sufficient and reasonably cheap. +::: + +At 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. + +In 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. + +## Step 6: Set up your server + +In order for the Continue extension to access your custom RAG system, you'll need to set up a server. This server is responsible for recieving a query from the extension, querying the vector database, and returning the results in the format expected by Continue. + +Here is a reference implementation using FastAPI that is capable of handling requests from Continue's "http" context provider. + +```python +""" +This is an example of a server that can be used with the "http" context provider. +""" + +from fastapi import FastAPI +from pydantic import BaseModel + + +class ContextProviderInput(BaseModel): + query: str + fullInput: str + + +app = FastAPI() + + +@app.post("/retrieve") +async def create_item(item: ContextProviderInput): + results = [] # TODO: Query your vector database here. + + # Construct the "context item" format expected by Continue + context_items = [] + for result in results: + context_items.append({ + "name": result.filename, + "description": result.filename, + "content": result.text, + }) + + return context_items +``` + +After you've set up your server, you can configure Continue to use it by adding the "http" context provider to your `contextProviders` array in `config.json`: + +```json title="config.json" +{ + "name": "http", + "params": { + "url": "https://myserver.com/retrieve", + "title": "http", + "description": "Custom HTTP Context Provider", + "displayTitle": "My Custom Context" + } +} +``` + +## Step 7 (Bonus): Set up reranking + +If 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. + +If you wish to return 10 total results for each query for example, then you would: + +1. Retrieve ~50 results from the vector database using similarity search +2. Send all of these 50 results to the reranker API along with the query in order to get relevancy scores for each +3. Sort the results by relevancy score and return the top 10 + +We recommend using the `rerank-1` model from Voyage AI, which has examples of usage [here](https://docs.voyageai.com/docs/reranker). diff --git a/docs/sidebars.js b/docs/sidebars.js index b569b31d8..ce6d985f7 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -87,7 +87,11 @@ const sidebars = { label: "🚶 Walkthroughs", collapsible: true, collapsed: true, - items: ["walkthroughs/set-up-codestral", "walkthroughs/llama3.1"], + items: [ + "walkthroughs/set-up-codestral", + "walkthroughs/llama3.1", + "walkthroughs/custom-code-rag", + ], }, { type: "category",