Class: Langchain::Tool::NewsRetriever

Inherits:
Object
  • Object
show all
Extended by:
Langchain::ToolDefinition
Defined in:
lib/langchain/tool/news_retriever.rb

Overview

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"])

Instance Method Summary collapse

Methods included from Langchain::ToolDefinition

define_function, function_schemas, tool_name

Constructor Details

#initialize(api_key: ) ⇒ NewsRetriever

Returns a new instance of NewsRetriever.



42
43
44
# File 'lib/langchain/tool/news_retriever.rb', line 42

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-separated 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-separated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.

  • exclude_domains (String) (defaults to: nil)

    A comma-separated 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



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

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.debug("#{self.class} - Retrieving all news")

  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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/langchain/tool/news_retriever.rb', line 130

def get_sources(
  category: nil,
  language: nil,
  country: nil
)
  Langchain.logger.debug("#{self.class} - Retrieving news sources")

  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-separated 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



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

def get_top_headlines(
  country: nil,
  category: nil,
  sources: nil,
  q: nil,
  page_size: 5,
  page: nil
)
  Langchain.logger.debug("#{self.class} - Retrieving top news headlines")

  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