Class: Langchain::Tool::NewsRetriever
- 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
-
#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.
-
#get_sources(category: nil, language: nil, country: nil) ⇒ String
Retrieve news sources.
-
#get_top_headlines(country: nil, category: nil, sources: nil, q: nil, page_size: 5, page: nil) ⇒ String
Retrieve top headlines.
-
#initialize(api_key: ENV["NEWS_API_KEY"]) ⇒ NewsRetriever
constructor
A new instance of NewsRetriever.
Methods inherited from Base
logger_options, #method_annotations, #name, #to_anthropic_tools, #to_google_gemini_tools, #to_openai_tools
Methods included from DependencyHelper
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
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
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
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 |