Before running your application, ensure you have the following environment variables set:
export ARIZE_SPACE_ID="YOUR_ARIZE_SPACE_ID"export ARIZE_API_KEY="YOUR_ARIZE_API_KEY"export OPENAI_API_KEY="YOUR_OPENAI_API_KEY" # Needed for the OpenAIGenerator example# Add other LLM provider API keys if used by Haystack components
You can find your Arize Space ID and API Key in your Arize account settings.
Connect to Arize using arize.otel.register and apply the HaystackInstrumentor.
import osfrom arize.otel import registerfrom openinference.instrumentation.haystack import HaystackInstrumentor# Setup OTel via Arize's convenience functiontracer_provider = register( space_id=os.getenv("ARIZE_SPACE_ID"), # or directly pass your Space ID api_key=os.getenv("ARIZE_API_KEY"), # or directly pass your API Key project_name="my-haystack-app" # Choose a project name)# Instrument HaystackHaystackInstrumentor().instrument(tracer_provider=tracer_provider)print("Haystack instrumented for Arize.")
Arize’s auto-instrumentor primarily collects traces from Haystack Pipelines. If you are using Haystack components outside of a Pipeline, you may need to manually instrument those parts or ensure the components themselves are instrumented if they make calls to instrumented LLMs (e.g., an instrumented OpenAI client).
Here’s how to set up and run a simple Haystack Pipeline. The instrumentor will capture traces from this pipeline.
# Ensure OPENAI_API_KEY is set in your environment for this examplefrom haystack import Pipelinefrom haystack.components.generators import OpenAIGeneratorfrom haystack.components.builders.prompt_builder import PromptBuilder # Added for a more complete example# Define a prompt templateprompt_template = """Answer the following question based on your knowledge.Question: {{question}}Answer:"""# Initialize the pipelinepipeline = Pipeline()# Initialize Haystack componentsprompt_builder = PromptBuilder(template=prompt_template)llm = OpenAIGenerator(model="gpt-3.5-turbo") # Uses OPENAI_API_KEY# Add components to the pipelinepipeline.add_component(name="prompt_builder", instance=prompt_builder)pipeline.add_component(name="llm", instance=llm)# Connect the componentspipeline.connect("prompt_builder.prompt", "llm.prompt")# Define the questionquestion_to_ask = "What is the location of the Hanging Gardens of Babylon?"# Run the pipelineresponse = pipeline.run({ "prompt_builder": {"question": question_to_ask}})# Print the response from the LLMif response and "llm" in response and "replies" in response["llm"]: print(f"Question: {question_to_ask}") print(f"Answer: {response['llm']['replies'][0]}")else: print(f"Failed to get a response or response format is unexpected: {response}")