69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
API路由模块,为前端提供REST API接口
|
||
"""
|
||
|
||
import os
|
||
from fastapi import APIRouter, Depends, HTTPException, status, Body
|
||
from typing import Dict, Any, List, Optional
|
||
from pydantic import BaseModel
|
||
import json
|
||
import logging
|
||
|
||
from cryptoai.api.deepseek_api import DeepSeekAPI
|
||
from cryptoai.utils.config_loader import ConfigLoader
|
||
from fastapi.responses import StreamingResponse
|
||
from cryptoai.routes.user import get_current_user
|
||
import requests
|
||
# 创建路由
|
||
router = APIRouter()
|
||
|
||
class ChatRequest(BaseModel):
|
||
user_prompt: str
|
||
|
||
|
||
@router.get("/list")
|
||
async def get_agents(current_user: Dict[str, Any] = Depends(get_current_user)):
|
||
"""
|
||
获取所有代理
|
||
"""
|
||
return [
|
||
{
|
||
"id": "1",
|
||
"name": "交易AI助手",
|
||
"hello_prompt": "您好,我是交易AI助手,为您提供专业的数字货币交易分析和建议",
|
||
"description": "为您提供专业的交易分析和建议",
|
||
}
|
||
]
|
||
|
||
|
||
@router.post("/chat")
|
||
async def chat(request: ChatRequest,current_user: Dict[str, Any] = Depends(get_current_user)):
|
||
"""
|
||
聊天接口
|
||
"""
|
||
|
||
token = "app-vhJecqbcLukf72g0uxAb9tcz"
|
||
url = "https://mate.aimateplus.com/v1/chat-messages"
|
||
headers = {
|
||
"Authorization": f"Bearer {token}",
|
||
"Content-Type": "application/json"
|
||
}
|
||
data = {
|
||
"inputs" : {},
|
||
"query" : request.user_prompt,
|
||
"response_mode" : "streaming",
|
||
"user" : current_user["mail"]
|
||
}
|
||
response = requests.post(url, headers=headers, json=data, stream=True)
|
||
|
||
#获取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")
|