mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 16:18:30 +00:00
* WIP. Procedure appears to be working well. Working on mocking properly for tests * All tests are passing now * rshift working * Add back in Gui's tool_usage fix * WIP * Going to start refactoring for pipeline_output * Update terminology * new pipeline flow with traces and usage metrics working. need to add more tests and make sure PipelineOutput behaves likew CrewOutput * Fix pipelineoutput to look more like crewoutput and taskoutput * Implemented additional tests for pipeline. One test is failing. Need team support * Update docs for pipeline * Update pipeline to properly process input and ouput dictionary * Update Pipeline docs * Add back in commentary at top of pipeline file * Starting to work on router * Drop router for now. will add in separately * In the middle of fixing router. A ton of circular dependencies. Moving over to a new design. * WIP. * Fix circular dependencies and updated PipelineRouter * Add in Eduardo feedback. Still need to add in more commentary describing the design decisions for pipeline * Add developer notes to explain what is going on in pipelines. * Add doc strings * Fix missing rag datatype * WIP. Converting usage metrics from a dict to an object * Fix tests that were checking usage metrics * Drop todo * Fix 1 type error in pipeline * Update pipeline to use UsageMetric * Add missing doc string * WIP. * Change names * Rename variables based on joaos feedback * Fix critical circular dependency issues. Now needing to fix trace issue. * Tests working now! * Add more tests which showed underlying issue with traces * Fix tests * Remove overly complicated test * Add router example to docs * Clean up end of docs * Clean up docs * Working on creating Crew templates and pipeline templates * WIP. * WIP * Fix poetry install from templates * WIP * Restructure * changes for lorenze * more todos * WIP: create pipelines cli working * wrapped up router * ignore mypy src on templates * ignored signature of copy * fix all verbose * rm print statements * brought back correct folders * fixes missing folders and then rm print statements * fixed tests * fixed broken test * fixed type checker * fixed type ignore * ignore types for templates * needed * revert * exclude only required * rm type errors on templates * rm excluding type checks for template files on github action * fixed missing quotes --------- Co-authored-by: Brandon Hancock <brandon@brandonhancock.io>
91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
from copy import deepcopy
|
|
from typing import Any, Callable, Dict, Generic, Tuple, TypeVar
|
|
|
|
from pydantic import BaseModel, Field, PrivateAttr
|
|
|
|
T = TypeVar("T", bound=Dict[str, Any])
|
|
U = TypeVar("U")
|
|
|
|
|
|
class Route(Generic[T, U]):
|
|
condition: Callable[[T], bool]
|
|
pipeline: U
|
|
|
|
def __init__(self, condition: Callable[[T], bool], pipeline: U):
|
|
self.condition = condition
|
|
self.pipeline = pipeline
|
|
|
|
|
|
class Router(BaseModel, Generic[T, U]):
|
|
routes: Dict[str, Route[T, U]] = Field(
|
|
default_factory=dict,
|
|
description="Dictionary of route names to (condition, pipeline) tuples",
|
|
)
|
|
default: U = Field(..., description="Default pipeline if no conditions are met")
|
|
_route_types: Dict[str, type] = PrivateAttr(default_factory=dict)
|
|
|
|
model_config = {"arbitrary_types_allowed": True}
|
|
|
|
def __init__(self, routes: Dict[str, Route[T, U]], default: U, **data):
|
|
super().__init__(routes=routes, default=default, **data)
|
|
self._check_copyable(default)
|
|
for name, route in routes.items():
|
|
self._check_copyable(route.pipeline)
|
|
self._route_types[name] = type(route.pipeline)
|
|
|
|
@staticmethod
|
|
def _check_copyable(obj):
|
|
if not hasattr(obj, "copy") or not callable(getattr(obj, "copy")):
|
|
raise ValueError(f"Object of type {type(obj)} must have a 'copy' method")
|
|
|
|
def add_route(
|
|
self,
|
|
name: str,
|
|
condition: Callable[[T], bool],
|
|
pipeline: U,
|
|
) -> "Router[T, U]":
|
|
"""
|
|
Add a named route with its condition and corresponding pipeline to the router.
|
|
|
|
Args:
|
|
name: A unique name for this route
|
|
condition: A function that takes a dictionary input and returns a boolean
|
|
pipeline: The Pipeline to execute if the condition is met
|
|
|
|
Returns:
|
|
The Router instance for method chaining
|
|
"""
|
|
self._check_copyable(pipeline)
|
|
self.routes[name] = Route(condition=condition, pipeline=pipeline)
|
|
self._route_types[name] = type(pipeline)
|
|
return self
|
|
|
|
def route(self, input_data: T) -> Tuple[U, str]:
|
|
"""
|
|
Evaluate the input against the conditions and return the appropriate pipeline.
|
|
|
|
Args:
|
|
input_data: The input dictionary to be evaluated
|
|
|
|
Returns:
|
|
A tuple containing the next Pipeline to be executed and the name of the route taken
|
|
"""
|
|
for name, route in self.routes.items():
|
|
if route.condition(input_data):
|
|
return route.pipeline, name
|
|
|
|
return self.default, "default"
|
|
|
|
def copy(self) -> "Router[T, U]":
|
|
"""Create a deep copy of the Router."""
|
|
new_routes = {
|
|
name: Route(
|
|
condition=deepcopy(route.condition),
|
|
pipeline=route.pipeline.copy(), # type: ignore
|
|
)
|
|
for name, route in self.routes.items()
|
|
}
|
|
new_default = self.default.copy() # type: ignore
|
|
|
|
return Router(routes=new_routes, default=new_default)
|