Skip to content

Commit 28ba943

Browse files
authored
Merge pull request #42 from idrisfl/main
feat: add yml yaml file support for indexation
2 parents 73550c9 + a1b3852 commit 28ba943

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/docs2vecs/subcommands/indexer/skills/ada002_embedding_skill.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ class AzureAda002EmbeddingSkill(IndexerSkill):
1111
def __init__(self, config: dict, global_config: Config):
1212
super().__init__(config, global_config)
1313

14-
def az_ada002_embeddings(self, content: str):
14+
def az_ada002_embeddings(self, content: str, chunk_id=None):
15+
self.logger.debug(
16+
f"Requesting embedding for chunk_id={chunk_id}, content_length={len(content)} chars"
17+
)
1518
embed_model = AzureOpenAIEmbedding(
1619
deployment_name=self._config["deployment_name"],
1720
api_key=self._config["api_key"],
1821
azure_endpoint=self._config["endpoint"],
1922
api_version=self._config["api_version"],
2023
)
21-
return embed_model.get_query_embedding(content)
24+
embedding = embed_model.get_query_embedding(content)
25+
self.logger.debug(
26+
f"Successfully received embedding for chunk_id={chunk_id}, embedding_dim={len(embedding) if embedding else 0}"
27+
)
28+
return embedding
2229

2330
def run(self, input: Optional[List[Document]] = None) -> Optional[List[Document]]:
2431
self.logger.info(
@@ -36,10 +43,8 @@ def run(self, input: Optional[List[Document]] = None) -> Optional[List[Document]
3643
self.logger.debug(f"Processing document: {doc.filename}")
3744
for chunk in doc.chunks:
3845
self.logger.debug(f"Creating embedding for chunk: {chunk.chunk_id}")
39-
chunk.embedding = (
40-
""
41-
if not chunk.content
42-
else self.az_ada002_embeddings(chunk.content)
46+
chunk.embedding = "" if not chunk.content else self.az_ada002_embeddings(
47+
chunk.content, chunk_id=chunk.chunk_id
4348
)
4449

45-
return input
50+
return input

src/docs2vecs/subcommands/indexer/skills/default_file_reader.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class DefaultFileReader(FileLoaderSkill):
2424
- .doc, .docx: Word documents using UnstructuredWordDocumentLoader
2525
- .ppt, .pptx: PowerPoint files using UnstructuredPowerPointLoader
2626
- .xls, .xlsx: Excel files using UnstructuredExcelLoader
27+
- .yml, .yaml: YAML files using PyYAML (multiple documents supported)
2728
"""
2829

2930
def __init__(self, skill_config: dict, global_config: Config) -> None:
@@ -38,7 +39,32 @@ def __init__(self, skill_config: dict, global_config: Config) -> None:
3839
".pptx": self._load_powerpoint,
3940
".xls": self._load_excel,
4041
".xlsx": self._load_excel,
42+
".yml": self._load_yaml,
43+
".yaml": self._load_yaml,
4144
}
45+
46+
def _load_yaml(self, file_path: Path) -> List[Document]:
47+
"""Load YAML files that may contain multiple documents separated by ---."""
48+
try:
49+
import yaml
50+
except ImportError as err:
51+
raise ImportError("yaml module is required to read YAML files.") from err
52+
53+
documents = []
54+
with file_path.open() as fp:
55+
for i, content in enumerate(yaml.safe_load_all(fp)):
56+
if content is not None:
57+
yaml_text = yaml.safe_dump(content)
58+
doc_name = f"{file_path.stem}_doc_{i}{file_path.suffix}"
59+
documents.append(
60+
Document(
61+
filename=str(file_path),
62+
source_url=str(file_path),
63+
text=yaml_text
64+
)
65+
)
66+
67+
return documents
4268

4369
def run(self, documents: Optional[List[Document]]) -> List[Document]:
4470
"""Process input documents by reading their content based on file extension.
@@ -118,4 +144,4 @@ def _load_excel(self, file_path: Path) -> List[Document]:
118144
"""Load Excel files using UnstructuredExcelLoader."""
119145
loader = UnstructuredExcelLoader(str(file_path))
120146
docs = loader.load()
121-
return [Document(filename=str(file_path), source_url=str(file_path), text=doc.page_content) for doc in docs]
147+
return [Document(filename=str(file_path), source_url=str(file_path), text=doc.page_content) for doc in docs]

0 commit comments

Comments
 (0)