Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from asapelkin/prompt_optimisation
Browse files Browse the repository at this point in the history
[Prompt optimizatoion] Use part of prompt for first lines of file
  • Loading branch information
Venthe authored Jul 21, 2023
2 parents 1f0893c + db839cf commit 7fb9a0c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Proportion of lines from the beginning of the file in the prompt
export const LEADING_LINES_PROP = 0.15;
25 changes: 19 additions & 6 deletions src/FauxpilotCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Configuration, CreateCompletionRequestPrompt, CreateCompletionResponse,
import { CancellationToken, InlineCompletionContext, InlineCompletionItem, InlineCompletionItemProvider, InlineCompletionList, Position, ProviderResult, Range, TextDocument, workspace, StatusBarItem } from 'vscode';
import { AxiosResponse } from 'axios';
import { nextId } from './Uuid';
import { LEADING_LINES_PROP } from './Constants';

export class FauxpilotCompletionProvider implements InlineCompletionItemProvider {
cachedPrompts: Map<string, number> = new Map<string, number>();
Expand Down Expand Up @@ -68,12 +69,24 @@ export class FauxpilotCompletionProvider implements InlineCompletionItemProvider
});
}

private getPrompt(document: TextDocument, position: Position): String | undefined {
const firstLine = Math.max(position.line - (workspace.getConfiguration('fauxpilot').get("maxLines") as number), 0);

return document.getText(
new Range(firstLine, 0, position.line, position.character)
);
private getPrompt(document: TextDocument, position: Position): String | undefined {
const promptLinesCount = workspace.getConfiguration('fauxpilot').get("maxLines") as number;

/*
Put entire file in prompt if it's small enough, otherwise only
take lines above the cursor and from the beginning of the file.
*/
if (document.lineCount <= promptLinesCount) {
const range = new Range(0, 0, position.line, position.character);
return document.getText(range);
} else {
const leadingLinesCount = Math.floor(LEADING_LINES_PROP * promptLinesCount);
const prefixLinesCount = promptLinesCount - leadingLinesCount;
const firstPrefixLine = position.line - prefixLinesCount;
const prefix = document.getText(new Range(firstPrefixLine, 0, position.line, position.character));
const leading = document.getText(new Range(0, 0, leadingLinesCount, 0));
return leading + prefix;
}
}

private isNil(value: String | undefined | null): boolean {
Expand Down

0 comments on commit 7fb9a0c

Please sign in to comment.