Spaces:
Sleeping
Sleeping
Update src/rag_engine.py
Browse files- src/rag_engine.py +29 -0
src/rag_engine.py
CHANGED
|
@@ -150,6 +150,35 @@ def save_uploaded_file(uploaded_file, username: str = "default") -> str:
|
|
| 150 |
logger.error(f"Error saving file: {e}")
|
| 151 |
return None
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
def ingest_file(file_path: str, username: str, strategy: str = "paragraph") -> Tuple[bool, str]:
|
| 154 |
"""
|
| 155 |
The High-Level Bridge: Takes a file path, chunks it, and saves to Vector DB.
|
|
|
|
| 150 |
logger.error(f"Error saving file: {e}")
|
| 151 |
return None
|
| 152 |
|
| 153 |
+
def process_and_add_text(text: str, source_name: str, username: str) -> Tuple[bool, str]:
|
| 154 |
+
"""
|
| 155 |
+
Ingests raw text string (e.g., from the Flattener tool) directly into Chroma.
|
| 156 |
+
"""
|
| 157 |
+
try:
|
| 158 |
+
user_db_path = os.path.join(CHROMA_PATH, username)
|
| 159 |
+
emb_fn = get_embedding_func()
|
| 160 |
+
|
| 161 |
+
# Initialize DB
|
| 162 |
+
db = Chroma(persist_directory=user_db_path, embedding_function=emb_fn)
|
| 163 |
+
|
| 164 |
+
# Create a Document object directly
|
| 165 |
+
doc = Document(
|
| 166 |
+
page_content=text,
|
| 167 |
+
metadata={
|
| 168 |
+
"source": source_name,
|
| 169 |
+
"strategy": "flattened_text",
|
| 170 |
+
"file_type": "generated"
|
| 171 |
+
}
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# Add single document
|
| 175 |
+
db.add_documents([doc])
|
| 176 |
+
return True, f"Successfully indexed flattened text: {source_name}"
|
| 177 |
+
|
| 178 |
+
except Exception as e:
|
| 179 |
+
logger.error(f"Error indexing raw text: {e}")
|
| 180 |
+
return False, f"Error: {str(e)}"
|
| 181 |
+
|
| 182 |
def ingest_file(file_path: str, username: str, strategy: str = "paragraph") -> Tuple[bool, str]:
|
| 183 |
"""
|
| 184 |
The High-Level Bridge: Takes a file path, chunks it, and saves to Vector DB.
|