Skip to content

Commit

Permalink
add option resendIfEmptyResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Aincvy committed Oct 15, 2023
1 parent 3fd4c48 commit c4def6e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fauxpilot",
"displayName": "Fauxpilot",
"description": "Get completions from Fauxpilot server",
"version": "1.2.1",
"version": "1.2.2",
"icon": "assets/icon.png",
"keywords": [
"code-suggestion",
Expand Down Expand Up @@ -123,7 +123,13 @@
},
"fauxpilot.trim1stLineBreak": {
"type":"boolean",
"default": false
"default": false,
"description": "Should it be removed if there are no characters before the first `\\n`, or if there are only spaces?"
},
"fauxpilot.resendIfEmptyResponse" : {
"type": "boolean",
"default" : false,
"description": "If `\\n` is in the `stopWordsArray` array and an empty response is received this time, should we resend the request? The new request will remove `\\n` from the `stopWordsArray` array."
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/AccessBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ function getCache(): AccessBackendCache {
return cacheInScript;
}

export function fetch(prompt: string): Promise<OpenAI.Completion> {
export function fetch(prompt: string, removedStopWord: string = ""): Promise<OpenAI.Completion> {

const tmpStopWords = removedStopWord && removedStopWord.length > 0
? fauxpilotClient.StopWords.filter(word => word !== removedStopWord) : fauxpilotClient.StopWords;
fauxpilotClient.log("tmpStopWords: " + JSON.stringify(tmpStopWords));

const data = {
model: fauxpilotClient.Model,
prompt: prompt,
max_tokens: fauxpilotClient.MaxTokens,
temperature: fauxpilotClient.Temperature,
stop: fauxpilotClient.StopWords
stop: tmpStopWords
};

if (fauxpilotClient.RequestType == RequestType.OpenAI) {
Expand Down
15 changes: 14 additions & 1 deletion src/FauxpilotClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class FauxpilotClient {
private leadingLinesRatio: number;
private reduceLineStep: number;
private trim1stLineBreak = false;
private resendIfEmptyResponse = false;
private fetchWithoutLineBreak = false;

public version: string;

Expand Down Expand Up @@ -78,6 +80,7 @@ export class FauxpilotClient {
this.serverMaxTokens = extConfig.get("serverMaxTokens", 2048);
this.reduceLineStep = extConfig.get("reduceLineStep", 1);
this.trim1stLineBreak = extConfig.get("trim1stLineBreak", false);
this.resendIfEmptyResponse = extConfig.get("resendIfEmptyResponse", false);

this.log(`enabled = ${this.enabled}`);
this.log(`baseUrl = ${this.baseUrl}`);
Expand All @@ -93,6 +96,7 @@ export class FauxpilotClient {
this.log(`serverMaxTokens = ${this.serverMaxTokens}`);
this.log(`reduceLineStep = ${this.reduceLineStep}`);
this.log(`trim1stLineBreak = ${this.trim1stLineBreak}`);
this.log(`resendIfEmptyResponse = ${this.resendIfEmptyResponse}`);

rebuildAccessBackendCache();
this.log("reload config finish.");
Expand Down Expand Up @@ -184,9 +188,18 @@ export class FauxpilotClient {
return this.trim1stLineBreak;
}

public get ResendIfEmptyResponse(): boolean {
return this.resendIfEmptyResponse;
}

public get IsFetchWithoutLineBreak(): boolean {
return this.fetchWithoutLineBreak;
}


public set IsFetchWithoutLineBreak(value: boolean) {
this.fetchWithoutLineBreak = value;
}

}

const client = new FauxpilotClient();
Expand Down
73 changes: 48 additions & 25 deletions src/FauxpilotCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,8 @@ export class FauxpilotCompletionProvider implements InlineCompletionItemProvider
// fauxpilotClient.log(promptStr);

fauxpilotClient.log("current id = " + currentId + " set request status to pending");
this.requestStatus = "pending";
this.statusBar.tooltip = "Fauxpilot - Working";
this.statusBar.text = "$(loading~spin)";
return fetch(promptStr).then((response) => {
this.statusBar.text = "$(light-bulb)";
// if (token.isCancellationRequested) {
// fauxpilotClient.log('request cancelled.');
// return [];
// }
if (!response) {
return;
}

const result = this.toInlineCompletions(response, position);
this.lastResponse = result;
this.lastResponseTime = Date.now();
fauxpilotClient.log("inline completions array length: " + result.length);
return result;
}).finally(() => {

return this.tryComplete(promptStr, position, token).finally(() => {
fauxpilotClient.log("current id = " + currentId + " set request status to done");
this.requestStatus = "done";
this.cachedPrompts.delete(currentId);
Expand All @@ -150,6 +133,45 @@ export class FauxpilotCompletionProvider implements InlineCompletionItemProvider
}
}

private tryComplete(promptStr: string, position: Position, token: CancellationToken, tryTimes = 0): Promise<InlineCompletionItem[]>{
const empty: InlineCompletionItem[] = [];
if (tryTimes >= 5) {
return Promise.resolve(empty);
}

this.requestStatus = "pending";
this.statusBar.tooltip = "Fauxpilot - Working";
this.statusBar.text = "$(loading~spin)";

const removedStopWord = fauxpilotClient.IsFetchWithoutLineBreak ? "\n" : "";
return fetch(promptStr, removedStopWord).then(response => {
this.statusBar.text = "$(light-bulb)";

if (!response) {
return empty;
}

fauxpilotClient.IsFetchWithoutLineBreak = false;
const result = this.toInlineCompletions(response, position);
if (result.length == 0 && fauxpilotClient.IsFetchWithoutLineBreak) {
if (token.isCancellationRequested) {
fauxpilotClient.log('request cancelled.');
fauxpilotClient.IsFetchWithoutLineBreak = false;
return empty;
}
// resend
fauxpilotClient.log("Fetching again");
var tmp = this.tryComplete(promptStr, position, token, tryTimes+1);
return tmp;
}

this.lastResponse = result;
this.lastResponseTime = Date.now();
fauxpilotClient.log("inline completions array length: " + result.length);
return result;
});
}

private isNil(value: String | undefined | null): boolean {
return value === undefined || value === null || value.length === 0;
}
Expand All @@ -165,14 +187,15 @@ export class FauxpilotCompletionProvider implements InlineCompletionItemProvider

// it seems always return 1 choice.
let choice1Text = value.choices[0].text;
if (!choice1Text) {
return [];
}
// if (!choice1Text) {
// return [];
// }

fauxpilotClient.log('Get choice text: ' + choice1Text);
const trimmedText = choice1Text.trim();
// fauxpilotClient.log('---------END-OF-CHOICE-TEXT-----------');
if (trimmedText.length <= 0) {
if (!choice1Text || choice1Text.trim().length <= 0) {
if (fauxpilotClient.ResendIfEmptyResponse) {
fauxpilotClient.IsFetchWithoutLineBreak = fauxpilotClient.StopWords.includes("\n");
}
return [];
}

Expand Down

0 comments on commit c4def6e

Please sign in to comment.