From e0bc4ace8bc70440ddba7caf362f4b7fdc3c61f0 Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Tue, 3 Sep 2024 10:11:28 -0700 Subject: [PATCH] fix: import `LanguageModelChat` from `@types/vscode` (#80) * docs: update examples for rendering prompts * fix: import `LanguageModelChat` from `@vscode/types` --- README.md | 27 +++++++++++++++------------ package-lock.json | 12 ++++++------ package.json | 4 ++-- src/base/index.ts | 4 ++-- src/base/tsx-globals.ts | 2 +- src/base/vscodeTypes.d.ts | 2 +- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index efaec2d..9f83dfb 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,10 @@ Note: if your codebase depends on both `@vscode/prompt-tsx` and another library Next, your extension can use `renderPrompt` to render a TSX prompt. Here is an example of using TSX prompts in a Copilot chat participant that suggests SQL queries based on database context: ```ts -import { renderPrompt, Cl100KBaseTokenizer } from '@vscode/prompt-tsx'; +import { renderPrompt } from '@vscode/prompt-tsx'; import * as vscode from 'vscode'; import { TestPrompt } from './prompt'; -const tokenizer = new Cl100KBaseTokenizer(); const participant = vscode.chat.createChatParticipant( "mssql", async ( @@ -73,19 +72,22 @@ const participant = vscode.chat.createChatParticipant( ) => { response.progress("Reading database context..."); + const models = await vscode.lm.selectChatModels({ family: 'gpt-4' }); + if (models.length === 0) { + // No models available, return early + return; + } + const chatModel = models[0]; + // Render TSX prompt const { messages } = await renderPrompt( TestPrompt, { userQuery: request.prompt }, { modelMaxPromptTokens: 4096 }, - tokenizer + chatModel ); - const models = await vscode.lm.selectChatModels({ family: 'gpt-4' }); - if (models.length === 0) { - // No models available, return early - return; - } - const chatRequest = await models[0].sendChatRequest( + + const chatRequest = await chatModel.sendChatRequest( messages, {}, token @@ -112,11 +114,12 @@ export interface PromptState { export class TestPrompt extends PromptElement { override async prepare() { + } + + async render(state: PromptState, sizing: PromptSizing) { const sqlExtensionApi = await vscode.extensions.getExtension('ms-mssql.mssql')?.activate(); - return { creationScript: await sqlExtensionApi.getDatabaseCreateScript?.() }; - } + const creationScript = await sqlExtensionApi.getDatabaseCreateScript?.(); - render(state: PromptState, sizing: PromptSizing) { return ( <> diff --git a/package-lock.json b/package-lock.json index fa476d6..a3fb3d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "@vscode/prompt-tsx", - "version": "0.2.6-alpha", + "version": "0.2.8-alpha", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@vscode/prompt-tsx", - "version": "0.2.6-alpha", + "version": "0.2.8-alpha", "license": "SEE LICENSE IN LICENSE", "devDependencies": { "@microsoft/tiktokenizer": "^1.0.6", "@types/node": "^20.11.30", - "@types/vscode": "^1.89.0", + "@types/vscode": "^1.92.0", "@vscode/test-cli": "^0.0.9", "@vscode/test-electron": "^2.4.1", "esbuild": "0.20.2", @@ -514,9 +514,9 @@ "dev": true }, "node_modules/@types/vscode": { - "version": "1.89.0", - "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.89.0.tgz", - "integrity": "sha512-TMfGKLSVxfGfoO8JfIE/neZqv7QLwS4nwPwL/NwMvxtAY2230H2I4Z5xx6836pmJvMAzqooRQ4pmLm7RUicP3A==", + "version": "1.92.0", + "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.92.0.tgz", + "integrity": "sha512-DcZoCj17RXlzB4XJ7IfKdPTcTGDLYvTOcTNkvtjXWF+K2TlKzHHkBEXNWQRpBIXixNEUgx39cQeTFunY0E2msw==", "dev": true }, "node_modules/@vscode/test-cli": { diff --git a/package.json b/package.json index bdfb348..24e39d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vscode/prompt-tsx", - "version": "0.2.7-alpha", + "version": "0.2.8-alpha", "description": "Declare LLM prompts with TSX", "main": "./dist/base/index.js", "types": "./dist/base/index.d.ts", @@ -23,7 +23,7 @@ "@types/node": "^20.11.30", "@vscode/test-cli": "^0.0.9", "@vscode/test-electron": "^2.4.1", - "@types/vscode": "^1.89.0", + "@types/vscode": "^1.92.0", "esbuild": "0.20.2", "mocha": "^10.2.0", "npm-dts": "^1.3.12", diff --git a/src/base/index.ts b/src/base/index.ts index d4721ef..3f93954 100644 --- a/src/base/index.ts +++ b/src/base/index.ts @@ -2,14 +2,14 @@ * Copyright (c) Microsoft Corporation and GitHub. All rights reserved. *--------------------------------------------------------------------------------------------*/ -import type { CancellationToken, Progress } from 'vscode'; +import type { CancellationToken, ChatResponsePart, LanguageModelChat, Progress } from 'vscode'; import { PromptElementJSON } from './jsonTypes'; import { ChatMessage, ChatRole } from './openai'; import { MetadataMap, PromptRenderer } from './promptRenderer'; import { PromptReference } from './results'; import { AnyTokenizer, ITokenizer } from './tokenizer/tokenizer'; import { BasePromptElementProps, IChatEndpointInfo, PromptElementCtor } from './types'; -import { ChatDocumentContext, ChatResponsePart, LanguageModelChat, LanguageModelChatMessage } from './vscodeTypes.d'; +import { ChatDocumentContext, LanguageModelChatMessage } from './vscodeTypes.d'; export * as JSONTree from './jsonTypes'; export { AssistantChatMessage, ChatMessage, ChatRole, FunctionChatMessage, SystemChatMessage, ToolChatMessage, UserChatMessage } from './openai'; diff --git a/src/base/tsx-globals.ts b/src/base/tsx-globals.ts index 4d7cd83..3e8bfb9 100644 --- a/src/base/tsx-globals.ts +++ b/src/base/tsx-globals.ts @@ -2,7 +2,7 @@ * Copyright (c) Microsoft Corporation and GitHub. All rights reserved. *--------------------------------------------------------------------------------------------*/ -import { PieceJSON, PromptElementJSON } from './jsonTypes'; +import { PromptElementJSON } from './jsonTypes'; import { PromptMetadata, PromptReference } from './results'; import { URI } from './util/vs/common/uri'; import { ChatDocumentContext } from './vscodeTypes'; diff --git a/src/base/vscodeTypes.d.ts b/src/base/vscodeTypes.d.ts index 575894d..7a41828 100644 --- a/src/base/vscodeTypes.d.ts +++ b/src/base/vscodeTypes.d.ts @@ -2,7 +2,7 @@ * Copyright (c) Microsoft Corporation and GitHub. All rights reserved. *--------------------------------------------------------------------------------------------*/ -import type { CancellationToken, Command, Location, MarkdownString, ProviderResult, Range, ThemeIcon, Uri } from 'vscode'; +import type { CancellationToken, Command, Location, MarkdownString, ProviderResult, ThemeIcon, Uri } from 'vscode'; /** * Represents a part of a chat response that is formatted as Markdown.