|
| 1 | +--- |
| 2 | +title: Haystack |
| 3 | +description: "Monitor your Haystack agents with AgentOps" |
| 4 | +--- |
| 5 | + |
| 6 | +[Haystack](https://docs.haystack.deepset.ai/docs/installation) is a flexible framework for building production-ready AI agents. AgentOps makes monitoring your Haystack agents seamless. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +<CodeGroup> |
| 11 | +```bash pip |
| 12 | +pip install agentops haystack-ai python-dotenv |
| 13 | +``` |
| 14 | +```bash poetry |
| 15 | +poetry add agentops haystack-ai python-dotenv |
| 16 | +``` |
| 17 | +```bash uv |
| 18 | +uv pip install agentops haystack-ai python-dotenv |
| 19 | +``` |
| 20 | +</CodeGroup> |
| 21 | + |
| 22 | +<Warning> |
| 23 | +We currently only support Haystack 2.x. |
| 24 | +</Warning> |
| 25 | + |
| 26 | +## Setting Up API Keys |
| 27 | + |
| 28 | +You'll need API keys for AgentOps and whatever model provider you use with Haystack (e.g. OpenAI): |
| 29 | + |
| 30 | +- **AGENTOPS_API_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/) |
| 31 | +- **OPENAI_API_KEY**: From the [OpenAI Platform](https://platform.openai.com/api-keys) *(if using OpenAI models)* |
| 32 | + |
| 33 | +Set these as environment variables or in a `.env` file. |
| 34 | + |
| 35 | +<CodeGroup> |
| 36 | +```bash Export to CLI |
| 37 | +export AGENTOPS_API_KEY="your_agentops_api_key_here" |
| 38 | +export OPENAI_API_KEY="your_openai_api_key_here" |
| 39 | +``` |
| 40 | +```txt Set in .env file |
| 41 | +AGENTOPS_API_KEY="your_agentops_api_key_here" |
| 42 | +OPENAI_API_KEY="your_openai_api_key_here" |
| 43 | +``` |
| 44 | +</CodeGroup> |
| 45 | + |
| 46 | +Then load them in your Python code: |
| 47 | + |
| 48 | +```python |
| 49 | +from dotenv import load_dotenv |
| 50 | +import os |
| 51 | + |
| 52 | +load_dotenv() |
| 53 | +AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") |
| 54 | +OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
| 55 | +``` |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +Integrating AgentOps with Haystack only takes a few lines: |
| 60 | + |
| 61 | +```python |
| 62 | +import agentops |
| 63 | +from haystack.components.generators.openai import OpenAIGenerator |
| 64 | + |
| 65 | +agentops.init(AGENTOPS_API_KEY) |
| 66 | + |
| 67 | +generator = OpenAIGenerator(model="gpt-4o-mini", api_key=OPENAI_API_KEY) |
| 68 | +result = generator.run(prompt="In one sentence, what is AgentOps?") |
| 69 | +print(result["replies"][0]) |
| 70 | +``` |
| 71 | + |
| 72 | +Run your script and visit the [AgentOps Dashboard](https://app.agentops.ai/drilldown) to monitor the trace. |
| 73 | + |
| 74 | +## Examples |
| 75 | + |
| 76 | +- [Simple Haystack example (OpenAI)](https://github.com/AgentOps-AI/agentops/blob/main/examples/haystack/haystack_example.py) |
| 77 | +- [Haystack Azure OpenAI Chat example](https://github.com/AgentOps-AI/agentops/blob/main/examples/haystack/azure_haystack_example.py) |
| 78 | + |
| 79 | +<script type="module" src="/scripts/github_stars.js"></script> |
| 80 | +<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script> |
| 81 | +<script type="module" src="/scripts/button_heartbeat_animation.js"></script> |
| 82 | +<script type="module" src="/scripts/adjust_api_dynamically.js"></script> |
| 83 | + |
0 commit comments