-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
llama-index-core/llama_index/core/query_pipeline/components/loop.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from llama_index.core.base.query_pipeline.query import ( | ||
InputKeys, | ||
OutputKeys, | ||
QueryComponent, | ||
) | ||
from llama_index.core.query_pipeline.query import QueryPipeline | ||
from llama_index.core.bridge.pydantic import BaseModel, Field | ||
from llama_index.core.callbacks.base import CallbackManager | ||
from typing import Any, Dict, Optional, List, cast, Callable | ||
from llama_index.core.query_pipeline.components.stateful import BaseStatefulComponent | ||
|
||
def _get_stateful_components(query_component: QueryComponent) -> List[BaseStatefulComponent]: | ||
"""Get stateful components.""" | ||
stateful_components: List[BaseStatefulComponent] = [] | ||
for c in query_component.sub_query_components: | ||
if isinstance(c, BaseStatefulComponent): | ||
stateful_components.append(cast(BaseStatefulComponent, c)) | ||
|
||
if len(c.sub_query_components) > 0: | ||
stateful_components.extend(_get_stateful_components(c)) | ||
|
||
return stateful_components | ||
|
||
class LoopComponent(QueryComponent): | ||
"""Loop component. | ||
""" | ||
|
||
pipeline: QueryPipeline = Field(..., description="Query pipeline") | ||
should_exit_fn: Optional[Callable] = Field(..., description="Should exit function") | ||
add_output_to_input_fn: Optional[Callable] = Field(..., description="Add output to input function. If not provided, will reuse the original input for the next iteration. If provided, will call the function to combine the output into the input for the next iteration.") | ||
max_iterations: Optional[int] = Field(5, description="Max iterations") | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
def __init__( | ||
self, | ||
pipeline: QueryPipeline, | ||
should_exit_fn: Optional[Callable] = None, | ||
add_output_to_input_fn: Optional[Callable] = None, | ||
max_iterations: Optional[int] = 5, | ||
) -> None: | ||
"""Init params.""" | ||
super().__init__(pipeline=pipeline, should_exit_fn=should_exit_fn, add_output_to_input_fn=add_output_to_input_fn, max_iterations=max_iterations) | ||
|
||
def set_callback_manager(self, callback_manager: CallbackManager) -> None: | ||
"""Set callback manager.""" | ||
# TODO: implement | ||
|
||
def _validate_component_inputs(self, input: Dict[str, Any]) -> Dict[str, Any]: | ||
pass | ||
|
||
@property | ||
def stateful_components(self) -> List[BaseStatefulComponent]: | ||
"""Get stateful component.""" | ||
# TODO: do this directly within the query pipeline | ||
return _get_stateful_components(self.pipeline) | ||
|
||
def _run_component(self, **kwargs: Any) -> Dict: | ||
"""Run component.""" | ||
state = {} | ||
# partial agent output component with state | ||
for stateful_component in self.stateful_components: | ||
stateful_component.partial(state=state) | ||
|
||
current_input = kwargs | ||
for i in range(self.max_iterations): | ||
output = self.pipeline.run_component(**current_input) | ||
if self.should_exit_fn: | ||
should_exit = self.should_exit_fn(output) | ||
if should_exit: | ||
break | ||
|
||
if self.add_output_to_input_fn: | ||
current_input = self.add_output_to_input_fn(current_input, output) | ||
|
||
return self.pipeline.run_component(**kwargs) | ||
|
||
async def _arun_component(self, **kwargs: Any) -> Any: | ||
"""Run component (async).""" | ||
return await self.pipeline.arun_component(**kwargs) | ||
|
||
@property | ||
def input_keys(self) -> InputKeys: | ||
"""Input keys.""" | ||
return self.pipeline.input_keys | ||
|
||
@property | ||
def output_keys(self) -> OutputKeys: | ||
"""Output keys.""" | ||
return self.pipeline.output_keys |
63 changes: 63 additions & 0 deletions
63
llama-index-core/llama_index/core/query_pipeline/components/stateful.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Agent components.""" | ||
|
||
from inspect import signature | ||
from typing import Any, Callable, Dict, Optional, Set, Tuple, cast | ||
|
||
from llama_index.core.base.query_pipeline.query import ( | ||
InputKeys, | ||
OutputKeys, | ||
QueryComponent, | ||
) | ||
from llama_index.core.bridge.pydantic import Field, PrivateAttr | ||
from llama_index.core.callbacks.base import CallbackManager | ||
from llama_index.core.query_pipeline.components.function import FnComponent, get_parameters | ||
# from llama_index.core.query_pipeline.components.input import InputComponent | ||
|
||
|
||
class BaseStatefulComponent(QueryComponent): | ||
"""Takes in agent inputs and transforms it into desired outputs.""" | ||
|
||
|
||
|
||
class StatefulFnComponent(BaseStatefulComponent, FnComponent): | ||
"""Query component that takes in an arbitrary function. | ||
Stateful version of `FnComponent`. Expects functions to have `state` as the first argument. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
fn: Callable, | ||
req_params: Optional[Set[str]] = None, | ||
opt_params: Optional[Set[str]] = None, | ||
**kwargs: Any | ||
) -> None: | ||
"""Init params.""" | ||
|
||
# determine parameters | ||
default_req_params, default_opt_params = get_parameters(fn) | ||
# make sure task and step are part of the list, and remove them from the list | ||
if "state" not in default_req_params: | ||
raise ValueError( | ||
"StatefulFnComponent must have 'state' as required parameters" | ||
) | ||
|
||
default_req_params = default_req_params - {"state"} | ||
default_opt_params = default_opt_params - {"state"} | ||
|
||
super().__init__(fn=fn, req_params=req_params, opt_params=opt_params, **kwargs) | ||
|
||
@property | ||
def input_keys(self) -> InputKeys: | ||
"""Input keys.""" | ||
return InputKeys.from_keys( | ||
required_keys={"state", *self._req_params}, | ||
optional_keys=self._opt_params, | ||
) | ||
|
||
@property | ||
def output_keys(self) -> OutputKeys: | ||
"""Output keys.""" | ||
# output can be anything, overrode validate function | ||
return OutputKeys.from_keys({self.output_key}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters