continue-custom/core/context/retrieval.ts
Nate Sesti 41046700d3
Nate/atwoods law (#753)
* 👷 basic skeleton for building with pkg

* ⚡️ first attempt at syncing in .ts

* 🎨 change lancedb storage format

* 🚧 add chunking to LanceDbIndex

* 🔥 move transformers.js from gui to core

* 🐛 fix tsc build

* 🐛 fix bugs loading native modules in vscode extension

* 👷 copy wasms and fix other syncing bugs

* 👷 esbuild modification to allow importMetaUrl for transformers.js

* 👷 add sharp as external module so transformers.js can be loade

* 🐛 more indexing fixes

* ✨ retrieve from lancedb

* 🎨 respect .gitignore when traversing file tree

* 🐛 fix logic bugs in indexing

* 🐛 fix prompt construction bug causing doubling of context items

* 🔥 remove legacy files

* 📌 vendor transformers.js to avoid dependency on sharp

* 👷 package necessary native modules

* 🎨 respect multi-root workspaces when indexing/retrieving

* 🔥 only ship onnxruntime-node binaries for current platform

* 🎨 trigger re-indexing on git branch change

* 🎨 make indexing resumable

* ✨ add folder context provider

* ✨ folders context provider in dropdown

* 🐛 polishing folders context provider
2024-01-16 00:33:02 -08:00

172 lines
4.2 KiB
TypeScript

import {
Chunk,
ContextItem,
ContextProviderExtras,
ILLM,
ModelProvider,
} from "..";
import { ExtensionIde } from "../ide";
import { getBasename } from "../util";
const RERANK_PROMPT = (
query: string,
documentId: string,
document: string
) => `You are an expert software developer responsible for helping detect whether the retrieved snippet of code is relevant to the query. For a given input, you need to output a single word: "Yes" or "No" indicating the retrieved snippet is relevant to the query.
Query: Where is the FastAPI server?
Snippet:
\`\`\`/Users/andrew/Desktop/server/main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {{"Hello": "World"}}
\`\`\`
Relevant: Yes
Query: Where in the documentation does it talk about the UI?
Snippet:
\`\`\`/Users/andrew/Projects/bubble_sort/src/lib.rs
fn bubble_sort<T: Ord>(arr: &mut [T]) {{
for i in 0..arr.len() {{
for j in 1..arr.len() - i {{
if arr[j - 1] > arr[j] {{
arr.swap(j - 1, j);
}}
}}
}}
}}
\`\`\`
Relevant: No
Query: ${query}
Snippet:
\`\`\`${documentId}
${document}
\`\`\`
Relevant:
`;
const PARALLEL_PROVIDERS: ModelProvider[] = [
"anthropic",
"bedrock",
"deepinfra",
"gemini",
"google-palm",
"huggingface-inference-api",
"huggingface-tgi",
"mistral",
"free-trial",
"replicate",
"together",
];
function llmCanGenerateInParallel(llm: ILLM): boolean {
if (llm.providerName === "openai") {
return llm.model.includes("gpt");
}
return PARALLEL_PROVIDERS.includes(llm.providerName);
}
async function scoreChunk(
chunk: Chunk,
llm: ILLM,
query: string
): Promise<number> {
const completion = await llm.complete(
RERANK_PROMPT(query, getBasename(chunk.filepath), chunk.content),
{
maxTokens: 1,
model:
llm.providerName.startsWith("openai") && llm.model.startsWith("gpt-4")
? "gpt-3.5-turbo"
: llm.model,
}
);
if (!completion) {
// TODO: Why is this happening?
return 0.0;
}
let answer = completion
.trim()
.toLowerCase()
.replace(/"/g, "")
.replace(/'/g, "");
if (answer === "yes") {
return 1.0;
} else if (answer === "no") {
return 0.0;
} else {
console.warn(
`Unexpected response from single token reranker: "${answer}". Expected "yes" or "no".`
);
return 0.0;
}
}
async function rerank(
chunks: Chunk[],
llm: ILLM,
query: string,
nFinal: number
): Promise<Chunk[]> {
const scores = await Promise.all(
chunks.map((chunk) => scoreChunk(chunk, llm, query))
);
const sorted = chunks
.map((chunk, i) => ({ chunk, score: scores[i] }))
.sort((a, b) => b.score - a.score);
return sorted.map((s) => s.chunk).slice(0, nFinal);
}
export async function retrieveContextItemsFromEmbeddings(
extras: ContextProviderExtras,
options: any | undefined,
filterDirectory: string | undefined
): Promise<ContextItem[]> {
if (!extras.embeddingsProvider) {
return [];
}
const nRetrieve = options?.nRetrieve || 20;
const nFinal = options?.nFinal || 8;
const useReranking =
llmCanGenerateInParallel(extras.llm) &&
(options?.useReranking === undefined ? false : options?.useReranking);
// Similarity search
let results = await new ExtensionIde().retrieveChunks(
extras.fullInput,
useReranking === false ? nFinal : nRetrieve,
filterDirectory
);
// Re-ranking
if (useReranking) {
results = await rerank(results, extras.llm, extras.fullInput, nFinal);
}
return [
...results.map((r) => {
const name = `${getBasename(r.filepath)} (${r.startLine}-${r.endLine})`;
const description = `${r.filepath} (${r.startLine}-${r.endLine})`;
return {
name,
description,
content: `\`\`\`${name}\n${r.content}\n\`\`\``,
};
}),
{
name: "Instructions",
description: "Instructions",
content:
"Use the above code to answer the following question. You should not reference any files outside of what is shown, unless they are commonly known files, like a .gitignore or package.json. Reference the filenames whenever possible. If there isn't enough information to answer the question, suggest where the user might look to learn more.",
},
];
}