-
Notifications
You must be signed in to change notification settings - Fork 15.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
community: Add PolygonLastQuote Tool and Toolkit (#15990)
**Description:** In this PR, I am adding a `PolygonLastQuote` Tool, which can be used to get the latest price quote for a given ticker / stock. Additionally, I've added a Polygon Toolkit, which we can use to encapsulate future tools that we build for Polygon. **Twitter handle:** [@virattt](https://twitter.com/virattt) --------- Co-authored-by: Harrison Chase <[email protected]>
- Loading branch information
Showing
12 changed files
with
286 additions
and
15 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e6fd05db-21c2-4227-9900-0840bc62cb31", | ||
"metadata": {}, | ||
"source": [ | ||
"# Polygon IO Toolkit\n", | ||
"\n", | ||
"This notebook shows how to use agents to interact with the [Polygon IO](https://polygon.io/) toolkit. The toolkit provides access to Polygon's Stock Market Data API." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a4da342d", | ||
"metadata": {}, | ||
"source": [ | ||
"## Example Use\n", | ||
"\n", | ||
"\n", | ||
"### Setup" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c17b33e0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%pip install --upgrade --quiet langchain-community > /dev/null" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3cd00ad2", | ||
"metadata": {}, | ||
"source": [ | ||
"Get your Polygon IO API key [here](https://polygon.io/), and then set it below.\n", | ||
"Note that the tool used in this example requires a \"Stocks Advanced\" subscription" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "a180a2b8", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"········\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import getpass\n", | ||
"import os\n", | ||
"\n", | ||
"os.environ[\"POLYGON_API_KEY\"] = getpass.getpass()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ed6f89fa", | ||
"metadata": {}, | ||
"source": [ | ||
"It's also helpful (but not needed) to set up [LangSmith](https://smith.langchain.com/) for best-in-class observability" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "56670cf6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", | ||
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7d93e6bd-03d7-4d3c-b915-8b73164e2ad8", | ||
"metadata": {}, | ||
"source": [ | ||
"### Initializing the agent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "648a2cb2-308e-4b2e-9b73-37109be4e258", | ||
"metadata": { | ||
"is_executing": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain import hub\n", | ||
"from langchain.agents import AgentExecutor, create_openai_functions_agent\n", | ||
"from langchain_community.agent_toolkits.polygon.toolkit import PolygonToolkit\n", | ||
"from langchain_community.utilities.polygon import PolygonAPIWrapper\n", | ||
"from langchain_openai import ChatOpenAI\n", | ||
"\n", | ||
"llm = ChatOpenAI(temperature=0)\n", | ||
"\n", | ||
"instructions = \"\"\"You are an assistant.\"\"\"\n", | ||
"base_prompt = hub.pull(\"langchain-ai/openai-functions-template\")\n", | ||
"prompt = base_prompt.partial(instructions=instructions)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "18650040-0ff8-4c0f-a4f2-be6aad7fe63e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"polygon = PolygonAPIWrapper()\n", | ||
"toolkit = PolygonToolkit.from_polygon_api_wrapper(polygon)\n", | ||
"agent = create_openai_functions_agent(llm, toolkit.get_tools(), prompt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "fd7463e4-8716-4d1d-860a-770533eaa742", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"agent_executor = AgentExecutor(\n", | ||
" agent=agent,\n", | ||
" tools=toolkit.get_tools(),\n", | ||
" verbose=True,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "71f05fc9-d80d-4614-b9a3-e0a5e43cbbbb", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get the last price quote for a stock" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b97409f3-dc87-425d-b555-406cf8466a28", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"agent_executor.invoke({\"input\": \"What is the latest stock price for AAPL?\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9e666ee1", | ||
"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.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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
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
1 change: 1 addition & 0 deletions
1
libs/community/langchain_community/agent_toolkits/polygon/__init__.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Polygon Toolkit""" |
27 changes: 27 additions & 0 deletions
27
libs/community/langchain_community/agent_toolkits/polygon/toolkit.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import List | ||
|
||
from langchain_community.agent_toolkits.base import BaseToolkit | ||
from langchain_community.tools import BaseTool | ||
from langchain_community.tools.polygon import PolygonLastQuote | ||
from langchain_community.utilities.polygon import PolygonAPIWrapper | ||
|
||
|
||
class PolygonToolkit(BaseToolkit): | ||
"""Polygon Toolkit.""" | ||
|
||
tools: List[BaseTool] = [] | ||
|
||
@classmethod | ||
def from_polygon_api_wrapper( | ||
cls, polygon_api_wrapper: PolygonAPIWrapper | ||
) -> "PolygonToolkit": | ||
tools = [ | ||
PolygonLastQuote( | ||
api_wrapper=polygon_api_wrapper, | ||
) | ||
] | ||
return cls(tools=tools) | ||
|
||
def get_tools(self) -> List[BaseTool]: | ||
"""Get the tools in the toolkit.""" | ||
return self.tools |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"""Polygon IO tools.""" | ||
|
||
from langchain_community.tools.polygon.last_quote import PolygonLastQuote | ||
|
||
__all__ = [ | ||
"PolygonLastQuote", | ||
] |
34 changes: 34 additions & 0 deletions
34
libs/community/langchain_community/tools/polygon/last_quote.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Optional, Type | ||
|
||
from langchain_core.callbacks import CallbackManagerForToolRun | ||
from langchain_core.pydantic_v1 import BaseModel | ||
from langchain_core.tools import BaseTool | ||
|
||
from langchain_community.utilities.polygon import PolygonAPIWrapper | ||
|
||
|
||
class Inputs(BaseModel): | ||
query: str | ||
|
||
|
||
class PolygonLastQuote(BaseTool): | ||
"""Tool that gets the last quote of a ticker from Polygon""" | ||
|
||
mode: str = "get_last_quote" | ||
name: str = "polygon_last_quote" | ||
description: str = ( | ||
"A wrapper around Polygon's Last Quote API. " | ||
"This tool is useful for fetching the latest price of a stock. " | ||
"Input should be the ticker that you want to query the last price quote for." | ||
) | ||
args_schema: Type[BaseModel] = Inputs | ||
|
||
api_wrapper: PolygonAPIWrapper | ||
|
||
def _run( | ||
self, | ||
query: str, | ||
run_manager: Optional[CallbackManagerForToolRun] = None, | ||
) -> str: | ||
"""Use the Polygon API tool.""" | ||
return self.api_wrapper.run(self.mode, ticker=query) |
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
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
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