From 7e075c0e02fc102592ce6ebf5e18da8dfa871876 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 20 Jan 2024 22:13:05 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20end-to-end=20docs=20retrieval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/context/providers/DocsContextProvider.ts | 9 +++- core/index.d.ts | 2 +- core/indexing/chunk/markdown.ts | 1 + core/indexing/docs/crawl.ts | 42 +++++++++---------- core/indexing/docs/db.ts | 23 ++++++---- core/indexing/docs/index.ts | 16 +++++-- core/util/paths.ts | 4 ++ extensions/vscode/src/activation/activate.ts | 22 ++++++++++ 8 files changed, 83 insertions(+), 36 deletions(-) diff --git a/core/context/providers/DocsContextProvider.ts b/core/context/providers/DocsContextProvider.ts index d1879f1a9..29a0cf943 100644 --- a/core/context/providers/DocsContextProvider.ts +++ b/core/context/providers/DocsContextProvider.ts @@ -19,7 +19,14 @@ class DocsContextProvider extends BaseContextProvider { query: string, extras: ContextProviderExtras ): Promise { - return []; + const { retrieveDocs } = await import("../../indexing/docs/db"); + const [vector] = await extras.embeddingsProvider.embed([query]); + const chunks = await retrieveDocs(query, vector); + return chunks.map((chunk) => ({ + name: chunk.filepath, + description: chunk.filepath, + content: chunk.content, + })); } async loadSubmenuItems( diff --git a/core/index.d.ts b/core/index.d.ts index c1a50fa40..3f06edf7e 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -77,7 +77,7 @@ export interface ContextProviderDescription { export interface ContextProviderExtras { fullInput: string; - embeddingsProvider?: EmbeddingsProvider; + embeddingsProvider: EmbeddingsProvider; llm: ILLM; ide: IDE; } diff --git a/core/indexing/chunk/markdown.ts b/core/indexing/chunk/markdown.ts index 9d5c385a4..e690044b6 100644 --- a/core/indexing/chunk/markdown.ts +++ b/core/indexing/chunk/markdown.ts @@ -16,6 +16,7 @@ export async function* markdownChunker( return; } else if (hLevel > 4) { yield* basicChunker(content, maxChunkSize); + return; } const h = "#".repeat(hLevel + 1) + " "; diff --git a/core/indexing/docs/crawl.ts b/core/indexing/docs/crawl.ts index 909483b0c..5cae6d9ce 100644 --- a/core/indexing/docs/crawl.ts +++ b/core/indexing/docs/crawl.ts @@ -1,8 +1,7 @@ import cheerio from "cheerio"; -import $ from "jquery"; import fetch from "node-fetch"; import { NodeHtmlMarkdown } from "node-html-markdown"; -const HCCrawler = require("headless-chrome-crawler"); +// const HCCrawler = require("headless-chrome-crawler"); const IGNORE_PATHS_ENDING_IN = ["favicon.ico", "robots.txt", ".rst.txt"]; @@ -20,7 +19,6 @@ async function crawlLinks(path: string, baseUrl: URL, visited: Set) { const response = await fetch(new URL(path, baseUrl)); const text = await response.text(); - console.log(text); const $ = cheerio.load(text); const children: string[] = []; @@ -99,23 +97,23 @@ const url2 = new URL("https://platform.openai.com/docs/api-reference"); // console.log(visited); // }); -async function hcCrawlLinks() { - const results: any[] = []; - const crawler = await HCCrawler.launch({ - // Function to be evaluated in browsers - evaluatePage: () => ({ - title: $("title").text(), - }), - // Function to be called with evaluated results from browsers - onSuccess: (result: any) => { - console.log(result); - results.push(result.url); - }, - }); - // Queue a request - await crawler.queue(url.toString()); - await crawler.onIdle(); // Resolved when no queue is left - await crawler.close(); // Close the crawler +// async function hcCrawlLinks() { +// const results: any[] = []; +// const crawler = await HCCrawler.launch({ +// // Function to be evaluated in browsers +// evaluatePage: () => ({ +// title: $("title").text(), +// }), +// // Function to be called with evaluated results from browsers +// onSuccess: (result: any) => { +// console.log(result); +// results.push(result.url); +// }, +// }); +// // Queue a request +// await crawler.queue(url.toString()); +// await crawler.onIdle(); // Resolved when no queue is left +// await crawler.close(); // Close the crawler - return results; -} +// return results; +// } diff --git a/core/indexing/docs/db.ts b/core/indexing/docs/db.ts index e16dff4cc..5d0cd2e3d 100644 --- a/core/indexing/docs/db.ts +++ b/core/indexing/docs/db.ts @@ -1,8 +1,8 @@ -import { open } from "sqlite"; +import { Database, open } from "sqlite"; import sqlite3 from "sqlite3"; import * as lancedb from "vectordb"; import { Chunk } from "../.."; -import { getIndexSqlitePath, getLanceDbPath } from "../../util/paths"; +import { getDocsSqlitePath, getLanceDbPath } from "../../util/paths"; const DOCS_TABLE_NAME = "docs"; @@ -18,6 +18,14 @@ interface LanceDbDocsRow { [key: string]: any; } +async function createDocsTable(db: Database) { + db.exec(`CREATE TABLE IF NOT EXISTS docs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title STRING NOT NULL, + baseUrl STRING NOT NULL + )`); +} + export async function retrieveDocs( baseUrl: string, vector: number[] @@ -47,14 +55,10 @@ export async function addDocs( embeddings: number[][] ) { const db = await open({ - filename: getIndexSqlitePath(), + filename: getDocsSqlitePath(), driver: sqlite3.Database, }); - await db.exec(`CREATE TABLE IF NOT EXISTS docs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - title STRING NOT NULL, - baseUrl STRING NOT NULL - )`); + await createDocsTable(db); await db.run( `INSERT INTO docs (title, baseUrl) VALUES (?, ?)`, title, @@ -85,9 +89,10 @@ export async function listDocs(): Promise< { title: string; baseUrl: string }[] > { const db = await open({ - filename: getIndexSqlitePath(), + filename: getDocsSqlitePath(), driver: sqlite3.Database, }); + await createDocsTable(db); const docs = await db.all(`SELECT title, baseUrl FROM docs`); return docs; } diff --git a/core/indexing/docs/index.ts b/core/indexing/docs/index.ts index 9cee2c5cb..fc30b05e0 100644 --- a/core/indexing/docs/index.ts +++ b/core/indexing/docs/index.ts @@ -7,19 +7,29 @@ import { import { MAX_CHUNK_SIZE } from "../../llm/constants"; import { markdownChunker } from "../chunk/markdown"; import { convertURLToMarkdown, crawlSubpages } from "./crawl"; -import { addDocs } from "./db"; +import { addDocs, listDocs } from "./db"; export async function* indexDocs( title: string, baseUrl: URL, embeddingsProvider: EmbeddingsProvider ): AsyncGenerator { + const existingDocs = await listDocs(); + if (existingDocs.find((doc) => doc.title === title)) { + yield { + progress: 1, + desc: "Already indexed", + }; + return; + } + yield { progress: 0, desc: "Finding subpages...", }; const subpaths = await crawlSubpages(baseUrl); + console.log("Found subpaths", subpaths); const chunks: Chunk[] = []; const embeddings: number[][] = []; @@ -38,7 +48,7 @@ export async function* indexDocs( markdownChunks.push(chunk); } - const embeddings = await embeddingsProvider.embed( + const subpathEmbeddings = await embeddingsProvider.embed( markdownChunks.map((chunk) => chunk.content) ); @@ -50,7 +60,7 @@ export async function* indexDocs( digest: subpath, }); }); - embeddings.push(...embeddings); + embeddings.push(...subpathEmbeddings); } await addDocs(title, baseUrl, chunks, embeddings); diff --git a/core/util/paths.ts b/core/util/paths.ts index 4b383e87d..89073ea7b 100644 --- a/core/util/paths.ts +++ b/core/util/paths.ts @@ -175,3 +175,7 @@ export function getIndexSqlitePath(): string { export function getLanceDbPath(): string { return path.join(getIndexFolderPath(), "lancedb"); } + +export function getDocsSqlitePath(): string { + return path.join(getIndexFolderPath(), "docs.sqlite"); +} diff --git a/extensions/vscode/src/activation/activate.ts b/extensions/vscode/src/activation/activate.ts index 0c3a51fb8..8ea4e3207 100644 --- a/extensions/vscode/src/activation/activate.ts +++ b/extensions/vscode/src/activation/activate.ts @@ -1,3 +1,4 @@ +import { indexDocs } from "core/indexing/docs"; import { getTsConfigPath } from "core/util/paths"; import * as fs from "fs"; import path from "path"; @@ -6,8 +7,10 @@ import * as vscode from "vscode"; import { registerAllCommands } from "../commands"; import IdeProtocolClient from "../continueIdeClient"; import { ContinueGUIWebviewViewProvider } from "../debugPanel"; +import { VsCodeIde } from "../ideProtocol"; import registerQuickFixProvider from "../lang-server/codeActions"; import { registerAllCodeLensProviders } from "../lang-server/codeLens"; +import { configHandler } from "../loadConfig"; import { vsCodeIndexCodebase } from "../util/indexCodebase"; import { getExtensionUri } from "../util/vscode"; import { setupInlineTips } from "./inlineTips"; @@ -91,6 +94,25 @@ export async function activateExtension(context: vscode.ExtensionContext) { startProxy(); vsCodeIndexCodebase(ideProtocolClient.getWorkspaceDirectories()); + (async () => { + const defaultDocsPages = [ + ["Socket.IO", "https://python-socketio.readthedocs.io/en/stable"], + // ["Flask", "https://flask.palletsprojects.com/en/2.0.x/"], + ]; + + const config = await configHandler.loadConfig(new VsCodeIde()); + + defaultDocsPages.forEach(async ([title, url]) => { + for await (const update of indexDocs( + title, + new URL(url), + config.embeddingsProvider + )) { + console.log(update.progress, update.desc); + } + }); + })(); + try { // Add icon theme for .continueignore const iconTheme = vscode.workspace