2
2
from langgraph .graph import StateGraph
3
3
from langgraph .types import Send
4
4
5
- from template_langgraph .agents .news_summarizer_agent .models import AgentState , Article , StructuredArticle
5
+ from template_langgraph .agents .news_summarizer_agent .models import (
6
+ AgentState ,
7
+ Article ,
8
+ StructuredArticle ,
9
+ SummarizeWebContentState ,
10
+ )
6
11
from template_langgraph .llms .azure_openais import AzureOpenAiWrapper
7
12
from template_langgraph .loggers import get_logger
8
13
9
14
logger = get_logger (__name__ )
10
15
11
16
12
17
class MockNotifier :
13
- def notify (self , request_id : str , body : dict ) -> None :
18
+ def notify (self , id : str , body : dict ) -> None :
14
19
"""Simulate sending a notification to the user."""
15
- logger .info (f"Notification sent for request { request_id } : { body } " )
20
+ logger .info (f"Notification sent for request { id } : { body } " )
16
21
17
22
18
23
class MockScraper :
@@ -85,16 +90,19 @@ def create_graph(self):
85
90
86
91
# Create nodes
87
92
workflow .add_node ("initialize" , self .initialize )
88
- workflow .add_node ("fetch_web_content " , self .fetch_web_content )
93
+ workflow .add_node ("summarize_web_content " , self .summarize_web_content )
89
94
workflow .add_node ("notify" , self .notify )
90
95
91
96
# Create edges
92
97
workflow .set_entry_point ("initialize" )
93
98
workflow .add_conditional_edges (
94
99
source = "initialize" ,
95
100
path = self .run_subtasks ,
101
+ path_map = {
102
+ "summarize_web_content" : "summarize_web_content" ,
103
+ },
96
104
)
97
- workflow .add_edge ("fetch_web_content " , "notify" )
105
+ workflow .add_edge ("summarize_web_content " , "notify" )
98
106
workflow .set_finish_point ("notify" )
99
107
return workflow .compile (
100
108
name = NewsSummarizerAgent .__name__ ,
@@ -111,61 +119,61 @@ def run_subtasks(self, state: AgentState) -> list[Send]:
111
119
logger .info (f"Running subtasks with state: { state } " )
112
120
return [
113
121
Send (
114
- node = "fetch_web_content" ,
115
- arg = AgentState (
116
- input = state .input ,
117
- output = state .output ,
118
- target_url_index = idx ,
122
+ node = "summarize_web_content" ,
123
+ arg = SummarizeWebContentState (
124
+ url = state .input .urls [idx ],
125
+ prompt = state .input .prompt ,
119
126
),
120
127
)
121
128
for idx , _ in enumerate (state .input .urls )
122
129
]
123
130
124
- def fetch_web_content (self , state : AgentState ):
125
- url : str = state .input .urls [state .target_url_index ]
126
- is_valid_url = url .startswith ("http" )
131
+ def summarize_web_content (self , state : SummarizeWebContentState ):
132
+ is_valid_url = state .url .startswith ("http" )
127
133
is_valid_content = False
128
134
content = ""
129
135
130
136
# Check if the URL is valid
131
137
if not is_valid_url :
132
- logger .error (f"Invalid URL: { url } " )
138
+ logger .error (f"Invalid URL: { state . url } " )
133
139
is_valid_content = False
134
140
else :
135
141
# Scrape the web content
136
142
try :
137
- logger .info (f"Scraping URL: { url } " )
138
- content = self .scraper .scrape (url )
143
+ logger .info (f"Scraping URL: { state . url } " )
144
+ content = self .scraper .scrape (state . url )
139
145
is_valid_content = True
140
146
except httpx .RequestError as e :
141
147
logger .error (f"Error fetching web content: { e } " )
142
148
143
149
if is_valid_content :
144
- logger .info (f"Summarizing content with LLM @ { state .target_url_index } : { url } " )
150
+ logger .info (f"Summarizing content with LLM: { state .url } " )
145
151
structured_article : StructuredArticle = self .summarizer .summarize (
146
- prompt = state .input . request ,
152
+ prompt = state .prompt ,
147
153
content = content ,
148
154
)
149
- state .output .articles .append (
150
- Article (
151
- is_valid_url = is_valid_url ,
152
- is_valid_content = is_valid_content ,
153
- content = content ,
154
- url = url ,
155
- structured_article = structured_article ,
156
- ),
157
- )
155
+ return {
156
+ "articles" : [
157
+ Article (
158
+ is_valid_url = is_valid_url ,
159
+ is_valid_content = is_valid_content ,
160
+ content = content ,
161
+ url = state .url ,
162
+ structured_article = structured_article ,
163
+ ),
164
+ ]
165
+ }
158
166
159
167
def notify (self , state : AgentState ) -> AgentState :
160
168
"""Send notifications to the user."""
161
169
logger .info (f"Sending notifications with state: { state } " )
162
170
# Simulate sending notifications
163
171
# convert list of articles to a dictionary for notification
164
172
summary = {}
165
- for i , article in enumerate (state .output . articles ):
173
+ for i , article in enumerate (state .articles ):
166
174
summary [i ] = article .model_dump ()
167
175
self .notifier .notify (
168
- request_id = state .input .request_id ,
176
+ id = state .input .id ,
169
177
body = summary ,
170
178
)
171
179
return state
0 commit comments