Elffuss BraidHub

kikocis/continue public

d691089ac9f7 · 4.0 KiBRawPermalink
1import { BranchAndDir, ContextItem, ContextProviderExtras } from "../../";
2import { getUriDescription } from "../../util/uri";
3import { INSTRUCTIONS_BASE_ITEM } from "../providers/utils";
4
5import { RetrievalPipelineOptions } from "./pipelines/BaseRetrievalPipeline";
6import NoRerankerRetrievalPipeline from "./pipelines/NoRerankerRetrievalPipeline";
7import RerankerRetrievalPipeline from "./pipelines/RerankerRetrievalPipeline";
8
9const DEFAULT_N_FINAL = 25;
10
11export async function retrieveContextItemsFromEmbeddings(
12 extras: ContextProviderExtras,
13 options: any | undefined,
14 filterDirectory: string | undefined,
15): Promise<ContextItem[]> {
16 // Currently you can use codebase without an embeddings provider and it will just skip the embeddings inputs
17 // if (!extras.embeddingsProvider) {
18 // void extras.ide.showToast(
19 // "warning",
20 // "Set up an embeddings model to use this feature. Visit the docs to learn more: " +
21 // "https://docs.continue.dev/customize/model-roles/embeddings",
22 // );
23 // return [];
24 // }
25 const includeEmbeddings = !!extras.config.selectedModelByRole.embed;
26
27 // Get tags to retrieve for
28 const workspaceDirs = await extras.ide.getWorkspaceDirs();
29
30 if (workspaceDirs.length === 0) {
31 throw new Error("No workspace directories found");
32 }
33
34 // Fill half of the context length, up to a max of 100 snippets
35 const contextLength = extras.llm.contextLength;
36 const tokensPerSnippet = 512;
37 const nFinal =
38 options?.nFinal ??
39 Math.min(DEFAULT_N_FINAL, contextLength / tokensPerSnippet / 2);
40 const useReranking = !!extras.reranker;
41 const nRetrieve = useReranking ? options?.nRetrieve || 2 * nFinal : nFinal;
42
43 const branches = (await Promise.race([
44 Promise.all(workspaceDirs.map((dir) => extras.ide.getBranch(dir))),
45 new Promise((resolve) => {
46 setTimeout(() => {
47 resolve(["NONE"]);
48 }, 500);
49 }),
50 ])) as string[];
51
52 const tags: BranchAndDir[] = workspaceDirs.map((directory, i) => ({
53 directory,
54 branch: branches[i],
55 }));
56
57 const pipelineType = useReranking
58 ? RerankerRetrievalPipeline
59 : NoRerankerRetrievalPipeline;
60
61 const pipelineOptions: RetrievalPipelineOptions = {
62 nFinal,
63 nRetrieve,
64 tags,
65 filterDirectory,
66 ide: extras.ide,
67 input: extras.fullInput,
68 llm: extras.llm,
69 config: extras.config,
70 };
71
72 const pipeline = new pipelineType(pipelineOptions);
73 const results = await pipeline.run({
74 tags,
75 filterDirectory,
76 query: extras.fullInput,
77 includeEmbeddings,
78 });
79
80 if (results.length === 0) {
81 if (extras.config.disableIndexing) {
82 void extras.ide.showToast("warning", "No results found.");
83 return [];
84 } else {
85 void extras.ide.showToast(
86 "warning",
87 "No results found. If you think this is an error, re-index your codebase.",
88 );
89 // TODO - add "re-index" option to warning message which clears and reindexes codebase
90 }
91 return [];
92 }
93
94 return [
95 {
96 ...INSTRUCTIONS_BASE_ITEM,
97 content:
98 "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.",
99 },
100 ...results
101 .sort((a, b) => a.filepath.localeCompare(b.filepath))
102 .map((r) => {
103 const { relativePathOrBasename, last2Parts, baseName } =
104 getUriDescription(r.filepath, workspaceDirs);
105
106 if (baseName === "package.json") {
107 console.warn("Retrieval pipeline: package.json detected");
108 }
109
110 return {
111 name: `${baseName} (${r.startLine + 1}-${r.endLine + 1})`,
112 description: last2Parts,
113 content: `\`\`\`${relativePathOrBasename}\n${r.content}\n\`\`\``,
114 uri: {
115 type: "file" as const,
116 value: r.filepath,
117 },
118 };
119 }),
120 ];
121}