Instrument and observe your DSPy application via the DSPyInstrumentor
DSPy is a framework for automatically prompting and fine-tuning language models. It provides composable and declarative APIs that allow developers to describe the architecture of their LLM application in the form of a "module" (inspired by PyTorch's nn.Module). It them compiles these modules using "teleprompters" that optimize the module for a particular task. The term "teleprompter" is meant to evoke "prompting at a distance," and could involve selecting few-shot examples, generating prompts, or fine-tuning language models.
Phoenix makes your DSPy applications observable by visualizing the underlying structure of each call to your compiled DSPy module.
import osfrom phoenix.otel import register# Add Phoenix API Key for tracingPHOENIX_API_KEY ="ADD YOUR API KEY"os.environ["PHOENIX_CLIENT_HEADERS"]=f"api_key={PHOENIX_API_KEY}"# configure the Phoenix tracertracer_provider =register( project_name="my-llm-app", # Default is 'default' endpoint="https://app.phoenix.arize.com/v1/traces",)
Your Phoenix API key can be found on the Keys section of your dashboard.
Launch your local Phoenix instance:
pipinstallarize-phoenixphoenixserve
For details on customizing a local terminal deployment, see Terminal Setup.
Install packages:
pipinstallarize-phoenix-otel
Connect your application to your instance using:
from phoenix.otel import registertracer_provider =register( project_name="my-llm-app", # Default is 'default' endpoint="http://localhost:6006/v1/traces",)
from phoenix.otel import registertracer_provider =register( project_name="my-llm-app", # Default is 'default' endpoint="http://localhost:6006/v1/traces",)
For more info on using Phoenix with Docker, see Docker
Install packages:
pipinstallarize-phoenix
Launch Phoenix:
import phoenix as pxpx.launch_app()
Connect your notebook to Phoenix:
from phoenix.otel import registertracer_provider =register( project_name="my-llm-app", # Default is 'default')
By default, notebook instances do not have persistent storage, so your traces will disappear after the notebook is closed. See Persistence or use one of the other deployment options to retain traces.
Install
pipinstallopeninference-instrumentation-dspydspy
Setup
Initialize the DSPyInstrumentor before your application code.
from openinference.instrumentation.dspy import DSPyInstrumentorDSPyInstrumentor().instrument(tracer_provider=tracer_provider)
DSPy uses LiteLLM under the hood to handle LLM calls. By also instrumenting LiteLLM, you'll be able to see token counts on your DSPy spans and traces.
from openinference.instrumentation.litellm import LiteLLMInstrumentorLiteLLMInstrumentor().instrument(tracer_provider=tracer_provider)
Run DSPy
Now run invoke your compiled DSPy module. Your traces should appear inside of Phoenix.
classBasicQA(dspy.Signature):"""Answer questions with short factoid answers.""" question = dspy.InputField() answer = dspy.OutputField(desc="often between 1 and 5 words")if__name__=="__main__": turbo = dspy.OpenAI(model="gpt-3.5-turbo") dspy.settings.configure(lm=turbo)withusing_attributes( session_id="my-test-session", user_id="my-test-user", metadata={"test-int": 1,"test-str": "string","test-list": [1, 2, 3],"test-dict": {"key-1": "val-1","key-2": "val-2", }, }, tags=["tag-1", "tag-2"], prompt_template_version="v1.0", prompt_template_variables={"city": "Johannesburg","date": "July 11th", }, ):# 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 united states?"# noqa: E501 )# noqa: E501print(f"Predicted Answer: {pred.answer}")
Observe
Now that you have tracing setup, all predictions will be streamed to your running Phoenix for observability and evaluation.