Skip to content

Commit

Permalink
Optimize Copilot chat model retrieval #873
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Oct 23, 2024
1 parent 61ae29c commit 46a7a49
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## [10.6.0] - 2024-xx-xx

### ✨ New features

### 🎨 Enhancements

- [#873](https://github.com/estruyf/vscode-front-matter/issues/873): Add retry logic to get the AI model for calling GitHub Copilot

### ⚡️ Optimizations

### 🐞 Fixes

## [10.5.0] - 2024-10-21 - [Release notes](https://beta.frontmatter.codes/updates/v10.5.0)

### 🎨 Enhancements
Expand Down
7 changes: 6 additions & 1 deletion src/listeners/panel/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ export class DataListener extends BaseListener {
}

private static async copilotSuggestTitle(command: string, requestId?: string, title?: string) {
if (!command || !requestId || !title) {
if (!command || !requestId) {
return;
}

if (!title) {
this.sendRequestError(command, requestId, 'No title provided');
return;
}

Expand Down
20 changes: 15 additions & 5 deletions src/services/Copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
LanguageModelChatResponse,
extensions,
lm,
version as VscodeVersion
version as VscodeVersion,
LanguageModelChat
} from 'vscode';
import { Logger, Settings, TaxonomyHelper } from '../helpers';
import {
Expand All @@ -14,6 +15,7 @@ import {
} from '../constants';
import { TagType } from '../panelWebView/TagType';
import { TaxonomyType } from '../models';
import { sleep } from '../utils';

export class Copilot {
private static personality =
Expand Down Expand Up @@ -51,7 +53,7 @@ export class Copilot {
LanguageModelChatMessage.User(`The title of the blog post is """${title}""".`)
];

const chatResponse = await this.getChatResponse(messages);
const chatResponse = await Copilot.getChatResponse(messages);
if (!chatResponse) {
return;
}
Expand Down Expand Up @@ -100,7 +102,7 @@ Response format: a single string wrapped in double quotes, e.g., "Boost your web
);
}

const chatResponse = await this.getChatResponse(messages);
const chatResponse = await Copilot.getChatResponse(messages);

if (!chatResponse) {
return;
Expand Down Expand Up @@ -179,7 +181,7 @@ Example: SEO, website optimization, digital marketing.`
);
}

const chatResponse = await this.getChatResponse(messages);
const chatResponse = await Copilot.getChatResponse(messages);

if (!chatResponse) {
return;
Expand Down Expand Up @@ -211,6 +213,9 @@ Example: SEO, website optimization, digital marketing.`

try {
const model = await this.getModel();
if (!model) {
return;
}
chatResponse = await model.sendRequest(messages, {}, new CancellationTokenSource().token);
} catch (err) {
Logger.error(`Copilot:getChatResponse:: ${(err as Error).message}`);
Expand All @@ -229,14 +234,19 @@ Example: SEO, website optimization, digital marketing.`
* Retrieves the chat model for the Copilot service.
* @returns A Promise that resolves to the chat model.
*/
private static async getModel() {
private static async getModel(retry = 0): Promise<LanguageModelChat | undefined> {
// const models = await lm.selectChatModels();
// console.log(models);
const [model] = await lm.selectChatModels({
vendor: 'copilot',
family: Settings.get<string>(SETTING_COPILOT_FAMILY) || 'gpt-4o-mini'
});

if ((!model || !model.sendRequest) && retry <= 5) {
await sleep(1000);
return Copilot.getModel(retry + 1);
}

return model;
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './readdirAsync';
export * from './renameAsync';
export * from './rmdirAsync';
export * from './sentryInit';
export * from './sleep';
export * from './sortPages';
export * from './unlinkAsync';
export * from './writeFileAsync';
1 change: 1 addition & 0 deletions src/utils/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

0 comments on commit 46a7a49

Please sign in to comment.