Add prompt templates & variables

This is only recommended for LLM Spans.

By instrumenting prompt templates and variables, users can experiment with prompt changes in the Arize Prompt Playground. You can directly modify the prompt, variables, and model in the Playground UI to explore how different configurations impact the output. From here, you can test your prompt on a dataset and run evals to see how your changes perform overall.

We provide a using_prompt_template context manager (example below) to add a prompt template to the current OpenTelemetry Context. OpenInference auto-instrumentors will read this Context and pass the prompt template fields as span attributes, following the OpenInference semantic conventions. The interface expects the following:

Param
Type
Example

template

str

"Please describe the best activity for me to do in {city} on {date}"

version

str

"v1.0"

variables

Dict[str]

{"city": "Johannesburg", "date":"July 11"}

pip install openinference-semantic-conventions openinference-instrumentation-openai arize-otel openai
from openai import OpenAI
from openinference.instrumentation.openai import OpenAIInstrumentor
from openinference.instrumentation import using_prompt_template
from arize.otel import register

# Setup OTEL via our convenience function.
tracer_provider = register(
    space_id= "", # your Arize Space ID
    api_key= "", # your Arize API Key
    project_name= "" # your Arize project name
)
from openinference.instrumentation import using_prompt_template

client = OpenAI()

prompt_template = "Please describe the best activity for me to do in {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",
    ):
    response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[
          {
              "role": "user",
              "content": prompt_template.format(**prompt_template_variables)},
        ]
    )

Last updated

Was this helpful?