Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Overall Task Context in Code Completion for Better Accuracy #487

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/contributes/codecomplete/promptCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as fs from 'fs/promises';
import * as fsF from 'fs';
import * as path from 'path';
import { logger } from "../../util/logger";
import { getAst, getAstNodeByRange, getTreePathAtCursor, RangeInFileWithContents } from "./ast/ast";
Expand Down Expand Up @@ -507,9 +508,35 @@ export async function findNeighborFileContext(filePath: string, fileContent: str
}
}

// create code task description as code completion context
export async function createTaskDescriptionContext() {
// task description is store in <WORKSPACEDIR>/.chat/complete.config, this config file
// is store as JSON format, and task description is store in "taskDescription" field as string
const workspacePath = UiUtilWrapper.workspaceFoldersFirstPath();
if (!workspacePath) {
return "";
}

const completeConfigPath = path.join(workspacePath, '.chat', 'complete.config');
if (!fsF.existsSync(completeConfigPath)) {
return "";
}

const completeConfig = JSON.parse(fsF.readFileSync(completeConfigPath, { encoding: 'utf8' }));
if (!completeConfig.taskDescription) {
return "";
}

return completeConfig.taskDescription;
}

export async function createPrompt(filePath: string, fileContent: string, line: number, column: number, posoffset: number, recentEdits: RecentEdit[]) {
const commentPrefix = await getCommentPrefix(filePath);

const taskDescriptionContext = await createTaskDescriptionContext();
// taskDescriptionContext is multi lines description, so we need add commentPrefix before each line


let { prefix, suffix } = await currentFileContext(filePath, fileContent, line, column);

let tokenCount = countTokens(prefix);
Expand All @@ -521,6 +548,26 @@ export async function createPrompt(filePath: string, fileContent: string, line:
suffix = "";
}

let taskDescriptionContextWithCommentPrefix = "";
if (tokenCount < CONTEXT_LIMITED_SIZE) {
const taskDescriptionContext = await createTaskDescriptionContext();
// taskDescriptionContext is multi lines description, so we need add commentPrefix before each line
if (taskDescriptionContext) {
taskDescriptionContext.split("\n").forEach(line => {
taskDescriptionContextWithCommentPrefix += `${commentPrefix}${line}\n`;
});

taskDescriptionContextWithCommentPrefix += "\n\n\n\n";
}

const taskDescriptionContextToken = countTokens(taskDescriptionContextWithCommentPrefix);
if (tokenCount + taskDescriptionContextToken < CONTEXT_LIMITED_SIZE) {
tokenCount += taskDescriptionContextToken;
} else {
taskDescriptionContextWithCommentPrefix = "";
}
}

let callDefContext = "";
if (tokenCount < CONTEXT_LIMITED_SIZE) {
const callCodeBlocks = await createContextCallDefine(filePath, fileContent, posoffset);
Expand Down Expand Up @@ -599,7 +646,7 @@ export async function createPrompt(filePath: string, fileContent: string, line:

logger.channel()?.info("Complete token:", tokenCount);

const prompt = "<fim_prefix>" + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + `${commentPrefix}${filePath}\n\n` + prefix + "<fim_suffix>" + suffix + "<fim_middle>";
const prompt = "<fim_prefix>" + taskDescriptionContextWithCommentPrefix + neighborFileContext + recentEditContext + symbolContext + callDefContext + similarBlockContext + `${commentPrefix}${filePath}\n\n` + prefix + "<fim_suffix>" + suffix + "<fim_middle>";
return prompt;
}

Expand Down