mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-15 13:25:51 +00:00
style: 统一代码格式和字符串引号使用
refactor: 优化代码结构和可读性 feat: 添加http_limit模块实现请求限制功能 fix: 修复异步任务中使用time.sleep的问题 chore: 更新依赖项并添加pytest测试框架 docs: 更新项目描述信息 perf: 优化Redis序列化方式使用JSON替代pickle test: 添加测试相关配置和依赖
This commit is contained in:
@@ -17,7 +17,12 @@ from langchain.agents.structured_output import (
|
||||
ToolStrategy,
|
||||
)
|
||||
from langchain.chat_models import init_chat_model
|
||||
from langchain.messages import AIMessage, HumanMessage, RemoveMessage, SystemMessage
|
||||
from langchain.messages import (
|
||||
AIMessage,
|
||||
HumanMessage,
|
||||
RemoveMessage,
|
||||
SystemMessage,
|
||||
)
|
||||
from langchain.tools import ToolRuntime, tool
|
||||
from langgraph.checkpoint.memory import InMemorySaver
|
||||
from langgraph.graph.message import REMOVE_ALL_MESSAGES
|
||||
@@ -46,6 +51,7 @@ def get_weather_for_location(city: str) -> str:
|
||||
@dataclass
|
||||
class Context:
|
||||
"""Custom runtime context schema."""
|
||||
|
||||
user_id: str
|
||||
|
||||
|
||||
@@ -57,17 +63,13 @@ def get_user_location(runtime: ToolRuntime[Context]) -> str:
|
||||
|
||||
|
||||
# =================定义模型=================
|
||||
model = init_chat_model(
|
||||
"claude-sonnet-4-5-20250929",
|
||||
temperature=0.5,
|
||||
timeout=10,
|
||||
max_tokens=1000
|
||||
)
|
||||
model = init_chat_model("claude-sonnet-4-5-20250929", temperature=0.5, timeout=10, max_tokens=1000)
|
||||
|
||||
|
||||
# =================定义响应模型=================
|
||||
class ResponseFormat(BaseModel):
|
||||
"""Response schema for the agent."""
|
||||
|
||||
rating: int | None = Field(description="Rating from 1-5", ge=1, le=5)
|
||||
comment: str = Field(description="Review comment")
|
||||
punny_response: str
|
||||
@@ -98,28 +100,21 @@ def trim_messages(state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
|
||||
recent_messages = messages[-3:] if len(messages) % 2 == 0 else messages[-4:]
|
||||
new_messages = [first_msg, *recent_messages]
|
||||
|
||||
return {
|
||||
"messages": [
|
||||
RemoveMessage(id=REMOVE_ALL_MESSAGES),
|
||||
*new_messages
|
||||
]
|
||||
}
|
||||
return {"messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES), *new_messages]}
|
||||
|
||||
|
||||
@after_model
|
||||
def validate_response(state: AgentState, runtime: Runtime) -> dict | None:
|
||||
"""Remove messages containing sensitive words."""
|
||||
STOP_WORDS = ["password", "secret"]
|
||||
last_message = state["messages"][-1]
|
||||
if any(word in last_message.content for word in STOP_WORDS):
|
||||
if any(word in last_message.content for word in ["password", "secret"]):
|
||||
return {"messages": [RemoveMessage(id=last_message.id or "")]}
|
||||
return None
|
||||
|
||||
|
||||
@wrap_model_call
|
||||
def inject_file_context(
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], ModelResponse]
|
||||
request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
|
||||
) -> ModelResponse:
|
||||
"""Inject context about files user has uploaded this session."""
|
||||
# Read from State: get uploaded files metadata
|
||||
@@ -127,7 +122,9 @@ def inject_file_context(
|
||||
|
||||
if uploaded_files:
|
||||
# Build context about available files
|
||||
file_descriptions = [f"- {file['name']} ({file['type']}): {file['summary']}" for file in uploaded_files]
|
||||
file_descriptions = [
|
||||
f"- {file['name']} ({file['type']}): {file['summary']}" for file in uploaded_files
|
||||
]
|
||||
|
||||
file_context = f"""Files you have access to in this conversation:
|
||||
{chr(10).join(file_descriptions)}
|
||||
@@ -155,10 +152,18 @@ agent = create_agent(
|
||||
model=model,
|
||||
system_prompt=SYSTEM_PROMPT,
|
||||
tools=[get_user_location, get_weather_for_location],
|
||||
middleware=[dynamic_system_prompt, trim_messages, validate_response, inject_file_context],
|
||||
middleware=[
|
||||
dynamic_system_prompt,
|
||||
trim_messages,
|
||||
validate_response,
|
||||
inject_file_context,
|
||||
],
|
||||
context_schema=Context,
|
||||
response_format=ToolStrategy(schema=ResponseFormat, handle_errors=(ValueError, TypeError, custom_error_handler)),
|
||||
checkpointer=checkpointer
|
||||
response_format=ToolStrategy(
|
||||
schema=ResponseFormat,
|
||||
handle_errors=(ValueError, TypeError, custom_error_handler),
|
||||
),
|
||||
checkpointer=checkpointer,
|
||||
)
|
||||
|
||||
# =================定义线程=================
|
||||
@@ -167,18 +172,14 @@ config = {"configurable": {"thread_id": "1"}}
|
||||
messages = [
|
||||
SystemMessage("You are a poetry expert"),
|
||||
HumanMessage("Write a haiku about spring"),
|
||||
AIMessage("Cherry blossoms bloom...")
|
||||
AIMessage("Cherry blossoms bloom..."),
|
||||
]
|
||||
|
||||
# =================运行智能体=================
|
||||
response = agent.invoke(
|
||||
input=messages,
|
||||
config=config,
|
||||
context=Context(user_id="1")
|
||||
)
|
||||
response = agent.invoke(input=messages, config=config, context=Context(user_id="1"))
|
||||
|
||||
# =================解析响应=================
|
||||
print(response['structured_response'])
|
||||
print(response["structured_response"])
|
||||
# ResponseFormat(
|
||||
# punny_response="Florida is still having a 'sun-derful' day! The sunshine is playing 'ray-dio' hits all day long! I'd say it's the perfect weather for some 'solar-bration'! If you were hoping for rain, I'm afraid that idea is all 'washed up' - the forecast remains 'clear-ly' brilliant!",
|
||||
# weather_conditions="It's always sunny in Florida!"
|
||||
|
||||
@@ -5,6 +5,7 @@ from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
from app.config.setting import settings
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import log
|
||||
|
||||
|
||||
@@ -16,14 +17,14 @@ class AIClient:
|
||||
def __init__(self) -> None:
|
||||
# 使用LangChain的ChatOpenAI类
|
||||
self.model = ChatOpenAI(
|
||||
api_key=settings.OPENAI_API_KEY,
|
||||
api_key=lambda: settings.OPENAI_API_KEY,
|
||||
model=settings.OPENAI_MODEL,
|
||||
base_url=settings.OPENAI_BASE_URL,
|
||||
temperature=0.7,
|
||||
streaming=True
|
||||
streaming=True,
|
||||
)
|
||||
|
||||
async def process(self, query: str) -> AsyncGenerator[str, Any]:
|
||||
async def process(self, query: str) -> AsyncGenerator[str, Any]:
|
||||
"""
|
||||
处理查询并返回流式响应
|
||||
|
||||
@@ -33,13 +34,15 @@ class AIClient:
|
||||
返回:
|
||||
- AsyncGenerator[str, Any]: 流式响应内容。
|
||||
"""
|
||||
system_prompt = """你是一个有用的AI助手,可以帮助用户回答问题和提供帮助。请用中文回答用户的问题。"""
|
||||
system_prompt = (
|
||||
"""你是一个有用的AI助手,可以帮助用户回答问题和提供帮助。请用中文回答用户的问题。"""
|
||||
)
|
||||
|
||||
try:
|
||||
# 使用LangChain的异步流式生成
|
||||
messages = [
|
||||
SystemMessage(content=system_prompt),
|
||||
HumanMessage(content=query)
|
||||
HumanMessage(content=query),
|
||||
]
|
||||
|
||||
# 使用LangChain的流式响应
|
||||
@@ -66,24 +69,33 @@ class AIClient:
|
||||
error_code = err.get("code")
|
||||
message = err.get("message")
|
||||
except Exception:
|
||||
# 忽略解析失败
|
||||
pass
|
||||
raise CustomException(f"解析 OpenAI 错误失败: {e!s}")
|
||||
|
||||
text = str(e)
|
||||
msg = message or text
|
||||
|
||||
# 特定错误映射
|
||||
# 欠费/账户状态异常
|
||||
if (error_code == "Arrearage") or (error_type == "Arrearage") or ("in good standing" in (msg or "")):
|
||||
if (
|
||||
(error_code == "Arrearage")
|
||||
or (error_type == "Arrearage")
|
||||
or ("in good standing" in (msg or ""))
|
||||
):
|
||||
return "账户欠费或结算异常,访问被拒绝。请检查账号状态或更换有效的 API Key。"
|
||||
# 鉴权失败
|
||||
if status_code == 401 or "invalid api key" in msg.lower():
|
||||
return "鉴权失败,API Key 无效或已过期。请检查系统配置中的 API Key。"
|
||||
# 权限不足或被拒绝
|
||||
if status_code == 403 or error_type in {"PermissionDenied", "permission_denied"}:
|
||||
if status_code == 403 or error_type in {
|
||||
"PermissionDenied",
|
||||
"permission_denied",
|
||||
}:
|
||||
return "访问被拒绝,权限不足或账号受限。请检查账户权限设置。"
|
||||
# 配额不足或限流
|
||||
if status_code == 429 or error_type in {"insufficient_quota", "rate_limit_exceeded"}:
|
||||
if status_code == 429 or error_type in {
|
||||
"insufficient_quota",
|
||||
"rate_limit_exceeded",
|
||||
}:
|
||||
return "请求过于频繁或配额已用尽。请稍后重试或提升账户配额。"
|
||||
# 客户端错误
|
||||
if status_code == 400:
|
||||
|
||||
Reference in New Issue
Block a user