File size: 10,550 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bc750c
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Direct LLM Knowledge Extraction Method

A streamlined approach that uses direct LLM API calls with structured output
instead of the CrewAI framework for better performance and cost efficiency.
"""

import json
import logging
import os
import sys
import time
from datetime import datetime
from typing import Any, Dict

from openai import OpenAI
from pydantic import ValidationError

from evaluation.knowledge_extraction.baselines.unified_method import KnowledgeGraph

# Add the parent directory to the path to ensure imports work correctly
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))

from evaluation.knowledge_extraction.baselines.base_method import BaseKnowledgeExtractionMethod

# Import shared prompt templates
from evaluation.knowledge_extraction.utils.prompts import (
    ENTITY_EXTRACTION_INSTRUCTION_PROMPT,
    ENTITY_EXTRACTION_SYSTEM_PROMPT,
    GRAPH_BUILDER_INSTRUCTION_PROMPT,
    GRAPH_BUILDER_SYSTEM_PROMPT,
    RELATION_EXTRACTION_INSTRUCTION_PROMPT,
    RELATION_EXTRACTION_SYSTEM_PROMPT,
)

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Set higher log levels for noisy libraries
logging.getLogger("openai").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)

class DirectLLMKnowledgeExtractor(BaseKnowledgeExtractionMethod):
    """Direct LLM knowledge extraction method using OpenAI API with structured output."""

    def __init__(self, model: str = "gpt-5-mini", **kwargs):
        super().__init__("direct_llm_method", **kwargs)
        self.client = OpenAI()
        self.model = model
        self.max_retries = 3
        self.retry_delay = 1.0

    def _get_optimized_system_prompt(self) -> str:
        """Get the optimized system prompt for knowledge graph extraction."""
        # Combine all system prompts for a unified extraction
        return f"""{ENTITY_EXTRACTION_SYSTEM_PROMPT}
{RELATION_EXTRACTION_SYSTEM_PROMPT}
{GRAPH_BUILDER_SYSTEM_PROMPT}"""

    def _get_extraction_instruction(self, text: str) -> str:
        """Get the extraction instruction with the input text."""
        # Combine entity and relation extraction instructions
        entity_instruction = ENTITY_EXTRACTION_INSTRUCTION_PROMPT.format(input_data=text)
        relation_instruction = RELATION_EXTRACTION_INSTRUCTION_PROMPT.format(input_data=text)
        graph_instruction = GRAPH_BUILDER_INSTRUCTION_PROMPT

        return f"""Extract a complete knowledge graph from the following agent system data.

First, extract entities following these instructions:
{entity_instruction}

Then, extract relations following these instructions:
{relation_instruction}

Finally, build the knowledge graph following these instructions:
{graph_instruction}
"""

    def _extract_with_retry(self, text: str) -> Dict[str, Any]:
        """Extract knowledge graph with retry logic using new Structured Outputs API."""
        last_error = None
        for attempt in range(self.max_retries):
            try:
                logger.info(f"Extraction attempt {attempt + 1}/{self.max_retries}")
                
                # Use the beta API with structured outputs
                response = self.client.beta.chat.completions.parse(
                    model=self.model,
                    messages=[
                        {"role": "system", "content": self._get_optimized_system_prompt()},
                        {"role": "user", "content": self._get_extraction_instruction(text)},
                    ],
                    response_format=KnowledgeGraph,
                    temperature=0,
                )

                # Get the parsed response
                parsed_response = response.choices[0].message.parsed
                
                # Handle refusal
                if response.choices[0].message.refusal:
                    raise ValueError(f"Model refused: {response.choices[0].message.refusal}")
                
                if not parsed_response:
                    raise ValueError("Empty parsed response from LLM")

                # Convert to dict
                kg_dict = parsed_response.model_dump()

                # Add metadata
                kg_dict["metadata"] = {
                    "method": "direct_llm",
                    "model": self.model,
                    "attempt": attempt + 1,
                    "timestamp": datetime.now().isoformat(),
                    "token_usage": {
                        "prompt_tokens": response.usage.prompt_tokens if response.usage else 0,
                        "completion_tokens": response.usage.completion_tokens if response.usage else 0,
                        "total_tokens": response.usage.total_tokens if response.usage else 0,
                    },
                }

                logger.info(
                    f"Successfully extracted KG with {len(kg_dict['entities'])} entities and {len(kg_dict['relations'])} relations"
                )
                return kg_dict

            except json.JSONDecodeError as e:
                last_error = f"JSON parsing error: {e}"
                logger.warning(f"Attempt {attempt + 1} failed: {last_error}")

            except ValidationError as e:
                last_error = f"Validation error: {e}"
                logger.warning(f"Attempt {attempt + 1} failed: {last_error}")

            except Exception as e:
                last_error = f"API error: {e}"
                logger.warning(f"Attempt {attempt + 1} failed: {last_error}")

            if attempt < self.max_retries - 1:
                time.sleep(self.retry_delay * (2**attempt))  # Exponential backoff

        # If all attempts failed, return empty structure
        logger.error(f"All extraction attempts failed. Last error: {last_error}")
        return {
            "entities": [],
            "relations": [],
            "system_name": "Failed Extraction",
            "system_summary": "Knowledge graph extraction failed after multiple attempts.",
            "metadata": {"error": last_error, "method": "direct_llm"},
        }

    def process_text(self, text: str) -> Dict[str, Any]:
        """
        Process input text using direct LLM API calls.

        Args:
            text: Input text to process

        Returns:
            Dictionary with kg_data, metadata, success, and optional error
        """
        start_time = time.time()

        try:
            logger.info(f"Processing text with Direct LLM method (length: {len(text)})")

            # Extract knowledge graph
            kg_data = self._extract_with_retry(text)

            processing_time = time.time() - start_time

            # Check if extraction was successful
            success = len(kg_data.get("entities", [])) > 0 or len(kg_data.get("relations", [])) > 0

            # Calculate statistics
            entity_count = len(kg_data.get("entities", []))
            relation_count = len(kg_data.get("relations", []))

            # Add processing metadata
            if "metadata" not in kg_data:
                kg_data["metadata"] = {}

            kg_data["metadata"].update(
                {
                    "processing_info": {
                        "method": "direct_llm",
                        "processing_time_seconds": processing_time,
                        "processed_at": datetime.now().isoformat(),
                        "model": self.model,
                        "api_calls": 1,
                        "entity_count": entity_count,
                        "relation_count": relation_count,
                    }
                }
            )

            return {
                "success": success,
                "kg_data": kg_data,
                "metadata": {
                    "approach": "direct_llm",
                    "model": self.model,
                    "method": self.method_name,
                    "processing_time_seconds": processing_time,
                    "entity_count": entity_count,
                    "relation_count": relation_count,
                    "entities_per_second": entity_count / processing_time if processing_time > 0 else 0,
                    "relations_per_second": relation_count / processing_time if processing_time > 0 else 0,
                    "api_calls": 1,
                    "token_usage": kg_data.get("metadata", {}).get("token_usage", {}),
                },
            }

        except Exception as e:
            processing_time = time.time() - start_time
            logger.error(f"Error in direct LLM knowledge extraction: {e}")
            import traceback

            logger.error(f"Traceback: {traceback.format_exc()}")

            return {
                "success": False,
                "error": str(e),
                "kg_data": {"entities": [], "relations": []},
                "metadata": {
                    "approach": "direct_llm",
                    "model": self.model,
                    "method": self.method_name,
                    "processing_time_seconds": processing_time,
                    "api_calls": 1,
                    "error": str(e),
                },
            }

    def extract_knowledge_graph(self, trace_data: str) -> Dict[str, Any]:
        """
        Extract knowledge graph from trace data.

        Args:
            trace_data: Agent trace data as JSON string

        Returns:
            Dictionary with entities and relations
        """
        try:
            logger.info(f"extract_knowledge_graph called with trace_data type: {type(trace_data)}")
            if isinstance(trace_data, str):
                logger.info(f"trace_data length: {len(trace_data)}")
                logger.info(f"trace_data first 200 chars: {repr(trace_data[:200])}")

            # Process the trace data
            result = self.process_text(trace_data)

            # Return just the knowledge graph data
            if result.get("success", False):
                return result.get("kg_data", {"entities": [], "relations": []})
            else:
                # Return empty knowledge graph on failure
                return {"entities": [], "relations": []}

        except Exception as e:
            logger.error(f"Error in extract_knowledge_graph: {e}")
            logger.error(f"trace_data type: {type(trace_data)}")
            if isinstance(trace_data, str):
                logger.error(f"trace_data content (first 200 chars): {repr(trace_data[:200])}")
            return {"entities": [], "relations": []}