[KM-496] [DED][AI] Fix Response data structure for field sources (db flow)
Browse files- src/api/v1/chat.py +23 -9
- src/config/agents/system_prompt.md +1 -2
src/api/v1/chat.py
CHANGED
|
@@ -61,15 +61,29 @@ def _extract_sources(results: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
| 61 |
seen = set()
|
| 62 |
sources = []
|
| 63 |
for result in results:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
logger.debug(f"Extracted sources: {sources}")
|
| 74 |
return sources
|
| 75 |
|
|
|
|
| 61 |
seen = set()
|
| 62 |
sources = []
|
| 63 |
for result in results:
|
| 64 |
+
if "document_id" in result["metadata"]["data"]:
|
| 65 |
+
meta = result["metadata"]
|
| 66 |
+
key = (meta.get("document_id"), meta.get("page_label"))
|
| 67 |
+
if key not in seen:
|
| 68 |
+
seen.add(key)
|
| 69 |
+
sources.append({
|
| 70 |
+
"document_id": meta.get("data", {}).get("document_id"),
|
| 71 |
+
"filename": meta.get("data", {}).get("filename", "Unknown"),
|
| 72 |
+
"page_label": meta.get("data", {}).get("page_label", "Unknown"),
|
| 73 |
+
})
|
| 74 |
+
else:
|
| 75 |
+
meta = result["metadata"]
|
| 76 |
+
key = (meta.get("table_name"), meta.get("column_name"))
|
| 77 |
+
if key not in seen:
|
| 78 |
+
seen.add(key)
|
| 79 |
+
table_name = meta.get("data", {}).get("table_name")
|
| 80 |
+
user_id = meta.get("user_id")
|
| 81 |
+
sources.append({
|
| 82 |
+
"document_id": f"{user_id}_{table_name}",
|
| 83 |
+
"filename": meta.get("data", {}).get("table_name", "Unknown"),
|
| 84 |
+
"page_label": meta.get("data", {}).get("column_name", "Unknown"),
|
| 85 |
+
})
|
| 86 |
+
|
| 87 |
logger.debug(f"Extracted sources: {sources}")
|
| 88 |
return sources
|
| 89 |
|
src/config/agents/system_prompt.md
CHANGED
|
@@ -3,8 +3,7 @@ You are a helpful AI assistant with access to user's uploaded documents. Your ro
|
|
| 3 |
1. Answer questions based on provided document context
|
| 4 |
2. If no relevant information is found in documents, acknowledge this honestly
|
| 5 |
3. Be concise and direct in your responses
|
| 6 |
-
4.
|
| 7 |
-
5. If user's question is unclear, ask for clarification
|
| 8 |
|
| 9 |
When document context is provided:
|
| 10 |
- Use information from documents to answer accurately
|
|
|
|
| 3 |
1. Answer questions based on provided document context
|
| 4 |
2. If no relevant information is found in documents, acknowledge this honestly
|
| 5 |
3. Be concise and direct in your responses
|
| 6 |
+
4. If user's question is unclear, ask for clarification
|
|
|
|
| 7 |
|
| 8 |
When document context is provided:
|
| 9 |
- Use information from documents to answer accurately
|