Skip to main content

Polygon IO Toolkit

This notebook shows how to use agents to interact with the Polygon IO toolkit. The toolkit provides access to Polygon's Stock Market Data API.

Example Useโ€‹

Setupโ€‹

%pip install --upgrade --quiet langchain-community > /dev/null

Get your Polygon IO API key here, and then set it below. Note that the tool used in this example requires a "Stocks Advanced" subscription

import getpass
import os

os.environ["POLYGON_API_KEY"] = getpass.getpass()
ยทยทยทยทยทยทยทยท

It's also helpful (but not needed) to set up LangSmith for best-in-class observability

# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

Initializing the agentโ€‹

from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_community.agent_toolkits.polygon.toolkit import PolygonToolkit
from langchain_community.utilities.polygon import PolygonAPIWrapper
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(temperature=0)

instructions = """You are an assistant."""
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)
polygon = PolygonAPIWrapper()
toolkit = PolygonToolkit.from_polygon_api_wrapper(polygon)
agent = create_openai_functions_agent(llm, toolkit.get_tools(), prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=toolkit.get_tools(),
verbose=True,
)

Get the last price quote for a stockโ€‹

agent_executor.invoke({"input": "What is the latest stock price for AAPL?"})

Help us out by providing feedback on this documentation page: