构建语言模型提示词
您可以通过字符串拼接来构建语言模型提示词,但这种方式很难组合功能,也难以确保提示词保持在语言模型的上下文窗口内。为了克服这些限制,您可以使用 @vscode/prompt-tsx 库。
@vscode/prompt-tsx 库提供了以下功能:
- 基于 TSX 的提示词渲染:使用 TSX 组件组合提示词,使其更具可读性和可维护性
- 基于优先级的修剪:自动修剪提示词中不重要的部分,以适应模型的上下文窗口
- 灵活的 Token 管理:使用
flexGrow、flexReserve和flexBasis等属性来协同使用 Token 配额 - 工具集成:与 VS Code 的语言模型工具 API 集成
有关所有功能的完整概述和详细使用说明,请参阅完整的 README。
本文介绍了使用该库设计提示词的实际示例。这些示例的完整代码可以在 prompt-tsx 仓库中找到。
管理对话历史记录的优先级
在提示词中包含对话历史记录非常重要,因为它使用户能够针对之前的消息提出后续问题。然而,您需要确保其优先级得到妥善处理,因为历史记录会随着时间的推移而变长。我们发现,最合理的模式通常是按以下顺序确定优先级:
- 基础提示指令
- 当前的各种用户查询
- 最近几次对话历史
- 任何辅助数据
- 尽可能多地包含剩余历史记录
因此,请将历史记录在提示词中拆分为两部分,其中最近的提示词轮次优先于一般上下文信息。
在此库中,树中的每个 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 值,元素将获得其 PromptSizing 对象中任何未使用的 Token 配额,该对象会传递给其 render() 和 prepare() 调用。您可以在 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 仓库中查看它。
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 仓库。