respan-exporter-haystack 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- respan_exporter_haystack-1.0.0/LICENSE +21 -0
- respan_exporter_haystack-1.0.0/PKG-INFO +348 -0
- respan_exporter_haystack-1.0.0/README.md +325 -0
- respan_exporter_haystack-1.0.0/pyproject.toml +26 -0
- respan_exporter_haystack-1.0.0/src/respan_exporter_haystack/__init__.py +15 -0
- respan_exporter_haystack-1.0.0/src/respan_exporter_haystack/connector.py +125 -0
- respan_exporter_haystack-1.0.0/src/respan_exporter_haystack/gateway.py +387 -0
- respan_exporter_haystack-1.0.0/src/respan_exporter_haystack/logger.py +68 -0
- respan_exporter_haystack-1.0.0/src/respan_exporter_haystack/tracer.py +429 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Keywords AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: respan-exporter-haystack
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Respan integration for Haystack pipelines with tracing and logging support
|
|
5
|
+
Home-page: https://respan.ai
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: haystack,respan,llm,observability,tracing,monitoring
|
|
8
|
+
Author: Respan
|
|
9
|
+
Author-email: team@respan.ai
|
|
10
|
+
Requires-Python: >=3.9,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Dist: haystack-ai (>=2.0.0,<3.0.0)
|
|
18
|
+
Requires-Dist: requests (>=2.31.0,<3.0.0)
|
|
19
|
+
Project-URL: Documentation, https://docs.respan.ai
|
|
20
|
+
Project-URL: Repository, https://github.com/respan-ai/respan-sdks
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# Keywords AI Haystack Integration
|
|
24
|
+
|
|
25
|
+
Monitor and optimize your Haystack pipelines with Keywords AI's LLM observability platform.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
### Gateway Mode
|
|
30
|
+
Route LLM calls through Keywords AI gateway:
|
|
31
|
+
- Automatic logging (zero config)
|
|
32
|
+
- Model fallbacks & retries
|
|
33
|
+
- Load balancing
|
|
34
|
+
- Cost optimization
|
|
35
|
+
- Rate limiting & caching
|
|
36
|
+
|
|
37
|
+
### Tracing Mode
|
|
38
|
+
Capture full workflow execution:
|
|
39
|
+
- Multi-component pipelines
|
|
40
|
+
- Parent-child span relationships
|
|
41
|
+
- Timing per component
|
|
42
|
+
- Input/output tracking
|
|
43
|
+
- RAG + Agent workflows
|
|
44
|
+
|
|
45
|
+
### Combined Mode (Recommended)
|
|
46
|
+
Use both together for:
|
|
47
|
+
- Gateway reliability + Tracing visibility
|
|
48
|
+
- Production-ready monitoring
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install keywordsai-exporter-haystack
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Quick Start
|
|
59
|
+
|
|
60
|
+
### 1. Get API Keys
|
|
61
|
+
|
|
62
|
+
- [Keywords AI API Key](https://platform.keywordsai.co/)
|
|
63
|
+
- OpenAI API Key (for examples)
|
|
64
|
+
|
|
65
|
+
### 2. Set Environment Variables
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
export KEYWORDSAI_API_KEY="your-keywords-ai-key"
|
|
69
|
+
export OPENAI_API_KEY="your-openai-key"
|
|
70
|
+
export HAYSTACK_CONTENT_TRACING_ENABLED="true" # For tracing mode
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Usage Examples
|
|
76
|
+
|
|
77
|
+
### Gateway Mode (Auto-Logging)
|
|
78
|
+
|
|
79
|
+
**Just replace `OpenAIGenerator` with `KeywordsAIGenerator`:**
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import os
|
|
83
|
+
from haystack import Pipeline
|
|
84
|
+
from haystack.components.builders import PromptBuilder
|
|
85
|
+
from keywordsai_exporter_haystack import KeywordsAIGenerator
|
|
86
|
+
|
|
87
|
+
# Create pipeline
|
|
88
|
+
pipeline = Pipeline()
|
|
89
|
+
pipeline.add_component("prompt", PromptBuilder(template="Tell me about {{topic}}."))
|
|
90
|
+
pipeline.add_component("llm", KeywordsAIGenerator(
|
|
91
|
+
model="gpt-4o-mini",
|
|
92
|
+
api_key=os.getenv("KEYWORDSAI_API_KEY")
|
|
93
|
+
))
|
|
94
|
+
pipeline.connect("prompt", "llm")
|
|
95
|
+
|
|
96
|
+
# Run
|
|
97
|
+
result = pipeline.run({"prompt": {"topic": "machine learning"}})
|
|
98
|
+
print(result["llm"]["replies"][0])
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**That's it!** All LLM calls are automatically logged to Keywords AI with no additional code.
|
|
102
|
+
|
|
103
|
+
**See:** [`examples/gateway_example.py`](examples/gateway_example.py)
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### Prompt Management
|
|
108
|
+
|
|
109
|
+
**Use platform-managed prompts** for centralized control:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import os
|
|
113
|
+
from haystack import Pipeline
|
|
114
|
+
from keywordsai_exporter_haystack import KeywordsAIGenerator
|
|
115
|
+
|
|
116
|
+
# Create prompt on platform: https://platform.keywordsai.co/platform/prompts
|
|
117
|
+
# Get your prompt_id from the platform
|
|
118
|
+
|
|
119
|
+
# Create pipeline with platform prompt (model config comes from platform)
|
|
120
|
+
pipeline = Pipeline()
|
|
121
|
+
pipeline.add_component("llm", KeywordsAIGenerator(
|
|
122
|
+
prompt_id="1210b368ce2f4e5599d307bc591d9b7a", # Your prompt ID
|
|
123
|
+
api_key=os.getenv("KEYWORDSAI_API_KEY")
|
|
124
|
+
))
|
|
125
|
+
|
|
126
|
+
# Run with prompt variables
|
|
127
|
+
result = pipeline.run({
|
|
128
|
+
"llm": {
|
|
129
|
+
"prompt_variables": {
|
|
130
|
+
"user_input": "The cat sat on the mat"
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
print("Response received successfully!")
|
|
136
|
+
print(f"Model: {result['llm']['meta'][0]['model']}")
|
|
137
|
+
print(f"Tokens: {result['llm']['meta'][0]['usage']['total_tokens']}")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Benefits:**
|
|
141
|
+
- Update prompts without code changes
|
|
142
|
+
- Model config managed on platform (no hardcoding)
|
|
143
|
+
- Version control & rollback
|
|
144
|
+
- A/B testing
|
|
145
|
+
- Team collaboration
|
|
146
|
+
|
|
147
|
+
**See:** [`examples/prompt_example.py`](examples/prompt_example.py)
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
### Tracing Mode (Workflow Monitoring)
|
|
152
|
+
|
|
153
|
+
**Add `KeywordsAIConnector` to capture the entire pipeline:**
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
import os
|
|
157
|
+
from haystack import Pipeline
|
|
158
|
+
from haystack.components.builders import PromptBuilder
|
|
159
|
+
from haystack.components.generators import OpenAIGenerator
|
|
160
|
+
from keywordsai_exporter_haystack import KeywordsAIConnector
|
|
161
|
+
|
|
162
|
+
os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
|
|
163
|
+
|
|
164
|
+
# Create pipeline with tracing
|
|
165
|
+
pipeline = Pipeline()
|
|
166
|
+
pipeline.add_component("tracer", KeywordsAIConnector("My Workflow"))
|
|
167
|
+
pipeline.add_component("prompt", PromptBuilder(template="Tell me about {{topic}}."))
|
|
168
|
+
pipeline.add_component("llm", OpenAIGenerator(model="gpt-4o-mini"))
|
|
169
|
+
pipeline.connect("prompt", "llm")
|
|
170
|
+
|
|
171
|
+
# Run
|
|
172
|
+
result = pipeline.run({"prompt": {"topic": "artificial intelligence"}})
|
|
173
|
+
print(result["llm"]["replies"][0])
|
|
174
|
+
print(f"\nTrace URL: {result['tracer']['trace_url']}")
|
|
175
|
+
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
**Dashboard shows:**
|
|
179
|
+
- Pipeline (root span)
|
|
180
|
+
- PromptBuilder (template processing)
|
|
181
|
+
- LLM (generation with tokens + cost)
|
|
182
|
+
|
|
183
|
+
**See:** [`examples/tracing_example.py`](examples/tracing_example.py)
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
### Combined Mode (Recommended for Production)
|
|
188
|
+
|
|
189
|
+
**Use BOTH gateway + prompt + tracing for the full stack:**
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
import os
|
|
193
|
+
from haystack import Pipeline
|
|
194
|
+
from keywordsai_exporter_haystack import KeywordsAIConnector, KeywordsAIGenerator
|
|
195
|
+
|
|
196
|
+
os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
|
|
197
|
+
|
|
198
|
+
# Create pipeline with gateway, prompt management, and tracing
|
|
199
|
+
pipeline = Pipeline()
|
|
200
|
+
pipeline.add_component("tracer", KeywordsAIConnector("Full Stack: Gateway + Prompt + Tracing"))
|
|
201
|
+
pipeline.add_component("llm", KeywordsAIGenerator(
|
|
202
|
+
prompt_id="1210b368ce2f4e5599d307bc591d9b7a", # Platform-managed prompt
|
|
203
|
+
api_key=os.getenv("KEYWORDSAI_API_KEY")
|
|
204
|
+
))
|
|
205
|
+
|
|
206
|
+
# Run with prompt variables
|
|
207
|
+
result = pipeline.run({
|
|
208
|
+
"llm": {
|
|
209
|
+
"prompt_variables": {
|
|
210
|
+
"user_input": "She sells seashells by the seashore"
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
print("Response received successfully!")
|
|
216
|
+
print(f"Trace URL: {result['tracer']['trace_url']}")
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**You get:**
|
|
220
|
+
1. **Gateway routing** with fallbacks, cost tracking, and reliability
|
|
221
|
+
2. **Platform prompts** managed centrally (no hardcoded prompts/models)
|
|
222
|
+
3. **Full workflow trace** with all components and timing
|
|
223
|
+
|
|
224
|
+
**See:** [`examples/combined_example.py`](examples/combined_example.py)
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
## What Gets Logged
|
|
229
|
+
|
|
230
|
+
### Gateway Mode
|
|
231
|
+
- Model used
|
|
232
|
+
- Prompt & completion
|
|
233
|
+
- Tokens & cost
|
|
234
|
+
- Latency
|
|
235
|
+
- Request metadata
|
|
236
|
+
|
|
237
|
+
### Tracing Mode
|
|
238
|
+
Each span includes:
|
|
239
|
+
- Component name & type
|
|
240
|
+
- Input data
|
|
241
|
+
- Output data
|
|
242
|
+
- Timing (latency)
|
|
243
|
+
- Parent-child relationships
|
|
244
|
+
|
|
245
|
+
For LLM spans, additionally:
|
|
246
|
+
- Model name
|
|
247
|
+
- Token counts
|
|
248
|
+
- Calculated cost (auto-computed)
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## View Your Data
|
|
253
|
+
|
|
254
|
+
All logs and traces appear in your Keywords AI dashboard:
|
|
255
|
+
|
|
256
|
+
**Dashboard:** https://platform.keywordsai.co/logs
|
|
257
|
+
|
|
258
|
+
- **Logs view:** Individual LLM calls
|
|
259
|
+
- **Traces view:** Full pipeline workflows with tree visualization
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## API Reference
|
|
264
|
+
|
|
265
|
+
### `KeywordsAIGenerator`
|
|
266
|
+
|
|
267
|
+
Gateway component for LLM calls.
|
|
268
|
+
|
|
269
|
+
```python
|
|
270
|
+
KeywordsAIGenerator(
|
|
271
|
+
model: Optional[str] = None, # Model name (e.g., "gpt-4o-mini") - optional if using prompt_id
|
|
272
|
+
api_key: Optional[str] = None, # Keywords AI API key (defaults to KEYWORDSAI_API_KEY env var)
|
|
273
|
+
base_url: Optional[str] = None, # API base URL (defaults to https://api.keywordsai.co)
|
|
274
|
+
prompt_id: Optional[str] = None, # Platform prompt ID for prompt management
|
|
275
|
+
generation_kwargs: Optional[Dict] = None
|
|
276
|
+
)
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
**Replaces:** `OpenAIGenerator` with gateway routing
|
|
280
|
+
|
|
281
|
+
**Note:** When using `prompt_id`, model config comes from the platform - no need to specify `model`
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
### `KeywordsAIConnector`
|
|
286
|
+
|
|
287
|
+
Tracing component for workflow monitoring.
|
|
288
|
+
|
|
289
|
+
```python
|
|
290
|
+
KeywordsAIConnector(
|
|
291
|
+
name: str, # Pipeline name for dashboard
|
|
292
|
+
api_key: Optional[str] = None, # Keywords AI API key (defaults to KEYWORDSAI_API_KEY env var)
|
|
293
|
+
base_url: Optional[str] = None, # API base URL (defaults to https://api.keywordsai.co)
|
|
294
|
+
metadata: Optional[Dict] = None # Custom metadata for all spans
|
|
295
|
+
)
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
**Returns:** `{"name": str, "trace_url": str}`
|
|
299
|
+
|
|
300
|
+
**Requires:** `HAYSTACK_CONTENT_TRACING_ENABLED=true` environment variable
|
|
301
|
+
|
|
302
|
+
---
|
|
303
|
+
|
|
304
|
+
## Examples
|
|
305
|
+
|
|
306
|
+
Run the examples:
|
|
307
|
+
|
|
308
|
+
```bash
|
|
309
|
+
# Set environment variables
|
|
310
|
+
export KEYWORDSAI_API_KEY="your-key"
|
|
311
|
+
export OPENAI_API_KEY="your-openai-key"
|
|
312
|
+
export HAYSTACK_CONTENT_TRACING_ENABLED="true"
|
|
313
|
+
|
|
314
|
+
# Gateway mode (auto-logging)
|
|
315
|
+
python examples/gateway_example.py
|
|
316
|
+
|
|
317
|
+
# Tracing mode (workflow monitoring)
|
|
318
|
+
python examples/tracing_example.py
|
|
319
|
+
|
|
320
|
+
# Prompt management (platform prompts)
|
|
321
|
+
python examples/prompt_example.py
|
|
322
|
+
|
|
323
|
+
# Combined mode (gateway + prompt + tracing)
|
|
324
|
+
python examples/combined_example.py
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## Requirements
|
|
330
|
+
|
|
331
|
+
- Python 3.9+
|
|
332
|
+
- `haystack-ai >= 2.0.0`
|
|
333
|
+
- `requests >= 2.31.0`
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
## Support
|
|
338
|
+
|
|
339
|
+
- **Documentation:** https://docs.keywordsai.co/
|
|
340
|
+
- **Dashboard:** https://platform.keywordsai.co/
|
|
341
|
+
- **Issues:** [GitHub Issues](https://github.com/Keywords-AI/keywordai_sdks/issues)
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## License
|
|
346
|
+
|
|
347
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
348
|
+
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
# Keywords AI Haystack Integration
|
|
2
|
+
|
|
3
|
+
Monitor and optimize your Haystack pipelines with Keywords AI's LLM observability platform.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
### Gateway Mode
|
|
8
|
+
Route LLM calls through Keywords AI gateway:
|
|
9
|
+
- Automatic logging (zero config)
|
|
10
|
+
- Model fallbacks & retries
|
|
11
|
+
- Load balancing
|
|
12
|
+
- Cost optimization
|
|
13
|
+
- Rate limiting & caching
|
|
14
|
+
|
|
15
|
+
### Tracing Mode
|
|
16
|
+
Capture full workflow execution:
|
|
17
|
+
- Multi-component pipelines
|
|
18
|
+
- Parent-child span relationships
|
|
19
|
+
- Timing per component
|
|
20
|
+
- Input/output tracking
|
|
21
|
+
- RAG + Agent workflows
|
|
22
|
+
|
|
23
|
+
### Combined Mode (Recommended)
|
|
24
|
+
Use both together for:
|
|
25
|
+
- Gateway reliability + Tracing visibility
|
|
26
|
+
- Production-ready monitoring
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install keywordsai-exporter-haystack
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
### 1. Get API Keys
|
|
39
|
+
|
|
40
|
+
- [Keywords AI API Key](https://platform.keywordsai.co/)
|
|
41
|
+
- OpenAI API Key (for examples)
|
|
42
|
+
|
|
43
|
+
### 2. Set Environment Variables
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
export KEYWORDSAI_API_KEY="your-keywords-ai-key"
|
|
47
|
+
export OPENAI_API_KEY="your-openai-key"
|
|
48
|
+
export HAYSTACK_CONTENT_TRACING_ENABLED="true" # For tracing mode
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Usage Examples
|
|
54
|
+
|
|
55
|
+
### Gateway Mode (Auto-Logging)
|
|
56
|
+
|
|
57
|
+
**Just replace `OpenAIGenerator` with `KeywordsAIGenerator`:**
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import os
|
|
61
|
+
from haystack import Pipeline
|
|
62
|
+
from haystack.components.builders import PromptBuilder
|
|
63
|
+
from keywordsai_exporter_haystack import KeywordsAIGenerator
|
|
64
|
+
|
|
65
|
+
# Create pipeline
|
|
66
|
+
pipeline = Pipeline()
|
|
67
|
+
pipeline.add_component("prompt", PromptBuilder(template="Tell me about {{topic}}."))
|
|
68
|
+
pipeline.add_component("llm", KeywordsAIGenerator(
|
|
69
|
+
model="gpt-4o-mini",
|
|
70
|
+
api_key=os.getenv("KEYWORDSAI_API_KEY")
|
|
71
|
+
))
|
|
72
|
+
pipeline.connect("prompt", "llm")
|
|
73
|
+
|
|
74
|
+
# Run
|
|
75
|
+
result = pipeline.run({"prompt": {"topic": "machine learning"}})
|
|
76
|
+
print(result["llm"]["replies"][0])
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**That's it!** All LLM calls are automatically logged to Keywords AI with no additional code.
|
|
80
|
+
|
|
81
|
+
**See:** [`examples/gateway_example.py`](examples/gateway_example.py)
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
### Prompt Management
|
|
86
|
+
|
|
87
|
+
**Use platform-managed prompts** for centralized control:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import os
|
|
91
|
+
from haystack import Pipeline
|
|
92
|
+
from keywordsai_exporter_haystack import KeywordsAIGenerator
|
|
93
|
+
|
|
94
|
+
# Create prompt on platform: https://platform.keywordsai.co/platform/prompts
|
|
95
|
+
# Get your prompt_id from the platform
|
|
96
|
+
|
|
97
|
+
# Create pipeline with platform prompt (model config comes from platform)
|
|
98
|
+
pipeline = Pipeline()
|
|
99
|
+
pipeline.add_component("llm", KeywordsAIGenerator(
|
|
100
|
+
prompt_id="1210b368ce2f4e5599d307bc591d9b7a", # Your prompt ID
|
|
101
|
+
api_key=os.getenv("KEYWORDSAI_API_KEY")
|
|
102
|
+
))
|
|
103
|
+
|
|
104
|
+
# Run with prompt variables
|
|
105
|
+
result = pipeline.run({
|
|
106
|
+
"llm": {
|
|
107
|
+
"prompt_variables": {
|
|
108
|
+
"user_input": "The cat sat on the mat"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
print("Response received successfully!")
|
|
114
|
+
print(f"Model: {result['llm']['meta'][0]['model']}")
|
|
115
|
+
print(f"Tokens: {result['llm']['meta'][0]['usage']['total_tokens']}")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Benefits:**
|
|
119
|
+
- Update prompts without code changes
|
|
120
|
+
- Model config managed on platform (no hardcoding)
|
|
121
|
+
- Version control & rollback
|
|
122
|
+
- A/B testing
|
|
123
|
+
- Team collaboration
|
|
124
|
+
|
|
125
|
+
**See:** [`examples/prompt_example.py`](examples/prompt_example.py)
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
### Tracing Mode (Workflow Monitoring)
|
|
130
|
+
|
|
131
|
+
**Add `KeywordsAIConnector` to capture the entire pipeline:**
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
import os
|
|
135
|
+
from haystack import Pipeline
|
|
136
|
+
from haystack.components.builders import PromptBuilder
|
|
137
|
+
from haystack.components.generators import OpenAIGenerator
|
|
138
|
+
from keywordsai_exporter_haystack import KeywordsAIConnector
|
|
139
|
+
|
|
140
|
+
os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
|
|
141
|
+
|
|
142
|
+
# Create pipeline with tracing
|
|
143
|
+
pipeline = Pipeline()
|
|
144
|
+
pipeline.add_component("tracer", KeywordsAIConnector("My Workflow"))
|
|
145
|
+
pipeline.add_component("prompt", PromptBuilder(template="Tell me about {{topic}}."))
|
|
146
|
+
pipeline.add_component("llm", OpenAIGenerator(model="gpt-4o-mini"))
|
|
147
|
+
pipeline.connect("prompt", "llm")
|
|
148
|
+
|
|
149
|
+
# Run
|
|
150
|
+
result = pipeline.run({"prompt": {"topic": "artificial intelligence"}})
|
|
151
|
+
print(result["llm"]["replies"][0])
|
|
152
|
+
print(f"\nTrace URL: {result['tracer']['trace_url']}")
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Dashboard shows:**
|
|
157
|
+
- Pipeline (root span)
|
|
158
|
+
- PromptBuilder (template processing)
|
|
159
|
+
- LLM (generation with tokens + cost)
|
|
160
|
+
|
|
161
|
+
**See:** [`examples/tracing_example.py`](examples/tracing_example.py)
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
### Combined Mode (Recommended for Production)
|
|
166
|
+
|
|
167
|
+
**Use BOTH gateway + prompt + tracing for the full stack:**
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import os
|
|
171
|
+
from haystack import Pipeline
|
|
172
|
+
from keywordsai_exporter_haystack import KeywordsAIConnector, KeywordsAIGenerator
|
|
173
|
+
|
|
174
|
+
os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
|
|
175
|
+
|
|
176
|
+
# Create pipeline with gateway, prompt management, and tracing
|
|
177
|
+
pipeline = Pipeline()
|
|
178
|
+
pipeline.add_component("tracer", KeywordsAIConnector("Full Stack: Gateway + Prompt + Tracing"))
|
|
179
|
+
pipeline.add_component("llm", KeywordsAIGenerator(
|
|
180
|
+
prompt_id="1210b368ce2f4e5599d307bc591d9b7a", # Platform-managed prompt
|
|
181
|
+
api_key=os.getenv("KEYWORDSAI_API_KEY")
|
|
182
|
+
))
|
|
183
|
+
|
|
184
|
+
# Run with prompt variables
|
|
185
|
+
result = pipeline.run({
|
|
186
|
+
"llm": {
|
|
187
|
+
"prompt_variables": {
|
|
188
|
+
"user_input": "She sells seashells by the seashore"
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
print("Response received successfully!")
|
|
194
|
+
print(f"Trace URL: {result['tracer']['trace_url']}")
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
**You get:**
|
|
198
|
+
1. **Gateway routing** with fallbacks, cost tracking, and reliability
|
|
199
|
+
2. **Platform prompts** managed centrally (no hardcoded prompts/models)
|
|
200
|
+
3. **Full workflow trace** with all components and timing
|
|
201
|
+
|
|
202
|
+
**See:** [`examples/combined_example.py`](examples/combined_example.py)
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
## What Gets Logged
|
|
207
|
+
|
|
208
|
+
### Gateway Mode
|
|
209
|
+
- Model used
|
|
210
|
+
- Prompt & completion
|
|
211
|
+
- Tokens & cost
|
|
212
|
+
- Latency
|
|
213
|
+
- Request metadata
|
|
214
|
+
|
|
215
|
+
### Tracing Mode
|
|
216
|
+
Each span includes:
|
|
217
|
+
- Component name & type
|
|
218
|
+
- Input data
|
|
219
|
+
- Output data
|
|
220
|
+
- Timing (latency)
|
|
221
|
+
- Parent-child relationships
|
|
222
|
+
|
|
223
|
+
For LLM spans, additionally:
|
|
224
|
+
- Model name
|
|
225
|
+
- Token counts
|
|
226
|
+
- Calculated cost (auto-computed)
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## View Your Data
|
|
231
|
+
|
|
232
|
+
All logs and traces appear in your Keywords AI dashboard:
|
|
233
|
+
|
|
234
|
+
**Dashboard:** https://platform.keywordsai.co/logs
|
|
235
|
+
|
|
236
|
+
- **Logs view:** Individual LLM calls
|
|
237
|
+
- **Traces view:** Full pipeline workflows with tree visualization
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## API Reference
|
|
242
|
+
|
|
243
|
+
### `KeywordsAIGenerator`
|
|
244
|
+
|
|
245
|
+
Gateway component for LLM calls.
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
KeywordsAIGenerator(
|
|
249
|
+
model: Optional[str] = None, # Model name (e.g., "gpt-4o-mini") - optional if using prompt_id
|
|
250
|
+
api_key: Optional[str] = None, # Keywords AI API key (defaults to KEYWORDSAI_API_KEY env var)
|
|
251
|
+
base_url: Optional[str] = None, # API base URL (defaults to https://api.keywordsai.co)
|
|
252
|
+
prompt_id: Optional[str] = None, # Platform prompt ID for prompt management
|
|
253
|
+
generation_kwargs: Optional[Dict] = None
|
|
254
|
+
)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
**Replaces:** `OpenAIGenerator` with gateway routing
|
|
258
|
+
|
|
259
|
+
**Note:** When using `prompt_id`, model config comes from the platform - no need to specify `model`
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
### `KeywordsAIConnector`
|
|
264
|
+
|
|
265
|
+
Tracing component for workflow monitoring.
|
|
266
|
+
|
|
267
|
+
```python
|
|
268
|
+
KeywordsAIConnector(
|
|
269
|
+
name: str, # Pipeline name for dashboard
|
|
270
|
+
api_key: Optional[str] = None, # Keywords AI API key (defaults to KEYWORDSAI_API_KEY env var)
|
|
271
|
+
base_url: Optional[str] = None, # API base URL (defaults to https://api.keywordsai.co)
|
|
272
|
+
metadata: Optional[Dict] = None # Custom metadata for all spans
|
|
273
|
+
)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
**Returns:** `{"name": str, "trace_url": str}`
|
|
277
|
+
|
|
278
|
+
**Requires:** `HAYSTACK_CONTENT_TRACING_ENABLED=true` environment variable
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## Examples
|
|
283
|
+
|
|
284
|
+
Run the examples:
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
# Set environment variables
|
|
288
|
+
export KEYWORDSAI_API_KEY="your-key"
|
|
289
|
+
export OPENAI_API_KEY="your-openai-key"
|
|
290
|
+
export HAYSTACK_CONTENT_TRACING_ENABLED="true"
|
|
291
|
+
|
|
292
|
+
# Gateway mode (auto-logging)
|
|
293
|
+
python examples/gateway_example.py
|
|
294
|
+
|
|
295
|
+
# Tracing mode (workflow monitoring)
|
|
296
|
+
python examples/tracing_example.py
|
|
297
|
+
|
|
298
|
+
# Prompt management (platform prompts)
|
|
299
|
+
python examples/prompt_example.py
|
|
300
|
+
|
|
301
|
+
# Combined mode (gateway + prompt + tracing)
|
|
302
|
+
python examples/combined_example.py
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
## Requirements
|
|
308
|
+
|
|
309
|
+
- Python 3.9+
|
|
310
|
+
- `haystack-ai >= 2.0.0`
|
|
311
|
+
- `requests >= 2.31.0`
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## Support
|
|
316
|
+
|
|
317
|
+
- **Documentation:** https://docs.keywordsai.co/
|
|
318
|
+
- **Dashboard:** https://platform.keywordsai.co/
|
|
319
|
+
- **Issues:** [GitHub Issues](https://github.com/Keywords-AI/keywordai_sdks/issues)
|
|
320
|
+
|
|
321
|
+
---
|
|
322
|
+
|
|
323
|
+
## License
|
|
324
|
+
|
|
325
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "respan-exporter-haystack"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Respan integration for Haystack pipelines with tracing and logging support"
|
|
5
|
+
authors = ["Respan <team@respan.ai>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
packages = [{include = "respan_exporter_haystack", from = "src"}]
|
|
9
|
+
homepage = "https://respan.ai"
|
|
10
|
+
repository = "https://github.com/respan-ai/respan-sdks"
|
|
11
|
+
documentation = "https://docs.respan.ai"
|
|
12
|
+
keywords = ["haystack", "respan", "llm", "observability", "tracing", "monitoring"]
|
|
13
|
+
|
|
14
|
+
[tool.poetry.dependencies]
|
|
15
|
+
python = "^3.9"
|
|
16
|
+
requests = "^2.31.0"
|
|
17
|
+
haystack-ai = "^2.0.0"
|
|
18
|
+
|
|
19
|
+
[tool.poetry.group.dev.dependencies]
|
|
20
|
+
pytest = "^8.3.0"
|
|
21
|
+
python-dotenv = "^1.0.0"
|
|
22
|
+
sentence-transformers = "^2.0.0"
|
|
23
|
+
|
|
24
|
+
[build-system]
|
|
25
|
+
requires = ["poetry-core"]
|
|
26
|
+
build-backend = "poetry.core.masonry.api"
|