参加你附近的 ,了解 VS Code 中的 AI 辅助开发。

语言模型聊天提供程序 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
  };
}

注册提供程序

  1. 第一步是在 package.jsoncontributes.languageModelChatProviders 部分注册提供程序。提供唯一的 vendor ID 和 displayName

    {
      "contributes": {
        "languageModelChatProviders": [
          {
            "vendor": "my-provider",
            "displayName": "My Provider"
          }
        ]
      }
    }
    
  2. 接下来,在你的扩展激活函数中,使用 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());
    }
    
  3. (可选)在 package.json 中提供 contributes.languageModelChatProviders.managementCommand,以允许用户管理语言模型提供程序。

    managementCommand 属性的值必须是你在 package.jsoncontributes.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 - 工具结果内容

入门

你可以从一个基本示例项目开始。