Spaces:
Running
Running
Commit
·
232e016
1
Parent(s):
edf083b
add
Browse files
agentgraph/methods/production/openai_structured_extractor.py
CHANGED
|
@@ -16,7 +16,7 @@ from openai import OpenAI
|
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
# Import Pydantic models
|
| 19 |
-
from agentgraph.shared.models.reference_based import KnowledgeGraph, Entity, Relation
|
| 20 |
|
| 21 |
# Load environment variables from root directory
|
| 22 |
load_dotenv('/Users/zekunwu/Desktop/agent_monitoring/.env')
|
|
@@ -24,80 +24,6 @@ load_dotenv('/Users/zekunwu/Desktop/agent_monitoring/.env')
|
|
| 24 |
# Configure logging
|
| 25 |
logger = logging.getLogger(__name__)
|
| 26 |
|
| 27 |
-
# Simplified models for OpenAI structured outputs
|
| 28 |
-
class SimpleEntity(BaseModel):
|
| 29 |
-
id: str
|
| 30 |
-
type: str # Agent, Task, Tool, Input, Output, Human
|
| 31 |
-
name: str
|
| 32 |
-
importance: str # HIGH, MEDIUM, LOW
|
| 33 |
-
|
| 34 |
-
class SimpleRelation(BaseModel):
|
| 35 |
-
id: str
|
| 36 |
-
source: str
|
| 37 |
-
target: str
|
| 38 |
-
type: str # PERFORMS, USES, etc.
|
| 39 |
-
importance: str
|
| 40 |
-
|
| 41 |
-
class SimpleKnowledgeGraph(BaseModel):
|
| 42 |
-
system_name: str
|
| 43 |
-
system_summary: str
|
| 44 |
-
entities: List[SimpleEntity]
|
| 45 |
-
relations: List[SimpleRelation]
|
| 46 |
-
|
| 47 |
-
def normalize_importance(importance: str) -> str:
|
| 48 |
-
"""Normalize importance values to HIGH/MEDIUM/LOW."""
|
| 49 |
-
importance_upper = importance.upper()
|
| 50 |
-
# Map common variations to standard values
|
| 51 |
-
mapping = {
|
| 52 |
-
"CRITICAL": "HIGH",
|
| 53 |
-
"VERY HIGH": "HIGH",
|
| 54 |
-
"VERY LOW": "LOW",
|
| 55 |
-
"NORMAL": "MEDIUM",
|
| 56 |
-
"STANDARD": "MEDIUM"
|
| 57 |
-
}
|
| 58 |
-
return mapping.get(importance_upper, importance_upper)
|
| 59 |
-
|
| 60 |
-
def convert_simple_to_full_kg(simple_kg: SimpleKnowledgeGraph) -> KnowledgeGraph:
|
| 61 |
-
"""Convert simplified KG to full KnowledgeGraph model."""
|
| 62 |
-
|
| 63 |
-
# Convert entities
|
| 64 |
-
entities = []
|
| 65 |
-
for se in simple_kg.entities:
|
| 66 |
-
entity = Entity(
|
| 67 |
-
id=se.id,
|
| 68 |
-
type=se.type,
|
| 69 |
-
name=se.name,
|
| 70 |
-
importance=normalize_importance(se.importance), # Normalize importance
|
| 71 |
-
raw_prompt="", # Empty as per requirements
|
| 72 |
-
raw_prompt_ref=[] # Empty for now
|
| 73 |
-
)
|
| 74 |
-
entities.append(entity)
|
| 75 |
-
|
| 76 |
-
# Convert relations
|
| 77 |
-
relations = []
|
| 78 |
-
for sr in simple_kg.relations:
|
| 79 |
-
relation = Relation(
|
| 80 |
-
id=sr.id,
|
| 81 |
-
source=sr.source,
|
| 82 |
-
target=sr.target,
|
| 83 |
-
type=sr.type,
|
| 84 |
-
importance=normalize_importance(sr.importance), # Normalize importance
|
| 85 |
-
interaction_prompt="", # Empty as per requirements
|
| 86 |
-
interaction_prompt_ref=[] # Empty for now
|
| 87 |
-
)
|
| 88 |
-
relations.append(relation)
|
| 89 |
-
|
| 90 |
-
# Create full KnowledgeGraph
|
| 91 |
-
kg = KnowledgeGraph(
|
| 92 |
-
system_name=simple_kg.system_name,
|
| 93 |
-
system_summary=simple_kg.system_summary,
|
| 94 |
-
entities=entities,
|
| 95 |
-
relations=relations,
|
| 96 |
-
failures=None, # Not generated by this simple method
|
| 97 |
-
optimizations=None # Not generated by this simple method
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
return kg
|
| 101 |
|
| 102 |
class OpenAIStructuredExtractor:
|
| 103 |
"""
|
|
@@ -128,8 +54,10 @@ class OpenAIStructuredExtractor:
|
|
| 128 |
"""
|
| 129 |
logger.info(f"Starting knowledge graph extraction for {len(input_data)} characters of input")
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
system_prompt = """You are an expert at analyzing agent system traces and extracting knowledge graphs.
|
|
|
|
|
|
|
| 133 |
|
| 134 |
Extract a knowledge graph with these entity types:
|
| 135 |
- Agent: AI agents with specific roles
|
|
@@ -151,13 +79,26 @@ Use these relationship types:
|
|
| 151 |
- DELIVERS_TO: Output→Human
|
| 152 |
- INTERVENES: Agent/Human→Task
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
Focus on
|
| 161 |
|
| 162 |
user_prompt = f"Analyze this agent system trace and extract a knowledge graph:\n\n{input_data}"
|
| 163 |
|
|
@@ -168,12 +109,11 @@ Focus on identifying the actual workflow, not framework details."""
|
|
| 168 |
{"role": "system", "content": system_prompt},
|
| 169 |
{"role": "user", "content": user_prompt}
|
| 170 |
],
|
| 171 |
-
text_format=
|
| 172 |
)
|
| 173 |
|
| 174 |
-
# Get the parsed response
|
| 175 |
-
|
| 176 |
-
knowledge_graph = convert_simple_to_full_kg(simple_kg)
|
| 177 |
|
| 178 |
logger.info(f"Extraction complete: {len(knowledge_graph.entities)} entities, {len(knowledge_graph.relations)} relations")
|
| 179 |
return knowledge_graph
|
|
|
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
# Import Pydantic models
|
| 19 |
+
from agentgraph.shared.models.reference_based import KnowledgeGraph, Entity, Relation, ContentReference
|
| 20 |
|
| 21 |
# Load environment variables from root directory
|
| 22 |
load_dotenv('/Users/zekunwu/Desktop/agent_monitoring/.env')
|
|
|
|
| 24 |
# Configure logging
|
| 25 |
logger = logging.getLogger(__name__)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
class OpenAIStructuredExtractor:
|
| 29 |
"""
|
|
|
|
| 54 |
"""
|
| 55 |
logger.info(f"Starting knowledge graph extraction for {len(input_data)} characters of input")
|
| 56 |
|
| 57 |
+
# System prompt for direct KnowledgeGraph extraction with content references
|
| 58 |
+
system_prompt = """You are an expert at analyzing agent system traces and extracting knowledge graphs with precise content references.
|
| 59 |
+
|
| 60 |
+
The input may contain line markers like <L1>, <L2>, etc. Use these to create accurate content references when available.
|
| 61 |
|
| 62 |
Extract a knowledge graph with these entity types:
|
| 63 |
- Agent: AI agents with specific roles
|
|
|
|
| 79 |
- DELIVERS_TO: Output→Human
|
| 80 |
- INTERVENES: Agent/Human→Task
|
| 81 |
|
| 82 |
+
For each entity provide:
|
| 83 |
+
- id: unique identifier (generate if needed)
|
| 84 |
+
- type: one of the types above
|
| 85 |
+
- name: descriptive name
|
| 86 |
+
- importance: HIGH, MEDIUM, or LOW
|
| 87 |
+
- raw_prompt: actual prompt/specification content that defines this entity
|
| 88 |
+
- raw_prompt_ref: list of content references with line_start and line_end (if line markers available)
|
| 89 |
+
|
| 90 |
+
For each relation provide:
|
| 91 |
+
- id: unique identifier
|
| 92 |
+
- source: source entity id
|
| 93 |
+
- target: target entity id
|
| 94 |
+
- type: one of the types above
|
| 95 |
+
- importance: HIGH, MEDIUM, or LOW
|
| 96 |
+
- interaction_prompt: runtime evidence showing this relationship occurred
|
| 97 |
+
- interaction_prompt_ref: list of content references (if line markers available)
|
| 98 |
+
|
| 99 |
+
Provide system_name and system_summary for the overall system.
|
| 100 |
|
| 101 |
+
Focus on extracting the actual workflow with meaningful entities and relationships."""
|
| 102 |
|
| 103 |
user_prompt = f"Analyze this agent system trace and extract a knowledge graph:\n\n{input_data}"
|
| 104 |
|
|
|
|
| 109 |
{"role": "system", "content": system_prompt},
|
| 110 |
{"role": "user", "content": user_prompt}
|
| 111 |
],
|
| 112 |
+
text_format=KnowledgeGraph,
|
| 113 |
)
|
| 114 |
|
| 115 |
+
# Get the parsed response directly as KnowledgeGraph
|
| 116 |
+
knowledge_graph = response.output_parsed
|
|
|
|
| 117 |
|
| 118 |
logger.info(f"Extraction complete: {len(knowledge_graph.entities)} entities, {len(knowledge_graph.relations)} relations")
|
| 119 |
return knowledge_graph
|