{ "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 }