🌌 COMSOL Agentic Expert
Autonomous RAG Agent · Self-Correcting · Deep Reasoning
import streamlit as st import pandas as pd import numpy as np import jieba import requests import os import time import json import re import random import subprocess import logging import psutil import traceback from openai import OpenAI, RateLimitError, APIStatusError from rank_bm25 import BM25Okapi from sklearn.metrics.pairwise import cosine_similarity from typing import List, Dict, Any # ================= 0. 日志与系统监控 (来自 16) ================= logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) def log_memory(): """监控内存,防止 HF Space OOM""" process = psutil.Process(os.getpid()) mem_info = process.memory_info() res_mem = mem_info.rss / (1024 * 1024) logger.info(f"💾 Memory Usage: {res_mem:.2f} MB") return res_mem # ================= 1. 全局配置与样式 (融合) ================= # API 配置 API_BASE = "https://api.siliconflow.cn/v1" API_KEY = os.getenv("SILICONFLOW_API_KEY") # 模型配置 EMBEDDING_MODEL = "Qwen/Qwen3-Embedding-4B" RERANK_MODEL = "Qwen/Qwen3-Reranker-4B" # 必须使用具备 Function Calling 或强推理能力的模型 GEN_MODEL_NAME = "MiniMaxAI/MiniMax-M2" # 备用建议模型 SUGGEST_MODEL_NAME = "Qwen/Qwen3-Next-80B-A3B-Instruct" # 数据路径 DATA_FILENAME = "comsol_embedded.parquet" DATA_URL = "https://share.leezhu.cn/graduation_design_data/comsol_embedded.parquet" # 预置问题 PRESET_QUESTIONS = [ "如何设置流固耦合接口?", "求解器不收敛通常怎么解决?", "低频电磁场网格划分有哪些技巧?", "如何定义随时间变化的边界条件?" ] st.set_page_config( page_title="COMSOL Agentic Expert", page_icon="🌌", layout="centered", # Agent 模式更适合居中流式阅读 initial_sidebar_state="expanded" ) # --- CSS 注入:融合 16 的深色美学与 17 的功能样式 --- st.markdown(""" """, unsafe_allow_html=True) # ================= 2. 数据下载与持久化 (来自 16) ================= def download_with_curl(url, output_path): try: cmd = [ "curl", "-L", "-A", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "-o", output_path, "--fail", url ] result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode != 0: logger.warning(f"Curl stderr: {result.stderr}") return False return True except Exception as e: logger.error(f"Curl error: {e}") return False def get_data_file_path(): possible_paths = [ DATA_FILENAME, os.path.join(os.getcwd(), DATA_FILENAME), "/app/" + DATA_FILENAME ] for path in possible_paths: if os.path.exists(path): return path download_target = os.path.join(os.getcwd(), DATA_FILENAME) status_container = st.empty() status_container.info("📡 正在接入神经元网络... (下载核心知识库)") if download_with_curl(DATA_URL, download_target): status_container.empty() return download_target try: r = requests.get(DATA_URL, stream=True) with open(download_target, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) status_container.empty() return download_target except Exception as e: st.error(f"❌ 数据下载失败: {e}") st.stop() # ================= 3. RAG 引擎 (内核来自 16,接口适配 17) ================= class RAGController: def __init__(self): if not API_KEY: st.error("⚠️ 未检测到 API Key。请在 Settings -> Secrets 中配置 `SILICONFLOW_API_KEY`。") st.stop() self.client = OpenAI(base_url=API_BASE, api_key=API_KEY) self._load_data() def _load_data(self): real_path = get_data_file_path() try: logger.info("Initializing Engine...") self.df = pd.read_parquet(real_path) self.documents = self.df['content'].tolist() self.filenames = self.df['filename'].tolist() self.embeddings = np.stack(self.df['embedding'].values) # 简单的分词用于 BM25 tokenized = [jieba.lcut(str(d).lower()) for d in self.documents] self.bm25 = BM25Okapi(tokenized) logger.info(f"✅ Loaded {len(self.documents)} docs.") log_memory() except Exception as e: st.error(f"Engine Load Failed: {e}") st.stop() def execute_retrieval(self, query: str, limit: int = 5) -> Dict[str, Any]: """供 Agent 调用的检索接口""" start_t = time.time() # 1. Vector Search try: q_vec = self.client.embeddings.create(model=EMBEDDING_MODEL, input=[query]).data[0].embedding except: return {"query": query, "docs": [], "error": "Embedding API Failed"} vec_sim = cosine_similarity([q_vec], self.embeddings)[0] # 2. Keyword Search bm25_scores = self.bm25.get_scores(jieba.lcut(query.lower())) # 3. RRF Fusion scores = {} for idx in np.argsort(vec_sim)[-100:]: scores[idx] = scores.get(idx, 0) + 1/(60 + list(np.argsort(vec_sim)).index(idx)) for idx in np.argsort(bm25_scores)[-100:]: scores[idx] = scores.get(idx, 0) + 1/(60 + list(np.argsort(bm25_scores)).index(idx)) top_idxs = sorted(scores.items(), key=lambda x:x[1], reverse=True)[:50] if not top_idxs: return {"query": query, "docs": []} recall_docs = [self.documents[i] for i, _ in top_idxs] # 4. Rerank try: payload = {"model": RERANK_MODEL, "query": query, "documents": recall_docs, "top_n": limit} headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"} resp = requests.post(f"{API_BASE}/rerank", json=payload, headers=headers, timeout=10) final_docs = [] if resp.status_code == 200: results = resp.json().get('results', []) for item in results: orig_idx = top_idxs[item['index']][0] final_docs.append({ "filename": self.filenames[orig_idx], "content": self.documents[orig_idx], "score": item['relevance_score'] }) else: # Fallback if rerank fails for i, _ in top_idxs[:limit]: final_docs.append({"filename": self.filenames[i], "content": self.documents[i], "score": 0}) except: for i, _ in top_idxs[:limit]: final_docs.append({"filename": self.filenames[i], "content": self.documents[i], "score": 0}) return { "query": query, "docs": final_docs, "meta": {"time": time.time() - start_t} } @st.cache_resource def get_engine(): return RAGController() # ================= 4. Agent 工具与流式逻辑 (来自 17) ================= tools_schema = [{ "type": "function", "function": { "name": "search_knowledge_base", "description": "Search for COMSOL technical documentation. Use this whenever user asks technical questions.", "parameters": { "type": "object", "properties": { "query": {"type": "string", "description": "Specific technical keywords"}, "limit": {"type": "integer", "default": 4} }, "required": ["query"] } } }] def stream_and_parse_flat(client, messages, container): """ 核心流式处理函数:同时渲染 Thinking, Tool Logs 和 Final Answer """ full_reasoning = "" full_content = "" tool_calls_buffer = {} # 占位符 reasoning_ph = None content_ph = None try: # Retry logic for 429 stream = None for attempt in range(3): try: stream = client.chat.completions.create( model=GEN_MODEL_NAME, messages=messages, tools=tools_schema, tool_choice="auto", temperature=0.1, stream=True ) break except (RateLimitError, APIStatusError): time.sleep(2 * (attempt + 1)) if not stream: return None, None, None for chunk in stream: delta = chunk.choices[0].delta # --- 1. Thinking Stream --- # 兼容 DeepSeek 和其他模型的 reasoning 字段 r_content = getattr(delta, 'reasoning_content', None) if r_content: if reasoning_ph is None: reasoning_ph = container.empty() full_reasoning += r_content # 使用自定义 CSS 样式渲染思考 reasoning_ph.markdown(f"
Autonomous RAG Agent · Self-Correcting · Deep Reasoning