编写语言模型提示
你可以通过字符串连接来构建语言模型提示,但这很难组合功能并确保你的提示保持在语言模型的上下文窗口内。为了克服这些限制,你可以使用 @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
值,元素在其传递到 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 仓库中查看它。
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 仓库。