Created
January 25, 2024 22:35
-
-
Save virattt/4d764c427892ce9fdf4534209edfb1f4 to your computer and use it in GitHub Desktop.
Langgraph-financial-agent-polygon.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyMdKDagBmyYvRu7UGgVfkdN", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/virattt/4d764c427892ce9fdf4534209edfb1f4/langgraph-financial-agent-polygon.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "GcvSoNAmVbXH" | |
}, | |
"outputs": [], | |
"source": [ | |
"!pip install langgraph\n", | |
"!pip install -U langchain langchain_openai langchainhub\n", | |
"!pip install -U polygon-api-client" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import getpass\n", | |
"import os\n", | |
"\n", | |
"# Set your OpenAI API key\n", | |
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass()" | |
], | |
"metadata": { | |
"id": "aw9453mxY2GZ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from langchain import hub\n", | |
"from langchain.agents import create_openai_functions_agent\n", | |
"from langchain_openai.chat_models import ChatOpenAI\n", | |
"from langchain_community.utilities.polygon import PolygonAPIWrapper\n", | |
"from langchain_community.tools import PolygonLastQuote\n", | |
"\n", | |
"# Get the prompt to use (default\n", | |
"prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", | |
"\n", | |
"# Choose the LLM that will drive the agent\n", | |
"llm = ChatOpenAI(model=\"gpt-4\")\n", | |
"\n", | |
"# Create the PolygonLastQuote tool\n", | |
"polygon = PolygonAPIWrapper(polygon_api_key=\"YOUR_POLYGON_API_KEY\")\n", | |
"tools = [PolygonLastQuote(api_wrapper=polygon)]\n", | |
"\n", | |
"agent_runnable = create_openai_functions_agent(llm, tools, prompt)" | |
], | |
"metadata": { | |
"id": "mczr2sUEVdYv" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from langchain_core.runnables import RunnablePassthrough\n", | |
"from langchain_core.agents import AgentFinish\n", | |
"\n", | |
"\n", | |
"# Define the agent\n", | |
"agent = RunnablePassthrough.assign(\n", | |
" agent_outcome = agent_runnable\n", | |
")\n", | |
"\n", | |
"# Define the function to execute tools\n", | |
"def execute_tools(data):\n", | |
" agent_action = data.pop('agent_outcome')\n", | |
" tool_to_use = {t.name: t for t in tools}[agent_action.tool]\n", | |
" observation = tool_to_use.invoke(agent_action.tool_input)\n", | |
" data['intermediate_steps'].append((agent_action, observation))\n", | |
" return data\n", | |
"\n", | |
"# Define logic that will be used to determine which conditional edge to go down\n", | |
"def should_continue(data):\n", | |
" if isinstance(data['agent_outcome'], AgentFinish):\n", | |
" return \"exit\"\n", | |
" else:\n", | |
" return \"continue\"" | |
], | |
"metadata": { | |
"id": "nTW70qJEV6-A" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from langgraph.graph import END, Graph\n", | |
"\n", | |
"workflow = Graph()\n", | |
"\n", | |
"workflow.add_node(\"agent\", agent)\n", | |
"workflow.add_node(\"tools\", execute_tools)\n", | |
"\n", | |
"# Set the entrypoint as `agent`\n", | |
"workflow.set_entry_point(\"agent\")\n", | |
"\n", | |
"workflow.add_conditional_edges(\n", | |
" \"agent\",\n", | |
" should_continue,\n", | |
" {\n", | |
" \"continue\": \"tools\",\n", | |
" \"exit\": END\n", | |
" }\n", | |
")\n", | |
"workflow.add_edge('tools', 'agent')\n", | |
"\n", | |
"\n", | |
"\n", | |
"# This compiles it into a LangChain Runnable,\n", | |
"# meaning we can use it as you would any other runnable\n", | |
"chain = workflow.compile()" | |
], | |
"metadata": { | |
"id": "xK5euZ6UWAhq" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"result = chain.invoke({\"input\": \"What is the latest stock price for AAPL, MSFT, and AMZN?\", \"intermediate_steps\": []})\n", | |
"output = result['agent_outcome'].return_values[\"output\"]\n", | |
"print(output)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "kBwr25yAWDVB", | |
"outputId": "33cb2179-a469-41c3-a6ec-4474a042210e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"The latest stock prices are:\n", | |
"- Apple (AAPL): $194.14\n", | |
"- Microsoft (MSFT): $404.00\n", | |
"- Amazon (AMZN): $157.5\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "7XeJKNsn9YWG" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment