Repository: phidatahq/phidata
File: libs/agno/agno/knowledge/chunking/semantic.py (Python)

Code before:
import inspect
from typing import Any, Dict, List, Optional

from agno.knowledge.chunking.strategy import ChunkingStrategy
from agno.knowledge.document.base import Document
from agno.knowledge.embedder.base import Embedder
from agno.utils.log import log_info


class SemanticChunking(ChunkingStrategy):
    """Chunking strategy that splits text into semantic chunks using chonkie"""

    def __init__(self, embedder: Optional[Embedder] = None, chunk_size: int = 5000, similarity_threshold: float = 0.5):
        if embedder is None:
            from agno.knowledge.embedder.openai import OpenAIEmbedder

            embedder = OpenAIEmbedder(id="text-embedding-3-small")  # type: ignore
            log_info("Embedder not provided, using OpenAIEmbedder as default.")
        self.embedder = embedder
        self.chunk_size = chunk_size
        self.similarity_threshold = similarity_threshold
        self.chunker = None  # Will be initialized lazily when needed

    def _initialize_chunker(self):
        """Lazily initialize the chunker with chonkie dependency."""
        if self.chunker is None:
            try:
                from chonkie import SemanticChunker
            except ImportError:
                raise ImportError(
                    "`chonkie` is required for semantic chunking. "
                    "Please install it using `pip install chonkie` to use SemanticChunking."
                )

            # Build arguments dynamically based on chonkie's supported signature
            params: Dict[str, Any] = {
                "chunk_size": self.chunk_size,
                "threshold": self.similarity_threshold,
            }

            try:
                sig = inspect.signature(SemanticChunker)

Review comment (suggestion):
```suggestion
            embedder = OpenAIEmbedder()  # type: ignore
```

Code after:
import inspect
from typing import Any, Dict, List, Optional

from agno.knowledge.chunking.strategy import ChunkingStrategy
from agno.knowledge.document.base import Document
from agno.knowledge.embedder.base import Embedder
from agno.utils.log import log_info


class SemanticChunking(ChunkingStrategy):
    """Chunking strategy that splits text into semantic chunks using chonkie"""

    def __init__(self, embedder: Optional[Embedder] = None, chunk_size: int = 5000, similarity_threshold: float = 0.5):
        if embedder is None:
            from agno.knowledge.embedder.openai import OpenAIEmbedder

            embedder = OpenAIEmbedder()  # type: ignore
            log_info("Embedder not provided, using OpenAIEmbedder as default.")
        self.embedder = embedder
        self.chunk_size = chunk_size
        self.similarity_threshold = similarity_threshold
        self.chunker = None  # Will be initialized lazily when needed

    def _initialize_chunker(self):
        """Lazily initialize the chunker with chonkie dependency."""
        if self.chunker is None:
            try:
                from chonkie import SemanticChunker
            except ImportError:
                raise ImportError(
                    "`chonkie` is required for semantic chunking. "
                    "Please install it using `pip install chonkie` to use SemanticChunking."
                )

            # Build arguments dynamically based on chonkie's supported signature
            params: Dict[str, Any] = {
                "chunk_size": self.chunk_size,
                "threshold": self.similarity_threshold,
            }

            try:
                sig = inspect.signature(SemanticChunker)