-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
145 lines (118 loc) · 4.72 KB
/
Copy pathalgorithm.py
File metadata and controls
145 lines (118 loc) · 4.72 KB
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
"""
Research Algorithm - LangGraph based dataset generation pipeline.
6-node architecture: Query Generation -> Search -> Validate -> Scrape -> Extract -> Deduplicate
"""
import json
from datetime import datetime
from typing import Dict, Optional, Callable, TypedDict, List
from langgraph.graph import StateGraph, START, END
from dotenv import find_dotenv, load_dotenv
import os
# Import node implementations
from nodes.query_generation import QueryGenerationNode
from nodes.query_expansion_matrix import QueryExpansionMatrixNode
from nodes.search import SearchNode
from nodes.validate import ValidateNode
from nodes.Crawl4AI_scrape import Crawl4AIScrapeNode
from nodes.Crawl4AI_extract import Crawl4AIExtractNode
from nodes.deduplicate import DeduplicateNode
# Load environment
dotenv_path = find_dotenv()
if dotenv_path:
load_dotenv(dotenv_path)
# ============================================================================
# State Definition
# ============================================================================
class ResearchState(TypedDict):
"""Algorithm state - tracks progress through pipeline"""
initial_prompt: str
column_specs: List[str]
queries: List[Dict | str]
search_results: List[Dict]
validated_urls: List[str]
validated_results: List[Dict]
scraped_content: List[Dict]
extracted_items: List[Dict]
final_dataset: List[Dict]
session_id: str
round: int
error: Optional[str]
previous_session_id: Optional[str]
tweak_instructions: Optional[str]
previous_queries: List[Dict | str]
previous_items: List[Dict]
columns: List[Dict]
priority_columns: List[str]
hard_identifier_columns: List[str]
soft_identifier_columns: List[str]
# ============================================================================
# Logging & Progress Tracking
# ============================================================================
class ProgressTracker:
"""Tracks progress and sends updates to frontend"""
def __init__(self, emit_fn: Optional[Callable] = None):
self.emit_fn = emit_fn
self.current_step = ""
self.current_detail = ""
def update(self, step: str, detail: str = "", data: Optional[Dict] = None):
"""Update progress"""
self.current_step = step
self.current_detail = detail
# Log to terminal
print(f"\n{'='*70}")
print(f"📍 {step}")
print(f" {detail}")
if data:
print(f" Data: {json.dumps(data, default=str)[:200]}")
print(f"{'='*70}")
# Emit to frontend
if self.emit_fn:
try:
self.emit_fn("progress", {
"step": step,
"detail": detail,
"data": data,
"timestamp": datetime.now().isoformat()
})
except Exception as e:
print(f"❌ Failed to emit progress: {e}")
# ============================================================================
# Algorithm Builder
# ============================================================================
def build_research_algorithm(emit_fn: Optional[Callable] = None):
"""Build the complete research algorithm LangGraph"""
progress = ProgressTracker(emit_fn)
# Initialize node instances
strategy = os.getenv("QUERY_GENERATION_STRATEGY", "qem").lower()
if strategy in {"simple", "baseline", "basic"}:
query_gen_node = QueryGenerationNode()
else:
query_gen_node = QueryExpansionMatrixNode()
search_node = SearchNode()
validate_node = ValidateNode(threshold=0.5)
scrape_node = Crawl4AIScrapeNode(timeout_ms=15000)
extract_node = Crawl4AIExtractNode(char_limit=12000)
deduplicate_node = DeduplicateNode()
# Create graph
graph = StateGraph(ResearchState)
# Wrap nodes to inject progress tracker
def make_node(node_instance):
async def wrapped(state):
return await node_instance.execute(state, progress)
return wrapped
# Add nodes
graph.add_node("query_generation", make_node(query_gen_node))
graph.add_node("search", make_node(search_node))
graph.add_node("validate", make_node(validate_node))
graph.add_node("scrape", make_node(scrape_node))
graph.add_node("extract", make_node(extract_node))
graph.add_node("deduplicate", make_node(deduplicate_node))
# Add edges
graph.add_edge(START, "query_generation")
graph.add_edge("query_generation", "search")
graph.add_edge("search", "validate")
graph.add_edge("validate", "scrape")
graph.add_edge("scrape", "extract")
graph.add_edge("extract", "deduplicate")
graph.add_edge("deduplicate", END)
return graph.compile()