docs: replace initialize_agent with create_react_agent in yahoo_finance_news.ipynb (#31108)

Thank you for contributing to LangChain!

- [x] **PR title**: "package: description"
- Where "package" is whichever of langchain, core, etc. is being
modified. Use "docs: ..." for purely docs changes, "infra: ..." for CI
changes.
  - Example: "core: add foobar LLM"


- [x] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [x] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Michael Li 2025-05-04 05:45:52 +10:00 committed by GitHub
parent 1e00116ae7
commit 1204fb8010
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,10 +17,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "38717a85-2c3c-4452-a1c7-1ed4dea3da86", "id": "38717a85-2c3c-4452-a1c7-1ed4dea3da86",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [ "source": [
"%pip install --upgrade --quiet yfinance" "%pip install --upgrade --quiet yfinance"
] ]
@ -35,121 +43,136 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 2,
"id": "d137dd6c-d3d3-4813-af65-59eaaa6b3d76", "id": "d137dd6c-d3d3-4813-af65-59eaaa6b3d76",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import os\n", "import os\n",
"\n", "\n",
"os.environ[\"OPENAI_API_KEY\"] = \"...\"" "os.environ[\"OPENAI_API_KEY\"] = \"..\""
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": null,
"id": "fc42f766-9ce6-4ba3-be6c-5ba8a345b0d3", "id": "af297977-4fc3-421f-9ce1-f62c1c5b026a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"USER_AGENT environment variable not set, consider setting it to identify your requests.\n"
]
}
],
"source": [ "source": [
"from langchain.agents import AgentType, initialize_agent\n",
"from langchain_community.tools.yahoo_finance_news import YahooFinanceNewsTool\n", "from langchain_community.tools.yahoo_finance_news import YahooFinanceNewsTool\n",
"from langchain_openai import ChatOpenAI\n", "from langgraph.prebuilt import create_react_agent\n",
"\n", "\n",
"llm = ChatOpenAI(temperature=0.0)\n",
"tools = [YahooFinanceNewsTool()]\n", "tools = [YahooFinanceNewsTool()]\n",
"agent_chain = initialize_agent(\n", "agent = create_react_agent(\"openai:gpt-4.1-mini\", tools)"
" tools,\n",
" llm,\n",
" agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
" verbose=True,\n",
")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 4,
"id": "3d1614b4-508e-4689-84b1-2a387f80aeb1", "id": "ac3cbec8-4135-4f5a-bb35-299730c000bd",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n", "\n",
"What happened today with Microsoft stocks?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" yahoo_finance_news (call_s1Waj1rAoJ89CfxWX1RWDiWL)\n",
" Call ID: call_s1Waj1rAoJ89CfxWX1RWDiWL\n",
" Args:\n",
" query: MSFT\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: yahoo_finance_news\n",
"\n", "\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "Microsoft (MSFT), Meta Platforms (META) Reported “Home Run” Results: Dan Ives Recent Comments\n",
"\u001b[32;1m\u001b[1;3mI should check the latest financial news about Microsoft stocks.\n", "Microsoft (MSFT) and Meta Platforms (META) delivered “home run” results yesterday, as the AI Revolution has not been slowed by the Trump administrations tariffs, Dan Ives, the Managing Director and Senior Equity Research Analyst at Wedbush Securities said on CNBC recently. Ives covers tech stocks. Tech Is Poised for a Comeback, Ives Indicates “The tech […]\n",
"Action: yahoo_finance_news\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\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", "\n",
"\u001b[1m> Finished chain.\u001b[0m\n" "Today, Microsoft (MSFT) reported strong financial results, described as \"home run\" results by Dan Ives, Managing Director and Senior Equity Research Analyst at Wedbush Securities. Despite the Trump administrations tariffs, the AI Revolution driving tech stocks like Microsoft has not slowed down, indicating a positive outlook for the company and the tech sector overall.\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": [ "source": [
"agent_chain.invoke(\n", "input_message = {\n",
" \"What happened today with Microsoft stocks?\",\n", " \"role\": \"user\",\n",
")" " \"content\": (\"What happened today with Microsoft stocks?\"),\n",
"}\n",
"\n",
"for step in agent.stream(\n",
" {\"messages\": [input_message]},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 5,
"id": "c899b64d-86a5-452c-b576-e94f485c27ea", "id": "4496b06b-8b57-4fa8-9b86-4db407caa807",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n", "\n",
"How does Microsoft feels today comparing with Nvidia?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" yahoo_finance_news (call_r9m4YxdEqWeXotkNgK8jGzeJ)\n",
" Call ID: call_r9m4YxdEqWeXotkNgK8jGzeJ\n",
" Args:\n",
" query: MSFT\n",
" yahoo_finance_news (call_fxj3AIKPB4MYuquvFFWrBD8B)\n",
" Call ID: call_fxj3AIKPB4MYuquvFFWrBD8B\n",
" Args:\n",
" query: NVDA\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: yahoo_finance_news\n",
"\n", "\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "NVIDIA Corporation (NVDA): Among Ken Fishers Technology Stock Picks with Huge Upside Potential\n",
"\u001b[32;1m\u001b[1;3mI should compare the current sentiment of Microsoft and Nvidia.\n", "We recently published an article titled Billionaire Ken Fishers 10 Technology Stock Picks with Huge Upside Potential. In this article, we are going to take a look at where NVIDIA Corporation (NASDAQ:NVDA) stands against the other technology stocks. Technology stocks have faced heightened volatility in 2025, with market sentiment swinging sharply in response to President Donald […]\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", "\n",
"\u001b[1m> Finished chain.\u001b[0m\n" "Nvidia (NVDA) Redesigns Chips to Sidestep U.S. Export Ban, Eyes June China Rollout\n",
"Nvidia plans China-specific AI chip revamp after new U.S. export limits\n",
"\n",
"Is NVIDIA (NVDA) the Best NASDAQ Stock to Buy According to Billionaires?\n",
"We recently published a list of 10 Best NASDAQ Stocks to Buy According to Billionaires. In this article, we are going to take a look at where NVIDIA Corporation (NASDAQ:NVDA) stands against other best NASDAQ stocks to buy according to billionaires. The latest market data shows that the US economy contracted at an annualized rate […]\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"Today, Microsoft (MSFT) is viewed positively with recent strong earnings reported, described as \"home run\" results, indicating confidence in its performance amid an ongoing AI revolution.\n",
"\n",
"Nvidia (NVDA) is also in focus with its strategic moves, such as redesigning AI chips to bypass U.S. export bans and targeting a China rollout. It is considered one of the technology stocks with significant upside potential, attracting attention from notable investors.\n",
"\n",
"In summary, both Microsoft and Nvidia have positive sentiments today, with Microsoft showing strong financial results and Nvidia making strategic advancements in AI technology and market positioning.\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": [ "source": [
"agent_chain.invoke(\n", "input_message = {\n",
" \"How does Microsoft feels today comparing with Nvidia?\",\n", " \"role\": \"user\",\n",
")" " \"content\": (\"How does Microsoft feels today comparing with Nvidia?\"),\n",
"}\n",
"\n",
"for step in agent.stream(\n",
" {\"messages\": [input_message]},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
] ]
}, },
{ {
@ -162,7 +185,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 6,
"id": "7879b79c-b5c7-4a5d-8338-edda53ff41a6", "id": "7879b79c-b5c7-4a5d-8338-edda53ff41a6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -172,17 +195,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 38, "execution_count": 7,
"id": "ac989456-33bc-4478-874e-98b9cb24d113", "id": "ac989456-33bc-4478-874e-98b9cb24d113",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'No news found for company that searched with NVDA ticker.'" "'NVIDIA Corporation (NVDA): Among Ken Fishers Technology Stock Picks with Huge Upside Potential\\nWe recently published an article titled Billionaire Ken Fishers 10 Technology Stock Picks with Huge Upside Potential. In this article, we are going to take a look at where NVIDIA Corporation (NASDAQ:NVDA) stands against the other technology stocks. Technology stocks have faced heightened volatility in 2025, with market sentiment swinging sharply in response to President Donald […]\\n\\nNvidia (NVDA) Redesigns Chips to Sidestep U.S. Export Ban, Eyes June China Rollout\\nNvidia plans China-specific AI chip revamp after new U.S. export limits\\n\\nIs NVIDIA (NVDA) the Best NASDAQ Stock to Buy According to Billionaires?\\nWe recently published a list of 10 Best NASDAQ Stocks to Buy According to Billionaires. In this article, we are going to take a look at where NVIDIA Corporation (NASDAQ:NVDA) stands against other best NASDAQ stocks to buy according to billionaires. The latest market data shows that the US economy contracted at an annualized rate […]'"
] ]
}, },
"execution_count": 38, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -193,7 +216,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 40, "execution_count": 8,
"id": "46c697aa-102e-48d4-9834-081671aad40a", "id": "46c697aa-102e-48d4-9834-081671aad40a",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -201,11 +224,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Top Research Reports for Apple, Broadcom & Caterpillar\n", "No news found for company that searched with AAPL ticker.\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"
] ]
} }
], ],
@ -239,7 +258,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.12" "version": "3.13.3"
} }
}, },
"nbformat": 4, "nbformat": 4,