Skip to content

Commit 1108594

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 8edb71d commit 1108594

File tree

23 files changed

+324
-240
lines changed

23 files changed

+324
-240
lines changed

EdgeCraftRAG/docker_compose/intel/gpu/arc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export MILVUS_ENABLED=0
129129
# export CHAT_HISTORY_ROUND= # change to your preference
130130

131131
# Launch EC-RAG service with compose
132-
# Below enviroments are exposed for vLLM config, you can change them to your preference:
132+
# Below environments are exposed for vLLM config, you can change them to your preference:
133133
# export VLLM_SERVICE_PORT_0=8100
134134
# export DTYPE=float16
135135
# export ZE_AFFINITY_MASK=0

EdgeCraftRAG/docs/API_Guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,4 @@ curl -X POST http://${HOST_IP}:16010/v1/retrieval -H "Content-Type: application/
219219

220220
```bash
221221
curl -X POST http://${HOST_IP}:16011/v1/chatqna -H "Content-Type: application/json" -d '{"messages":"#REPLACE WITH YOUR QUESTION HERE#", "top_n":5, "max_tokens":512}' | jq '.'
222-
```
222+
```

EdgeCraftRAG/edgecraftrag/api/v1/chatqna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def chatqna(request: ChatCompletionRequest):
3434
set_current_session(sessionid)
3535
kb = ctx.knowledgemgr.get_active_experience()
3636
if kb:
37-
request.tool_choice = 'auto' if kb.experience_active else 'none'
37+
request.tool_choice = "auto" if kb.experience_active else "none"
3838
generator = ctx.get_pipeline_mgr().get_active_pipeline().generator
3939
if generator:
4040
request.model = generator.model_id

EdgeCraftRAG/edgecraftrag/api/v1/knowledge_base.py

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import json
66
import os
77
import re
8+
from typing import Dict, List, Union
89

9-
from typing import List, Dict, Union
1010
from edgecraftrag.api.v1.data import add_data
11-
from edgecraftrag.api_schema import DataIn, KnowledgeBaseCreateIn, ExperienceIn
11+
from edgecraftrag.api_schema import DataIn, ExperienceIn, KnowledgeBaseCreateIn
1212
from edgecraftrag.base import IndexerType
1313
from edgecraftrag.context import ctx
1414
from edgecraftrag.utils import compare_mappings
@@ -50,7 +50,7 @@ async def create_knowledge_base(knowledge: KnowledgeBaseCreateIn):
5050
detail="Knowledge base names must begin with a letter or underscore",
5151
)
5252
kb = ctx.knowledgemgr.create_knowledge_base(knowledge)
53-
if kb.active and kb.comp_type =="knowledge":
53+
if kb.active and kb.comp_type == "knowledge":
5454
active_pl.indexer.reinitialize_indexer(kb.name)
5555
active_pl.update_indexer_to_retriever()
5656
await save_knowledge_to_file()
@@ -68,9 +68,14 @@ async def delete_knowledge_base(knowledge_name: str):
6868
active_pl = ctx.get_pipeline_mgr().get_active_pipeline()
6969
if rm_kb.comp_type == "knowledge":
7070
if not active_kb:
71-
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Please activate a knowledge base before proceeding.")
71+
raise HTTPException(
72+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
73+
detail="Please activate a knowledge base before proceeding.",
74+
)
7275
if active_kb.name == knowledge_name or active_kb.idx == knowledge_name:
73-
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Cannot delete a running knowledge base.")
76+
raise HTTPException(
77+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Cannot delete a running knowledge base."
78+
)
7479
kb_file_path = rm_kb.get_file_paths()
7580
if kb_file_path:
7681
if active_pl.indexer.comp_subtype == "milvus_vector":
@@ -80,7 +85,10 @@ async def delete_knowledge_base(knowledge_name: str):
8085
active_pl.update_indexer_to_retriever()
8186
if rm_kb.comp_type == "experience":
8287
if rm_kb.experience_active:
83-
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Cannot delete a running experience knowledge base.")
88+
raise HTTPException(
89+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
90+
detail="Cannot delete a running experience knowledge base.",
91+
)
8492
else:
8593
rm_kb.clear_experiences()
8694
result = ctx.knowledgemgr.delete_knowledge_base(knowledge_name)
@@ -130,8 +138,10 @@ async def add_file_to_knowledge_base(knowledge_name, file_path: DataIn):
130138
try:
131139
active_pl = ctx.get_pipeline_mgr().get_active_pipeline()
132140
kb = ctx.knowledgemgr.get_knowledge_base_by_name_or_id(knowledge_name)
133-
if kb.comp_type =="experience":
134-
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="The experience type cannot perform file operations.")
141+
if kb.comp_type == "experience":
142+
raise HTTPException(
143+
status_code=status.HTTP_404_NOT_FOUND, detail="The experience type cannot perform file operations."
144+
)
135145
# Validate and normalize the user-provided path
136146
user_path = file_path.local_path
137147
normalized_path = os.path.normpath(os.path.join(KNOWLEDGE_BASE_ROOT, user_path))
@@ -183,8 +193,10 @@ async def remove_file_from_knowledge_base(knowledge_name, file_path: DataIn):
183193
try:
184194
active_pl = ctx.get_pipeline_mgr().get_active_pipeline()
185195
kb = ctx.knowledgemgr.get_knowledge_base_by_name_or_id(knowledge_name)
186-
if kb.comp_type =="experience":
187-
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="The experience type cannot perform file operations.")
196+
if kb.comp_type == "experience":
197+
raise HTTPException(
198+
status_code=status.HTTP_404_NOT_FOUND, detail="The experience type cannot perform file operations."
199+
)
188200
active_kb = ctx.knowledgemgr.get_active_knowledge_base()
189201
if file_path.local_path in kb.get_file_paths():
190202
kb.remove_file_path(file_path.local_path)
@@ -212,14 +224,16 @@ async def remove_file_from_knowledge_base(knowledge_name, file_path: DataIn):
212224
except ValueError as e:
213225
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
214226

227+
215228
@kb_app.post("/v1/experience")
216229
def get_experience_by_question(req: ExperienceIn):
217-
kb = ctx.knowledgemgr.get_experience_kb()
230+
kb = ctx.knowledgemgr.get_experience_kb()
218231
result = kb.get_experience_by_question(req.question)
219232
if not result:
220233
raise HTTPException(404, detail="Experience not found")
221234
return result
222235

236+
223237
@kb_app.get("/v1/experiences")
224238
def get_all_experience():
225239
kb = ctx.knowledgemgr.get_experience_kb()
@@ -228,48 +242,53 @@ def get_all_experience():
228242
else:
229243
return kb
230244

245+
231246
@kb_app.patch("/v1/experiences")
232247
def update_experience(experience: ExperienceIn):
233248
kb = ctx.knowledgemgr.get_experience_kb()
234249
result = kb.update_experience(experience.question, experience.content)
235250
if not result:
236-
raise HTTPException(404, detail=f"Question not found")
251+
raise HTTPException(404, detail="Question not found")
237252
return result
238253

254+
239255
@kb_app.delete("/v1/experiences")
240-
def delete_experience(req :ExperienceIn):
256+
def delete_experience(req: ExperienceIn):
241257
kb = ctx.knowledgemgr.get_experience_kb()
242258
success = kb.delete_experience(req.question)
243259
if not success:
244260
raise HTTPException(404, detail=f"Question {req.question} not found")
245-
return {"message": f"Question deleted"}
261+
return {"message": "Question deleted"}
262+
246263

247264
@kb_app.post("/v1/multiple_experiences/check")
248265
def check_duplicate_multiple_experiences(experiences: List[Dict[str, Union[str, List[str]]]]):
249266
kb = ctx.knowledgemgr.get_experience_kb()
250267
if not kb:
251-
raise HTTPException(404, detail=f"No active experience type knowledge base")
268+
raise HTTPException(404, detail="No active experience type knowledge base")
252269
all_existing = kb.get_all_experience()
253270
existing_questions = {item["question"] for item in all_existing}
254271
new_questions = [exp["question"] for exp in experiences if "question" in exp]
255272
duplicate_questions = [q for q in new_questions if q in existing_questions]
256273
if duplicate_questions:
257274
return {"code": 2001, "detail": "Duplicate experiences are appended OR overwritten!"}
258275
else:
259-
kb.add_multiple_experiences(experiences=experiences, flag=True)
260-
return {"status": "success","detail": "No duplicate experiences, added successfully"}
276+
kb.add_multiple_experiences(experiences=experiences, flag=True)
277+
return {"status": "success", "detail": "No duplicate experiences, added successfully"}
278+
261279

262280
@kb_app.post("/v1/multiple_experiences/confirm")
263-
def confirm_multiple_experiences(experiences: List[Dict[str, Union[str, List[str]]]],flag: bool):
281+
def confirm_multiple_experiences(experiences: List[Dict[str, Union[str, List[str]]]], flag: bool):
264282
kb = ctx.knowledgemgr.get_experience_kb()
265283
try:
266284
if not kb:
267-
raise HTTPException(404, detail=f"No active experience type knowledge base")
285+
raise HTTPException(404, detail="No active experience type knowledge base")
268286
kb.add_multiple_experiences(experiences=experiences, flag=flag)
269287
return {"status": "success", "detail": "Experiences added successfully"}
270288
except Exception as e:
271289
raise HTTPException(status_code=500, detail=f"Add Failure:{str(e)}")
272290

291+
273292
@kb_app.post("/v1/experiences/files")
274293
def add_experiences_from_file(req: DataIn):
275294
kb = ctx.knowledgemgr.get_experience_kb()
@@ -279,6 +298,7 @@ def add_experiences_from_file(req: DataIn):
279298
except Exception as e:
280299
raise HTTPException(status_code=400, detail=str(e))
281300

301+
282302
# Update knowledge base data
283303
async def update_knowledge_base_handler(file_path=None, knowledge_name: str = "default_kb", add_file: bool = False):
284304
if ctx.get_pipeline_mgr().get_active_pipeline() is None:
@@ -334,7 +354,7 @@ async def load_knowledge_from_file():
334354
for Knowledgebase_data in all_data:
335355
pipeline_req = KnowledgeBaseCreateIn(**Knowledgebase_data)
336356
kb = ctx.knowledgemgr.create_knowledge_base(pipeline_req)
337-
if kb.comp_type =="knowledge":
357+
if kb.comp_type == "knowledge":
338358
if Knowledgebase_data["file_map"]:
339359
if active_pl.indexer.comp_subtype != "milvus_vector" and Knowledgebase_data["active"]:
340360
for file_path in Knowledgebase_data["file_map"].values():
@@ -364,7 +384,14 @@ async def save_knowledge_to_file():
364384
kb_base = ctx.knowledgemgr.get_all_knowledge_bases()
365385
knowledgebases_data = []
366386
for kb in kb_base:
367-
kb_json = {"name": kb.name, "description": kb.description, "active": kb.active, "file_map": kb.file_map, "comp_type": kb.comp_type, "experience_active": kb.experience_active}
387+
kb_json = {
388+
"name": kb.name,
389+
"description": kb.description,
390+
"active": kb.active,
391+
"file_map": kb.file_map,
392+
"comp_type": kb.comp_type,
393+
"experience_active": kb.experience_active,
394+
}
368395
knowledgebases_data.append(kb_json)
369396
json_str = json.dumps(knowledgebases_data, indent=2, ensure_ascii=False)
370397
with open(KNOWLEDGEBASE_FILE, "w", encoding="utf-8") as f:
@@ -375,15 +402,18 @@ async def save_knowledge_to_file():
375402

376403
all_pipeline_milvus_maps = {}
377404
current_pipeline_kb_map = {}
405+
406+
378407
async def refresh_milvus_map(milvus_name):
379408
current_pipeline_kb_map.clear()
380409
knowledge_bases_list = await get_all_knowledge_bases()
381410
for kb in knowledge_bases_list:
382411
if kb.comp_type == "experience":
383-
continue
412+
continue
384413
current_pipeline_kb_map[kb.name] = kb.file_map
385414
all_pipeline_milvus_maps[milvus_name] = copy.deepcopy(current_pipeline_kb_map)
386415

416+
387417
async def Synchronizing_vector_data(old_active_pl, new_active_pl):
388418
try:
389419
active_kb = ctx.knowledgemgr.get_active_knowledge_base()

EdgeCraftRAG/edgecraftrag/api_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ class KnowledgeBaseCreateIn(BaseModel):
8181
description: Optional[str] = None
8282
active: Optional[bool] = None
8383
comp_type: Optional[str] = "knowledge"
84-
experience_active: Optional[bool] = None
84+
experience_active: Optional[bool] = None
85+
8586

8687
class ExperienceIn(BaseModel):
8788
question: str
8889
content: list[str] = None
8990

91+
9092
class MilvusConnectRequest(BaseModel):
9193
vector_uri: str

EdgeCraftRAG/edgecraftrag/components/knowledge_base.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Copyright (C) 2024 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
33

4-
import os, json
5-
from typing import Any, List, Optional, Dict, Union
4+
import json
5+
import os
6+
from typing import Any, Dict, List, Optional, Union
67

78
from edgecraftrag.base import BaseComponent
89
from pydantic import model_serializer
@@ -40,7 +41,7 @@ def ensure_file_exists(self):
4041
dir_path = os.path.dirname(self.file_paths[0])
4142
os.makedirs(dir_path, exist_ok=True)
4243
if not os.path.exists(self.file_paths[0]):
43-
with open(self.file_paths[0], 'w', encoding='utf-8') as f:
44+
with open(self.file_paths[0], "w", encoding="utf-8") as f:
4445
json.dump([], f, ensure_ascii=False, indent=4)
4546

4647
def get_all_experience(self) -> List[Dict]:
@@ -49,80 +50,82 @@ def get_all_experience(self) -> List[Dict]:
4950
self.file_paths.append(experinence_file)
5051
if not os.path.isfile(self.file_paths[0]):
5152
self.ensure_file_exists()
52-
with open(self.file_paths[0], 'r', encoding='utf-8') as f:
53+
with open(self.file_paths[0], "r", encoding="utf-8") as f:
5354
return json.load(f)
5455

5556
def get_experience_by_question(self, question: str) -> Optional[Dict]:
5657
for item in self.get_all_experience():
57-
if item.get('question') == question:
58+
if item.get("question") == question:
5859
return item
5960
return None
6061

61-
def add_multiple_experiences(self, experiences: List[Dict[str, Union[str, List[str]]]], flag: bool = True) -> List[Dict]:
62+
def add_multiple_experiences(
63+
self, experiences: List[Dict[str, Union[str, List[str]]]], flag: bool = True
64+
) -> List[Dict]:
6265
all_experiences = self.get_all_experience()
6366
result = []
6467
for exp in experiences:
65-
question = exp.get('question')
68+
question = exp.get("question")
6669
if not question:
6770
raise ValueError("Must exist when uploading question")
68-
content = exp.get('content', [])
71+
content = exp.get("content", [])
6972
found = False
7073
for item in all_experiences:
71-
if item['question'] == question:
74+
if item["question"] == question:
7275
if flag:
73-
item['content'].extend([c for c in content if c not in item['content']])
76+
item["content"].extend([c for c in content if c not in item["content"]])
7477
else:
75-
item['content'] = content
78+
item["content"] = content
7679
result.append(item)
7780
found = True
7881
break
7982
if not found:
80-
new_item = {'question': question, 'content': content}
83+
new_item = {"question": question, "content": content}
8184
all_experiences.append(new_item)
8285
result.append(new_item)
83-
with open(self.file_paths[0], 'w', encoding='utf-8') as f:
86+
with open(self.file_paths[0], "w", encoding="utf-8") as f:
8487
json.dump(all_experiences, f, ensure_ascii=False, indent=4)
8588
return result
8689

8790
def delete_experience(self, question: str) -> bool:
8891
items = self.get_all_experience()
89-
remaining_items = [item for item in items if item.get('question') != question]
92+
remaining_items = [item for item in items if item.get("question") != question]
9093
if len(remaining_items) == len(items):
9194
return False
92-
with open(self.file_paths[0], 'w', encoding='utf-8') as f:
95+
with open(self.file_paths[0], "w", encoding="utf-8") as f:
9396
json.dump(remaining_items, f, ensure_ascii=False, indent=4)
9497
return True
9598

9699
def clear_experiences(self) -> bool:
97100
all_experiences = self.get_all_experience()
98-
with open(self.file_paths[0], 'w', encoding='utf-8') as f:
101+
with open(self.file_paths[0], "w", encoding="utf-8") as f:
99102
json.dump([], f, ensure_ascii=False, indent=4)
100103
return True
101104

102105
def update_experience(self, question: str, content: List[str]) -> Optional[Dict]:
103106
items = self.get_all_experience()
104107
for i, item in enumerate(items):
105-
if item.get('question') == question:
106-
updated_item = {'question': question, 'content': content}
108+
if item.get("question") == question:
109+
updated_item = {"question": question, "content": content}
107110
items[i] = updated_item
108-
with open(self.file_paths[0], 'w', encoding='utf-8') as f:
111+
with open(self.file_paths[0], "w", encoding="utf-8") as f:
109112
json.dump(items, f, ensure_ascii=False, indent=4)
110113
return updated_item
111114
return None
112115

113116
def add_experiences_from_file(self, file_path: str, flag: bool = False) -> List[Dict]:
114-
if not file_path.endswith('.json'):
117+
if not file_path.endswith(".json"):
115118
raise ValueError("File upload type error")
116119
try:
117-
with open(file_path, 'r', encoding='utf-8') as f:
120+
with open(file_path, "r", encoding="utf-8") as f:
118121
experiences = json.load(f)
119122
if not isinstance(experiences, list):
120123
raise ValueError("The contents of the file must be a list")
121124
return self.add_multiple_experiences(experiences=experiences, flag=flag)
122125
except json.JSONDecodeError as e:
123-
raise ValueError(f"File parsing failure")
126+
raise ValueError("File parsing failure")
124127
except Exception as e:
125-
raise RuntimeError(f"File Error")
128+
raise RuntimeError("File Error")
126129

127130
def calculate_totals(self):
128131
if self.comp_type == "knowledge":
@@ -146,6 +149,6 @@ def ser_model(self):
146149
"description": self.description,
147150
"active": self.active,
148151
"experience_active": self.experience_active,
149-
"total": self.calculate_totals()
152+
"total": self.calculate_totals(),
150153
}
151154
return set

0 commit comments

Comments
 (0)