feat: api_key 通过 Header 传递 (api-key 或 Authorization)
This commit is contained in:
@@ -9,7 +9,7 @@ import aiohttp
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi import FastAPI, HTTPException, Query, Header, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel, Field
|
||||
import uvicorn
|
||||
@@ -130,7 +130,6 @@ class HealthResponse(BaseModel):
|
||||
class ChatRequest(BaseModel):
|
||||
"""Chat 请求"""
|
||||
message: str = Field(..., description="用户消息")
|
||||
api_key: Optional[str] = Field(None, description="LLM API Key")
|
||||
user_id: Optional[str] = Field(None, description="用户ID")
|
||||
|
||||
|
||||
@@ -632,11 +631,26 @@ async def chat_with_llm(message: str, context: str, api_key: str) -> str:
|
||||
|
||||
|
||||
@app.post("/chat", response_model=ChatResponse)
|
||||
async def chat(request: ChatRequest):
|
||||
"""智能对话 - 获取技术分析并提供投资建议"""
|
||||
if not request.api_key:
|
||||
raise HTTPException(status_code=400, detail="请提供 api_key 参数")
|
||||
api_key = request.api_key
|
||||
async def chat(
|
||||
request: ChatRequest,
|
||||
api_key: Optional[str] = Header(None, alias="api-key"),
|
||||
authorization: Optional[str] = Header(None)
|
||||
):
|
||||
"""智能对话 - 获取技术分析并提供投资建议
|
||||
|
||||
api_key 通过请求头传递:
|
||||
- api-key: your-api-key
|
||||
- 或 Authorization: Bearer your-api-key
|
||||
"""
|
||||
# 从 Header 获取 api_key
|
||||
if not api_key and authorization:
|
||||
if authorization.startswith("Bearer "):
|
||||
api_key = authorization[7:]
|
||||
else:
|
||||
api_key = authorization
|
||||
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=401, detail="请在请求头中提供 api-key 或 Authorization")
|
||||
|
||||
# 从消息中提取股票代码
|
||||
import re
|
||||
|
||||
@@ -9,7 +9,7 @@ import aiohttp
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi import FastAPI, HTTPException, Query, Header, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel, Field
|
||||
import uvicorn
|
||||
@@ -112,7 +112,6 @@ class HealthResponse(BaseModel):
|
||||
class ChatRequest(BaseModel):
|
||||
"""Chat 请求"""
|
||||
message: str = Field(..., description="用户消息")
|
||||
api_key: Optional[str] = Field(None, description="LLM API Key")
|
||||
user_id: Optional[str] = Field(None, description="用户ID")
|
||||
|
||||
|
||||
@@ -458,11 +457,26 @@ async def chat_with_llm(message: str, context: str, api_key: str) -> str:
|
||||
|
||||
|
||||
@app.post("/chat", response_model=ChatResponse)
|
||||
async def chat(request: ChatRequest):
|
||||
"""智能对话 - 获取新闻并提供分析"""
|
||||
if not request.api_key:
|
||||
raise HTTPException(status_code=400, detail="请提供 api_key 参数")
|
||||
api_key = request.api_key
|
||||
async def chat(
|
||||
request: ChatRequest,
|
||||
api_key: Optional[str] = Header(None, alias="api-key"),
|
||||
authorization: Optional[str] = Header(None)
|
||||
):
|
||||
"""智能对话 - 获取新闻并提供分析
|
||||
|
||||
api_key 通过请求头传递:
|
||||
- api-key: your-api-key
|
||||
- 或 Authorization: Bearer your-api-key
|
||||
"""
|
||||
# 从 Header 获取 api_key
|
||||
if not api_key and authorization:
|
||||
if authorization.startswith("Bearer "):
|
||||
api_key = authorization[7:]
|
||||
else:
|
||||
api_key = authorization
|
||||
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=401, detail="请在请求头中提供 api-key 或 Authorization")
|
||||
|
||||
# 从消息中提取关键词
|
||||
import re
|
||||
|
||||
@@ -9,7 +9,7 @@ import aiohttp
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi import FastAPI, HTTPException, Query, Header, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel, Field
|
||||
import uvicorn
|
||||
@@ -112,7 +112,6 @@ class HealthResponse(BaseModel):
|
||||
class ChatRequest(BaseModel):
|
||||
"""Chat 请求"""
|
||||
message: str = Field(..., description="用户消息")
|
||||
api_key: Optional[str] = Field(None, description="LLM API Key")
|
||||
user_id: Optional[str] = Field(None, description="用户ID")
|
||||
|
||||
|
||||
@@ -412,11 +411,26 @@ def extract_symbols_from_message(message: str) -> List[str]:
|
||||
|
||||
|
||||
@app.post("/chat", response_model=ChatResponse)
|
||||
async def chat(request: ChatRequest):
|
||||
"""智能对话 - 支持自然语言查询股票信息"""
|
||||
if not request.api_key:
|
||||
raise HTTPException(status_code=400, detail="请提供 api_key 参数")
|
||||
api_key = request.api_key
|
||||
async def chat(
|
||||
request: ChatRequest,
|
||||
api_key: Optional[str] = Header(None, alias="api-key"),
|
||||
authorization: Optional[str] = Header(None)
|
||||
):
|
||||
"""智能对话 - 支持自然语言查询股票信息
|
||||
|
||||
api_key 通过请求头传递:
|
||||
- api-key: your-api-key
|
||||
- 或 Authorization: Bearer your-api-key
|
||||
"""
|
||||
# 从 Header 获取 api_key
|
||||
if not api_key and authorization:
|
||||
if authorization.startswith("Bearer "):
|
||||
api_key = authorization[7:]
|
||||
else:
|
||||
api_key = authorization
|
||||
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=401, detail="请在请求头中提供 api-key 或 Authorization")
|
||||
|
||||
# 从消息中提取股票代码
|
||||
symbols = extract_symbols_from_message(request.message)
|
||||
|
||||
Reference in New Issue
Block a user