use request.model

This commit is contained in:
isidorn
2024-10-25 15:24:29 +02:00
parent 7ce43a47d7
commit 93c518e360
4 changed files with 20108 additions and 55 deletions

View File

@ -8,18 +8,18 @@
"name": "chat-sample",
"version": "0.1.0",
"dependencies": {
"@vscode/prompt-tsx": "^0.2.11-alpha"
"@vscode/prompt-tsx": "^0.3.0-alpha"
},
"devDependencies": {
"@types/node": "^20",
"@types/vscode": "1.90.0",
"@types/vscode": "1.94.0",
"@typescript-eslint/eslint-plugin": "^7.14.0",
"@typescript-eslint/parser": "^7.14.0",
"eslint": "^8.26.0",
"typescript": "^5.6.2"
},
"engines": {
"vscode": "^1.92.0"
"vscode": "^1.95.0"
}
},
"node_modules/@aashutoshrathi/word-wrap": {
@ -167,9 +167,9 @@
}
},
"node_modules/@types/vscode": {
"version": "1.90.0",
"resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.90.0.tgz",
"integrity": "sha512-oT+ZJL7qHS9Z8bs0+WKf/kQ27qWYR3trsXpq46YDjFqBsMLG4ygGGjPaJ2tyrH0wJzjOEmDyg9PDJBBhWg9pkQ==",
"version": "1.94.0",
"resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.94.0.tgz",
"integrity": "sha512-UyQOIUT0pb14XSqJskYnRwD2aG0QrPVefIfrW1djR+/J4KeFQ0i1+hjZoaAmeNf3Z2jleK+R2hv+EboG/m8ruw==",
"dev": true
},
"node_modules/@typescript-eslint/eslint-plugin": {
@ -388,9 +388,9 @@
"dev": true
},
"node_modules/@vscode/prompt-tsx": {
"version": "0.2.11-alpha",
"resolved": "https://registry.npmjs.org/@vscode/prompt-tsx/-/prompt-tsx-0.2.11-alpha.tgz",
"integrity": "sha512-U/hmOwcWla5EHCiwCAAZni6w9N6lbG56EpciNXDis0oYFu03fGD+BzTw7jxbYd9NCS8CSHb76JoW49jyLilFxg=="
"version": "0.3.0-alpha.12",
"resolved": "https://registry.npmjs.org/@vscode/prompt-tsx/-/prompt-tsx-0.3.0-alpha.12.tgz",
"integrity": "sha512-2ANm569UBXIzjPbaDFjzRkucelhsnlnmYIPdDo+USeFq2Do0Q70gKiiRWYrQf5rPqCxrChDvgU14nsdJLUSaOQ=="
},
"node_modules/acorn": {
"version": "8.12.0",

View File

@ -9,7 +9,7 @@
},
"version": "0.1.0",
"engines": {
"vscode": "^1.92.0"
"vscode": "^1.95.0"
},
"categories": [
"AI",
@ -72,11 +72,11 @@
"watch": "tsc -watch -p ./"
},
"dependencies": {
"@vscode/prompt-tsx": "^0.2.11-alpha"
"@vscode/prompt-tsx": "^0.3.0-alpha"
},
"devDependencies": {
"@types/node": "^20",
"@types/vscode": "1.90.0",
"@types/vscode": "1.94.0",
"@typescript-eslint/eslint-plugin": "^7.14.0",
"@typescript-eslint/parser": "^7.14.0",
"eslint": "^8.26.0",

View File

@ -11,9 +11,6 @@ interface ICatChatResult extends vscode.ChatResult {
}
}
// Use gpt-4o since it is fast and high quality. gpt-3.5-turbo and gpt-4 are also available.
const MODEL_SELECTOR: vscode.LanguageModelChatSelector = { vendor: 'copilot', family: 'gpt-4o' };
export function activate(context: vscode.ExtensionContext) {
// Define a Cat chat handler.
@ -25,19 +22,16 @@ export function activate(context: vscode.ExtensionContext) {
stream.progress('Picking the right topic to teach...');
const topic = getTopic(context.history);
try {
// To get a list of all available models, do not pass any selector to the selectChatModels.
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
if (model) {
const messages = [
vscode.LanguageModelChatMessage.User('You are a cat! Your job is to explain computer science concepts in the funny manner of a cat. Always start your response by stating what concept you are explaining. Always include code samples.'),
vscode.LanguageModelChatMessage.User(topic)
];
const messages = [
vscode.LanguageModelChatMessage.User('You are a cat! Your job is to explain computer science concepts in the funny manner of a cat. Always start your response by stating what concept you are explaining. Always include code samples.'),
vscode.LanguageModelChatMessage.User(topic)
];
const chatResponse = await model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
stream.markdown(fragment);
}
const chatResponse = await request.model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
stream.markdown(fragment);
}
} catch(err) {
handleError(logger, err, stream);
}
@ -52,20 +46,18 @@ export function activate(context: vscode.ExtensionContext) {
} else if (request.command === 'play') {
stream.progress('Throwing away the computer science books and preparing to play with some Python code...');
try {
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
if (model) {
// Here's an example of how to use the prompt-tsx library to build a prompt
const { messages } = await renderPrompt(
PlayPrompt,
{ userQuery: request.prompt },
{ modelMaxPromptTokens: model.maxInputTokens },
model);
const chatResponse = await model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
stream.markdown(fragment);
}
// Here's an example of how to use the prompt-tsx library to build a prompt
const { messages } = await renderPrompt(
PlayPrompt,
{ userQuery: request.prompt },
{ modelMaxPromptTokens: request.model.maxInputTokens },
request.model);
const chatResponse = await request.model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
stream.markdown(fragment);
}
} catch(err) {
handleError(logger, err, stream);
}
@ -74,21 +66,18 @@ export function activate(context: vscode.ExtensionContext) {
return { metadata: { command: 'play' } };
} else {
try {
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
if (model) {
const messages = [
vscode.LanguageModelChatMessage.User(`You are a cat! Think carefully and step by step like a cat would.
Your job is to explain computer science concepts in the funny manner of a cat, using cat metaphors. Always start your response by stating what concept you are explaining. Always include code samples.`),
vscode.LanguageModelChatMessage.User(request.prompt)
];
const chatResponse = await model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
// Process the output from the language model
// Replace all python function definitions with cat sounds to make the user stop looking at the code and start playing with the cat
const catFragment = fragment.replaceAll('def', 'meow');
stream.markdown(catFragment);
}
const messages = [
vscode.LanguageModelChatMessage.User(`You are a cat! Think carefully and step by step like a cat would.
Your job is to explain computer science concepts in the funny manner of a cat, using cat metaphors. Always start your response by stating what concept you are explaining. Always include code samples.`),
vscode.LanguageModelChatMessage.User(request.prompt)
];
const chatResponse = await request.model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
// Process the output from the language model
// Replace all python function definitions with cat sounds to make the user stop looking at the code and start playing with the cat
const catFragment = fragment.replaceAll('def', 'meow');
stream.markdown(catFragment);
}
} catch(err) {
handleError(logger, err, stream);
@ -144,7 +133,8 @@ export function activate(context: vscode.ExtensionContext) {
let chatResponse: vscode.LanguageModelChatResponse | undefined;
try {
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
// Use gpt-4o since it is fast and high quality.
const [model] = await vscode.lm.selectChatModels({ vendor: 'copilot', family: 'gpt-4o' });
if (!model) {
console.log('Model not found. Please make sure the GitHub Copilot Chat extension is installed and enabled.');
return;

20063
chat-sample/vscode.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff