类名:InstructionUtils
java.lang.Object
com.google.adk.utils.InstructionUtils
用于处理指令模板的一些实用方法(utility methods)。
-
方法概述
Modifier and TypeMethodDescriptionstatic io.reactivex.rxjava3.core.Single<String> injectSessionState(InvocationContext context, String template) 使用会话状态中的数据或已加载的资源(artifacts),填充指令模板字符串中的占位符(placeholders)。
-
方法详细信息(Method Details)
-
injectSessionState
public static io.reactivex.rxjava3.core.Single<String> injectSessionState(InvocationContext context, String template) 使用会话状态中的数据或已加载的资源(artifacts),填充指令模板字符串中的占位符(placeholders)。占位符语法(Placeholder Syntax):
占位符的开始和结束部分都用一个或多个大括号({ })括起来,例如:
{key}或者{{key}}. 核心key该内容是从最内层大括号对之间的所有文本中提取出来的;在提取之前,会先去除空白字符(如空格、换行符等),并且可能会删除某些不必要的内容。?“which”表示某种选择或可能性(例如:……){key?}). Thekey该内容本身不得包含大括号({ })。在典型用法中,只需要使用一对大括号即可。{my_variable}这是标准配置(is standard)。被提取的内容
key用于确定该值的来源及其名称:- 会话状态变量(Session State Variables): The
key(e.g.,"variable_name"或者"prefix:variable_name"“)”指的是会话状态(session state)中的一个变量。 - Artifacts: The
key以 “” 开头artifact." (e.g.,"artifact.file_name"). - 可选的占位符: A
key可以通过在选项后添加问号(?)来将其标记为“可选”选项。?在代码的最后部分,位于大括号({ })的内部。- 示例:
{optional_variable?},{{artifact.optional_file.txt?}} - 如果某个可选的占位符无法被解析(例如:变量未找到、相关资源未找到),则该占位符会被替换为一个空字符串。
- 示例:
- 已逃出的占位符(Escaped Placeholders): 一个紧接在……之后的占位符。
$或者\例如:${key},${{key}}或者\{key}.- 当……的时候
RunConfig.dollarEscapedPlaceholders()是true该内容保持原样,包括反斜杠(\)。 - 当……的时候
false默认情况下,该占位符会像其他占位符一样被处理(即会被替换为相应的内容)。$或者\在它被保存之前……${key}使用(with)key设置为9.99变成;成为$9.99.
- 当……的时候
示例用法:
InvocationContext context = ...; // Assume this is initialized with session and artifact service Session session = context.session(); session.state().put("user:name", "Alice"); context.artifactService().saveArtifact( session.appName(), session.userId(), session.id(), "knowledge.txt", Part.fromText("Origins of the universe: At first, there was-")); String template = "You are {user:name}'s assistant. Answer questions based on your knowledge. Your knowledge: {artifact.knowledge.txt}." + " Your extra knowledge: {artifact.missing_artifact.txt?}"; Single<String> populatedStringSingle = InstructionUtils.injectSessionState(context, template); populatedStringSingle.subscribe( result -> System.out.println(result), // Expected: "You are Alice's assistant. Answer questions based on your knowledge. Your knowledge: Origins of the universe: At first, there was-. Your extra knowledge: " error -> System.err.println("Error populating template: " + error.getMessage()) );- Parameters:
context- The invocation context providing access to session state and artifact services.template- The instruction template string containing placeholders to be populated.- Returns:
- A
Singlethat will emit the populated instruction string upon successful resolution of all non-optional placeholders. Emits the original template if it is empty or contains no placeholders that are processed. - Throws:
NullPointerException- if the template or context is null.IllegalArgumentException- if a non-optional variable or artifact is not found.
- 会话状态变量(Session State Variables): The
-