1. Installation
Install the traceAI and dspy package.
pip install traceAI-DSPy dspy
2. Set Environment Variables
Set up your environment variables to authenticate with FutureAGI and OpenAI.
import os
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"
3. Initialize Trace Provider
Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines .
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType
trace_provider = register(
project_type=ProjectType.OBSERVE,
project_name="dspy_project",
)
4. Instrument your Project
Initialize the DSPy instrumentor to enable automatic tracing.
from traceai_dspy import DSPyInstrumentor
DSPyInstrumentor().instrument(tracer_provider=trace_provider)
5. Create DSPy Components and Run your application
Run DSPy as you normally would. Our Instrumentor will automatically trace and send the telemetry data to our platform.
import dspy
class BasicQA(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.LM(model="openai/gpt-4")
dspy.settings.configure(lm=turbo)
# 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?")
print(f"Predicted Answer: {pred.answer}")