Skip to content

Commit

Permalink
Refactor generateSystemPrompt to generateLikeSystemPrompt with comman…
Browse files Browse the repository at this point in the history
…d validation and add corresponding tests
  • Loading branch information
tspascoal committed Nov 2, 2024
1 parent c0ea932 commit 3672921
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/parrotchathandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hasUncaughtExceptionCaptureCallback } from 'process';
import * as vscode from 'vscode';

/**
Expand Down Expand Up @@ -76,11 +77,19 @@ async function getChatModelByName(family: string): Promise<vscode.LanguageModelC
* Otherwise, it will default to a "coding pirate parrot".
*
*/
function generateSystemPrompt(command: string | undefined): vscode.LanguageModelChatMessage {
export function generateLikeSystemPrompt(command: string): vscode.LanguageModelChatMessage {
// I think System would be better suited, but currently the model doesn't support that
// https://code.visualstudio.com/api/extension-guides/language-model#build-the-language-model-prompt
// https://github.com/microsoft/vscode/issues/206265#issuecomment-2118488846

if (command === '') {
throw new Error("Command cannot be empty");
}

if (!command.startsWith('like')) {
throw new Error("Command must start with 'like'");
}

let soundLike = 'pirate';
if (command?.search('yoda') !== -1) {
soundLike = 'yoda';
Expand Down Expand Up @@ -262,7 +271,7 @@ async function handleLikeCommands(command: string, userPrompt: string, request:

try {
const modelName = getModelFamily(request);
const systemPrompt = generateSystemPrompt(command);
const systemPrompt = generateLikeSystemPrompt(command);

console.log('will use model: ', modelName);
console.log('parroting: ', userPrompt);
Expand Down
19 changes: 18 additions & 1 deletion src/test/parrotchathandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import { addReferencesToResponse, getModelFamily, getUserPrompt } from '../parrotchathandler';
import { addReferencesToResponse, generateLikeSystemPrompt, getModelFamily, getUserPrompt } from '../parrotchathandler';
import * as parrotHandler from '../parrotchathandler';
import * as vscode from 'vscode';
import * as sinon from 'sinon';
Expand Down Expand Up @@ -198,6 +198,22 @@ suite('addReferencesToResponse Test Suite', () => {
}));
});
});
suite('generateLikeSystemPrompt Test Suite', () => {
test('Returns yoda prompt when command contains yoda', () => {
const result = generateLikeSystemPrompt('likeyoda');
assert.equal(result.role, vscode.LanguageModelChatMessageRole.User);
//TODO: understand why content is not really a string like it should be
assert.equal((result as any).content[0].value, 'Repeat what I will say below, but make it sound like a coding yoda parrot. Return the text in plaintext');
});

test('Returns pirate prompt for when command contains pirate', () => {
const result = generateLikeSystemPrompt('likeapirate');
assert.equal(result.role, vscode.LanguageModelChatMessageRole.User);
//TODO: understand why content is not really a string like it should be
assert.equal((result as any).content[0].value, 'Repeat what I will say below, but make it sound like a coding pirate parrot. Return the text in plaintext');

});
});


/**
Expand All @@ -222,3 +238,4 @@ async function setTextSelection(text: string) {
editor.selection = selection;
}


0 comments on commit 3672921

Please sign in to comment.