Log Prompt Templates & Variables

By instrumenting the prompt template, users can take full advantage of the Arize prompt playground. You don't need to deploy a new template version in order to see if prompt text or prompt variables changes have the intended effect. Instead, you can experiment with these changes in the playground UI.

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:

ParamTypeExample

template

str

"Please describe the weather forecast for {city} on {date}"

version

str

"v1.0"

variables

Dict[str]

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

Refer to the code below for a working example:

pip install -qq opentelemetry-api opentelemetry-sdk openinference-semantic-conventions openinference-instrumentation-openai opentelemetry-exporter-otlp arize-otel openai
import os
from getpass import getpass

import openai
import opentelemetry
from arize_otel import Endpoints, register_otel
from openai import OpenAI
from openinference.instrumentation import using_prompt_template
from openinference.instrumentation.openai import OpenAIInstrumentor


os.environ["OPENAI_API_KEY"] = getpass("Enter your Open AI API key: ")

my_first_model = "my first model"
register_otel(
    endpoints=Endpoints.ARIZE,
    space_id=getpass("Enter your Arize Space ID: "),
    api_key=getpass("Enter your Arize API Key: "),
    model_id=my_first_model,
)
OpenAIInstrumentor().instrument()
client = OpenAI()

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",
    ):
    response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[
          {
              "role": "user",
              "content": prompt_template.format(**prompt_template_variables)},
        ]
    )

Last updated

Copyright © 2023 Arize AI, Inc