现已发布!阅读关于 11 月新增功能和修复的内容。

构建语言模型提示

您可以通过字符串拼接来构建语言模型提示,但这很难组合功能并确保您的提示保持在语言模型的上下文窗口内。为了克服这些限制,您可以使用 @vscode/prompt-tsx 库。

@vscode/prompt-tsx 库提供以下功能

  • 基于 TSX 的提示渲染:使用 TSX 组件组合提示,使其更具可读性和可维护性
  • 基于优先级的修剪:自动修剪提示中不太重要的部分,以适应模型的上下文窗口
  • 灵活的 token 管理:使用 flexGrowflexReserveflexBasis 等属性来协同使用 token 预算
  • 工具集成:与 VS Code 的语言模型工具 API 集成

有关所有功能的完整概述和详细用法说明,请参阅 完整的 README

本文介绍了使用该库进行提示设计的实用示例。这些示例的完整代码可以在 prompt-tsx 存储库中找到。

管理对话历史记录中的优先级

在提示中包含对话历史记录很重要,因为它使用户能够对先前的消息提出后续问题。但是,您需要确保其优先级得到适当处理,因为历史记录会随着时间的推移而增长。我们发现,最有意义的模式通常是按以下顺序确定优先级

  1. 基础提示指令
  2. 当前用户查询
  3. 最近几次对话历史记录
  4. 任何支持数据
  5. 尽可能多的剩余历史记录

因此,在提示中将历史记录分成两部分,其中最近的提示轮次优先于一般的上下文信息。

在此库中,树中的每个 TSX 节点都有一个优先级,概念上类似于 zIndex,数字越大表示优先级越高。

步骤 1:定义 HistoryMessages 组件

要列出历史消息,请定义一个 HistoryMessages 组件。此示例提供了一个良好的起点,但如果您处理更复杂的数据类型,可能需要对其进行扩展。

此示例使用了 PrioritizedList 辅助组件,该组件会自动为其每个子项分配升序或降序的优先级。

import {
	UserMessage,
	AssistantMessage,
	PromptElement,
	BasePromptElementProps,
	PrioritizedList,
} from '@vscode/prompt-tsx';
import { ChatContext, ChatRequestTurn, ChatResponseTurn, ChatResponseMarkdownPart } from 'vscode';

interface IHistoryMessagesProps extends BasePromptElementProps {
	history: ChatContext['history'];
}

export class HistoryMessages extends PromptElement<IHistoryMessagesProps> {
	render(): PromptPiece {
		const history: (UserMessage | AssistantMessage)[] = [];
		for (const turn of this.props.history) {
			if (turn instanceof ChatRequestTurn) {
				history.push(<UserMessage>{turn.prompt}</UserMessage>);
			} else if (turn instanceof ChatResponseTurn) {
				history.push(
					<AssistantMessage name={turn.participant}>
						{chatResponseToMarkdown(turn)}
					</AssistantMessage>
				);
			}
		}
		return (
			<PrioritizedList priority={0} descending={false}>
				{history}
			</PrioritizedList>
		);
	}
}

步骤 2:定义 Prompt 组件

接下来,定义一个 MyPrompt 组件,其中包括基础指令、用户查询和具有适当优先级的历史消息。优先级值在同级之间是本地的。请记住,您可能希望在触及提示中的任何其他内容之前修剪历史记录中较旧的消息,因此需要将两个 <HistoryMessages> 元素分开

import {
	UserMessage,
	PromptElement,
	BasePromptElementProps,
} from '@vscode/prompt-tsx';

interface IMyPromptProps extends BasePromptElementProps {
	history: ChatContext['history'];
	userQuery: string;
}

export class MyPrompt extends PromptElement<IMyPromptProps> {
	render() {
		return (
			<>
				<UserMessage priority={100}>
					Here are your base instructions. They have the highest priority because you want to make
					sure they're always included!
				</UserMessage>
				{/* Older messages in the history have the lowest priority since they're less relevant */}
				<HistoryMessages history={this.props.history.slice(0, -2)} priority={0} />
				{/* The last 2 history messages are preferred over any workspace context you have below */}
				<HistoryMessages history={this.props.history.slice(-2)} priority={80} />
				{/* The user query is right behind the based instructions in priority */}
				<UserMessage priority={90}>{this.props.userQuery}</UserMessage>
				<UserMessage priority={70}>
					With a slightly lower priority, you can include some contextual data about the workspace
					or files here...
				</UserMessage>
			</>
		);
	}
}

现在,在库尝试修剪提示的其他元素之前,所有较旧的历史消息都会被修剪。

步骤 3:定义 History 组件

为了使使用更轻松,请定义一个 History 组件来包装历史消息,并使用 passPriority 属性充当直通容器。通过 passPriority,其子项在优先级方面被视为直接子项。

import { PromptElement, BasePromptElementProps } from '@vscode/prompt-tsx';

interface IHistoryProps extends BasePromptElementProps {
	history: ChatContext['history'];
	newer: number; // last 2 message priority values
	older: number; // previous message priority values
	passPriority: true; // require this prop be set!
}

export class History extends PromptElement<IHistoryProps> {
	render(): PromptPiece {
		return (
			<>
				<HistoryMessages history={this.props.history.slice(0, -2)} priority={this.props.older} />
				<HistoryMessages history={this.props.history.slice(-2)} priority={this.props.newer} />
			</>
		);
	}
}

现在,您可以单独使用并重用此元素来包含聊天历史记录

<History history={this.props.history} passPriority older={0} newer={80}/>

扩展文件内容以适应

在此示例中,您希望在提示中包含用户当前正在查看的所有文件的内容。这些文件可能很大,以至于包含所有文件会导致其文本被修剪!此示例演示了如何使用 flexGrow 属性来协同调整文件内容的大小,以适应 token 预算。

步骤 1:定义基础指令和用户查询

首先,您需要定义一个包含基础指令的 UserMessage 组件。

<UserMessage priority={100}>Here are your base instructions.</UserMessage>

然后,您可以使用 UserMessage 组件包含用户查询。此组件具有高优先级,以确保其紧随基础指令之后。

<UserMessage priority={90}>{this.props.userQuery}</UserMessage>

步骤 2:包含文件内容

现在,您可以使用 FileContext 组件包含文件内容。为其分配 flexGrow 值为 1,以确保其在基础指令、用户查询和历史记录之后呈现。

<FileContext priority={70} flexGrow={1} files={this.props.files} />

使用 flexGrow 值,该元素将在传递到其 render()prepare() 调用中的 PromptSizing 对象中获得任何未使用的 token 预算。您可以在 prompt-tsx 文档中了解有关 flex 元素行为的更多信息。

步骤 3:包含历史记录

接下来,使用您之前创建的 History 组件包含历史消息。这有点棘手,因为您确实希望显示一些历史记录,但也希望文件内容占据提示的大部分。

因此,将 History 组件的 flexGrow 值设置为 2,以确保它在所有其他元素(包括 <FileContext />)之后呈现。但是,还将 flexReserve 值设置为 "/5",以将总预算的 1/5 保留给历史记录。

<History
	history={this.props.history}
	passPriority
	older={0}
	newer={80}
	flexGrow={2}
	flexReserve="/5"
/>

步骤 3:组合提示的所有元素

现在,将所有元素组合到 MyPrompt 组件中。

import {
	UserMessage,
	PromptElement,
	BasePromptElementProps,
} from '@vscode/prompt-tsx';
import { History } from './history';

interface IFilesToInclude {
	document: TextDocument;
	line: number;
}

interface IMyPromptProps extends BasePromptElementProps {
	history: ChatContext['history'];
	userQuery: string;
	files: IFilesToInclude[];
}

export class MyPrompt extends PromptElement<IMyPromptProps> {
	render() {
		return (
			<>
				<UserMessage priority={100}>Here are your base instructions.</UserMessage>
				<History
					history={this.props.history}
					passPriority
					older={0}
					newer={80}
					flexGrow={2}
					flexReserve="/5"
				/>
				<UserMessage priority={90}>{this.props.userQuery}</UserMessage>
				<FileContext priority={70} flexGrow={1} files={this.props.files} />
			</>
		);
	}
}

步骤 4:定义 FileContext 组件

最后,定义一个 FileContext 组件,其中包含用户当前正在查看的文件的内容。由于您使用了 flexGrow,因此可以通过使用 PromptSizing 中的信息,实现逻辑以获取“有趣”行周围的尽可能多的行。

为了简洁起见,省略了 getExpandedFiles 的实现逻辑。您可以在 prompt-tsx repo 中查看它。

import { PromptElement, BasePromptElementProps, PromptSizing, PromptPiece } from '@vscode/prompt-tsx';

class FileContext extends PromptElement<{ files: IFilesToInclude[] } & BasePromptElementProps> {
	async render(_state: void, sizing: PromptSizing): Promise<PromptPiece> {
		const files = await this.getExpandedFiles(sizing);
		return <>{files.map(f => f.toString())}</>;
	}

	private async getExpandedFiles(sizing: PromptSizing) {
		// Implementation details are summarized here.
		// Refer to the repo for the complete implementation.
	}
}

总结

在这些示例中,您创建了一个 MyPrompt 组件,其中包含具有不同优先级的基准指令、用户查询、历史消息和文件内容。您使用 flexGrow 来协同调整文件内容的大小,以适应 token 预算。

通过遵循此模式,您可以确保您的提示中最重要的部分始终被包含,而不太重要的部分则根据需要进行修剪,以适应模型的上下文窗口。有关 getExpandedFiles 方法和 FileContextTracker 类的完整实现详细信息,请参阅 prompt-tsx repo

© . This site is unofficial and not affiliated with Microsoft.