chore: remove docs cache (#7108)
* chore: remove docs cache * Update DocsService.ts
This commit is contained in:
parent
de8755bf59
commit
4eeb34940f
3 changed files with 0 additions and 255 deletions
|
|
@ -1,25 +0,0 @@
|
|||
import OpenAI from "../../llm/llms/OpenAI";
|
||||
import { DocsCache } from "./DocsCache"; // adjust import path as needed
|
||||
|
||||
describe("DocsCache", () => {
|
||||
let openAIEmbeddings: OpenAI;
|
||||
|
||||
beforeAll(() => {
|
||||
openAIEmbeddings = new OpenAI({
|
||||
apiKey: "",
|
||||
model: "text-embedding-ada-002",
|
||||
});
|
||||
});
|
||||
|
||||
test("normalizeEmbeddingId() produces a valid ID without constructor name", async () => {
|
||||
// Get the embedding ID from OpenAI embeddings provider
|
||||
const embeddingId = DocsCache.normalizeEmbeddingId(
|
||||
openAIEmbeddings.embeddingId,
|
||||
);
|
||||
|
||||
// The ID should not contain the constructor name (OpenAI)
|
||||
expect(embeddingId).toEqual(
|
||||
`${openAIEmbeddings.model}::${openAIEmbeddings.maxEmbeddingChunkSize}`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
import request from "request";
|
||||
import { Chunk } from "../..";
|
||||
|
||||
export interface SiteIndexingResults {
|
||||
chunks: (Chunk & { embedding: number[] })[];
|
||||
url: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip off the trailing forward slash from the given URL
|
||||
* @param url The URL of the document
|
||||
* @returns A sanitized URL without trailing forward slash
|
||||
*/
|
||||
const stripTrailingSlash = (url: string): string => {
|
||||
return url.replace(/\/+$/, "");
|
||||
};
|
||||
|
||||
export class DocsCache {
|
||||
static readonly AWS_REGION: string = "us-west-1";
|
||||
static readonly BUCKET_NAME: string = "continue-preindexed-docs";
|
||||
|
||||
/**
|
||||
* Normalizes an embedding ID by stripping the constructor name part.
|
||||
* This is done because we don't care about the provider, just the
|
||||
* model and the max embedding chunk size.
|
||||
*/
|
||||
static normalizeEmbeddingId(embeddingId: string): string {
|
||||
// Split by "::" and remove the first part (constructor name)
|
||||
const parts = embeddingId.split("::");
|
||||
if (parts.length <= 1) return embeddingId; // Return original if no "::" found
|
||||
|
||||
// Return everything except the first part, joining with "::"
|
||||
return parts.slice(1).join("::");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the filepath for a given embedding ID and URL
|
||||
*/
|
||||
static getFilepathForEmbeddingIdAndUrl(
|
||||
embeddingId: string,
|
||||
url: string,
|
||||
): string {
|
||||
const normalizedEmbeddingId = DocsCache.normalizeEmbeddingId(embeddingId);
|
||||
const sanitizedUrl = stripTrailingSlash(url);
|
||||
const normalizedUrl = encodeURIComponent(sanitizedUrl.replace(/\//g, "_"));
|
||||
return normalizedEmbeddingId + "/" + normalizedUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the fully qualified S3 URL for a given filepath
|
||||
*/
|
||||
private static getS3Url(filepath: string): string {
|
||||
const pathname = filepath.split("/").map(encodeURIComponent).join("/");
|
||||
return `https://${this.BUCKET_NAME}.s3.${this.AWS_REGION}.amazonaws.com/${pathname}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads cached site indexing results from S3 for a given embedding ID and URL
|
||||
* @param embeddingId The embedding ID
|
||||
* @param url The URL of the document
|
||||
* @returns The downloaded data as a string
|
||||
*/
|
||||
static async getDocsCacheForUrl(
|
||||
embeddingId: string,
|
||||
url: string,
|
||||
): Promise<string> {
|
||||
const filepath = DocsCache.getFilepathForEmbeddingIdAndUrl(
|
||||
embeddingId,
|
||||
url,
|
||||
);
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
let data = "";
|
||||
const url = this.getS3Url(filepath);
|
||||
const download = request({
|
||||
url,
|
||||
});
|
||||
|
||||
download.on("response", (response: any) => {
|
||||
if (response.statusCode !== 200) {
|
||||
reject(
|
||||
new Error("There was an error retrieving the pre-indexed doc"),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
download.on("error", (err: any) => reject(err));
|
||||
download.on("data", (chunk: any) => (data += chunk));
|
||||
download.on("end", () => resolve(data));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -37,7 +37,6 @@ import { runLanceMigrations, runSqliteMigrations } from "./migrations";
|
|||
|
||||
import type * as LanceType from "vectordb";
|
||||
import { LLMError } from "../../llm";
|
||||
import { DocsCache, SiteIndexingResults } from "./DocsCache";
|
||||
|
||||
// Purposefully lowercase because lancedb converts
|
||||
export interface LanceDbDocsRow {
|
||||
|
|
@ -433,60 +432,6 @@ export default class DocsService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to load document embeddings from cache
|
||||
* @returns true if cache was successfully loaded, false otherwise
|
||||
*/
|
||||
private async tryLoadFromCache(
|
||||
startUrl: string,
|
||||
embeddingId: string,
|
||||
siteIndexingConfig: SiteIndexingConfig,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
// Set a initial status when retrieving cache
|
||||
this.handleStatusUpdate({
|
||||
type: "docs",
|
||||
id: startUrl,
|
||||
embeddingsProviderId: embeddingId,
|
||||
isReindexing: false,
|
||||
title: siteIndexingConfig.title,
|
||||
debugInfo: "Loaded from cache",
|
||||
icon: siteIndexingConfig.faviconUrl,
|
||||
url: startUrl,
|
||||
progress: 0,
|
||||
description: "Try to retrieve cache",
|
||||
status: "indexing",
|
||||
});
|
||||
|
||||
const cacheHit = await this.tryFetchFromCache(startUrl, embeddingId);
|
||||
|
||||
if (cacheHit) {
|
||||
console.log(`Successfully loaded cached embeddings for ${startUrl}`);
|
||||
// Update status to complete
|
||||
this.handleStatusUpdate({
|
||||
type: "docs",
|
||||
id: startUrl,
|
||||
embeddingsProviderId: embeddingId,
|
||||
isReindexing: false,
|
||||
title: siteIndexingConfig.title,
|
||||
debugInfo: "Loaded from cache",
|
||||
icon: siteIndexingConfig.faviconUrl,
|
||||
url: startUrl,
|
||||
progress: 1,
|
||||
description: "Complete",
|
||||
status: "complete",
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(`Error trying to fetch from cache: ${e}`);
|
||||
// Continue with regular indexing
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-statements
|
||||
async indexAndAdd(
|
||||
siteIndexingConfig: SiteIndexingConfig,
|
||||
|
|
@ -508,19 +453,6 @@ export default class DocsService {
|
|||
return;
|
||||
}
|
||||
|
||||
// Try to fetch from cache first before crawling
|
||||
if (!forceReindex) {
|
||||
const cacheLoaded = await this.tryLoadFromCache(
|
||||
startUrl,
|
||||
provider.embeddingId,
|
||||
siteIndexingConfig,
|
||||
);
|
||||
|
||||
if (cacheLoaded) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const startedWithEmbedder = provider.embeddingId;
|
||||
|
||||
// Check if doc has been successfully indexed with the given embedder
|
||||
|
|
@ -811,52 +743,6 @@ export default class DocsService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to fetch embeddings from the S3 cache for any document URL
|
||||
* @param startUrl The URL of the documentation site
|
||||
* @param embeddingsProviderId The ID of the embeddings provider
|
||||
* @returns True if cache hit and successfully loaded, false otherwise
|
||||
*/
|
||||
private async tryFetchFromCache(
|
||||
startUrl: string,
|
||||
embeddingId: string,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const data = await DocsCache.getDocsCacheForUrl(embeddingId, startUrl);
|
||||
|
||||
// Parse the cached data
|
||||
const siteEmbeddings = JSON.parse(data) as SiteIndexingResults;
|
||||
|
||||
// Try to get a favicon for the site
|
||||
const favicon =
|
||||
this.statuses.get(startUrl)?.icon ||
|
||||
(await fetchFavicon(new URL(startUrl)));
|
||||
|
||||
// Always try to make the title match what users set in the config
|
||||
const title =
|
||||
this.statuses.get(startUrl)?.title ||
|
||||
siteEmbeddings.title ||
|
||||
new URL(startUrl).hostname;
|
||||
|
||||
// Add the cached embeddings to our database
|
||||
await this.add({
|
||||
favicon,
|
||||
siteIndexingConfig: {
|
||||
startUrl,
|
||||
title,
|
||||
},
|
||||
chunks: siteEmbeddings.chunks,
|
||||
embeddings: siteEmbeddings.chunks.map((c) => c.embedding),
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
// Cache miss or error - silently fail
|
||||
console.log(`Cache miss for ${startUrl} with provider ${embeddingId}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve docs embeds based on user input
|
||||
async retrieveChunksFromQuery(
|
||||
query: string,
|
||||
|
|
@ -940,7 +826,6 @@ export default class DocsService {
|
|||
}
|
||||
}
|
||||
// This function attempts to retrieve chunks by vector similarity
|
||||
// It will also attempt to fetch from cache if no results are found
|
||||
async retrieveChunks(
|
||||
startUrl: string,
|
||||
vector: number[],
|
||||
|
|
@ -970,28 +855,6 @@ export default class DocsService {
|
|||
console.warn("Error retrieving chunks from LanceDB", e);
|
||||
}
|
||||
|
||||
// If no docs are found and this isn't a retry, try fetching from cache
|
||||
if (docs.length === 0 && !isRetry) {
|
||||
try {
|
||||
// Try to fetch the document from the S3 cache
|
||||
const cacheHit = await this.tryFetchFromCache(
|
||||
startUrl,
|
||||
provider.embeddingId,
|
||||
);
|
||||
|
||||
if (cacheHit) {
|
||||
// If cache hit, refresh the submenu items for docs
|
||||
this.messenger?.send("refreshSubmenuItems", {
|
||||
providers: ["docs"],
|
||||
});
|
||||
// If cache hit, retry the search once
|
||||
return await this.retrieveChunks(startUrl, vector, nRetrieve, true);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Error trying to fetch from cache:", e);
|
||||
}
|
||||
}
|
||||
|
||||
return docs.map(this.lanceDBRowToChunk);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue