|
| 1 | +import asyncio |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | +from openinference.instrumentation.llama_index import ( |
| 5 | + LlamaIndexInstrumentor, |
| 6 | + get_current_span, |
| 7 | +) |
| 8 | +from uipath._cli._evals._console_progress_reporter import ConsoleProgressReporter |
| 9 | +from uipath._cli._evals._progress_reporter import StudioWebProgressReporter |
| 10 | +from uipath._cli._evals._runtime import UiPathEvalContext, UiPathEvalRuntime |
| 11 | +from uipath._cli._runtime._contracts import ( |
| 12 | + UiPathRuntimeFactory, |
| 13 | +) |
| 14 | +from uipath._cli._utils._eval_set import EvalHelpers |
| 15 | +from uipath._cli.middlewares import MiddlewareResult |
| 16 | +from uipath._events._event_bus import EventBus |
| 17 | +from uipath.eval._helpers import auto_discover_entrypoint |
| 18 | + |
| 19 | +from ._runtime._context import UiPathLlamaIndexRuntimeContext |
| 20 | +from ._runtime._runtime import UiPathLlamaIndexRuntime |
| 21 | +from ._tracing._attribute_normalizer import AttributeNormalizingSpanProcessor |
| 22 | +from ._tracing._oteladapter import LlamaIndexExporter |
| 23 | +from ._utils._config import LlamaIndexConfig |
| 24 | + |
| 25 | + |
| 26 | +def llamaindex_eval_middleware( |
| 27 | + entrypoint: Optional[str], eval_set: Optional[str], eval_ids: List[str], **kwargs |
| 28 | +) -> MiddlewareResult: |
| 29 | + """Middleware to handle LlamaIndex evaluation runs""" |
| 30 | + config = LlamaIndexConfig() |
| 31 | + if not config.exists: |
| 32 | + return MiddlewareResult( |
| 33 | + should_continue=True |
| 34 | + ) # Continue with normal flow if no llama_index.json |
| 35 | + |
| 36 | + try: |
| 37 | + event_bus = EventBus() |
| 38 | + if kwargs.get("register_progress_reporter", False): |
| 39 | + progress_reporter = StudioWebProgressReporter( |
| 40 | + spans_exporter=LlamaIndexExporter() |
| 41 | + ) |
| 42 | + asyncio.run(progress_reporter.subscribe_to_eval_runtime_events(event_bus)) |
| 43 | + console_reporter = ConsoleProgressReporter() |
| 44 | + asyncio.run(console_reporter.subscribe_to_eval_runtime_events(event_bus)) |
| 45 | + |
| 46 | + def generate_runtime_context( |
| 47 | + context_entrypoint: str, **context_kwargs |
| 48 | + ) -> UiPathLlamaIndexRuntimeContext: |
| 49 | + context = UiPathLlamaIndexRuntimeContext.with_defaults(**context_kwargs) |
| 50 | + context.entrypoint = context_entrypoint |
| 51 | + context.config = config |
| 52 | + return context |
| 53 | + |
| 54 | + runtime_entrypoint = entrypoint or auto_discover_entrypoint() |
| 55 | + |
| 56 | + eval_context = UiPathEvalContext.with_defaults( |
| 57 | + entrypoint=runtime_entrypoint, **kwargs |
| 58 | + ) |
| 59 | + eval_context.eval_set = eval_set or EvalHelpers.auto_discover_eval_set() |
| 60 | + eval_context.eval_ids = eval_ids |
| 61 | + |
| 62 | + def generate_runtime( |
| 63 | + ctx: UiPathLlamaIndexRuntimeContext, |
| 64 | + ) -> UiPathLlamaIndexRuntime: |
| 65 | + return UiPathLlamaIndexRuntime(ctx) |
| 66 | + |
| 67 | + runtime_factory = UiPathRuntimeFactory( |
| 68 | + UiPathLlamaIndexRuntime, |
| 69 | + UiPathLlamaIndexRuntimeContext, |
| 70 | + context_generator=lambda **context_kwargs: generate_runtime_context( |
| 71 | + context_entrypoint=runtime_entrypoint, |
| 72 | + **context_kwargs, |
| 73 | + ), |
| 74 | + runtime_generator=generate_runtime, |
| 75 | + ) |
| 76 | + runtime_factory.add_span_processor(AttributeNormalizingSpanProcessor()) |
| 77 | + |
| 78 | + if eval_context.job_id: |
| 79 | + runtime_factory.add_span_exporter(LlamaIndexExporter()) |
| 80 | + |
| 81 | + runtime_factory.add_instrumentor(LlamaIndexInstrumentor, get_current_span) |
| 82 | + |
| 83 | + async def execute(): |
| 84 | + async with UiPathEvalRuntime.from_eval_context( |
| 85 | + factory=runtime_factory, context=eval_context, event_bus=event_bus |
| 86 | + ) as eval_runtime: |
| 87 | + await eval_runtime.execute() |
| 88 | + await event_bus.wait_for_all() |
| 89 | + |
| 90 | + asyncio.run(execute()) |
| 91 | + return MiddlewareResult(should_continue=False) |
| 92 | + |
| 93 | + except Exception as e: |
| 94 | + return MiddlewareResult( |
| 95 | + should_continue=False, error_message=f"Error running evaluation: {str(e)}" |
| 96 | + ) |
0 commit comments