Skip to content

Commit

Permalink
update sample to latest api (#1021)
Browse files Browse the repository at this point in the history
* update sample to latest api

* Fix family

* Minor cleanup

* Add fullname

---------

Co-authored-by: Rob Lourens <roblourens@gmail.com>
  • Loading branch information
isidorn and roblourens authored May 15, 2024
1 parent 209ce0e commit cfec264
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 302 deletions.
2 changes: 1 addition & 1 deletion chat-sample/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion chat-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"version": "0.1.0",
"engines": {
"vscode": "^1.88.0"
"vscode": "^1.90.0"
},
"extensionDependencies": [
"github.copilot-chat"
Expand All @@ -27,6 +27,7 @@
"chatParticipants": [
{
"id": "chat-sample.cat",
"fullName": "Cat",
"name": "cat",
"description": "Meow! What can I teach you?",
"isSticky": true,
Expand Down
60 changes: 21 additions & 39 deletions chat-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ICatChatResult extends vscode.ChatResult {
}
}

const LANGUAGE_MODEL_ID = 'copilot-gpt-3.5-turbo'; // Use faster model. Alternative is 'copilot-gpt-4', which is slower but more powerful
const MODEL_SELECTOR: vscode.LanguageModelChatSelector = { vendor: 'copilot', family: 'gpt-3.5-turbo' };

export function activate(context: vscode.ExtensionContext) {

Expand All @@ -22,11 +22,12 @@ export function activate(context: vscode.ExtensionContext) {
stream.progress('Picking the right topic to teach...');
const topic = getTopic(context.history);
const messages = [
new vscode.LanguageModelChatUserMessage('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.'),
new vscode.LanguageModelChatUserMessage(topic)
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 vscode.lm.sendChatRequest(LANGUAGE_MODEL_ID, messages, {}, token);
for await (const fragment of chatResponse.stream) {
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
const chatResponse = await model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
stream.markdown(fragment);
}

Expand All @@ -39,22 +40,24 @@ 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...');
const messages = [
new vscode.LanguageModelChatUserMessage('You are a cat! Reply in the voice of a cat, using cat analogies when appropriate. Be concise to prepare for cat play time.'),
new vscode.LanguageModelChatUserMessage('Give a small random python code samples (that have cat names for variables). ' + request.prompt)
vscode.LanguageModelChatMessage.User('You are a cat! Reply in the voice of a cat, using cat analogies when appropriate. Be concise to prepare for cat play time.'),
vscode.LanguageModelChatMessage.User('Give a small random python code samples (that have cat names for variables). ' + request.prompt)
];
const chatResponse = await vscode.lm.sendChatRequest(LANGUAGE_MODEL_ID, messages, {}, token);
for await (const fragment of chatResponse.stream) {
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
const chatResponse = await model.sendRequest(messages, {}, token);
for await (const fragment of chatResponse.text) {
stream.markdown(fragment);
}
return { metadata: { command: 'play' } };
} else {
const messages = [
new vscode.LanguageModelChatUserMessage(`You are a cat! Think carefully and step by step like a cat would.
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.`),
new vscode.LanguageModelChatUserMessage(request.prompt)
vscode.LanguageModelChatMessage.User(request.prompt)
];
const chatResponse = await vscode.lm.sendChatRequest(LANGUAGE_MODEL_ID, messages, {}, token);
for await (const fragment of chatResponse.stream) {
const [model] = await vscode.lm.selectChatModels(MODEL_SELECTOR);
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');
Expand All @@ -80,43 +83,22 @@ export function activate(context: vscode.ExtensionContext) {
}
};

vscode.chat.registerChatVariableResolver('cat_context', 'Describes the state of mind and version of the cat', {
resolve: (name, context, token) => {
if (name == 'cat_context') {
const mood = Math.random() > 0.5 ? 'happy' : 'grumpy';
return [
{
level: vscode.ChatVariableLevel.Short,
value: 'version 1.3 ' + mood
},
{
level: vscode.ChatVariableLevel.Medium,
value: 'I am a playful cat, version 1.3, and I am ' + mood
},
{
level: vscode.ChatVariableLevel.Full,
value: 'I am a playful cat, version 1.3, this version prefer to explain everything using mouse and tail metaphores. I am ' + mood
}
]
}
}
});

context.subscriptions.push(
cat,
// Register the command handler for the /meow followup
vscode.commands.registerTextEditorCommand(CAT_NAMES_COMMAND_ID, async (textEditor: vscode.TextEditor) => {
// Replace all variables in active editor with cat names and words
const text = textEditor.document.getText();
const messages = [
new vscode.LanguageModelChatUserMessage(`You are a cat! Think carefully and step by step like a cat would.
new vscode.LanguageModelChatMessage(vscode.LanguageModelChatMessageRole.User, `You are a cat! Think carefully and step by step like a cat would.
Your job is to replace all variable names in the following code with funny cat variable names. Be creative. IMPORTANT respond just with code. Do not use markdown!`),
new vscode.LanguageModelChatUserMessage(text)
new vscode.LanguageModelChatMessage(vscode.LanguageModelChatMessageRole.User, text)
];

let chatResponse: vscode.LanguageModelChatResponse | undefined;
try {
chatResponse = await vscode.lm.sendChatRequest(LANGUAGE_MODEL_ID, messages, {}, new vscode.CancellationTokenSource().token);
const [model] = await vscode.lm.selectChatModels({ vendor: 'copilot', family: 'gpt-3.5-turbo' });
chatResponse = await model.sendRequest(messages, {}, new vscode.CancellationTokenSource().token);

} catch (err) {
// making the chat request might fail because
Expand All @@ -138,7 +120,7 @@ export function activate(context: vscode.ExtensionContext) {

// Stream the code into the editor as it is coming in from the Language Model
try {
for await (const fragment of chatResponse.stream) {
for await (const fragment of chatResponse.text) {
await textEditor.edit(edit => {
const lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);
const position = new vscode.Position(lastLine.lineNumber, lastLine.text.length);
Expand Down
Loading

0 comments on commit cfec264

Please sign in to comment.