community: Add PolygonTickerNews Tool (#17808)

Description:
In this PR, I am adding a PolygonTickerNews Tool, which can be used to
get the latest news for a given ticker / stock.

Twitter handle: [@virattt](https://twitter.com/virattt)
This commit is contained in:
Virat Singh
2024-02-20 13:15:29 -05:00
committed by GitHub
parent 441160d6b3
commit 92e52e89ca
8 changed files with 217 additions and 25 deletions

View File

@@ -39,8 +39,29 @@ class PolygonAPIWrapper(BaseModel):
return data.get("results", None)
def get_ticker_news(self, ticker: str) -> Optional[dict]:
"""
Get the most recent news articles relating to a stock ticker symbol,
including a summary of the article and a link to the original source.
"""
url = (
f"{POLYGON_BASE_URL}v2/reference/news?"
f"ticker={ticker}&"
f"apiKey={self.polygon_api_key}"
)
response = requests.get(url)
data = response.json()
status = data.get("status", None)
if status != "OK":
raise ValueError(f"API Error: {data}")
return data.get("results", None)
def run(self, mode: str, ticker: str) -> str:
if mode == "get_last_quote":
return json.dumps(self.get_last_quote(ticker))
elif mode == "get_ticker_news":
return json.dumps(self.get_ticker_news(ticker))
else:
raise ValueError(f"Invalid mode {mode} for Polygon API.")