The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
`Yahoo Finance News` tool (#10014)
Leonid Ganeline2221194450eb
diff --git a/docs/extras/integrations/tools/yahoo_finance_news.ipynb b/docs/extras/integrations/tools/yahoo_finance_news.ipynbnew file mode 100644index 0000000000..0c9494448c--- /dev/null+++ b/docs/extras/integrations/tools/yahoo_finance_news.ipynb@@ -0,0 +1,248 @@+{+ "cells": [+ {+ "cell_type": "markdown",+ "id": "71406a01-e5e5-4fe0-a1fa-49216871779e",+ "metadata": {},+ "source": [+ "# Yahoo Finance News\n",+ "\n",+ "This notebook goes over how to use the `yahoo_finance_news` tool with an agent. \n",+ "\n",+ "\n",+ "## Setting up\n",+ "\n",+ "First, you need to install `yfinance` python package."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "38717a85-2c3c-4452-a1c7-1ed4dea3da86",+ "metadata": {},+ "outputs": [],+ "source": [+ "!pip install yfinance"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "4527b5f9-b496-45d8-8147-7a4ebb89734b",+ "metadata": {},+ "source": [+ "## Example with Chain"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 4,+ "id": "d137dd6c-d3d3-4813-af65-59eaaa6b3d76",+ "metadata": {},+ "outputs": [],+ "source": [+ "import os\n",+ "\n",+ "os.environ[\"OPENAI_API_KEY\"] = \"...\""+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 26,+ "id": "fc42f766-9ce6-4ba3-be6c-5ba8a345b0d3",+ "metadata": {},+ "outputs": [],+ "source": [+ "from langchain.chat_models import ChatOpenAI\n",+ "from langchain.agents import initialize_agent, AgentType\n",+ "from langchain.tools.yahoo_finance_news import YahooFinanceNewsTool\n",+ " \n",+ "\n",+ "llm = ChatOpenAI(temperature=0.0)\n",+ "tools = [YahooFinanceNewsTool()]\n",+ "agent_chain = initialize_agent(\n",+ " tools,\n",+ " llm,\n",+ " agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",+ " verbose=True,\n",+ ")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 19,+ "id": "3d1614b4-508e-4689-84b1-2a387f80aeb1",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "\n",+ "\n",+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",+ "\u001b[32;1m\u001b[1;3mI should check the latest financial news about Microsoft stocks.\n",+ "Action: yahoo_finance_news\n",+ "Action Input: MSFT\u001b[0m\n",+ "Observation: \u001b[36;1m\u001b[1;3mMicrosoft (MSFT) Gains But Lags Market: What You Should Know\n",+ "In the latest trading session, Microsoft (MSFT) closed at $328.79, marking a +0.12% move from the previous day.\u001b[0m\n",+ "Thought:\u001b[32;1m\u001b[1;3mI have the latest information on Microsoft stocks.\n",+ "Final Answer: Microsoft (MSFT) closed at $328.79, with a +0.12% move from the previous day.\u001b[0m\n",+ "\n",+ "\u001b[1m> Finished chain.\u001b[0m\n"+ ]+ },+ {+ "data": {+ "text/plain": [+ "'Microsoft (MSFT) closed at $328.79, with a +0.12% move from the previous day.'"+ ]+ },+ "execution_count": 19,+ "metadata": {},+ "output_type": "execute_result"+ }+ ],+ "source": [+ "agent_chain.run(\n",+ " \"What happens today with Microsoft stocks?\",\n",+ ")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 20,+ "id": "c899b64d-86a5-452c-b576-e94f485c27ea",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "\n",+ "\n",+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",+ "\u001b[32;1m\u001b[1;3mI should compare the current sentiment of Microsoft and Nvidia.\n",+ "Action: yahoo_finance_news\n",+ "Action Input: MSFT\u001b[0m\n",+ "Observation: \u001b[36;1m\u001b[1;3mMicrosoft (MSFT) Gains But Lags Market: What You Should Know\n",+ "In the latest trading session, Microsoft (MSFT) closed at $328.79, marking a +0.12% move from the previous day.\u001b[0m\n",+ "Thought:\u001b[32;1m\u001b[1;3mI need to find the current sentiment of Nvidia as well.\n",+ "Action: yahoo_finance_news\n",+ "Action Input: NVDA\u001b[0m\n",+ "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n",+ "Thought:\u001b[32;1m\u001b[1;3mI now know the current sentiment of both Microsoft and Nvidia.\n",+ "Final Answer: I cannot compare the sentiment of Microsoft and Nvidia as I only have information about Microsoft.\u001b[0m\n",+ "\n",+ "\u001b[1m> Finished chain.\u001b[0m\n"+ ]+ },+ {+ "data": {+ "text/plain": [+ "'I cannot compare the sentiment of Microsoft and Nvidia as I only have information about Microsoft.'"+ ]+ },+ "execution_count": 20,+ "metadata": {},+ "output_type": "execute_result"+ }+ ],+ "source": [+ "agent_chain.run(\n",+ " \"How does Microsoft feels today comparing with Nvidia?\",\n",+ ")"+ ]+ },+ {+ "cell_type": "markdown",+ "id": "366b57dc-5292-4011-9b99-7b7c86237def",+ "metadata": {},+ "source": [+ "# How YahooFinanceNewsTool works?"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 37,+ "id": "7879b79c-b5c7-4a5d-8338-edda53ff41a6",+ "metadata": {},+ "outputs": [],+ "source": [+ "tool = YahooFinanceNewsTool()"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 38,+ "id": "ac989456-33bc-4478-874e-98b9cb24d113",+ "metadata": {},+ "outputs": [+ {+ "data": {+ "text/plain": [+ "'No news found for company that searched with NVDA ticker.'"+ ]+ },+ "execution_count": 38,+ "metadata": {},+ "output_type": "execute_result"+ }+ ],+ "source": [+ "tool.run(\"NVDA\")"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": 40,+ "id": "46c697aa-102e-48d4-9834-081671aad40a",+ "metadata": {},+ "outputs": [+ {+ "name": "stdout",+ "output_type": "stream",+ "text": [+ "Top Research Reports for Apple, Broadcom & Caterpillar\n",+ "Today's Research Daily features new research reports on 16 major stocks, including Apple Inc. (AAPL), Broadcom Inc. (AVGO) and Caterpillar Inc. (CAT).\n",+ "\n",+ "Apple Stock on Pace for Worst Month of the Year\n",+ "Apple (AAPL) shares are on pace for their worst month of the year, according to Dow Jones Market Data. The stock is down 4.8% so far in August, putting it on pace for its worst month since December 2022, when it fell 12%.\n"+ ]+ }+ ],+ "source": [+ "res = tool.run(\"AAPL\")\n",+ "print(res)"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "fa415bf7-dd92-43f5-89aa-de7b7deaaf2b",+ "metadata": {},+ "outputs": [],+ "source": []+ }+ ],+ "metadata": {+ "kernelspec": {+ "display_name": "Python 3 (ipykernel)",+ "language": "python",+ "name": "python3"+ },+ "language_info": {+ "codemirror_mode": {+ "name": "ipython",+ "version": 3+ },+ "file_extension": ".py",+ "mimetype": "text/x-python",+ "name": "python",+ "nbconvert_exporter": "python",+ "pygments_lexer": "ipython3",+ "version": "3.10.12"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 5+}diff --git a/libs/langchain/langchain/tools/yahoo_finance_news.py b/libs/langchain/langchain/tools/yahoo_finance_news.pynew file mode 100644index 0000000000..d1aa55dd60--- /dev/null+++ b/libs/langchain/langchain/tools/yahoo_finance_news.py@@ -0,0 +1,67 @@+from typing import Iterable, Optional++from requests.exceptions import HTTPError, ReadTimeout+from urllib3.exceptions import ConnectionError++from langchain.callbacks.manager import CallbackManagerForToolRun+from langchain.document_loaders.web_base import WebBaseLoader+from langchain.schema import Document+from langchain.tools.base import BaseTool+++class YahooFinanceNewsTool(BaseTool):+ """Tool that searches financial news on Yahoo Finance."""++ name: str = "yahoo_finance_news"+ description: str = (+ "Useful for when you need to find financial news "+ "about a public company. "+ "Input should be a company ticker. "+ "For example, AAPL for Apple, MSFT for Microsoft."+ )+ top_k: int = 10+ """The number of results to return."""++ def _run(+ self,+ query: str,+ run_manager: Optional[CallbackManagerForToolRun] = None,+ ) -> str:+ """Use the Yahoo Finance News tool."""+ try:+ import yfinance+ except ImportError:+ raise ImportError(+ "Could not import yfinance python package. "+ "Please install it with `pip install yfinance`."+ )+ company = yfinance.Ticker(query)+ try:+ if company.isin is None:+ return f"Company ticker {query} not found."+ except (HTTPError, ReadTimeout, ConnectionError):+ return f"Company ticker {query} not found."++ links = []+ try:+ links = [n["link"] for n in company.news if n["type"] == "STORY"]+ except (HTTPError, ReadTimeout, ConnectionError):+ if not links:+ return f"No news found for company that searched with {query} ticker."+ if not links:+ return f"No news found for company that searched with {query} ticker."+ loader = WebBaseLoader(links)+ docs = loader.load()+ result = self._format_results(docs, query)+ if not result:+ return f"No news found for company that searched with {query} ticker."+ return result++ @staticmethod+ def _format_results(docs: Iterable[Document], query: str) -> str:+ doc_strings = [+ "\n".join([doc.metadata["title"], doc.metadata["description"]])+ for doc in docs+ if query in doc.metadata["description"] or query in doc.metadata["title"]+ ]+ return "\n\n".join(doc_strings)diff --git a/libs/langchain/tests/integration_tests/tools/test_yahoo_finance_news.py b/libs/langchain/tests/integration_tests/tools/test_yahoo_finance_news.pynew file mode 100644index 0000000000..45be765dec--- /dev/null+++ b/libs/langchain/tests/integration_tests/tools/test_yahoo_finance_news.py@@ -0,0 +1,31 @@+import pytest++from langchain.tools.yahoo_finance_news import YahooFinanceNewsTool++# skip all tests if yfinance is not installed+yfinance = pytest.importorskip("yfinance")+++def test_success() -> None:+ """Test that the tool runs successfully."""+ tool = YahooFinanceNewsTool()+ query = "Microsoft"+ result = tool.run(query)+ assert result is not None+ assert f"Company ticker {query} not found." not in result+++def test_failure_no_ticker() -> None:+ """Test that the tool fails."""+ tool = YahooFinanceNewsTool()+ query = ""+ result = tool.run(query)+ assert f"Company ticker {query} not found." in result+++def test_failure_wrong_ticker() -> None:+ """Test that the tool fails."""+ tool = YahooFinanceNewsTool()+ query = "NOT_A_COMPANY"+ result = tool.run(query)+ assert f"Company ticker {query} not found." in result