Alberto Carmona commited on
Commit
c28c6ad
·
1 Parent(s): fec8f67

Update news fetching logic to use 'query' instead of 'topics' in get_news function and refine documentation

Browse files
Files changed (2) hide show
  1. basic_llama_agent.py +1 -1
  2. tools.py +31 -30
basic_llama_agent.py CHANGED
@@ -9,7 +9,7 @@ SYSTEM_PROMPT = """
9
  You are a news assistant.
10
  You can get news with 'get news'.
11
 
12
- When getting news, fetch the latest articles for the current topics, analyze their sentiment and extract entities, then present them.
13
  After presenting news, ask if the user wants to know implications (e.g., 'implications for 1'), why it happened (e.g., 'why happened for 1'), or social media reactions (e.g., 'social media reaction for 1').
14
 
15
  For 'implications', generate possible implications.
 
9
  You are a news assistant.
10
  You can get news with 'get news'.
11
 
12
+ When getting news, fetch the latest articles for the current query, analyze their sentiment and extract entities, then present them.
13
  After presenting news, ask if the user wants to know implications (e.g., 'implications for 1'), why it happened (e.g., 'why happened for 1'), or social media reactions (e.g., 'social media reaction for 1').
14
 
15
  For 'implications', generate possible implications.
tools.py CHANGED
@@ -1,21 +1,21 @@
1
- from tavily import TavilyClient
2
  import os
3
  from typing import Dict, List
4
 
5
  import requests
6
  from llama_index.core.llms import ChatMessage
 
7
 
8
  from llms import llm_openai
9
 
10
-
11
  last_news = []
12
-
13
- def get_news(topics: List[str]) -> List[Dict]:
 
14
  """
15
- Fetches news articles related to the specified topics using the NewsAPI, analyzes their sentiment and named entities, and returns a list of processed news items.
16
 
17
  Args:
18
- topics: A list of topics to search for in news articles.
19
 
20
  Returns:
21
  List[Dict]: A list of dictionaries, each containing:
@@ -30,25 +30,23 @@ def get_news(topics: List[str]) -> List[Dict]:
30
  None. All exceptions are caught and returned as error messages in the result.
31
  """
32
  global last_news
33
- print(f"Fetching news for topics: {topics}")
34
-
35
  last_news.clear() # Clear previous news to avoid duplication
36
- for topic in topics:
37
- search_results = web_search(f'Find the latest news related to: {topic}')
38
- if search_results:
39
- for res in search_results:
40
- last_news.append({
41
- "index": len(last_news) + 1,
42
- "title": res.get("title", "No title available"),
43
- "summary": res.get("content", "No summary available"),
44
- # "sentiment": analyze_sentiment(res.get("snippet", "")),
45
- # "entities": recognize_entities(res.get("snippet", ""))
46
- })
47
  print(f"Found {len(last_news)} articles.")
48
  return last_news
49
 
50
 
51
-
52
  def analyze_sentiment(text: str) -> str:
53
  """
54
  Analyzes the sentiment of the given text and returns the sentiment label.
@@ -77,7 +75,6 @@ def analyze_sentiment(text: str) -> str:
77
  return "NEUTRAL"
78
 
79
 
80
-
81
  def recognize_entities(text: str) -> List[str]:
82
  """
83
  Recognizes named entities in the given text.
@@ -101,7 +98,6 @@ def recognize_entities(text: str) -> List[str]:
101
  return entities if entities else ["No entities found."]
102
 
103
 
104
-
105
  def generate_implications(article_index: int) -> str:
106
  """
107
  Generates a string describing the possible implications of a news article based on its index.
@@ -164,7 +160,6 @@ def browse_page(url: str, query: str) -> str:
164
  return f"Error fetching page: {str(e)}"
165
 
166
 
167
-
168
  def get_lead_up_events(article_index: int) -> str:
169
  """
170
  Retrieves a brief timeline or background of events leading up to a news article's topic.
@@ -205,6 +200,7 @@ def get_lead_up_events(article_index: int) -> str:
205
  print(f"Generated background information: {result.message.content}")
206
  return f"Background information for article {article_index}: {result.message.content}"
207
 
 
208
  def call_llm(prompt: str) -> str:
209
  """
210
  Calls the LLM with a given prompt and returns the response.
@@ -223,6 +219,7 @@ def call_llm(prompt: str) -> str:
223
  except Exception as e:
224
  return f"Error calling LLM: {str(e)}"
225
 
 
226
  def get_social_media_opinions(article_index: int) -> str:
227
  """
228
  Analyzes social media opinions related to a news article by its index.
@@ -242,13 +239,17 @@ def get_social_media_opinions(article_index: int) -> str:
242
  return "Invalid article index."
243
  article = last_news[article_index - 1]
244
  title = article["title"]
245
-
246
- pos_posts = web_search(f'What are the positive social media reactions related to: {title}?')
247
- neg_posts = web_search(f'What are the negative social media reactions related to: {title}?')
248
-
 
 
249
  # haz un resumen con el llm de los posts positivos
250
- pos_summary = call_llm('Make a summary of the following social media posts: ' + str(pos_posts))
251
- neg_summary = call_llm('Make a summary of the following social media posts: ' + str(neg_posts))
 
 
252
 
253
  print(f"Positive summary: {pos_summary}")
254
  print(f"Negative summary: {neg_summary}")
@@ -257,4 +258,4 @@ def get_social_media_opinions(article_index: int) -> str:
257
  Social Media Opinions for Article {article_index}:
258
  Positive Summary: {pos_summary}
259
  Negative Summary: {neg_summary}
260
- """
 
 
1
  import os
2
  from typing import Dict, List
3
 
4
  import requests
5
  from llama_index.core.llms import ChatMessage
6
+ from tavily import TavilyClient
7
 
8
  from llms import llm_openai
9
 
 
10
  last_news = []
11
+
12
+
13
+ def get_news(query: str) -> List[Dict]:
14
  """
15
+ Fetches news articles related to the specified query, analyzes their sentiment and named entities, and returns a list of processed news items.
16
 
17
  Args:
18
+ query: A string representing the search query for news articles.
19
 
20
  Returns:
21
  List[Dict]: A list of dictionaries, each containing:
 
30
  None. All exceptions are caught and returned as error messages in the result.
31
  """
32
  global last_news
33
+ print(f"Fetching news for query: {query}")
 
34
  last_news.clear() # Clear previous news to avoid duplication
35
+
36
+ search_results = web_search(f'Find the latest news related to: {query}')
37
+ if search_results:
38
+ for res in search_results:
39
+ last_news.append({
40
+ "index": len(last_news) + 1,
41
+ "title": res.get("title", "No title available"),
42
+ "summary": res.get("content", "No summary available"),
43
+ # "sentiment": analyze_sentiment(res.get("snippet", "")),
44
+ # "entities": recognize_entities(res.get("snippet", ""))
45
+ })
46
  print(f"Found {len(last_news)} articles.")
47
  return last_news
48
 
49
 
 
50
  def analyze_sentiment(text: str) -> str:
51
  """
52
  Analyzes the sentiment of the given text and returns the sentiment label.
 
75
  return "NEUTRAL"
76
 
77
 
 
78
  def recognize_entities(text: str) -> List[str]:
79
  """
80
  Recognizes named entities in the given text.
 
98
  return entities if entities else ["No entities found."]
99
 
100
 
 
101
  def generate_implications(article_index: int) -> str:
102
  """
103
  Generates a string describing the possible implications of a news article based on its index.
 
160
  return f"Error fetching page: {str(e)}"
161
 
162
 
 
163
  def get_lead_up_events(article_index: int) -> str:
164
  """
165
  Retrieves a brief timeline or background of events leading up to a news article's topic.
 
200
  print(f"Generated background information: {result.message.content}")
201
  return f"Background information for article {article_index}: {result.message.content}"
202
 
203
+
204
  def call_llm(prompt: str) -> str:
205
  """
206
  Calls the LLM with a given prompt and returns the response.
 
219
  except Exception as e:
220
  return f"Error calling LLM: {str(e)}"
221
 
222
+
223
  def get_social_media_opinions(article_index: int) -> str:
224
  """
225
  Analyzes social media opinions related to a news article by its index.
 
239
  return "Invalid article index."
240
  article = last_news[article_index - 1]
241
  title = article["title"]
242
+
243
+ pos_posts = web_search(
244
+ f'What are the positive social media reactions related to: {title}?')
245
+ neg_posts = web_search(
246
+ f'What are the negative social media reactions related to: {title}?')
247
+
248
  # haz un resumen con el llm de los posts positivos
249
+ pos_summary = call_llm(
250
+ 'Make a summary of the following social media posts: ' + str(pos_posts))
251
+ neg_summary = call_llm(
252
+ 'Make a summary of the following social media posts: ' + str(neg_posts))
253
 
254
  print(f"Positive summary: {pos_summary}")
255
  print(f"Negative summary: {neg_summary}")
 
258
  Social Media Opinions for Article {article_index}:
259
  Positive Summary: {pos_summary}
260
  Negative Summary: {neg_summary}
261
+ """