提示模板与变量的结构化定义

该文档说明提示模板由模板字符串、版本号和变量字典三部分组成,变量字典保存到 OTEL Context 时序列化为 JSON,作为 span 属性发送时保持 JSON 字符串形式。

设置快速模板和变量,使您能够跟踪和可视化快速变化。这些也可以与 [实验](<https://arize.com/docs/phoenix/datasets-and-experiments/how-to-experiments/running-experiments>) 以测量您每个提示所驱动的性能变化。 - Python 键 - TypeScript - 滚开 我们提供 `using_prompt_template` 上下文管理器在当前的 OpenTelemetry 上下文中添加一个快速模板(包括其版本和变量). OpenInference [auto-instrumentors](<https://github.com/Arize-ai/openinference/tree/main/python/instrumentation>) 将在 OpenInference 之后读取此上下文并按跨度属性通过提示模板字段 [语义公约](<https://github.com/Arize-ai/openinference/tree/main/python/openinference-semantic-conventions>)其投入必须是以下类型: - Template:非空字符串. - 版本:非空字符串. - 变量:一个带有字符串键的字典. 此字典在保存到OTEL上下文时会被序列化为JSON,在作为跨度属性发送时仍为JSON字符串. ``` from phoenix.otel import using_prompt_template prompt_template = "Please describe the weather forecast for {city} on {date}" prompt_template_variables = {"city": "Johannesburg", "date":"July 11"} with using_prompt_template( template=prompt_template, variables=prompt_template_variables, version="v1.0", ): # Commonly precedes a chat completion to append templates to auto instrumentation # response = client.chat.completions.create() # Calls within this block will generate spans with the attributes: # "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}" # "llm.prompt_template.version" = "v1.0" # "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized ... ``` 也可以用作装饰品: ``` @using_prompt_template( template=prompt_template, variables=prompt_template_variables, version="v1.0", ) def call_fn(*args, **kwargs): # Calls within this function will generate spans with the attributes: # "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}" # "llm.prompt_template.version" = "v1.0" # "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized ... ``` 我们提供 `setPromptTemplate` 函数允许您在上下文上设置模板、版本和变量。您可以结合 [`context.with`](<https://opentelemetry.io/docs/languages/js/context/#set-active-context>) 来设置活动上下文。然后,OpenInference 自动仪器将拾取这些属性,并将其添加到在 `context.with` 调用。 快速模板的组件是 : - 模板 - 有模板变量的字符串。 `"hello {{name}}"` - 变量 - 一个具有可变名称及其数值的对象。 `{name: "world"}` - 版本 - 模板前的字符串版本。 `v1.0` 所有这些都是可选的。 将变量应用到模板中通常会在调用到 llm 之前发生, 并且可能不会被自动仪器采集。 因此, 这样做有助于确保您在排除故障时能够看到模板和变量 。 ``` import { context, setPromptTemplate } from "@arizeai/phoenix-otel" context.with( setPromptTemplate( context.active(), { template: "hello {{name}}", variables: { name: "world" }, version: "v1.0" } ), () => { // Calls within this block will generate spans with the attributes: // "llm.prompt_template.template" = "hello {{name}}" // "llm.prompt_template.version" = "v1.0" // "llm.prompt_template.variables" = '{ "name": "world" }' } ) ``` Go 今天没有即时模板上下文帮助器。 使用输入的常数直接在 LLM 间距上设置即时模板属性 。 [`github.com/Arize-ai/openinference/go/openinference-semantic-conventions`](<https://github.com/Arize-ai/openinference/tree/main/go/openinference-semantic-conventions>): ``` import ( "encoding/json" "go.opentelemetry.io/otel/attribute" "github.com/Arize-ai/openinference/go/openinference-semantic-conventions" ) variables := map[string]string{"city": "Johannesburg", "date": "July 11"} varsJSON, _ := json.Marshal(variables) span.SetAttributes( attribute.String(semconv.LLMPromptTemplate, "Please describe the weather forecast for {city} on {date}"), attribute.String(semconv.LLMPromptTemplateVersion, "v1.0"), attribute.String(semconv.LLMPromptTemplateVariables, string(varsJSON)), ) ``` 这页有用吗? [建议编辑](<https://github.com/arize-ai/phoenix/edit/main/docs/phoenix/tracing/how-to-tracing/add-metadata/instrumenting-prompt-templates-and-prompt-variables.mdx>)提出问题

查看原始来源