langchain/libs/community/langchain_community/tools/polygon/aggregates.py
Erick Friis c2a3021bb0
multiple: pydantic 2 compatibility, v0.3 (#26443)
Signed-off-by: ChengZi <chen.zhang@zilliz.com>
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com>
Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com>
Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no>
Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: ccurme <chester.curme@gmail.com>
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com>
Co-authored-by: ZhangShenao <15201440436@163.com>
Co-authored-by: Friso H. Kingma <fhkingma@gmail.com>
Co-authored-by: ChengZi <chen.zhang@zilliz.com>
Co-authored-by: Nuno Campos <nuno@langchain.dev>
Co-authored-by: Morgante Pell <morgantep@google.com>
2024-09-13 14:38:45 -07:00

78 lines
2.5 KiB
Python

from typing import Optional, Type
from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
from langchain_community.utilities.polygon import PolygonAPIWrapper
class PolygonAggregatesSchema(BaseModel):
"""Input for PolygonAggregates."""
ticker: str = Field(
description="The ticker symbol to fetch aggregates for.",
)
timespan: str = Field(
description="The size of the time window. "
"Possible values are: "
"second, minute, hour, day, week, month, quarter, year. "
"Default is 'day'",
)
timespan_multiplier: int = Field(
description="The number of timespans to aggregate. "
"For example, if timespan is 'day' and "
"timespan_multiplier is 1, the result will be daily bars. "
"If timespan is 'day' and timespan_multiplier is 5, "
"the result will be weekly bars. "
"Default is 1.",
)
from_date: str = Field(
description="The start of the aggregate time window. "
"Either a date with the format YYYY-MM-DD or "
"a millisecond timestamp.",
)
to_date: str = Field(
description="The end of the aggregate time window. "
"Either a date with the format YYYY-MM-DD or "
"a millisecond timestamp.",
)
class PolygonAggregates(BaseTool):
"""
Tool that gets aggregate bars (stock prices) over a
given date range for a given ticker from Polygon.
"""
mode: str = "get_aggregates"
name: str = "polygon_aggregates"
description: str = (
"A wrapper around Polygon's Aggregates API. "
"This tool is useful for fetching aggregate bars (stock prices) for a ticker. "
"Input should be the ticker, date range, timespan, and timespan multiplier"
" that you want to get the aggregate bars for."
)
args_schema: Type[PolygonAggregatesSchema] = PolygonAggregatesSchema
api_wrapper: PolygonAPIWrapper
def _run(
self,
ticker: str,
timespan: str,
timespan_multiplier: int,
from_date: str,
to_date: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
"""Use the Polygon API tool."""
return self.api_wrapper.run(
mode=self.mode,
ticker=ticker,
timespan=timespan,
timespan_multiplier=timespan_multiplier,
from_date=from_date,
to_date=to_date,
)