102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
from fastapi import APIRouter
|
|
from typing import Optional
|
|
from cryptoai.utils.db_manager import get_db_manager
|
|
from fastapi import Depends
|
|
from pydantic import BaseModel
|
|
from cryptoai.routes.user import get_current_user
|
|
from fastapi import HTTPException
|
|
from fastapi.responses import StreamingResponse
|
|
import requests
|
|
|
|
|
|
class AnalysisHistoryRequest(BaseModel):
|
|
symbol: str
|
|
content: str
|
|
timeframe: Optional[str] = None
|
|
type: str
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/analysis_history")
|
|
async def analysis_history(request: AnalysisHistoryRequest,
|
|
current_user: dict = Depends(get_current_user)):
|
|
|
|
get_db_manager().analysis_history_manager.add_analysis_history(current_user["id"], request.type, request.symbol, request.content, request.timeframe)
|
|
|
|
return {"message": "ok"}
|
|
|
|
@router.get("/analysis_histories")
|
|
async def get_analysis_histories(current_user: dict = Depends(get_current_user),
|
|
limit: int = 10,
|
|
offset: int = 0):
|
|
history = get_db_manager().analysis_history_manager.get_user_analysis_history(current_user["id"], limit=limit, offset=offset)
|
|
return history
|
|
|
|
|
|
class AnalysisRequest(BaseModel):
|
|
symbol: Optional[str] = None
|
|
timeframe: Optional[str] = None
|
|
stock_code: Optional[str] = None
|
|
type: str
|
|
|
|
@router.post("/analysis")
|
|
async def analysis(request: AnalysisRequest,
|
|
current_user: dict = Depends(get_current_user)):
|
|
|
|
if request.type == 'crypto':
|
|
# 检查symbol是否存在
|
|
tokens = get_db_manager().token_manager.search_token(request.symbol)
|
|
if not tokens or len(tokens) == 0:
|
|
raise HTTPException(status_code=400, detail="您输入的币种在币安不存在,请检查后重新输入。")
|
|
|
|
symbol = tokens[0]["symbol"]
|
|
token = 'app-BbaqIAMPi0ktgaV9IizMlc2N'
|
|
|
|
payload = {
|
|
"inputs" : {
|
|
"symbol" : symbol,
|
|
"timeframe" : request.timeframe
|
|
},
|
|
"response_mode": "streaming",
|
|
"user": current_user["mail"]
|
|
}
|
|
|
|
get_db_manager().user_question_manager.save_user_question(current_user["id"], symbol, "请分析以下加密货币:" + symbol + ",并给出分析报告。")
|
|
|
|
|
|
else:
|
|
stock_code = request.stock_code
|
|
token = 'app-nWuCOa0YfQVtAosTY3Jr5vFV'
|
|
|
|
payload = {
|
|
"inputs" : {
|
|
"stock_code": stock_code
|
|
},
|
|
"response_mode": "streaming",
|
|
"user": current_user["mail"]
|
|
}
|
|
|
|
get_db_manager().user_question_manager.save_user_question(current_user["id"], stock_code, "请分析以下股票:" + stock_code + ",并给出分析报告。")
|
|
|
|
url = 'https://mate.aimateplus.com/v1/workflows/run'
|
|
headers = {
|
|
'Authorization': f'Bearer {token}',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
response = requests.post(url, headers=headers, json=payload, stream=True)
|
|
|
|
# 如果响应不成功,返回错误
|
|
if response.status_code != 200:
|
|
raise HTTPException(
|
|
status_code=response.status_code,
|
|
detail=f"Failed to get response from Dify API: {response.text}"
|
|
)
|
|
|
|
# 获取response的stream
|
|
def stream_response():
|
|
for chunk in response.iter_content(chunk_size=1024):
|
|
if chunk:
|
|
yield chunk
|
|
|
|
return StreamingResponse(stream_response(), media_type="text/plain") |