Spaces:
Sleeping
Sleeping
Commit ·
f7cf6c0
1
Parent(s): 7326774
Initial FastAPI CrewAI setup
Browse files- agents/books/apa_agent.py +7 -3
- rag/automation_metadata.py +6 -1
agents/books/apa_agent.py
CHANGED
|
@@ -127,21 +127,25 @@ def run_metadata_agent(source: dict) -> dict:
|
|
| 127 |
|
| 128 |
result = crew.kickoff(inputs={"source": source})
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
# =====================================================
|
| 131 |
# Normalize output (SAFE)
|
| 132 |
# =====================================================
|
| 133 |
if hasattr(result, "json_dict"):
|
| 134 |
jd = result.json_dict
|
| 135 |
-
return jd() if callable(jd) else jd
|
| 136 |
|
| 137 |
if hasattr(result, "model_dump"):
|
| 138 |
md = result.model_dump
|
| 139 |
-
return md() if callable(md) else md
|
| 140 |
|
| 141 |
try:
|
| 142 |
parsed = json.loads(str(result))
|
| 143 |
if isinstance(parsed, dict):
|
| 144 |
-
return parsed
|
| 145 |
except Exception:
|
| 146 |
pass
|
| 147 |
|
|
|
|
| 127 |
|
| 128 |
result = crew.kickoff(inputs={"source": source})
|
| 129 |
|
| 130 |
+
tokens={}
|
| 131 |
+
if result.token_usage:
|
| 132 |
+
tokens = result.token_usage.dict()
|
| 133 |
+
|
| 134 |
# =====================================================
|
| 135 |
# Normalize output (SAFE)
|
| 136 |
# =====================================================
|
| 137 |
if hasattr(result, "json_dict"):
|
| 138 |
jd = result.json_dict
|
| 139 |
+
return (jd() if callable(jd) else jd), tokens
|
| 140 |
|
| 141 |
if hasattr(result, "model_dump"):
|
| 142 |
md = result.model_dump
|
| 143 |
+
return (md() if callable(md) else md), tokens
|
| 144 |
|
| 145 |
try:
|
| 146 |
parsed = json.loads(str(result))
|
| 147 |
if isinstance(parsed, dict):
|
| 148 |
+
return parsed, tokens
|
| 149 |
except Exception:
|
| 150 |
pass
|
| 151 |
|
rag/automation_metadata.py
CHANGED
|
@@ -41,6 +41,7 @@ def process_user_metadata(user_id: str, book_id: str | None = None) -> Dict[str,
|
|
| 41 |
processed = 0
|
| 42 |
failed = 0
|
| 43 |
items: List[Dict[str, Any]] = []
|
|
|
|
| 44 |
|
| 45 |
if total == 0:
|
| 46 |
return {
|
|
@@ -75,7 +76,7 @@ def process_user_metadata(user_id: str, book_id: str | None = None) -> Dict[str,
|
|
| 75 |
|
| 76 |
try:
|
| 77 |
agent_in = _build_agent_input(raw)
|
| 78 |
-
out = run_metadata_agent(agent_in)
|
| 79 |
except Exception as e:
|
| 80 |
print(f"⚠️ LLM internal exception for doc_id {doc_id}: {e}")
|
| 81 |
out = None
|
|
@@ -93,6 +94,9 @@ def process_user_metadata(user_id: str, book_id: str | None = None) -> Dict[str,
|
|
| 93 |
}
|
| 94 |
)
|
| 95 |
continue
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
print(f"Metadata agent output for doc_id {doc_id}: {type(out)} - {out}")
|
| 98 |
print(f"is_valid: {out.get('is_valid')}")
|
|
@@ -168,4 +172,5 @@ def process_user_metadata(user_id: str, book_id: str | None = None) -> Dict[str,
|
|
| 168 |
"failed": failed,
|
| 169 |
"items": items,
|
| 170 |
"mode": "sequential",
|
|
|
|
| 171 |
}
|
|
|
|
| 41 |
processed = 0
|
| 42 |
failed = 0
|
| 43 |
items: List[Dict[str, Any]] = []
|
| 44 |
+
tokens = []
|
| 45 |
|
| 46 |
if total == 0:
|
| 47 |
return {
|
|
|
|
| 76 |
|
| 77 |
try:
|
| 78 |
agent_in = _build_agent_input(raw)
|
| 79 |
+
out, token_usage = run_metadata_agent(agent_in)
|
| 80 |
except Exception as e:
|
| 81 |
print(f"⚠️ LLM internal exception for doc_id {doc_id}: {e}")
|
| 82 |
out = None
|
|
|
|
| 94 |
}
|
| 95 |
)
|
| 96 |
continue
|
| 97 |
+
|
| 98 |
+
if token_usage:
|
| 99 |
+
tokens.append(token_usage)
|
| 100 |
|
| 101 |
print(f"Metadata agent output for doc_id {doc_id}: {type(out)} - {out}")
|
| 102 |
print(f"is_valid: {out.get('is_valid')}")
|
|
|
|
| 172 |
"failed": failed,
|
| 173 |
"items": items,
|
| 174 |
"mode": "sequential",
|
| 175 |
+
"tokens": tokens,
|
| 176 |
}
|