mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-08-09 11:48:31 +00:00
Simplified the preprocessing interface.
This commit is contained in:
parent
1eeba2cc8a
commit
ad4cf97604
8 changed files with 31 additions and 43 deletions
|
@ -70,19 +70,8 @@ class PreprocessorStore(Protocol):
|
||||||
class Preprocessing(Protocol):
|
class Preprocessing(Protocol):
|
||||||
preprocessor_store: PreprocessorStore
|
preprocessor_store: PreprocessorStore
|
||||||
|
|
||||||
input_types: List[PreprocessingDataType]
|
|
||||||
output_types: List[PreprocessingDataType]
|
|
||||||
|
|
||||||
@webmethod(route="/preprocess", method="POST")
|
@webmethod(route="/preprocess", method="POST")
|
||||||
async def preprocess(
|
async def preprocess(
|
||||||
self,
|
|
||||||
preprocessor_id: str,
|
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
|
||||||
options: Optional[PreprocessorOptions] = None,
|
|
||||||
) -> PreprocessorResponse: ...
|
|
||||||
|
|
||||||
@webmethod(route="/chain_preprocess", method="POST")
|
|
||||||
async def chain_preprocess(
|
|
||||||
self,
|
self,
|
||||||
preprocessors: PreprocessorChain,
|
preprocessors: PreprocessorChain,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
|
|
|
@ -45,7 +45,6 @@ from llama_stack.apis.preprocessing import (
|
||||||
Preprocessing,
|
Preprocessing,
|
||||||
PreprocessingDataElement,
|
PreprocessingDataElement,
|
||||||
PreprocessorChain,
|
PreprocessorChain,
|
||||||
PreprocessorOptions,
|
|
||||||
PreprocessorResponse,
|
PreprocessorResponse,
|
||||||
)
|
)
|
||||||
from llama_stack.apis.safety import RunShieldResponse, Safety
|
from llama_stack.apis.safety import RunShieldResponse, Safety
|
||||||
|
@ -714,22 +713,6 @@ class PreprocessingRouter(Preprocessing):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def preprocess(
|
async def preprocess(
|
||||||
self,
|
|
||||||
preprocessor_id: str,
|
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
|
||||||
options: Optional[PreprocessorOptions] = None,
|
|
||||||
) -> PreprocessorResponse:
|
|
||||||
logcat.debug(
|
|
||||||
"core",
|
|
||||||
f"PreprocessingRouter.preprocess: {preprocessor_id}, {len(preprocessor_inputs)} inputs, options={options}",
|
|
||||||
)
|
|
||||||
return await self.routing_table.get_provider_impl(preprocessor_id).preprocess(
|
|
||||||
preprocessor_id=preprocessor_id,
|
|
||||||
preprocessor_inputs=preprocessor_inputs,
|
|
||||||
options=options,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def chain_preprocess(
|
|
||||||
self,
|
self,
|
||||||
preprocessors: PreprocessorChain,
|
preprocessors: PreprocessorChain,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
|
|
|
@ -8,16 +8,16 @@ from itertools import pairwise
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from llama_stack.apis.preprocessing import (
|
from llama_stack.apis.preprocessing import (
|
||||||
Preprocessing,
|
|
||||||
PreprocessingDataElement,
|
PreprocessingDataElement,
|
||||||
PreprocessorChain,
|
PreprocessorChain,
|
||||||
PreprocessorResponse,
|
PreprocessorResponse,
|
||||||
)
|
)
|
||||||
|
from llama_stack.providers.datatypes import PreprocessorsProtocolPrivate
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def validate_chain(chain_impls: List[Preprocessing]) -> bool:
|
def validate_chain(chain_impls: List[PreprocessorsProtocolPrivate]) -> bool:
|
||||||
if len(chain_impls) == 0:
|
if len(chain_impls) == 0:
|
||||||
log.error("Empty preprocessing chain was provided")
|
log.error("Empty preprocessing chain was provided")
|
||||||
return False
|
return False
|
||||||
|
@ -37,7 +37,7 @@ def validate_chain(chain_impls: List[Preprocessing]) -> bool:
|
||||||
|
|
||||||
async def execute_preprocessor_chain(
|
async def execute_preprocessor_chain(
|
||||||
preprocessor_chain: PreprocessorChain,
|
preprocessor_chain: PreprocessorChain,
|
||||||
preprocessor_chain_impls: List[Preprocessing],
|
preprocessor_chain_impls: List[PreprocessorsProtocolPrivate],
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
) -> PreprocessorResponse:
|
) -> PreprocessorResponse:
|
||||||
if not validate_chain(preprocessor_chain_impls):
|
if not validate_chain(preprocessor_chain_impls):
|
||||||
|
@ -50,7 +50,7 @@ async def execute_preprocessor_chain(
|
||||||
# TODO: replace with a parallel implementation
|
# TODO: replace with a parallel implementation
|
||||||
for i, current_params in enumerate(preprocessor_chain):
|
for i, current_params in enumerate(preprocessor_chain):
|
||||||
current_impl = preprocessor_chain_impls[i]
|
current_impl = preprocessor_chain_impls[i]
|
||||||
response = await current_impl.preprocess(
|
response = await current_impl.do_preprocess(
|
||||||
preprocessor_id=current_params.preprocessor_id,
|
preprocessor_id=current_params.preprocessor_id,
|
||||||
preprocessor_inputs=current_inputs,
|
preprocessor_inputs=current_inputs,
|
||||||
options=current_params.options,
|
options=current_params.options,
|
||||||
|
|
|
@ -13,7 +13,13 @@ from llama_stack.apis.benchmarks import Benchmark
|
||||||
from llama_stack.apis.datasets import Dataset
|
from llama_stack.apis.datasets import Dataset
|
||||||
from llama_stack.apis.datatypes import Api
|
from llama_stack.apis.datatypes import Api
|
||||||
from llama_stack.apis.models import Model
|
from llama_stack.apis.models import Model
|
||||||
from llama_stack.apis.preprocessing import Preprocessor
|
from llama_stack.apis.preprocessing import (
|
||||||
|
PreprocessingDataElement,
|
||||||
|
PreprocessingDataType,
|
||||||
|
Preprocessor,
|
||||||
|
PreprocessorOptions,
|
||||||
|
PreprocessorResponse,
|
||||||
|
)
|
||||||
from llama_stack.apis.scoring_functions import ScoringFn
|
from llama_stack.apis.scoring_functions import ScoringFn
|
||||||
from llama_stack.apis.shields import Shield
|
from llama_stack.apis.shields import Shield
|
||||||
from llama_stack.apis.tools import Tool
|
from llama_stack.apis.tools import Tool
|
||||||
|
@ -60,10 +66,20 @@ class ToolsProtocolPrivate(Protocol):
|
||||||
|
|
||||||
|
|
||||||
class PreprocessorsProtocolPrivate(Protocol):
|
class PreprocessorsProtocolPrivate(Protocol):
|
||||||
|
input_types: List[PreprocessingDataType]
|
||||||
|
output_types: List[PreprocessingDataType]
|
||||||
|
|
||||||
async def register_preprocessor(self, preprocessor: Preprocessor) -> None: ...
|
async def register_preprocessor(self, preprocessor: Preprocessor) -> None: ...
|
||||||
|
|
||||||
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
||||||
|
|
||||||
|
async def do_preprocess(
|
||||||
|
self,
|
||||||
|
preprocessor_id: str,
|
||||||
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
|
options: Optional[PreprocessorOptions] = None,
|
||||||
|
) -> PreprocessorResponse: ...
|
||||||
|
|
||||||
|
|
||||||
@json_schema_type
|
@json_schema_type
|
||||||
class ProviderSpec(BaseModel):
|
class ProviderSpec(BaseModel):
|
||||||
|
|
|
@ -52,7 +52,7 @@ class InclineBasicPreprocessorImpl(Preprocessing, PreprocessorsProtocolPrivate):
|
||||||
|
|
||||||
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
||||||
|
|
||||||
async def preprocess(
|
async def do_preprocess(
|
||||||
self,
|
self,
|
||||||
preprocessor_id: str,
|
preprocessor_id: str,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
|
@ -98,12 +98,12 @@ class InclineBasicPreprocessorImpl(Preprocessing, PreprocessorsProtocolPrivate):
|
||||||
success=True, output_data_type=PreprocessingDataType.raw_text_document, results=results
|
success=True, output_data_type=PreprocessingDataType.raw_text_document, results=results
|
||||||
)
|
)
|
||||||
|
|
||||||
async def chain_preprocess(
|
async def preprocess(
|
||||||
self,
|
self,
|
||||||
preprocessors: PreprocessorChain,
|
preprocessors: PreprocessorChain,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
) -> PreprocessorResponse:
|
) -> PreprocessorResponse:
|
||||||
return await self.preprocess(preprocessor_id="", preprocessor_inputs=preprocessor_inputs)
|
return await self.do_preprocess(preprocessor_id="", preprocessor_inputs=preprocessor_inputs)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _resolve_input_type(preprocessor_input: PreprocessingDataElement) -> PreprocessingDataType:
|
def _resolve_input_type(preprocessor_input: PreprocessingDataElement) -> PreprocessingDataType:
|
||||||
|
|
|
@ -47,7 +47,7 @@ class InclineDoclingPreprocessorImpl(Preprocessing, PreprocessorsProtocolPrivate
|
||||||
|
|
||||||
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
||||||
|
|
||||||
async def preprocess(
|
async def do_preprocess(
|
||||||
self,
|
self,
|
||||||
preprocessor_id: str,
|
preprocessor_id: str,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
|
@ -106,9 +106,9 @@ class InclineDoclingPreprocessorImpl(Preprocessing, PreprocessorsProtocolPrivate
|
||||||
)
|
)
|
||||||
return PreprocessorResponse(success=True, output_data_type=output_data_type, results=results)
|
return PreprocessorResponse(success=True, output_data_type=output_data_type, results=results)
|
||||||
|
|
||||||
async def chain_preprocess(
|
async def preprocess(
|
||||||
self,
|
self,
|
||||||
preprocessors: PreprocessorChain,
|
preprocessors: PreprocessorChain,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
) -> PreprocessorResponse:
|
) -> PreprocessorResponse:
|
||||||
return await self.preprocess(preprocessor_id="", preprocessor_inputs=preprocessor_inputs)
|
return await self.do_preprocess(preprocessor_id="", preprocessor_inputs=preprocessor_inputs)
|
||||||
|
|
|
@ -47,7 +47,7 @@ class InclineSimpleChunkingImpl(Preprocessing, PreprocessorsProtocolPrivate):
|
||||||
|
|
||||||
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
async def unregister_preprocessor(self, preprocessor_id: str) -> None: ...
|
||||||
|
|
||||||
async def preprocess(
|
async def do_preprocess(
|
||||||
self,
|
self,
|
||||||
preprocessor_id: str,
|
preprocessor_id: str,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
|
@ -72,12 +72,12 @@ class InclineSimpleChunkingImpl(Preprocessing, PreprocessorsProtocolPrivate):
|
||||||
|
|
||||||
return PreprocessorResponse(success=True, output_data_type=PreprocessingDataType.chunks, results=chunks)
|
return PreprocessorResponse(success=True, output_data_type=PreprocessingDataType.chunks, results=chunks)
|
||||||
|
|
||||||
async def chain_preprocess(
|
async def preprocess(
|
||||||
self,
|
self,
|
||||||
preprocessors: PreprocessorChain,
|
preprocessors: PreprocessorChain,
|
||||||
preprocessor_inputs: List[PreprocessingDataElement],
|
preprocessor_inputs: List[PreprocessingDataElement],
|
||||||
) -> PreprocessorResponse:
|
) -> PreprocessorResponse:
|
||||||
return await self.preprocess(preprocessor_id="", preprocessor_inputs=preprocessor_inputs)
|
return await self.do_preprocess(preprocessor_id="", preprocessor_inputs=preprocessor_inputs)
|
||||||
|
|
||||||
def _resolve_chunk_size_params(self, options: PreprocessorOptions) -> Tuple[int, int]:
|
def _resolve_chunk_size_params(self, options: PreprocessorOptions) -> Tuple[int, int]:
|
||||||
window_len = (options or {}).get(
|
window_len = (options or {}).get(
|
||||||
|
|
|
@ -81,7 +81,7 @@ class MemoryToolRuntimeImpl(ToolsProtocolPrivate, ToolRuntime, RAGToolRuntime):
|
||||||
preprocessor_chain: Optional[PreprocessorChain] = None,
|
preprocessor_chain: Optional[PreprocessorChain] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
preprocessor_inputs = [self._rag_document_to_preprocessor_input(d) for d in documents]
|
preprocessor_inputs = [self._rag_document_to_preprocessor_input(d) for d in documents]
|
||||||
preprocessor_response = await self.preprocessing_api.chain_preprocess(
|
preprocessor_response = await self.preprocessing_api.preprocess(
|
||||||
preprocessors=preprocessor_chain or self.DEFAULT_PREPROCESSING_CHAIN,
|
preprocessors=preprocessor_chain or self.DEFAULT_PREPROCESSING_CHAIN,
|
||||||
preprocessor_inputs=preprocessor_inputs,
|
preprocessor_inputs=preprocessor_inputs,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue