import dspy
import os
from openinference.semconv.trace import SpanAttributes # For using_attributes
from openinference.instrumentation import using_attributes # For custom attributes
# Ensure OPENAI_API_KEY is set in your environment
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
class BasicQA(dspy.Signature):
"""Answer questions with short factoid answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
# Configure the LLM. DSPy can use various LLMs, often via LiteLLM.
# For OpenAI models, ensure OPENAI_API_KEY is set.
# The dspy.OpenAI client might internally use LiteLLM or a direct OpenAI client.
turbo = dspy.LM(model="gpt-3.5-turbo")
dspy.configure(lm=turbo)
# Example of adding custom attributes to traces using OpenInference context manager
with using_attributes(
session_id="my-dspy-session-001",
user_id="user-dspy-example",
metadata={
"environment": "testing",
"dspy_module": "BasicQA",
},
tags=["dspy", "qa"],
prompt_template_version="1.0",
prompt_template_variables={
"signature_desc": BasicQA.__doc__.strip()
}
):
# Define the predictor.
generate_answer = dspy.Predict(BasicQA)
# Call the predictor on a particular input.
pred = generate_answer(
question="What is the capital of the Atlantis?"
)
print(f"Question: What is the capital of the Atlantis?")
print(f"Predicted Answer: {pred.answer}")
pred_europe = generate_answer(
question="What is the capital of France?"
)
print(f"Question: What is the capital of France?")
print(f"Predicted Answer: {pred_europe.answer}")
print("DSPy example run complete. Check Arize for traces.")