Owadokun Tosin Tobi commited on
removed asyncio
Browse filesChanges: Cleaned imports, removed nest_asyncio, fixed output_type argument, added robust result parsing.
- agents/extractor.py +16 -14
agents/extractor.py
CHANGED
|
@@ -1,29 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
-
import nest_asyncio
|
| 3 |
from pydantic_ai import Agent
|
| 4 |
from .models import InvoiceSchema, ResumeSchema, GenericSchema
|
| 5 |
from utils.client import get_api_key
|
| 6 |
from utils.prompt_loader import load_prompt
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
nest_asyncio.apply()
|
| 10 |
-
|
| 11 |
-
# Ensure key is available
|
| 12 |
os.environ["GOOGLE_API_KEY"] = get_api_key()
|
| 13 |
|
| 14 |
-
# Load
|
| 15 |
master_system_prompt = load_prompt("system.yaml")
|
| 16 |
|
| 17 |
-
# Initialize Agent
|
|
|
|
| 18 |
agent = Agent(
|
| 19 |
'google-gla:models/gemini-2.5-flash',
|
| 20 |
-
system_prompt=master_system_prompt
|
| 21 |
)
|
| 22 |
|
| 23 |
async def process_data(text: str, schema_type: str):
|
| 24 |
"""
|
| 25 |
-
Orchestrates the extraction process
|
|
|
|
| 26 |
"""
|
|
|
|
| 27 |
if schema_type == "invoice":
|
| 28 |
target_model = InvoiceSchema
|
| 29 |
elif schema_type == "resume":
|
|
@@ -32,14 +31,17 @@ async def process_data(text: str, schema_type: str):
|
|
| 32 |
target_model = GenericSchema
|
| 33 |
|
| 34 |
try:
|
| 35 |
-
#
|
|
|
|
| 36 |
result = await agent.run(text, output_type=target_model)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
return result.data
|
| 41 |
-
|
| 42 |
return result.output
|
|
|
|
|
|
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
return {"error": f"
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from pydantic_ai import Agent
|
| 3 |
from .models import InvoiceSchema, ResumeSchema, GenericSchema
|
| 4 |
from utils.client import get_api_key
|
| 5 |
from utils.prompt_loader import load_prompt
|
| 6 |
|
| 7 |
+
# Ensure API key is loaded into environment for the library
|
|
|
|
|
|
|
|
|
|
| 8 |
os.environ["GOOGLE_API_KEY"] = get_api_key()
|
| 9 |
|
| 10 |
+
# Load Externalized Prompt
|
| 11 |
master_system_prompt = load_prompt("system.yaml")
|
| 12 |
|
| 13 |
+
# Initialize Agent (Gemini 2.5 Flash)
|
| 14 |
+
# Note: Using the model ID compatible with Google's GenAI SDK
|
| 15 |
agent = Agent(
|
| 16 |
'google-gla:models/gemini-2.5-flash',
|
| 17 |
+
system_prompt=master_system_prompt
|
| 18 |
)
|
| 19 |
|
| 20 |
async def process_data(text: str, schema_type: str):
|
| 21 |
"""
|
| 22 |
+
Orchestrates the extraction process.
|
| 23 |
+
Selects the correct Pydantic schema and enforces type-safety.
|
| 24 |
"""
|
| 25 |
+
# 1. Router Logic
|
| 26 |
if schema_type == "invoice":
|
| 27 |
target_model = InvoiceSchema
|
| 28 |
elif schema_type == "resume":
|
|
|
|
| 31 |
target_model = GenericSchema
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# 2. Execution (With Schema Enforcement)
|
| 35 |
+
# Using 'output_type' as required by PydanticAI v1.40+
|
| 36 |
result = await agent.run(text, output_type=target_model)
|
| 37 |
|
| 38 |
+
# 3. Result Parsing (Handle version differences in return object)
|
| 39 |
+
if hasattr(result, 'data'):
|
| 40 |
return result.data
|
| 41 |
+
elif hasattr(result, 'output'):
|
| 42 |
return result.output
|
| 43 |
+
else:
|
| 44 |
+
return result
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
return {"error": f"Extraction Logic Failed: {str(e)}"}
|