Created
February 27, 2024 23:04
-
-
Save virattt/c9899db28e5a83fc4dad6b3baff1e35b to your computer and use it in GitHub Desktop.
financial-agent-langchain-polygon.ipynb
This file contains hidden or 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": "ABX9TyOWbimMz0D9/XUqMzPiTCKt", | |
"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/c9899db28e5a83fc4dad6b3baff1e35b/financial-agent-langchain-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": [ | |
"os.environ[\"POLYGON_API_KEY\"] = getpass.getpass()" | |
], | |
"metadata": { | |
"id": "wLgG34qXylsg" | |
}, | |
"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, PolygonTickerNews\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()\n", | |
"tools = [\n", | |
" PolygonLastQuote(api_wrapper=polygon),\n", | |
" PolygonTickerNews(api_wrapper=polygon),\n", | |
" ]\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\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 and news for ABNB?\", \"intermediate_steps\": []})\n", | |
"output = result['agent_outcome'].return_values[\"output\"]\n", | |
"print(output)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "kBwr25yAWDVB", | |
"outputId": "e9afbb61-8f22-4b7f-ae60-21a3caab902e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"The latest price for ABNB (Airbnb) stock is $152.\n", | |
"\n", | |
"Here are the latest news articles related to ABNB:\n", | |
"\n", | |
"1. [Airbnb Stock: Buy, Sell, or Hold?](https://www.fool.com/investing/2024/02/27/airbnb-stock-buy-sell-or-hold/)\n", | |
" - Published by: The Motley Fool\n", | |
" - Date: 2024-02-27\n", | |
" - Description: Airbnb is facing pushback in some cities, but the short-term rental company continues to put up strong financial results.\n", | |
"\n", | |
"2. [Is Airbnb a Buy in 2024, or Should Investors Stay Away?](https://www.fool.com/investing/2024/02/27/is-airbnb-a-buy-in-2024-or-should-investors-stay-a/)\n", | |
" - Published by: The Motley Fool\n", | |
" - Date: 2024-02-27\n", | |
" - Description: The travel disruptor isn't without risk, but the latest numbers look strong.\n", | |
"\n", | |
"3. [3 Things to Know About Airbnb Stock](https://www.fool.com/investing/2024/02/27/3-things-to-know-about-airbnb-stock/)\n", | |
" - Published by: The Motley Fool\n", | |
" - Date: 2024-02-27\n", | |
" - Description: The company benefits from some less-than-obvious competitive advantages.\n", | |
"\n", | |
"Please click on the titles to read more about each news article.\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, will try it later.