Class: Langchain::Tool::NewsRetriever

Inherits:
Base
  • Object
show all
Defined in:
lib/langchain/tool/news_retriever/news_retriever.rb

Constant Summary collapse

NAME =

A tool that retrieves latest news from various sources via newsapi.org/. An API key needs to be obtained from newsapi.org/ to use this tool.

Usage:

news_retriever = Langchain::Tool::NewsRetriever.new(api_key: ENV["NEWS_API_KEY"])
"news_retriever"
ANNOTATIONS_PATH =
Langchain.root.join("./langchain/tool/#{NAME}/#{NAME}.json").to_path

Instance Method Summary collapse

Methods inherited from Base

logger_options, #method_annotations, #name, #to_anthropic_tools, #to_google_gemini_tools, #to_openai_tools

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(api_key: ENV["NEWS_API_KEY"]) ⇒ NewsRetriever

Returns a new instance of NewsRetriever.



15
16
17
# File 'lib/langchain/tool/news_retriever/news_retriever.rb', line 15

def initialize(api_key: ENV["NEWS_API_KEY"])
  @api_key = api_key
end

Instance Method Details

#get_everything(q: nil, search_in: nil, sources: nil, domains: nil, exclude_domains: nil, from: nil, to: nil, language: nil, sort_by: nil, page_size: 5, page: nil) ⇒ String

Retrieve all news

Parameters:

  • q (String) (defaults to: nil)

    Keywords or phrases to search for in the article title and body.

  • search_in (String) (defaults to: nil)

    The fields to restrict your q search to. The possible options are: title, description, content.

  • sources (String) (defaults to: nil)

    A comma-seperated string of identifiers (maximum 20) for the news sources or blogs you want headlines from. Use the /sources endpoint to locate these programmatically or look at the sources index.

  • domains (String) (defaults to: nil)

    A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.

  • exclude_domains (String) (defaults to: nil)

    A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to remove from the results.

  • from (String) (defaults to: nil)

    A date and optional time for the oldest article allowed. This should be in ISO 8601 format.

  • to (String) (defaults to: nil)

    A date and optional time for the newest article allowed. This should be in ISO 8601 format.

  • language (String) (defaults to: nil)

    The 2-letter ISO-639-1 code of the language you want to get headlines for. Possible options: ar, de, en, es, fr, he, it, nl, no, pt, ru, se, ud, zh.

  • sort_by (String) (defaults to: nil)

    The order to sort the articles in. Possible options: relevancy, popularity, publishedAt.

  • page_size (Integer) (defaults to: 5)

    The number of results to return per page. 20 is the API’s default, 100 is the maximum. Our default is 5.

  • page (Integer) (defaults to: nil)

    Use this to page through the results.

Returns:

  • (String)

    JSON response



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
# File 'lib/langchain/tool/news_retriever/news_retriever.rb', line 34

def get_everything(
  q: nil,
  search_in: nil,
  sources: nil,
  domains: nil,
  exclude_domains: nil,
  from: nil,
  to: nil,
  language: nil,
  sort_by: nil,
  page_size: 5, # The API default is 20 but that's too many.
  page: nil
)
  Langchain.logger.info("Retrieving all news", for: self.class)

  params = {apiKey: @api_key}
  params[:q] = q if q
  params[:searchIn] = search_in if search_in
  params[:sources] = sources if sources
  params[:domains] = domains if domains
  params[:excludeDomains] = exclude_domains if exclude_domains
  params[:from] = from if from
  params[:to] = to if to
  params[:language] = language if language
  params[:sortBy] = sort_by if sort_by
  params[:pageSize] = page_size if page_size
  params[:page] = page if page

  send_request(path: "everything", params: params)
end

#get_sources(category: nil, language: nil, country: nil) ⇒ String

Retrieve news sources

Parameters:

  • category (String) (defaults to: nil)

    The category you want to get headlines for. Possible options: business, entertainment, general, health, science, sports, technology.

  • language (String) (defaults to: nil)

    The 2-letter ISO-639-1 code of the language you want to get headlines for. Possible options: ar, de, en, es, fr, he, it, nl, no, pt, ru, se, ud, zh.

  • country (String) (defaults to: nil)

    The 2-letter ISO 3166-1 code of the country you want to get headlines for. Possible options: ae, ar, at, au, be, bg, br, ca, ch, cn, co, cu, cz, de, eg, fr, gb, gr, hk, hu, id, ie, il, in, it, jp, kr, lt, lv, ma, mx, my, ng, nl, no, nz, ph, pl, pt, ro, rs, ru, sa, se, sg, si, sk, th, tr, tw, ua, us, ve, za.

Returns:

  • (String)

    JSON response



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/langchain/tool/news_retriever/news_retriever.rb', line 103

def get_sources(
  category: nil,
  language: nil,
  country: nil
)
  Langchain.logger.info("Retrieving news sources", for: self.class)

  params = {apiKey: @api_key}
  params[:country] = country if country
  params[:category] = category if category
  params[:language] = language if language

  send_request(path: "top-headlines/sources", params: params)
end

#get_top_headlines(country: nil, category: nil, sources: nil, q: nil, page_size: 5, page: nil) ⇒ String

Retrieve top headlines

Parameters:

  • country (String) (defaults to: nil)

    The 2-letter ISO 3166-1 code of the country you want to get headlines for. Possible options: ae, ar, at, au, be, bg, br, ca, ch, cn, co, cu, cz, de, eg, fr, gb, gr, hk, hu, id, ie, il, in, it, jp, kr, lt, lv, ma, mx, my, ng, nl, no, nz, ph, pl, pt, ro, rs, ru, sa, se, sg, si, sk, th, tr, tw, ua, us, ve, za.

  • category (String) (defaults to: nil)

    The category you want to get headlines for. Possible options: business, entertainment, general, health, science, sports, technology.

  • sources (String) (defaults to: nil)

    A comma-seperated string of identifiers for the news sources or blogs you want headlines from. Use the /sources endpoint to locate these programmatically.

  • q (String) (defaults to: nil)

    Keywords or a phrase to search for.

  • page_size (Integer) (defaults to: 5)

    The number of results to return per page. 20 is the API’s default, 100 is the maximum. Our default is 5.

  • page (Integer) (defaults to: nil)

    Use this to page through the results.

Returns:

  • (String)

    JSON response



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/langchain/tool/news_retriever/news_retriever.rb', line 75

def get_top_headlines(
  country: nil,
  category: nil,
  sources: nil,
  q: nil,
  page_size: 5,
  page: nil
)
  Langchain.logger.info("Retrieving top news headlines", for: self.class)

  params = {apiKey: @api_key}
  params[:country] = country if country
  params[:category] = category if category
  params[:sources] = sources if sources
  params[:q] = q if q
  params[:pageSize] = page_size if page_size
  params[:page] = page if page

  send_request(path: "top-headlines", params: params)
end