mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 15:48:29 +00:00
Some checks failed
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Notify Downstream / notify-downstream (push) Has been cancelled
Update Test Durations / update-durations (3.10) (push) Has been cancelled
Update Test Durations / update-durations (3.11) (push) Has been cancelled
Update Test Durations / update-durations (3.12) (push) Has been cancelled
Update Test Durations / update-durations (3.13) (push) Has been cancelled
Build uv cache / build-cache (3.10) (push) Has been cancelled
Build uv cache / build-cache (3.11) (push) Has been cancelled
Build uv cache / build-cache (3.12) (push) Has been cancelled
Build uv cache / build-cache (3.13) (push) Has been cancelled
33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
from pydantic import Field
|
|
|
|
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
|
|
|
|
|
|
class StringKnowledgeSource(BaseKnowledgeSource):
|
|
"""A knowledge source that stores and queries plain text content using embeddings."""
|
|
|
|
content: str = Field(...)
|
|
collection_name: str | None = Field(default=None)
|
|
|
|
def model_post_init(self, _):
|
|
"""Post-initialization method to validate content."""
|
|
self.validate_content()
|
|
|
|
def validate_content(self):
|
|
"""Validate string content."""
|
|
if not isinstance(self.content, str):
|
|
raise ValueError("StringKnowledgeSource only accepts string content")
|
|
|
|
def add(self) -> None:
|
|
"""Add string content to the knowledge source, chunk it, compute embeddings, and save them."""
|
|
new_chunks = self._chunk_text(self.content)
|
|
self.chunks.extend(new_chunks)
|
|
self._save_documents()
|
|
|
|
def _chunk_text(self, text: str) -> list[str]:
|
|
"""Utility method to split text into chunks."""
|
|
return [
|
|
text[i : i + self.chunk_size]
|
|
for i in range(0, len(text), self.chunk_size - self.chunk_overlap)
|
|
]
|