Initial commit: 国土空间规划课程智能体 v1.0
单容器 Docker 架构的国土空间规划课程智能问答系统,集成 FastAPI 后端与 Next.js 前端。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""
|
||||
硅基流动大模型API集成
|
||||
"""
|
||||
import os
|
||||
from typing import List, Dict, Any, Optional, AsyncGenerator
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage, SystemMessage
|
||||
from langchain_core.callbacks.base import BaseCallbackHandler
|
||||
from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
||||
|
||||
from ..core.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
|
||||
class SiliconFlowLLM:
|
||||
"""硅基流动大模型客户端"""
|
||||
|
||||
def __init__(self, model: Optional[str] = None):
|
||||
"""初始化LLM客户端"""
|
||||
# 设置环境变量
|
||||
os.environ["OPENAI_API_KEY"] = settings.siliconflow_api_key
|
||||
os.environ["OPENAI_API_BASE"] = settings.siliconflow_base_url
|
||||
|
||||
# 使用传入的模型或默认模型
|
||||
self.model_name = model or settings.siliconflow_model
|
||||
print(f"[DEBUG-LLM] 初始化LLM客户端,使用模型: {self.model_name} (传入参数: {model}, 默认配置: {settings.siliconflow_model})")
|
||||
|
||||
# 创建LLM实例
|
||||
self.llm = ChatOpenAI(
|
||||
model=self.model_name,
|
||||
api_key=settings.siliconflow_api_key,
|
||||
base_url=settings.siliconflow_base_url,
|
||||
temperature=0.7,
|
||||
max_tokens=2000,
|
||||
streaming=True
|
||||
)
|
||||
# 检查实际使用的模型名称
|
||||
actual_model = getattr(self.llm, 'model_name', None) or getattr(self.llm, 'model', None) or str(self.llm)
|
||||
print(f"[DEBUG-LLM] ChatOpenAI实例创建完成,实际模型: {actual_model}")
|
||||
|
||||
def chat(self, messages: List[BaseMessage], **kwargs) -> str:
|
||||
"""同步聊天"""
|
||||
try:
|
||||
response = self.llm.invoke(messages, **kwargs)
|
||||
return response.content
|
||||
except Exception as e:
|
||||
raise Exception(f"LLM调用失败: {str(e)}")
|
||||
|
||||
async def achat(self, messages: List[BaseMessage], **kwargs) -> str:
|
||||
"""异步聊天"""
|
||||
try:
|
||||
response = await self.llm.ainvoke(messages, **kwargs)
|
||||
return response.content
|
||||
except Exception as e:
|
||||
raise Exception(f"LLM异步调用失败: {str(e)}")
|
||||
|
||||
async def stream_chat(self, messages: List[BaseMessage], **kwargs) -> AsyncGenerator[str, None]:
|
||||
"""流式聊天"""
|
||||
try:
|
||||
async for chunk in self.llm.astream(messages, **kwargs):
|
||||
if hasattr(chunk, 'content') and chunk.content:
|
||||
yield chunk.content
|
||||
except Exception as e:
|
||||
raise Exception(f"LLM流式调用失败: {str(e)}")
|
||||
|
||||
def create_messages(
|
||||
self,
|
||||
user_message: str,
|
||||
system_prompt: Optional[str] = None,
|
||||
chat_history: Optional[List[Dict[str, str]]] = None
|
||||
) -> List[BaseMessage]:
|
||||
"""创建消息列表"""
|
||||
messages = []
|
||||
|
||||
# 添加系统提示
|
||||
if system_prompt:
|
||||
messages.append(SystemMessage(content=system_prompt))
|
||||
|
||||
# 添加聊天历史
|
||||
if chat_history:
|
||||
for msg in chat_history:
|
||||
if msg["role"] == "user":
|
||||
messages.append(HumanMessage(content=msg["content"]))
|
||||
elif msg["role"] == "assistant":
|
||||
messages.append(AIMessage(content=msg["content"]))
|
||||
|
||||
# 添加当前用户消息
|
||||
messages.append(HumanMessage(content=user_message))
|
||||
|
||||
return messages
|
||||
|
||||
|
||||
# 全局LLM实例(使用默认模型)
|
||||
llm_client = SiliconFlowLLM()
|
||||
|
||||
|
||||
def get_llm_client(model: Optional[str] = None) -> SiliconFlowLLM:
|
||||
"""获取LLM客户端实例
|
||||
|
||||
Args:
|
||||
model: 可选的模型名称,如果提供则创建新的实例,否则返回默认实例
|
||||
"""
|
||||
if model is None:
|
||||
return llm_client
|
||||
else:
|
||||
# 为指定模型创建新实例
|
||||
return SiliconFlowLLM(model=model)
|
||||
Reference in New Issue
Block a user