Sachin-Hansaka commited on
Commit
e454405
·
verified ·
1 Parent(s): f86f712

final fixed code

Browse files
src/langgraphagenticai/nodes/ai_news_node.py CHANGED
@@ -8,8 +8,6 @@ class AINewsNode:
8
  """
9
  self.tavily = TavilyClient()
10
  self.llm = llm
11
- # this is used to capture various steps in this file so that later can be use for steps shown
12
- self.state = {}
13
 
14
  def fetch_news(self, state: dict) -> dict:
15
  """
@@ -20,7 +18,6 @@ class AINewsNode:
20
  dict: Updated state with 'news_data' key containing fetched news.
21
  """
22
  frequency = state['messages'][0].content.lower()
23
- self.state['frequency'] = frequency
24
 
25
  time_range_map = {'daily': 'd', 'weekly': 'w', 'monthly': 'm', 'year': 'y'}
26
  days_map = {'daily': 1, 'weekly': 7, 'monthly': 30, 'year': 366}
@@ -35,7 +32,8 @@ class AINewsNode:
35
  )
36
 
37
  state['news_data'] = response.get('results', [])
38
- self.state['news_data'] = state['news_data']
 
39
  return state
40
 
41
  def summarize_news(self, state: dict) -> dict:
@@ -46,7 +44,8 @@ class AINewsNode:
46
  Returns:
47
  dict: Updated state with 'summary' key containing the summarized news.
48
  """
49
- news_items = self.state['news_data']
 
50
 
51
  prompt_template = ChatPromptTemplate.from_messages([
52
  ("system", """Summarize AI news articles into markdown format. For each item include:
@@ -69,11 +68,10 @@ class AINewsNode:
69
  response = self.llm.invoke(prompt_template.format(articles=articles_str))
70
 
71
  # Create formatted summary with header
72
- frequency = self.state['frequency']
73
  formatted_summary = f"# {frequency.capitalize()} AI News Summary\n\n{response.content}"
74
 
75
  state['summary'] = formatted_summary
76
- self.state['summary'] = formatted_summary
77
 
 
78
  return state
79
 
 
8
  """
9
  self.tavily = TavilyClient()
10
  self.llm = llm
 
 
11
 
12
  def fetch_news(self, state: dict) -> dict:
13
  """
 
18
  dict: Updated state with 'news_data' key containing fetched news.
19
  """
20
  frequency = state['messages'][0].content.lower()
 
21
 
22
  time_range_map = {'daily': 'd', 'weekly': 'w', 'monthly': 'm', 'year': 'y'}
23
  days_map = {'daily': 1, 'weekly': 7, 'monthly': 30, 'year': 366}
 
32
  )
33
 
34
  state['news_data'] = response.get('results', [])
35
+ state['frequency'] = frequency
36
+
37
  return state
38
 
39
  def summarize_news(self, state: dict) -> dict:
 
44
  Returns:
45
  dict: Updated state with 'summary' key containing the summarized news.
46
  """
47
+ news_items = state.get('news_data', [])
48
+ frequency = state.get('frequency', 'daily')
49
 
50
  prompt_template = ChatPromptTemplate.from_messages([
51
  ("system", """Summarize AI news articles into markdown format. For each item include:
 
68
  response = self.llm.invoke(prompt_template.format(articles=articles_str))
69
 
70
  # Create formatted summary with header
 
71
  formatted_summary = f"# {frequency.capitalize()} AI News Summary\n\n{response.content}"
72
 
73
  state['summary'] = formatted_summary
 
74
 
75
+ # CRITICAL FIX: Return state, not self.state
76
  return state
77