⚡️ improve prompting for codebase context
This commit is contained in:
parent
1da38e226d
commit
af6b1b5d8d
4 changed files with 62 additions and 23 deletions
|
|
@ -44,15 +44,23 @@ class CodebaseContextProvider extends BaseContextProvider {
|
|||
// TODO
|
||||
}
|
||||
|
||||
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\`\`\``,
|
||||
};
|
||||
});
|
||||
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.",
|
||||
},
|
||||
];
|
||||
}
|
||||
async load(): Promise<void> {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,17 +4,35 @@ import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider";
|
|||
class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider {
|
||||
static defaultOptions: Partial<EmbedOptions> | undefined = {
|
||||
apiBase: "https://api.openai.com",
|
||||
model: "text-embedding-ada-002",
|
||||
};
|
||||
|
||||
async embed(chunks: string[]) {
|
||||
const resp = await fetch(`${this.options.apiBase}/v1/embeddings`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({}),
|
||||
headers: {
|
||||
Authorization: `Bearer: ${this.options.apiKey}`,
|
||||
},
|
||||
});
|
||||
return [] as number[][];
|
||||
let apiBase = this.options.apiBase;
|
||||
if (apiBase?.endsWith("/")) {
|
||||
apiBase = apiBase.slice(0, -1);
|
||||
}
|
||||
if (apiBase?.endsWith("/v1")) {
|
||||
apiBase = apiBase.slice(0, -3);
|
||||
}
|
||||
|
||||
return await Promise.all(
|
||||
chunks.map(async (chunk) => {
|
||||
const resp = await fetch(`${apiBase}/v1/embeddings`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
input: chunk,
|
||||
model: this.options.model,
|
||||
}),
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.options.apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
const data = await resp.json();
|
||||
return data.data[0].embedding;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ function pruneStringFromBottom(
|
|||
return prompt;
|
||||
}
|
||||
|
||||
return encoding.decode(tokens.slice(-maxTokens));
|
||||
return encoding.decode(tokens.slice(0, maxTokens));
|
||||
}
|
||||
|
||||
function pruneStringFromTop(
|
||||
|
|
@ -77,7 +77,7 @@ function pruneStringFromTop(
|
|||
return prompt;
|
||||
}
|
||||
|
||||
return encoding.decode(tokens.slice(-maxTokens));
|
||||
return encoding.decode(tokens.slice(tokens.length - maxTokens));
|
||||
}
|
||||
|
||||
function pruneRawPromptFromTop(
|
||||
|
|
@ -91,6 +91,17 @@ function pruneRawPromptFromTop(
|
|||
return pruneStringFromTop(modelName, maxTokens, prompt);
|
||||
}
|
||||
|
||||
function pruneRawPromptFromBottom(
|
||||
modelName: string,
|
||||
contextLength: number,
|
||||
prompt: string,
|
||||
tokensForCompletion: number
|
||||
): string {
|
||||
const maxTokens =
|
||||
contextLength - tokensForCompletion - TOKEN_BUFFER_FOR_SAFETY;
|
||||
return pruneStringFromBottom(modelName, maxTokens, prompt);
|
||||
}
|
||||
|
||||
function summarize(message: string): string {
|
||||
return message.substring(0, 100) + "...";
|
||||
}
|
||||
|
|
@ -121,7 +132,7 @@ function pruneChatHistory(
|
|||
);
|
||||
|
||||
for (let i = 0; i < longerThanOneThird.length; i++) {
|
||||
// Prune line-by-line
|
||||
// Prune line-by-line from the top
|
||||
const message = longerThanOneThird[i];
|
||||
let lines = message.content.split("\n");
|
||||
let tokensRemoved = 0;
|
||||
|
|
@ -130,7 +141,7 @@ function pruneChatHistory(
|
|||
totalTokens > contextLength &&
|
||||
lines.length > 0
|
||||
) {
|
||||
const delta = countTokens("\n" + lines.pop()!, modelName);
|
||||
const delta = countTokens("\n" + lines.shift()!, modelName);
|
||||
tokensRemoved += delta;
|
||||
totalTokens -= delta;
|
||||
}
|
||||
|
|
@ -232,7 +243,7 @@ function compileChatMessages(
|
|||
|
||||
if (maxTokens + functionTokens + TOKEN_BUFFER_FOR_SAFETY >= contextLength) {
|
||||
throw new Error(
|
||||
`max_tokens (${maxTokens}) is too close to context_length (${contextLength}), which doesn't leave room for chat. This would cause incoherent responses. Try increasing the context_length parameter of the model in your config file.`
|
||||
`maxTokens (${maxTokens}) is too close to contextLength (${contextLength}), which doesn't leave room for response. Try increasing the contextLength parameter of the model in your config.json.`
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -91,7 +91,9 @@ function resolveParagraph(p: JSONContent): [string, MentionAttrs[], string] {
|
|||
if (child.type === "text") {
|
||||
text += child.text;
|
||||
} else if (child.type === "mention") {
|
||||
text += `@${child.attrs.label}`;
|
||||
if (!["codebase"].includes(child.attrs.id)) {
|
||||
text += child.attrs.label;
|
||||
}
|
||||
contextItems.push(child.attrs);
|
||||
} else if (child.type === "slashcommand") {
|
||||
if (typeof slashCommand === "undefined") {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue