{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools import WikipediaQueryRun\n", "from langchain_community.utilities import WikipediaAPIWrapper\n", "from langchain_community.tools import DuckDuckGoSearchResults\n", "from langchain_community.tools.yahoo_finance_news import YahooFinanceNewsTool\n", "from langchain_core.messages import HumanMessage\n", "from langgraph.checkpoint.memory import MemorySaver\n", "from langgraph.prebuilt import create_react_agent\n", "from langchain_core.tools import tool" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create the agent\n", "memory = MemorySaver()\n", "llm = ChatOpenAI(model_name=\"gpt-4o-mini\", temperature=0)\n", "tool_search = DuckDuckGoSearchResults(max_results=2)\n", "tool_finance = YahooFinanceNewsTool()\n", "tool_wiki = WikipediaQueryRun(\n", " api_wrapper=WikipediaAPIWrapper(\n", " top_k_results=2,\n", " doc_content_chars_max=500))\n", "@tool\n", "def tool_mult(a: int, b: int) -> int:\n", " 'Multiplies two given numbers'\n", " return a * b\n", "tools = [tool_search, tool_finance, tool_wiki, tool_mult]\n", "agent_executor = create_react_agent(\n", " llm,\n", " tools,\n", " checkpointer = memory\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# use the agent\n", "config = {'configurable': {'thread_id': '1'}}\n", "user_input = input(\"User: \")\n", "for chunk in agent_executor.stream(\n", " {\"messages\": [HumanMessage(content=user_input)]},\n", " config):\n", " print(chunk)" ] } ], "metadata": { "kernelspec": { "display_name": "langchain_311", "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.11.1" } }, "nbformat": 4, "nbformat_minor": 2 }