|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in |
| 5 | +# the root directory of this source tree. |
| 6 | + |
| 7 | +from typing import Any, Protocol, runtime_checkable |
| 8 | + |
| 9 | +from pydantic import BaseModel |
| 10 | + |
| 11 | +from llama_stack.apis.common.tracing import telemetry_traceable |
| 12 | +from llama_stack.apis.vector_io.vector_io import Chunk, VectorStoreChunkingStrategy |
| 13 | +from llama_stack.apis.version import LLAMA_STACK_API_V1ALPHA |
| 14 | +from llama_stack.schema_utils import json_schema_type, webmethod |
| 15 | + |
| 16 | + |
| 17 | +@json_schema_type |
| 18 | +class ProcessFileRequest(BaseModel): |
| 19 | + """Request for processing a file into structured content.""" |
| 20 | + |
| 21 | + file_data: bytes |
| 22 | + """Raw file data to process.""" |
| 23 | + |
| 24 | + filename: str |
| 25 | + """Original filename for format detection and processing hints.""" |
| 26 | + |
| 27 | + options: dict[str, Any] | None = None |
| 28 | + """Optional processing options. Provider-specific parameters.""" |
| 29 | + |
| 30 | + chunking_strategy: VectorStoreChunkingStrategy | None = None |
| 31 | + """Optional chunking strategy for splitting content into chunks.""" |
| 32 | + |
| 33 | + include_embeddings: bool = False |
| 34 | + """Whether to generate embeddings for chunks.""" |
| 35 | + |
| 36 | + |
| 37 | +@json_schema_type |
| 38 | +class ProcessedContent(BaseModel): |
| 39 | + """Result of file processing operation.""" |
| 40 | + |
| 41 | + content: str |
| 42 | + """Extracted text content from the file.""" |
| 43 | + |
| 44 | + chunks: list[Chunk] | None = None |
| 45 | + """Optional chunks if chunking strategy was provided.""" |
| 46 | + |
| 47 | + embeddings: list[list[float]] | None = None |
| 48 | + """Optional embeddings for chunks if requested.""" |
| 49 | + |
| 50 | + metadata: dict[str, Any] |
| 51 | + """Processing metadata including processor name, timing, and provider-specific data.""" |
| 52 | + |
| 53 | + |
| 54 | +@telemetry_traceable |
| 55 | +@runtime_checkable |
| 56 | +class FileProcessor(Protocol): |
| 57 | + """ |
| 58 | + File Processor API for converting files into structured, processable content. |
| 59 | +
|
| 60 | + This API provides a flexible interface for processing various file formats |
| 61 | + (PDFs, documents, images, etc.) into text content that can be used for |
| 62 | + vector store ingestion, RAG applications, or standalone content extraction. |
| 63 | +
|
| 64 | + The API supports: |
| 65 | + - Multiple file formats through extensible provider architecture |
| 66 | + - Configurable processing options per provider |
| 67 | + - Integration with vector store chunking strategies |
| 68 | + - Optional embedding generation for chunks |
| 69 | + - Rich metadata about processing results |
| 70 | +
|
| 71 | + Future providers can extend this interface to support additional formats, |
| 72 | + processing capabilities, and optimization strategies. |
| 73 | + """ |
| 74 | + |
| 75 | + @webmethod(route="/file-processor/process", method="POST", level=LLAMA_STACK_API_V1ALPHA) |
| 76 | + async def process_file( |
| 77 | + self, |
| 78 | + file_data: bytes, |
| 79 | + filename: str, |
| 80 | + options: dict[str, Any] | None = None, |
| 81 | + chunking_strategy: VectorStoreChunkingStrategy | None = None, |
| 82 | + include_embeddings: bool = False, |
| 83 | + ) -> ProcessedContent: |
| 84 | + """ |
| 85 | + Process a file into structured content with optional chunking and embeddings. |
| 86 | +
|
| 87 | + This method processes raw file data and converts it into text content for applications such as vector store ingestion. |
| 88 | +
|
| 89 | + :param file_data: Raw bytes of the file to process. |
| 90 | + :param filename: Original filename for format detection. |
| 91 | + :param options: Provider-specific processing options (e.g., OCR settings, output format). |
| 92 | + :param chunking_strategy: Optional strategy for splitting content into chunks. |
| 93 | + :param include_embeddings: Whether to generate embeddings for chunks. |
| 94 | + :returns: ProcessedContent with extracted text, optional chunks, and metadata. |
| 95 | + """ |
| 96 | + ... |
0 commit comments