Feat/assistant app (#2086)
Co-authored-by: chenhe <guchenhe@gmail.com> Co-authored-by: Pascal M <11357019+perzeuss@users.noreply.github.com>
This commit is contained in:
BIN
api/core/tools/provider/builtin/yahoo/_assets/icon.png
Normal file
BIN
api/core/tools/provider/builtin/yahoo/_assets/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
69
api/core/tools/provider/builtin/yahoo/tools/analytics.py
Normal file
69
api/core/tools/provider/builtin/yahoo/tools/analytics.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
|
||||
from typing import Any, Dict, List, Union
|
||||
from requests.exceptions import HTTPError, ReadTimeout
|
||||
from datetime import datetime
|
||||
|
||||
from yfinance import download
|
||||
import pandas as pd
|
||||
|
||||
class YahooFinanceAnalyticsTool(BuiltinTool):
|
||||
def _invoke(self, user_id: str, tool_paramters: Dict[str, Any]) \
|
||||
-> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
symbol = tool_paramters.get('symbol', '')
|
||||
if not symbol:
|
||||
return self.create_text_message('Please input symbol')
|
||||
|
||||
time_range = [None, None]
|
||||
start_date = tool_paramters.get('start_date', '')
|
||||
if start_date:
|
||||
time_range[0] = start_date
|
||||
else:
|
||||
time_range[0] = '1800-01-01'
|
||||
|
||||
end_date = tool_paramters.get('end_date', '')
|
||||
if end_date:
|
||||
time_range[1] = end_date
|
||||
else:
|
||||
time_range[1] = datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
stock_data = download(symbol, start=time_range[0], end=time_range[1])
|
||||
max_segments = min(15, len(stock_data))
|
||||
rows_per_segment = len(stock_data) // max_segments
|
||||
summary_data = []
|
||||
for i in range(max_segments):
|
||||
start_idx = i * rows_per_segment
|
||||
end_idx = (i + 1) * rows_per_segment if i < max_segments - 1 else len(stock_data)
|
||||
segment_data = stock_data.iloc[start_idx:end_idx]
|
||||
segment_summary = {
|
||||
'Start Date': segment_data.index[0],
|
||||
'End Date': segment_data.index[-1],
|
||||
'Average Close': segment_data['Close'].mean(),
|
||||
'Average Volume': segment_data['Volume'].mean(),
|
||||
'Average Open': segment_data['Open'].mean(),
|
||||
'Average High': segment_data['High'].mean(),
|
||||
'Average Low': segment_data['Low'].mean(),
|
||||
'Average Adj Close': segment_data['Adj Close'].mean(),
|
||||
'Max Close': segment_data['Close'].max(),
|
||||
'Min Close': segment_data['Close'].min(),
|
||||
'Max Volume': segment_data['Volume'].max(),
|
||||
'Min Volume': segment_data['Volume'].min(),
|
||||
'Max Open': segment_data['Open'].max(),
|
||||
'Min Open': segment_data['Open'].min(),
|
||||
'Max High': segment_data['High'].max(),
|
||||
'Min High': segment_data['High'].min(),
|
||||
}
|
||||
|
||||
summary_data.append(segment_summary)
|
||||
|
||||
summary_df = pd.DataFrame(summary_data)
|
||||
|
||||
try:
|
||||
return self.create_text_message(str(summary_df.to_dict()))
|
||||
except (HTTPError, ReadTimeout):
|
||||
return self.create_text_message(f'There is a internet connection problem. Please try again later.')
|
||||
|
||||
46
api/core/tools/provider/builtin/yahoo/tools/analytics.yaml
Normal file
46
api/core/tools/provider/builtin/yahoo/tools/analytics.yaml
Normal file
@@ -0,0 +1,46 @@
|
||||
identity:
|
||||
name: yahoo_finance_analytics
|
||||
author: Dify
|
||||
label:
|
||||
en_US: Analytics
|
||||
zh_Hans: 分析
|
||||
icon: icon.svg
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for get analytics about a ticker from Yahoo Finance.
|
||||
zh_Hans: 一个用于从雅虎财经获取分析数据的工具。
|
||||
llm: A tool for get analytics from Yahoo Finance. Input should be the ticker symbol like AAPL.
|
||||
parameters:
|
||||
- name: symbol
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Ticker symbol
|
||||
zh_Hans: 股票代码
|
||||
human_description:
|
||||
en_US: The ticker symbol of the company you want to analyze.
|
||||
zh_Hans: 你想要搜索的公司的股票代码。
|
||||
llm_description: The ticker symbol of the company you want to analyze.
|
||||
form: llm
|
||||
- name: start_date
|
||||
type: string
|
||||
required: false
|
||||
label:
|
||||
en_US: Start date
|
||||
zh_Hans: 开始日期
|
||||
human_description:
|
||||
en_US: The start date of the analytics.
|
||||
zh_Hans: 分析的开始日期。
|
||||
llm_description: The start date of the analytics, the format of the date must be YYYY-MM-DD like 2020-01-01.
|
||||
form: llm
|
||||
- name: end_date
|
||||
type: string
|
||||
required: false
|
||||
label:
|
||||
en_US: End date
|
||||
zh_Hans: 结束日期
|
||||
human_description:
|
||||
en_US: The end date of the analytics.
|
||||
zh_Hans: 分析的结束日期。
|
||||
llm_description: The end date of the analytics, the format of the date must be YYYY-MM-DD like 2024-01-01.
|
||||
form: llm
|
||||
46
api/core/tools/provider/builtin/yahoo/tools/news.py
Normal file
46
api/core/tools/provider/builtin/yahoo/tools/news.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
|
||||
from typing import Any, Dict, List, Union
|
||||
from requests.exceptions import HTTPError, ReadTimeout
|
||||
|
||||
import yfinance
|
||||
|
||||
class YahooFinanceSearchTickerTool(BuiltinTool):
|
||||
def _invoke(self,user_id: str, tool_paramters: Dict[str, Any]) \
|
||||
-> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
'''
|
||||
invoke tools
|
||||
'''
|
||||
|
||||
query = tool_paramters.get('symbol', '')
|
||||
if not query:
|
||||
return self.create_text_message('Please input symbol')
|
||||
|
||||
try:
|
||||
return self.run(ticker=query, user_id=user_id)
|
||||
except (HTTPError, ReadTimeout):
|
||||
return self.create_text_message(f'There is a internet connection problem. Please try again later.')
|
||||
|
||||
def run(self, ticker: str, user_id: str) -> ToolInvokeMessage:
|
||||
company = yfinance.Ticker(ticker)
|
||||
try:
|
||||
if company.isin is None:
|
||||
return self.create_text_message(f'Company ticker {ticker} not found.')
|
||||
except (HTTPError, ReadTimeout, ConnectionError):
|
||||
return self.create_text_message(f'Company ticker {ticker} not found.')
|
||||
|
||||
links = []
|
||||
try:
|
||||
links = [n['link'] for n in company.news if n['type'] == 'STORY']
|
||||
except (HTTPError, ReadTimeout, ConnectionError):
|
||||
if not links:
|
||||
return self.create_text_message(f'There is nothing about {ticker} ticker')
|
||||
if not links:
|
||||
return self.create_text_message(f'No news found for company that searched with {ticker} ticker.')
|
||||
|
||||
result = '\n\n'.join([
|
||||
self.get_url(link) for link in links
|
||||
])
|
||||
|
||||
return self.create_text_message(self.summary(user_id=user_id, content=result))
|
||||
24
api/core/tools/provider/builtin/yahoo/tools/news.yaml
Normal file
24
api/core/tools/provider/builtin/yahoo/tools/news.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
identity:
|
||||
name: yahoo_finance_news
|
||||
author: Dify
|
||||
label:
|
||||
en_US: News
|
||||
zh_Hans: 新闻
|
||||
icon: icon.svg
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for get news about a ticker from Yahoo Finance.
|
||||
zh_Hans: 一个用于从雅虎财经获取新闻的工具。
|
||||
llm: A tool for get news from Yahoo Finance. Input should be the ticker symbol like AAPL.
|
||||
parameters:
|
||||
- name: symbol
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Ticker symbol
|
||||
zh_Hans: 股票代码
|
||||
human_description:
|
||||
en_US: The ticker symbol of the company you want to search.
|
||||
zh_Hans: 你想要搜索的公司的股票代码。
|
||||
llm_description: The ticker symbol of the company you want to search.
|
||||
form: llm
|
||||
25
api/core/tools/provider/builtin/yahoo/tools/ticker.py
Normal file
25
api/core/tools/provider/builtin/yahoo/tools/ticker.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
|
||||
from typing import Any, Dict, List, Union
|
||||
from requests.exceptions import HTTPError, ReadTimeout
|
||||
|
||||
from yfinance import Ticker
|
||||
|
||||
class YahooFinanceSearchTickerTool(BuiltinTool):
|
||||
def _invoke(self, user_id: str, tool_paramters: Dict[str, Any]) \
|
||||
-> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
query = tool_paramters.get('symbol', '')
|
||||
if not query:
|
||||
return self.create_text_message('Please input symbol')
|
||||
|
||||
try:
|
||||
return self.create_text_message(self.run(ticker=query))
|
||||
except (HTTPError, ReadTimeout):
|
||||
return self.create_text_message(f'There is a internet connection problem. Please try again later.')
|
||||
|
||||
def run(self, ticker: str) -> str:
|
||||
return str(Ticker(ticker).info)
|
||||
24
api/core/tools/provider/builtin/yahoo/tools/ticker.yaml
Normal file
24
api/core/tools/provider/builtin/yahoo/tools/ticker.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
identity:
|
||||
name: yahoo_finance_ticker
|
||||
author: Dify
|
||||
label:
|
||||
en_US: Ticker
|
||||
zh_Hans: 股票信息
|
||||
icon: icon.svg
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for search ticker information from Yahoo Finance.
|
||||
zh_Hans: 一个用于从雅虎财经搜索股票信息的工具。
|
||||
llm: A tool for search ticker information from Yahoo Finance. Input should be the ticker symbol like AAPL.
|
||||
parameters:
|
||||
- name: symbol
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Ticker symbol
|
||||
zh_Hans: 股票代码
|
||||
human_description:
|
||||
en_US: The ticker symbol of the company you want to search.
|
||||
zh_Hans: 你想要搜索的公司的股票代码。
|
||||
llm_description: The ticker symbol of the company you want to search.
|
||||
form: llm
|
||||
20
api/core/tools/provider/builtin/yahoo/yahoo.py
Normal file
20
api/core/tools/provider/builtin/yahoo/yahoo.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
|
||||
from core.tools.provider.builtin.yahoo.tools.ticker import YahooFinanceSearchTickerTool
|
||||
|
||||
class YahooFinanceProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: dict) -> None:
|
||||
try:
|
||||
YahooFinanceSearchTickerTool().fork_tool_runtime(
|
||||
meta={
|
||||
"credentials": credentials,
|
||||
}
|
||||
).invoke(
|
||||
user_id='',
|
||||
tool_paramters={
|
||||
"ticker": "MSFT",
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
raise ToolProviderCredentialValidationError(str(e))
|
||||
11
api/core/tools/provider/builtin/yahoo/yahoo.yaml
Normal file
11
api/core/tools/provider/builtin/yahoo/yahoo.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
identity:
|
||||
author: Dify
|
||||
name: yahoo
|
||||
label:
|
||||
en_US: YahooFinance
|
||||
zh_Hans: 雅虎财经
|
||||
description:
|
||||
en_US: Finance, and Yahoo! get the latest news, stock quotes, and interactive chart with Yahoo!
|
||||
zh_Hans: 雅虎财经,获取并整理出最新的新闻、股票报价等一切你想要的财经信息。
|
||||
icon: icon.png
|
||||
credentails_for_provider:
|
||||
Reference in New Issue
Block a user