-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpipeline.py
More file actions
419 lines (340 loc) · 17.6 KB
/
pipeline.py
File metadata and controls
419 lines (340 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import asyncio
from typing import Any
import yaml
from pydantic import BaseModel
from deepset_mcp.api.exceptions import BadRequestError, ResourceNotFoundError, UnexpectedAPIError
from deepset_mcp.api.pipeline.models import (
DeepsetPipeline,
DeepsetSearchResponse,
LogLevel,
PipelineLog,
PipelineValidationResult,
)
from deepset_mcp.api.protocols import AsyncClientProtocol
from deepset_mcp.api.shared_models import PaginatedResponse
async def list_pipelines(
*, client: AsyncClientProtocol, workspace: str, after: str | None = None
) -> PaginatedResponse[DeepsetPipeline] | str:
"""Retrieves a list of all pipeline available within the currently configured deepset workspace.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param after: The cursor to fetch the next page of results.
If there are more results to fetch, the cursor will appear as `next_cursor` on the response.
:returns: List of pipelines or error message.
"""
try:
return await client.pipelines(workspace=workspace).list(after=after)
except ResourceNotFoundError:
return f"There is no workspace named '{workspace}'. Did you mean to configure it?"
except (BadRequestError, UnexpectedAPIError) as e:
return f"Failed to list pipelines: {e}"
async def get_pipeline(*, client: AsyncClientProtocol, workspace: str, pipeline_name: str) -> DeepsetPipeline | str:
"""Fetches detailed configuration information for a specific pipeline, identified by its unique `pipeline_name`.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: The name of the pipeline to fetch.
:returns: Pipeline details or error message.
"""
try:
return await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name)
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'."
except (BadRequestError, UnexpectedAPIError) as e:
return f"Failed to fetch pipeline '{pipeline_name}': {e}"
class PipelineValidationResultWithYaml(BaseModel):
"""Model for pipeline validation result that includes the original YAML."""
validation_result: PipelineValidationResult
"Result of validating the pipeline configuration"
yaml_config: str
"Original YAML configuration that was validated"
async def validate_pipeline(
*, client: AsyncClientProtocol, workspace: str, yaml_configuration: str
) -> PipelineValidationResultWithYaml | str:
"""Validates the provided pipeline YAML configuration against the deepset API.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param yaml_configuration: The YAML configuration to validate.
:returns: Validation result with original YAML or error message.
"""
if not yaml_configuration or not yaml_configuration.strip():
return "You need to provide a YAML configuration to validate."
try:
yaml.safe_load(yaml_configuration)
except yaml.YAMLError as e:
return f"Invalid YAML provided: {e}"
try:
response = await client.pipelines(workspace=workspace).validate(yaml_configuration)
return PipelineValidationResultWithYaml(validation_result=response, yaml_config=yaml_configuration)
except ResourceNotFoundError:
return f"There is no workspace named '{workspace}'. Did you mean to configure it?"
except (BadRequestError, UnexpectedAPIError) as e:
return f"Failed to validate pipeline: {e}"
class PipelineOperationWithErrors(BaseModel):
"""Model for pipeline operations that complete with validation errors."""
message: str
"Descriptive message about the pipeline operation"
validation_result: PipelineValidationResult
"Validation errors encountered during the operation"
pipeline: DeepsetPipeline
"Pipeline object after the operation completed"
async def create_pipeline(
*,
client: AsyncClientProtocol,
workspace: str,
pipeline_name: str,
yaml_configuration: str,
skip_validation_errors: bool = True,
) -> DeepsetPipeline | PipelineOperationWithErrors | str:
"""Creates a new pipeline within the currently configured deepset workspace.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: Name of the pipeline to create.
:param yaml_configuration: YAML configuration for the pipeline.
:param skip_validation_errors: If True (default), creates the pipeline even if validation fails.
If False, stops creation when validation fails.
:returns: Created pipeline or error message.
"""
try:
validation_response = await client.pipelines(workspace=workspace).validate(yaml_configuration)
if not validation_response.valid and not skip_validation_errors:
error_messages = [f"{error.code}: {error.message}" for error in validation_response.errors]
return "Pipeline validation failed:\n" + "\n".join(error_messages)
await client.pipelines(workspace=workspace).create(name=pipeline_name, yaml_config=yaml_configuration)
# Get the full pipeline after creation
pipeline = await client.pipelines(workspace=workspace).get(pipeline_name)
# If validation failed but we proceeded anyway, return the special model
if not validation_response.valid:
return PipelineOperationWithErrors(
message="The operation completed with errors", validation_result=validation_response, pipeline=pipeline
)
# Otherwise return just the pipeline
return pipeline
except ResourceNotFoundError:
return f"There is no workspace named '{workspace}'. Did you mean to configure it?"
except BadRequestError as e:
return f"Failed to create pipeline '{pipeline_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to create pipeline '{pipeline_name}': {e}"
async def update_pipeline(
*,
client: AsyncClientProtocol,
workspace: str,
pipeline_name: str,
original_config_snippet: str,
replacement_config_snippet: str,
skip_validation_errors: bool = True,
) -> DeepsetPipeline | PipelineOperationWithErrors | str:
"""
Updates a pipeline configuration in the specified workspace with a replacement configuration snippet.
This function validates the replacement configuration snippet before applying it to the pipeline.
If the validation fails and skip_validation_errors is False, it returns error messages.
Otherwise, the replacement snippet is used to update the pipeline's configuration.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: Name of the pipeline to update.
:param original_config_snippet: The configuration snippet to replace.
:param replacement_config_snippet: The new configuration snippet.
:param skip_validation_errors: If True (default), updates the pipeline even if validation fails.
If False, stops update when validation fails.
:returns: Updated pipeline or error message.
"""
try:
original_pipeline = await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name)
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}'. Did you mean to create it?"
except (BadRequestError, UnexpectedAPIError) as e:
return f"Failed to fetch pipeline '{pipeline_name}': {e}"
if original_pipeline.yaml_config is None:
return f"The pipeline '{pipeline_name}' does not have a YAML configuration."
occurrences = original_pipeline.yaml_config.count(original_config_snippet)
if occurrences == 0:
return f"No occurrences of the provided configuration snippet were found in the pipeline '{pipeline_name}'."
if occurrences > 1:
return (
f"Multiple occurrences ({occurrences}) of the provided configuration snippet were found in the pipeline "
f"'{pipeline_name}'. Specify a more precise snippet to proceed with the update."
)
updated_yaml_configuration = original_pipeline.yaml_config.replace(
original_config_snippet, replacement_config_snippet, 1
)
try:
validation_response = await client.pipelines(workspace=workspace).validate(updated_yaml_configuration)
if not validation_response.valid and not skip_validation_errors:
error_messages = [f"{error.code}: {error.message}" for error in validation_response.errors]
return "Pipeline validation failed:\n" + "\n".join(error_messages)
await client.pipelines(workspace=workspace).update(
pipeline_name=pipeline_name, yaml_config=updated_yaml_configuration
)
# Get the full pipeline after update
pipeline = await client.pipelines(workspace=workspace).get(pipeline_name)
# If validation failed but we proceeded anyway, return the special model
if not validation_response.valid:
return PipelineOperationWithErrors(
message="The operation completed with errors", validation_result=validation_response, pipeline=pipeline
)
# Otherwise return just the pipeline
return pipeline
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}'. Did you mean to create it?"
except BadRequestError as e:
return f"Failed to update the pipeline '{pipeline_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to update the pipeline '{pipeline_name}': {e}"
async def get_pipeline_logs(
*,
client: AsyncClientProtocol,
workspace: str,
pipeline_name: str,
limit: int = 30,
level: LogLevel | None = None,
after: str | None = None,
) -> PaginatedResponse[PipelineLog] | str:
"""Fetches logs for a specific pipeline.
Retrieves log entries for the specified pipeline, with optional filtering by log level.
This is useful for debugging pipeline issues or monitoring pipeline execution.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: Name of the pipeline to fetch logs for.
:param limit: Maximum number of log entries to return (default: 30).
:param level: Filter logs by level. If None, returns all levels.
:param after: The cursor to fetch the next page of results.
:returns: Pipeline logs or error message.
"""
try:
return await client.pipelines(workspace=workspace).get_logs(
pipeline_name=pipeline_name, limit=limit, level=level, after=after
)
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'."
except BadRequestError as e:
return f"Failed to fetch logs for pipeline '{pipeline_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to fetch logs for pipeline '{pipeline_name}': {e}"
async def deploy_pipeline(
*,
client: AsyncClientProtocol,
workspace: str,
pipeline_name: str,
wait_for_deployment: bool = False,
timeout_seconds: float = 600,
poll_interval: float = 10,
) -> PipelineValidationResult | str:
"""Deploys a pipeline to production.
This function attempts to deploy the specified pipeline in the given workspace.
If the deployment fails due to validation errors, it returns a validation result.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: Name of the pipeline to deploy.
:param wait_for_deployment: If True, waits for the pipeline to reach DEPLOYED status.
:param timeout_seconds: Maximum time to wait for deployment when wait_for_deployment is True (default: 600.0).
:param poll_interval: Time between status checks in seconds when wait_for_deployment is True (default: 10.0).
:returns: Deployment validation result or error message.
"""
try:
deployment_result = await client.pipelines(workspace=workspace).deploy(pipeline_name=pipeline_name)
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'."
except BadRequestError as e:
return f"Failed to deploy pipeline '{pipeline_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to deploy pipeline '{pipeline_name}': {e}"
if not deployment_result.valid:
return deployment_result
# If not waiting for deployment, return success immediately
if not wait_for_deployment:
return deployment_result
start_time = asyncio.get_event_loop().time()
while True:
current_time = asyncio.get_event_loop().time()
if current_time - start_time > timeout_seconds:
return (
f"Pipeline '{pipeline_name}' deployment initiated successfully, but did not reach DEPLOYED status "
f"within {timeout_seconds} seconds. You can check the pipeline status manually."
)
try:
# Get the current pipeline status
pipeline = await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name, include_yaml=False)
if pipeline.status == "DEPLOYED":
return deployment_result # Return the successful validation result
elif pipeline.status == "FAILED":
return f"Pipeline '{pipeline_name}' deployment failed. Current status: FAILED."
# Wait before next poll
await asyncio.sleep(poll_interval)
except Exception as e:
return f"Pipeline '{pipeline_name}' deployment initiated, but failed to check deployment status: {e}"
async def search_pipeline(
*, client: AsyncClientProtocol, workspace: str, pipeline_name: str, query: str
) -> DeepsetSearchResponse | str:
"""Searches using a pipeline.
Uses the specified pipeline to perform a search with the given query.
Before executing the search, checks if the pipeline is deployed (status = DEPLOYED).
Returns search results.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: Name of the pipeline to use for search.
:param query: The search query to execute.
:returns: Search results or error message.
"""
try:
# First, check if the pipeline exists and get its status
pipeline = await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name)
# Check if pipeline is deployed
if pipeline.status != "DEPLOYED":
return (
f"Pipeline '{pipeline_name}' is not deployed (current status: {pipeline.status}). "
f"Please deploy the pipeline first using the deploy_pipeline tool before attempting to search."
)
# Execute the search
return await client.pipelines(workspace=workspace).search(pipeline_name=pipeline_name, query=query)
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'."
except BadRequestError as e:
return f"Failed to search using pipeline '{pipeline_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to search using pipeline '{pipeline_name}': {e}"
except Exception as e:
return f"An unexpected error occurred while searching with pipeline '{pipeline_name}': {str(e)}"
async def search_pipeline_with_filters(
*,
client: AsyncClientProtocol,
workspace: str,
pipeline_name: str,
query: str,
filters: dict[str, Any] | None = None,
) -> DeepsetSearchResponse | str:
"""Searches using a pipeline with filters.
Uses the specified pipeline to perform a search with the given query and filters.
Filters follow the Haystack filter syntax: https://docs.haystack.deepset.ai/docs/metadata-filtering.
Before executing the search, checks if the pipeline is deployed (status = DEPLOYED).
Returns search results.
:param client: The async client for API communication.
:param workspace: The workspace name.
:param pipeline_name: Name of the pipeline to use for search.
:param query: The search query to execute.
:param filters: The filters to apply to the search.
:returns: Search results or error message.
"""
try:
# First, check if the pipeline exists and get its status
pipeline = await client.pipelines(workspace=workspace).get(pipeline_name=pipeline_name)
# Check if pipeline is deployed
if pipeline.status != "DEPLOYED":
return (
f"Pipeline '{pipeline_name}' is not deployed (current status: {pipeline.status}). "
f"Please deploy the pipeline first using the deploy_pipeline tool before attempting to search."
)
# Execute the search
return await client.pipelines(workspace=workspace).search(
pipeline_name=pipeline_name, query=query, filters=filters if filters is not None else None
)
except ResourceNotFoundError:
return f"There is no pipeline named '{pipeline_name}' in workspace '{workspace}'."
except BadRequestError as e:
return f"Failed to search using pipeline '{pipeline_name}': {e}"
except UnexpectedAPIError as e:
return f"Failed to search using pipeline '{pipeline_name}': {e}"
except Exception as e:
return f"An unexpected error occurred while searching with pipeline '{pipeline_name}': {str(e)}"