mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 17:18:29 +00:00
* initial knowledge * WIP * Adding core knowledge sources * Improve types and better support for file paths * added additional sources * fix linting * update yaml to include optional deps * adding in lorenze feedback * ensure embeddings are persisted * improvements all around Knowledge class * return this * properly reset memory * properly reset memory+knowledge * consolodation and improvements * linted * cleanup rm unused embedder * fix test * fix duplicate * generating cassettes for knowledge test * updated default embedder * None embedder to use default on pipeline cloning * improvements * fixed text_file_knowledge * mypysrc fixes * type check fixes * added extra cassette * just mocks * linted * mock knowledge query to not spin up db * linted * verbose run * put a flag * fix * adding docs * better docs * improvements from review * more docs * linted * rm print * more fixes * clearer docs * added docstrings and type hints for cli --------- Co-authored-by: João Moura <joaomdmoura@gmail.com> Co-authored-by: Lorenze Jay <lorenzejaytech@gmail.com>
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
from typing import List
|
|
|
|
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(...)
|
|
|
|
def model_post_init(self, _):
|
|
"""Post-initialization method to validate content."""
|
|
self.load_content()
|
|
|
|
def load_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(metadata=self.metadata)
|
|
|
|
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)
|
|
]
|