🚧 end-to-end docs retrieval
This commit is contained in:
parent
74a4539bfa
commit
7e075c0e02
8 changed files with 83 additions and 36 deletions
|
|
@ -19,7 +19,14 @@ class DocsContextProvider extends BaseContextProvider {
|
|||
query: string,
|
||||
extras: ContextProviderExtras
|
||||
): Promise<ContextItem[]> {
|
||||
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(
|
||||
|
|
|
|||
2
core/index.d.ts
vendored
2
core/index.d.ts
vendored
|
|
@ -77,7 +77,7 @@ export interface ContextProviderDescription {
|
|||
|
||||
export interface ContextProviderExtras {
|
||||
fullInput: string;
|
||||
embeddingsProvider?: EmbeddingsProvider;
|
||||
embeddingsProvider: EmbeddingsProvider;
|
||||
llm: ILLM;
|
||||
ide: IDE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ export async function* markdownChunker(
|
|||
return;
|
||||
} else if (hLevel > 4) {
|
||||
yield* basicChunker(content, maxChunkSize);
|
||||
return;
|
||||
}
|
||||
|
||||
const h = "#".repeat(hLevel + 1) + " ";
|
||||
|
|
|
|||
|
|
@ -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<string>) {
|
|||
|
||||
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;
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -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<sqlite3.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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<IndexingProgressUpdate> {
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue