Class: Langchain::Tool::NewsRetriever
- Inherits:
-
Object
- Object
- Langchain::Tool::NewsRetriever
- 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
-
#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) ⇒ Langchain::Tool::Response
Retrieve all news.
-
#get_sources(category: nil, language: nil, country: nil) ⇒ Langchain::Tool::Response
Retrieve news sources.
-
#get_top_headlines(country: nil, category: nil, sources: nil, q: nil, page_size: 5, page: nil) ⇒ Langchain::Tool::Response
Retrieve top headlines.
-
#initialize(api_key: ) ⇒ NewsRetriever
constructor
A new instance of NewsRetriever.
Methods included from Langchain::ToolDefinition
define_function, extended, 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) ⇒ Langchain::Tool::Response
Retrieve all news
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 91 |
# 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 response = send_request(path: "everything", params: params) tool_response(content: response) end |
#get_sources(category: nil, language: nil, country: nil) ⇒ Langchain::Tool::Response
Retrieve news sources
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/langchain/tool/news_retriever.rb', line 132 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 response = send_request(path: "top-headlines/sources", params: params) tool_response(content: response) end |
#get_top_headlines(country: nil, category: nil, sources: nil, q: nil, page_size: 5, page: nil) ⇒ Langchain::Tool::Response
Retrieve top headlines
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/langchain/tool/news_retriever.rb', line 103 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 response = send_request(path: "top-headlines", params: params) tool_response(content: response) end |