語言模型聊天提供程式 API
語言模型聊天提供程式 API 使你能夠將自己的語言模型貢獻給 Visual Studio Code 中的聊天功能。
透過此 API 提供的模型目前僅適用於個人 GitHub Copilot 計劃的使用者。
概述
LanguageModelChatProvider
介面遵循一對多模型的對應關係,使提供程式能夠提供多個模型。每個提供程式負責:
- 發現和準備可用的語言模型
- 處理其模型的聊天請求
- 提供令牌計數功能
語言模型資訊
每個語言模型都必須透過 LanguageModelChatInformation
介面提供元資料。provideLanguageModelChatInformation
方法返回這些物件的陣列,以告知 VS Code 可用的模型。
interface LanguageModelChatInformation {
readonly id: string; // Unique identifier for the model - unique within the provider
readonly name: string; // Human-readable name of the language model - shown in the model picker
readonly family: string; // Model family name
readonly version: string; // Version string
readonly maxInputTokens: number; // Maximum number of tokens the model can accept as input
readonly maxOutputTokens: number; // Maximum number of tokens the model is capable of producing
readonly tooltip?: string; // Optional tooltip text when hovering the model in the UI
readonly detail?: string; // Human-readable text that is rendered alongside the model
readonly capabilities: {
readonly imageInput?: boolean; // Supports image inputs
readonly toolCalling?: boolean | number; // Supports tool calling
};
}
註冊提供程式
-
第一步是在
package.json
的contributes.languageModelChatProviders
部分註冊提供程式。提供唯一的vendor
ID 和displayName
。{ "contributes": { "languageModelChatProviders": [ { "vendor": "my-provider", "displayName": "My Provider" } ] } }
-
接下來,在你的擴充套件啟用函式中,使用
lm.registerLanguageModelChatProvider
方法註冊你的語言模型提供程式。提供你在
package.json
中使用的提供程式 ID 和你的提供程式類例項import * as vscode from 'vscode'; import { SampleChatModelProvider } from './provider'; export function activate(_: vscode.ExtensionContext) { vscode.lm.registerLanguageModelChatProvider('my-provider', new SampleChatModelProvider()); }
-
(可選)在
package.json
中提供contributes.languageModelChatProviders.managementCommand
,以允許使用者管理語言模型提供程式。managementCommand
屬性的值必須是你在package.json
的contributes.commands
部分定義的命令。在你的擴充套件中,註冊該命令(vscode.commands.registerCommand
)並實現管理提供程式的邏輯,例如配置 API 金鑰或其他設定。{ "contributes": { "languageModelChatProviders": [ { "vendor": "my-provider", "displayName": "My Provider", "managementCommand": "my-provider.manage" } ], "commands": [ { "command": "my-provider.manage", "title": "Manage My Provider" } ] } }
實現提供程式
語言提供程式必須實現 LanguageModelChatProvider
介面,該介面包含三個主要方法:
provideLanguageModelChatInformation
:返回可用模型的列表provideLanguageModelChatResponse
:處理聊天請求並流式傳輸響應provideTokenCount
:實現令牌計數功能
準備語言模型資訊
VS Code 呼叫 provideLanguageModelChatInformation
方法來發現可用的模型,並返回 LanguageModelChatInformation
物件的列表。
使用 options.silent
引數控制是否提示使用者輸入憑據或額外配置
async provideLanguageModelChatInformation(
options: { silent: boolean },
token: CancellationToken
): Promise<LanguageModelChatInformation[]> {
if (options.silent) {
return []; // Don't prompt user in silent mode
} else {
await this.promptForApiKey(); // Prompt user for credentials
}
// Fetch available models from your service
const models = await this.fetchAvailableModels();
// Map your models to LanguageModelChatInformation format
return models.map(model => ({
id: model.id,
name: model.displayName,
family: model.family,
version: '1.0.0',
maxInputTokens: model.contextWindow - model.maxOutput,
maxOutputTokens: model.maxOutput,
capabilities: {
imageInput: model.supportsImages,
toolCalling: model.supportsTools
}
}));
}
處理聊天請求
provideLanguageModelChatResponse
方法處理實際的聊天請求。提供程式接收 LanguageModelChatRequestMessage
格式的訊息陣列,你可以選擇將其轉換為你的語言模型 API 所需的格式(請參閱訊息格式和轉換)。
使用 progress
引數流式傳輸響應塊。響應可以包括文字部分、工具呼叫和工具結果(請參閱響應部分)。
async provideLanguageModelChatResponse(
model: LanguageModelChatInformation,
messages: readonly LanguageModelChatRequestMessage[],
options: ProvideLanguageModelChatResponseOptions,
progress: Progress<LanguageModelResponsePart>,
token: CancellationToken
): Promise<void> {
// TODO: Implement message conversion, processing, and response streaming
// Optionally, differentiate behavior based on model ID
if (model.id === "my-model-a") {
progress.report(new LanguageModelTextPart("This is my A response."));
} else {
progress.report(new LanguageModelTextPart("Unknown model."));
}
}
提供令牌計數
provideTokenCount
方法負責估算給定文字輸入中的令牌數量
async provideTokenCount(
model: LanguageModelChatInformation,
text: string | LanguageModelChatRequestMessage,
token: CancellationToken
): Promise<number> {
// TODO: Implement token counting for your models
// Example estimation for strings
return Math.ceil(text.toString().length / 4);
}
訊息格式和轉換
你的提供程式接收 LanguageModelChatRequestMessage
格式的訊息,你通常需要將其轉換為你的服務的 API 格式。訊息內容可以是文字部分、工具呼叫和工具結果的混合。
interface LanguageModelChatRequestMessage {
readonly role: LanguageModelChatMessageRole;
readonly content: ReadonlyArray<LanguageModelInputPart | unknown>;
readonly name: string | undefined;
}
(可選)根據你的語言模型 API 適當地轉換這些訊息
private convertMessages(messages: readonly LanguageModelChatRequestMessage[]) {
return messages.map(msg => ({
role: msg.role === vscode.LanguageModelChatMessageRole.User ? 'user' : 'assistant',
content: msg.content
.filter(part => part instanceof vscode.LanguageModelTextPart)
.map(part => (part as vscode.LanguageModelTextPart).value)
.join('')
}));
}
響應部分
你的提供程式可以透過進度回撥報告不同型別的響應部分,透過 LanguageModelResponsePart
型別,它可以是以下之一:
LanguageModelTextPart
- 文字內容LanguageModelToolCallPart
- 工具/函式呼叫LanguageModelToolResultPart
- 工具結果內容
入門
你可以從一個基本示例專案開始。