Skip to content

Commit 4c452db

Browse files
authored
Merge pull request #10 from code0-tech/feat/#9
External few shot configuration
2 parents 77b1ea7 + d680375 commit 4c452db

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

src/store/few_shots_store.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@ def __init__(self, memory_client: QdrantClient, vector_model: StaticModel):
2525
self._load_models_from_json()
2626

2727
def _load_models_from_json(self):
28-
filepath = Path(__file__).parent.parent.parent / "few_shots.configuration.json"
28+
project_root = Path(__file__).parent.parent.parent
29+
internal_path = project_root / "few_shots.configuration.json"
30+
external_path = project_root / "few_shots_external.configuration.json"
2931

30-
if not filepath.exists():
31-
raise FileNotFoundError(f"File not found: {filepath}")
32-
33-
with open(filepath, 'r', encoding='utf-8') as f:
34-
raw_data = json.load(f)
35-
36-
if not isinstance(raw_data, list):
37-
raw_data = [raw_data]
32+
raw_data: List[dict] = []
33+
raw_data.extend(self._read_few_shots_file(internal_path, required=True))
34+
raw_data.extend(self._read_few_shots_file(external_path, required=False))
3835

3936
for item in raw_data:
4037
shot = FewShot(**item)
@@ -44,6 +41,39 @@ def _load_models_from_json(self):
4441

4542
log.success(f"FewShotsStore ready — {len(raw_data)} example(s) loaded") # type: ignore[attr-defined]
4643

44+
@staticmethod
45+
def _read_few_shots_file(filepath: Path, required: bool) -> List[dict]:
46+
if not filepath.exists():
47+
if required:
48+
raise FileNotFoundError(f"File not found: {filepath}")
49+
log.info(f"No external few-shots file at {filepath} — skipping")
50+
return []
51+
52+
if not filepath.is_file():
53+
if required:
54+
raise IsADirectoryError(f"Expected a file but found a directory: {filepath}")
55+
log.warning(f"External few-shots path {filepath} is not a file — skipping")
56+
return []
57+
58+
try:
59+
with open(filepath, 'r', encoding='utf-8') as f:
60+
content = f.read().strip()
61+
if not content:
62+
if required:
63+
raise ValueError(f"File is empty: {filepath}")
64+
log.info(f"External few-shots file {filepath} is empty — skipping")
65+
return []
66+
data = json.loads(content)
67+
except json.JSONDecodeError as e:
68+
if required:
69+
raise
70+
log.warning(f"External few-shots file {filepath} is not valid JSON ({e}) — skipping")
71+
return []
72+
73+
if not isinstance(data, list):
74+
data = [data]
75+
return data
76+
4777
def insert(self, group_identifier: str, payload: FewShot) -> None:
4878
super().insert(group_identifier, payload)
4979

0 commit comments

Comments
 (0)