尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

[DeepSeek Harness深度拆解-15]体验系统提示词的构建

发布时间:2026/9/27 15:10:23

资讯中心
01
ARTICLE

[DeepSeek Harness深度拆解-15]体验系统提示词的构建

[DeepSeek Harness深度拆解-15]体验系统提示词的构建
DSH利用插件SystemPrompt将自己注册为服务服务名为systemPrompt来以类似于Builder模式来渲染系统提示词我们可以将此服务作为Builder通过提供相应配置注册构成最终系统提示词的相关元素PromptSection和PromptContext并同时提供描述有序工具的Schema。本篇以实例演示的方式来介绍如何利用SystemPrompt来生成我们希望的系统提示词。1. 默认的提示词如下的演示程序体现了默认提示的内容。如代码所示我们通过注入systemPrompt服务注册了一个测试插件并在其中调用ctx.systemPrompt的assemble方法组装了一个PromptAssembly对象并以此为参数调用了renderPrompt函数将其渲染成一个提示词文本并输出。import{Context}fromdeepseek-ai/cordisimport{SystemPrompt,renderPrompt}fromdeepseek-ai/dsh-system-promptconstctxnewContext()ctx.plugin(SystemPrompt)constfiberctx.inject([systemPrompt],asyncctx{constassembleawaitctx.systemPrompt.assemble()console.log(renderPrompt(assemble))})awaitfiber.await()输出You are an AI agent powered by DeepSeek Harness.上面输出的这段提示词文本被称为DeepSeek Harness身份标识默认会称为系统提示词的组成部分。assemble方法返回PromptAssembly可以视为SystemPrompt的核心输出对应接口定义如下exportinterfacePromptAssembly{sections:AssembledSection[]contexts:AssembledContext[]tools:ToolSchema[]variables:Recordstring,string|undefined}四个字段说明如下sections系统提示词的正文片段已求值但未插值按order排序最终由renderPrompt插值、过滤、拼接成提示词字符串contexts动态运行时上下文快照已求值但未插值按order排序最终渲染成用户消息插入模型历史tools面向模型的工具Schema列表已结构化、已按toolOrder排序不做文本渲染直接作为LLM API的tools参数传入variables变量名到值的扁平映射已求值仅作为sections和contexts插值时的查表来源。2. 为SystemPrompt服务提供配置SystemPrompt插件具有如下这个Config接口表示的配置我们可以在注册此插件时这利用它对系统提示词的组装行为进行定制。exportinterfaceConfig{includeHarnessIdentity?:booleanincludeRuntimeContext?:booleanpersonaPrefix?:stringpersonaSuffix?:stringtoolOrder?:string[]}五个配置选项说明如下includeHarnessIdentity是否在部署persona为LLM设置的人设之前插入固定的 DeepSeek Harness 身份标识默认为true。如果部署方想完全自定义身份、不暴露 Harness 品牌可设为falseincludeRuntimeContext是否把动态运行时上下文快照时间、环境、用户信息等注入到模型历史中默认为truepersonaPrefix部署级的persona前缀模板插在第一方指导语之前默认为空字符串personaSuffix部署级的persona后缀模板插在第一方指导语之后默认为空字符串toolOrder指定面向模型的工具名顺序省略则按字典序排列。我们在上面演示程序的基础上在调用ctx.plugin方法注册SystemPrompt插件的同时提供了一个定制的Config对象并设置了作为人设的personaPrefix和personaSuffix。import{Context}fromdeepseek-ai/cordisimport{SystemPrompt,renderPrompt,Config}fromdeepseek-ai/dsh-system-promptconstconfig:Config{personaPrefix:你是一个皆具时尚敏感度有深谙养生之道的个人助理。,personaSuffix:全程使用中文回答问题。}constctxnewContext()ctx.plugin(SystemPrompt,config)constfiberctx.inject([systemPrompt],asyncctx{constassembleawaitctx.systemPrompt.assemble()console.log(renderPrompt(assemble))})awaitfiber.await()输出You are an AI agent powered by DeepSeek Harness. 你是一个皆具时尚敏感度有深谙养生之道的个人助理。 全程使用中文回答问题。由于这两个配置最终会转换成AssembledSection出现在PromptAssembly的sections字段中所以调用renderPrompt函数渲染注册来系统提示词文本会包含它们的内容。3. 注册一组有序的“章节”PromptAssembly的sections返回的PromptSection列表是系统提示词的核心组成部分我们可以调用SystemPrompt的section方法对此进行注册。exportclassSystemPromptextendsService{section(section:PromptSection):()void}exportinterfacePromptSection{readonlyname:stringreadonlyorder:numberreadonlytext:string|((context:AssembleContext)string)readonlycomplete?:boolean}exportinterfaceAssembleContext{scope?:ScopeKey signal?:AbortSignal}name唯一标识同层重复注册报错作用域同名遮蔽全局order拼接顺序升序text正文静态字符串或按AssembleContext求值的函数complete标记排他性完整提示词整个PromptSection列表中最多只能出现一个。如果出现只会使用这个唯一的PromptSection。在上面演示程序的基础上我们调用了section方法注册了两个PromptSection并刻意以注册的相反的顺序设置了它的order字段。import{Context}fromdeepseek-ai/cordisimport{SystemPrompt,renderPrompt,Config}fromdeepseek-ai/dsh-system-promptconstconfig:Config{personaPrefix:你是一个皆具时尚敏感度有深谙养生之道的个人助理。,personaSuffix:全程使用中文回答问题。}constctxnewContext()ctx.plugin(SystemPrompt,config)constfiberctx.inject([systemPrompt],asyncctx{constdisposables:(()void)[][]disposables.push(ctx.systemPrompt.section({name:requirement-focus,text:对于用户提出的问题应该尽量分析他/她的最终需求并针对需求回答问题。,order:2}))disposables.push(ctx.systemPrompt.section({name:concise,text:回答问题尽可能简洁不要啰嗦。,order:1}))constassembleawaitctx.systemPrompt.assemble()console.log(renderPrompt(assemble))return()disposables.forEach(disposedispose())})awaitfiber.await()输出You are an AI agent powered by DeepSeek Harness. 你是一个皆具时尚敏感度有深谙养生之道的个人助理。 回答问题尽可能简洁不要啰嗦。 对于用户提出的问题应该尽量分析他/她的最终需求并针对需求回答问题。 全程使用中文回答问题。4. 注册一组有序的动态上下文SystemPrompt的context方法用来注册一条动态运行时上下文返回一个。注册的内容最终会作为运行时快照消息插入模型历史并不是以进入系统提示词文本的形式出现的。exportclassSystemPromptextendsService{context(context:PromptContext):()void}exportinterfacePromptContext{readonlyname:stringreadonlyorder:numberreadonlytext:string|((context:AssembleContext)string)}PromptContext定义更为简单相较于PromptSection少了complete字段。我们在上面演示程序的基础上调用context方法注册了一个名为current-date提供了当前的日期。由于renderPrompt函数仅仅用来渲染系统提示词文本所以我们需要利用renderContextSnapshot函数以上下文快照的形式进行渲染。import{Context}fromdeepseek-ai/cordisimport{SystemPrompt,renderPrompt,renderContextSnapshot,Config}fromdeepseek-ai/dsh-system-promptconstconfig:Config{personaPrefix:你是一个皆具时尚敏感度有深谙养生之道的个人助理。,personaSuffix:全程使用中文回答问题。,includeRuntimeContext:true}constctxnewContext()ctx.plugin(SystemPrompt,config)constfiberctx.inject([systemPrompt],asyncctx{constdisposables:(()void)[][]...disposables.push(ctx.systemPrompt.context({name:current-date,text:_当前日期为${newDate()},order:1}))constassembleawaitctx.systemPrompt.assemble()console.log(renderPrompt(assemble))console.log(renderContextSnapshot(assemble))return()disposables.forEach(disposedispose())})awaitfiber.await()输出你是一个皆具时尚敏感度有深谙养生之道的个人助理。 回答问题尽可能简洁不要啰嗦。 对于用户提出的问题应该尽量分析他/她的最终需求并针对需求回答问题。 全程使用中文回答问题。 Current runtime context. This snapshot supersedes earlier runtime-context snapshots. 当前日期为Sat Sep 26 2026 06:53:02 GMT0800 (China Standard Time)5. 提供工具SchemaSystemPrompt的tools方法用来注册工具的Schema它的参数是一个根据指定的AssembleContext用来提供ToolProviderResult的函数。ToolProviderResult接口包含两个字段schemas提供一组描述工具的SchemaknownNames提供有已知的工具名称。exportclassSystemPromptextendsService{tools(provider:(context:AssembleContext)ToolProviderResult):()void}exportinterfaceToolProviderResult{readonlyschemas:readonlyToolSchema[]readonlyknownNames?:readonlystring[]}DeepSeek Harness深度拆解-12:揭秘工具完整的执行流程对工具执行流程进行了详细介绍但依然漏掉很多细节比如在作为工具运行时ToolRuntime的构造函数中会将注册工具基于当前Scope的Schema和名称列表生成一个ToolProviderResult对象并通过调用tools方法注册到SystemPrompt服务上。exportclassToolRuntimeextendsService{constructor(ctx:Context,config:Config{}){...ctx.systemPrompt.tools(contextthis.wireSchemas(context.scope))...}privatewireSchemas(scope?:ScopeKey):ToolProviderResult}我们在上面演示实例的基础上进一步以如下的方式调用tools方法注册了一个get_weather工具对应的ToolProviderResult我们指定了工具描述和参数Schema。import{Context}fromdeepseek-ai/cordisimport{SystemPrompt,ToolProviderResult,renderPrompt,renderContextSnapshot,Config}fromdeepseek-ai/dsh-system-promptconstconfig:Config{personaPrefix:你是一个皆具时尚敏感度有深谙养生之道的个人助理。,personaSuffix:全程使用中文回答问题。,includeRuntimeContext:true}constctxnewContext()ctx.plugin(SystemPrompt,config)constfiberctx.inject([systemPrompt],asyncctx{constdisposables:(()void)[][]...consttoolProvider:ToolProviderResult{knownNames:[get_weather],schemas:[{name:get_weather,description:Get specified citys weather information.,parameters:{city:{type:string,description:The city to look up weather for.},}}]asconst}disposables.push(ctx.systemPrompt.tools(_toolProvider))constassembleawaitctx.systemPrompt.assemble()console.log(renderPrompt(assemble))console.log(renderContextSnapshot(assemble))console.log(\nassemble.tools:)console.log(JSON.stringify(assemble.tools,null,2))return()disposables.forEach(disposedispose())})awaitfiber.await()输出You are an AI agent powered by DeepSeek Harness. 你是一个皆具时尚敏感度有深谙养生之道的个人助理。 回答问题尽可能简洁不要啰嗦。 对于用户提出的问题应该尽量分析他/她的最终需求并针对需求回答问题。 全程使用中文回答问题。 Current runtime context. This snapshot supersedes earlier runtime-context snapshots. 当前日期为Sat Sep 26 2026 08:38:16 GMT0800 (China Standard Time) assemble.tools: [ { name: get_weather, description: Get specified citys weather information., parameters: { city: { type: string, description: The city to look up weather for. } } } ]6. 使用变量在Agent开发中提示词一般以模板的形式定义也就是说我们将提示词文本内容包括PromptSection和PromptContext的text字段定义成包含占位符的模板在渲染的时候利用指定的变量对占位符进行替换。SystemPrompt提供了用来定义变量的variable方法具体的变量由指定的将AssembleContext作为输入的函数来提供。exportclassSystemPromptextendsService{variable(name:string,provider:(context:AssembleContext)string|undefined):()void}在如下的演示程序中我们将Config的personaPrefix设置成一个包含占位符{{persona}}的模板对应的变量在注册的插件中通过调用ctx.systemPrompt.variable方法定义。import{Context}fromdeepseek-ai/cordisimport{SystemPrompt,renderPrompt,Config}fromdeepseek-ai/dsh-system-promptconstconfig:Config{personaPrefix:你是一个{{persona}}。,personaSuffix:全程使用中文回答问题。,includeRuntimeContext:true}constctxnewContext()ctx.plugin(SystemPrompt,config)constfiberctx.inject([systemPrompt],asyncctx{constdisposables:(()void)[][]ctx.systemPrompt.variable(persona,_皆具时尚敏感度有深谙养生之道的个人助理)constassembleawaitctx.systemPrompt.assemble()console.log(renderPrompt(assemble))return()disposables.forEach(disposedispose())})awaitfiber.await()输出You are an AI agent powered by DeepSeek Harness. 你是一个皆具时尚敏感度有深谙养生之道的个人助理。 全程使用中文回答问题。7. 基于Scope的注册SystemPrompt被注册为一个全局单例服务它基于Scope的注册体现在如下两个方面方法(section、context、tools和variable等)注册的元素于当前Context所在的Scope关联并由此具有作用范围比如针对具体的AgentPromptSection、PromptContext的text字段以及variable方法注册的变量可以是一段静态文本也可以是一个将AssembleContext作为输入的函数意味着对应的值可以根据当前Scope动态决定。SystemPrompt的assemble方法也提供了context参数我们有利用它指定具体的ScopeKey获取指定范围的PromptAssemble对象。exportclassSystemPromptextendsService{asyncassemble(context:AssembleContext{})}上述的基于Scope的提示词注册清晰地体现在如下这个演示程序中。如代码所示我们将配置的personaPrefix字段设置为一个包含{{persona}}占位符的模板。在调用inject方法以systemPrompt作为依赖服务的插件中我们调用createScope函数创建两个Scope分别模拟两个分别用来安排交通行程和酒店住宿的Agent。我们调用ctx.systemPrompt.variable方法根据AssembleContext提供的Scope动态执行persona变量的值。并在这两个Scope对用的Context中通过注册了同名authorization的PromptSection。最后我们将注册的内嵌插件中针对两个Scope生成了对应的PromptAssemble并将渲染的文本输出来。import{Context}fromdeepseek-ai/cordisimport{createScope,}fromdeepseek-ai/dsh-scopeimport{SystemPrompt,renderPrompt}fromdeepseek-ai/dsh-system-promptconstctxnewContext()ctx.plugin(SystemPrompt,{personaPrefix:你是一个{{persona}}。})consttransport_assistant{agent:transport assistant}constaccommodation_assistant{agent:accommodation assistant}ctx.inject([systemPrompt],ctx{ctx.systemPrompt.variable(persona,context{if(transport_assistantcontext.scope){return专门负责交通行程安排的助理}if(accommodation_assistantcontext.scope){return专门住宿安排的助理}return资深个人助理})consttransport_assistant_scopecreateScope(ctx,transport_assistant)constaccommodation_assistant_scopecreateScope(ctx,accommodation_assistant)transport_assistant_scope.ctx.systemPrompt.section({name:authorization,text:授予你自动下单购买机票的权限,order:1})accommodation_assistant_scope.ctx.systemPrompt.section({name:authorization,text:授予你自动预定酒店的权限,order:1})ctx.inject([systemPrompt],asyncctx{letassembleawaitctx.systemPrompt.assemble({scope:transport_assistant})console.log(For transport assistant:\n\n${renderPrompt(assemble)})assembleawaitctx.systemPrompt.assemble({scope:accommodation_assistant})console.log(For accommodation assistant:\n\n${renderPrompt(assemble)})})})输出For transport assistant: You are an AI agent powered by DeepSeek Harness. 你是一个专门负责交通行程安排的助理。 授予你自动下单购买机票的权限 For accommodation assistant: You are an AI agent powered by DeepSeek Harness. 你是一个专门住宿安排的助理。 授予你自动预定酒店的权限
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

◈

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

◐

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

▲

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。