server.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. """
  2. KnowHub Server
  3. Agent 工具使用经验的共享平台。
  4. FastAPI + Milvus Lite(知识)+ SQLite(资源),单文件部署。
  5. """
  6. import os
  7. import re
  8. import json
  9. import sqlite3
  10. import asyncio
  11. import base64
  12. import time
  13. import uuid
  14. from contextlib import asynccontextmanager
  15. from datetime import datetime, timezone
  16. from typing import Optional
  17. from pathlib import Path
  18. from cryptography.hazmat.primitives.ciphers.aead import AESGCM
  19. from fastapi import FastAPI, HTTPException, Query, Header
  20. from fastapi.responses import HTMLResponse
  21. from pydantic import BaseModel, Field
  22. # 导入 LLM 调用(需要 agent 模块在 Python path 中)
  23. import sys
  24. sys.path.insert(0, str(Path(__file__).parent.parent))
  25. # 加载环境变量
  26. from dotenv import load_dotenv
  27. load_dotenv(Path(__file__).parent.parent / ".env")
  28. from agent.llm.openrouter import openrouter_llm_call
  29. # 导入向量存储和 embedding
  30. from knowhub.vector_store import MilvusStore
  31. from knowhub.embeddings import get_embedding, get_embeddings_batch
  32. BRAND_NAME = os.getenv("BRAND_NAME", "KnowHub")
  33. BRAND_API_ENV = os.getenv("BRAND_API_ENV", "KNOWHUB_API")
  34. BRAND_DB = os.getenv("BRAND_DB", "knowhub.db")
  35. # 组织密钥配置(格式:org1:key1_base64,org2:key2_base64)
  36. ORG_KEYS_RAW = os.getenv("ORG_KEYS", "")
  37. ORG_KEYS = {}
  38. if ORG_KEYS_RAW:
  39. for pair in ORG_KEYS_RAW.split(","):
  40. if ":" in pair:
  41. org, key_b64 = pair.split(":", 1)
  42. ORG_KEYS[org.strip()] = key_b64.strip()
  43. DB_PATH = Path(__file__).parent / BRAND_DB
  44. MILVUS_DATA_DIR = Path(__file__).parent / "milvus_data"
  45. # 全局 Milvus 存储实例
  46. milvus_store: Optional[MilvusStore] = None
  47. # --- 数据库 ---
  48. def get_db() -> sqlite3.Connection:
  49. conn = sqlite3.connect(str(DB_PATH))
  50. conn.row_factory = sqlite3.Row
  51. conn.execute("PRAGMA journal_mode=WAL")
  52. return conn
  53. # --- 加密/解密 ---
  54. def get_org_key(resource_id: str) -> Optional[bytes]:
  55. """从content_id提取组织前缀,返回对应密钥"""
  56. if "/" in resource_id:
  57. org = resource_id.split("/")[0]
  58. if org in ORG_KEYS:
  59. return base64.b64decode(ORG_KEYS[org])
  60. return None
  61. def encrypt_content(resource_id: str, plaintext: str) -> str:
  62. """加密内容,返回格式:encrypted:AES256-GCM:{base64_data}"""
  63. if not plaintext:
  64. return ""
  65. key = get_org_key(resource_id)
  66. if not key:
  67. # 没有配置密钥,明文存储(不推荐)
  68. return plaintext
  69. aesgcm = AESGCM(key)
  70. nonce = os.urandom(12) # 96-bit nonce
  71. ciphertext = aesgcm.encrypt(nonce, plaintext.encode("utf-8"), None)
  72. # 组合 nonce + ciphertext
  73. encrypted_data = nonce + ciphertext
  74. encoded = base64.b64encode(encrypted_data).decode("ascii")
  75. return f"encrypted:AES256-GCM:{encoded}"
  76. def decrypt_content(resource_id: str, encrypted_text: str, provided_key: Optional[str] = None) -> str:
  77. """解密内容,如果没有提供密钥或密钥错误,返回[ENCRYPTED]"""
  78. if not encrypted_text:
  79. return ""
  80. if not encrypted_text.startswith("encrypted:AES256-GCM:"):
  81. # 未加密的内容,直接返回
  82. return encrypted_text
  83. # 提取加密数据
  84. encoded = encrypted_text.split(":", 2)[2]
  85. encrypted_data = base64.b64decode(encoded)
  86. nonce = encrypted_data[:12]
  87. ciphertext = encrypted_data[12:]
  88. # 获取密钥
  89. key = None
  90. if provided_key:
  91. # 使用提供的密钥
  92. try:
  93. key = base64.b64decode(provided_key)
  94. except Exception:
  95. return "[ENCRYPTED]"
  96. else:
  97. # 从配置中获取
  98. key = get_org_key(resource_id)
  99. if not key:
  100. return "[ENCRYPTED]"
  101. try:
  102. aesgcm = AESGCM(key)
  103. plaintext = aesgcm.decrypt(nonce, ciphertext, None)
  104. return plaintext.decode("utf-8")
  105. except Exception:
  106. return "[ENCRYPTED]"
  107. def serialize_milvus_result(data):
  108. """将 Milvus 返回的数据转换为可序列化的字典"""
  109. # 基本类型直接返回
  110. if data is None or isinstance(data, (str, int, float, bool)):
  111. return data
  112. # 字典类型递归处理
  113. if isinstance(data, dict):
  114. return {k: serialize_milvus_result(v) for k, v in data.items()}
  115. # 列表/元组类型递归处理
  116. if isinstance(data, (list, tuple)):
  117. return [serialize_milvus_result(item) for item in data]
  118. # 尝试转换为字典(对于有 to_dict 方法的对象)
  119. if hasattr(data, 'to_dict') and callable(getattr(data, 'to_dict')):
  120. try:
  121. return serialize_milvus_result(data.to_dict())
  122. except:
  123. pass
  124. # 尝试转换为列表(对于可迭代对象,如 RepeatedScalarContainer)
  125. if hasattr(data, '__iter__') and not isinstance(data, (str, bytes, dict)):
  126. try:
  127. # 强制转换为列表并递归处理
  128. result = []
  129. for item in data:
  130. result.append(serialize_milvus_result(item))
  131. return result
  132. except:
  133. pass
  134. # 尝试获取对象的属性字典
  135. if hasattr(data, '__dict__'):
  136. try:
  137. return serialize_milvus_result(vars(data))
  138. except:
  139. pass
  140. # 最后的 fallback:对于无法处理的类型,返回 None 而不是字符串表示
  141. # 这样可以避免产生无法序列化的字符串
  142. return None
  143. def init_db():
  144. """初始化 SQLite(仅用于 resources)"""
  145. conn = get_db()
  146. conn.execute("""
  147. CREATE TABLE IF NOT EXISTS experiences (
  148. id INTEGER PRIMARY KEY AUTOINCREMENT,
  149. name TEXT NOT NULL,
  150. url TEXT DEFAULT '',
  151. category TEXT DEFAULT '',
  152. task TEXT NOT NULL,
  153. score INTEGER CHECK(score BETWEEN 1 AND 5),
  154. outcome TEXT DEFAULT '',
  155. tips TEXT DEFAULT '',
  156. content_id TEXT DEFAULT '',
  157. submitted_by TEXT DEFAULT '',
  158. created_at TEXT NOT NULL
  159. )
  160. """)
  161. conn.execute("CREATE INDEX IF NOT EXISTS idx_name ON experiences(name)")
  162. conn.execute("""
  163. CREATE TABLE IF NOT EXISTS resources (
  164. id TEXT PRIMARY KEY,
  165. title TEXT DEFAULT '',
  166. body TEXT NOT NULL,
  167. secure_body TEXT DEFAULT '',
  168. content_type TEXT DEFAULT 'text',
  169. metadata TEXT DEFAULT '{}',
  170. sort_order INTEGER DEFAULT 0,
  171. submitted_by TEXT DEFAULT '',
  172. created_at TEXT NOT NULL,
  173. updated_at TEXT DEFAULT ''
  174. )
  175. """)
  176. conn.commit()
  177. conn.close()
  178. # --- Models ---
  179. class ResourceIn(BaseModel):
  180. id: str
  181. title: str = ""
  182. body: str
  183. secure_body: str = ""
  184. content_type: str = "text" # text|code|credential|cookie
  185. metadata: dict = {}
  186. sort_order: int = 0
  187. submitted_by: str = ""
  188. class ResourcePatchIn(BaseModel):
  189. """PATCH /api/resource/{id} 请求体"""
  190. title: Optional[str] = None
  191. body: Optional[str] = None
  192. secure_body: Optional[str] = None
  193. content_type: Optional[str] = None
  194. metadata: Optional[dict] = None
  195. # Knowledge Models
  196. class KnowledgeIn(BaseModel):
  197. task: str
  198. content: str
  199. types: list[str] = ["strategy"]
  200. tags: dict = {}
  201. scopes: list[str] = ["org:cybertogether"]
  202. owner: str = ""
  203. message_id: str = ""
  204. resource_ids: list[str] = []
  205. source: dict = {} # {name, category, urls, agent_id, submitted_by, timestamp}
  206. eval: dict = {} # {score, helpful, harmful, confidence}
  207. class KnowledgeOut(BaseModel):
  208. id: str
  209. message_id: str
  210. types: list[str]
  211. task: str
  212. tags: dict
  213. scopes: list[str]
  214. owner: str
  215. content: str
  216. resource_ids: list[str]
  217. source: dict
  218. eval: dict
  219. created_at: str
  220. updated_at: str
  221. class KnowledgeUpdateIn(BaseModel):
  222. add_helpful_case: Optional[dict] = None
  223. add_harmful_case: Optional[dict] = None
  224. update_score: Optional[int] = Field(default=None, ge=1, le=5)
  225. evolve_feedback: Optional[str] = None
  226. class KnowledgePatchIn(BaseModel):
  227. """PATCH /api/knowledge/{id} 请求体(直接字段编辑)"""
  228. task: Optional[str] = None
  229. content: Optional[str] = None
  230. types: Optional[list[str]] = None
  231. tags: Optional[dict] = None
  232. scopes: Optional[list[str]] = None
  233. owner: Optional[str] = None
  234. class MessageExtractIn(BaseModel):
  235. """POST /api/extract 请求体(消息历史提取)"""
  236. messages: list[dict] # [{role: str, content: str}, ...]
  237. agent_id: str = "unknown"
  238. submitted_by: str # 必填,作为 owner
  239. session_key: str = ""
  240. class KnowledgeBatchUpdateIn(BaseModel):
  241. feedback_list: list[dict]
  242. class KnowledgeSearchResponse(BaseModel):
  243. results: list[dict]
  244. count: int
  245. class ResourceNode(BaseModel):
  246. id: str
  247. title: str
  248. class ResourceOut(BaseModel):
  249. id: str
  250. title: str
  251. body: str
  252. secure_body: str = ""
  253. content_type: str = "text"
  254. metadata: dict = {}
  255. toc: Optional[ResourceNode] = None
  256. children: list[ResourceNode]
  257. prev: Optional[ResourceNode] = None
  258. next: Optional[ResourceNode] = None
  259. # --- App ---
  260. @asynccontextmanager
  261. async def lifespan(app: FastAPI):
  262. global milvus_store
  263. # 初始化 SQLite(resources)
  264. init_db()
  265. # 初始化 Milvus Lite(knowledge)
  266. milvus_store = MilvusStore(data_dir=str(MILVUS_DATA_DIR))
  267. yield
  268. # 清理(Milvus Lite 会自动处理)
  269. app = FastAPI(title=BRAND_NAME, lifespan=lifespan)
  270. # --- Knowledge API ---
  271. @app.post("/api/resource", status_code=201)
  272. def submit_resource(resource: ResourceIn):
  273. conn = get_db()
  274. try:
  275. now = datetime.now(timezone.utc).isoformat()
  276. # 加密敏感内容
  277. encrypted_secure_body = encrypt_content(resource.id, resource.secure_body)
  278. conn.execute(
  279. "INSERT OR REPLACE INTO resources"
  280. "(id, title, body, secure_body, content_type, metadata, sort_order, submitted_by, created_at, updated_at)"
  281. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  282. (
  283. resource.id,
  284. resource.title,
  285. resource.body,
  286. encrypted_secure_body,
  287. resource.content_type,
  288. json.dumps(resource.metadata),
  289. resource.sort_order,
  290. resource.submitted_by,
  291. now,
  292. now,
  293. ),
  294. )
  295. conn.commit()
  296. return {"status": "ok", "id": resource.id}
  297. finally:
  298. conn.close()
  299. @app.get("/api/resource/{resource_id:path}", response_model=ResourceOut)
  300. def get_resource(resource_id: str, x_org_key: Optional[str] = Header(None)):
  301. conn = get_db()
  302. try:
  303. row = conn.execute(
  304. "SELECT id, title, body, secure_body, content_type, metadata, sort_order FROM resources WHERE id = ?",
  305. (resource_id,),
  306. ).fetchone()
  307. if not row:
  308. raise HTTPException(status_code=404, detail=f"Resource not found: {resource_id}")
  309. # 解密敏感内容
  310. secure_body = decrypt_content(resource_id, row["secure_body"] or "", x_org_key)
  311. # 解析metadata
  312. metadata = json.loads(row["metadata"] or "{}")
  313. # 计算导航上下文
  314. root_id = resource_id.split("/")[0] if "/" in resource_id else resource_id
  315. # TOC (根节点)
  316. toc = None
  317. if "/" in resource_id:
  318. toc_row = conn.execute(
  319. "SELECT id, title FROM resources WHERE id = ?",
  320. (root_id,),
  321. ).fetchone()
  322. if toc_row:
  323. toc = ResourceNode(id=toc_row["id"], title=toc_row["title"])
  324. # Children (子节点)
  325. children = []
  326. children_rows = conn.execute(
  327. "SELECT id, title FROM resources WHERE id LIKE ? AND id != ? ORDER BY sort_order",
  328. (f"{resource_id}/%", resource_id),
  329. ).fetchall()
  330. children = [ResourceNode(id=r["id"], title=r["title"]) for r in children_rows]
  331. # Prev/Next (同级节点)
  332. prev_node = None
  333. next_node = None
  334. if "/" in resource_id:
  335. siblings = conn.execute(
  336. "SELECT id, title, sort_order FROM resources WHERE id LIKE ? AND id NOT LIKE ? ORDER BY sort_order",
  337. (f"{root_id}/%", f"{root_id}/%/%"),
  338. ).fetchall()
  339. for i, sib in enumerate(siblings):
  340. if sib["id"] == resource_id:
  341. if i > 0:
  342. prev_node = ResourceNode(id=siblings[i-1]["id"], title=siblings[i-1]["title"])
  343. if i < len(siblings) - 1:
  344. next_node = ResourceNode(id=siblings[i+1]["id"], title=siblings[i+1]["title"])
  345. break
  346. return ResourceOut(
  347. id=row["id"],
  348. title=row["title"],
  349. body=row["body"],
  350. secure_body=secure_body,
  351. content_type=row["content_type"],
  352. metadata=metadata,
  353. toc=toc,
  354. children=children,
  355. prev=prev_node,
  356. next=next_node,
  357. )
  358. finally:
  359. conn.close()
  360. @app.patch("/api/resource/{resource_id:path}")
  361. def patch_resource(resource_id: str, patch: ResourcePatchIn):
  362. """更新resource字段"""
  363. conn = get_db()
  364. try:
  365. # 检查是否存在
  366. row = conn.execute("SELECT id FROM resources WHERE id = ?", (resource_id,)).fetchone()
  367. if not row:
  368. raise HTTPException(status_code=404, detail=f"Resource not found: {resource_id}")
  369. # 构建更新语句
  370. updates = []
  371. params = []
  372. if patch.title is not None:
  373. updates.append("title = ?")
  374. params.append(patch.title)
  375. if patch.body is not None:
  376. updates.append("body = ?")
  377. params.append(patch.body)
  378. if patch.secure_body is not None:
  379. encrypted = encrypt_content(resource_id, patch.secure_body)
  380. updates.append("secure_body = ?")
  381. params.append(encrypted)
  382. if patch.content_type is not None:
  383. updates.append("content_type = ?")
  384. params.append(patch.content_type)
  385. if patch.metadata is not None:
  386. updates.append("metadata = ?")
  387. params.append(json.dumps(patch.metadata))
  388. if not updates:
  389. return {"status": "ok", "message": "No fields to update"}
  390. # 添加updated_at
  391. updates.append("updated_at = ?")
  392. params.append(datetime.now(timezone.utc).isoformat())
  393. # 执行更新
  394. params.append(resource_id)
  395. sql = f"UPDATE resources SET {', '.join(updates)} WHERE id = ?"
  396. conn.execute(sql, params)
  397. conn.commit()
  398. return {"status": "ok", "id": resource_id}
  399. finally:
  400. conn.close()
  401. @app.get("/api/resource")
  402. def list_resources(
  403. content_type: Optional[str] = Query(None),
  404. limit: int = Query(100, ge=1, le=1000)
  405. ):
  406. """列出所有resource"""
  407. conn = get_db()
  408. try:
  409. sql = "SELECT id, title, content_type, metadata, created_at FROM resources"
  410. params = []
  411. if content_type:
  412. sql += " WHERE content_type = ?"
  413. params.append(content_type)
  414. sql += " ORDER BY id LIMIT ?"
  415. params.append(limit)
  416. rows = conn.execute(sql, params).fetchall()
  417. results = []
  418. for row in rows:
  419. results.append({
  420. "id": row["id"],
  421. "title": row["title"],
  422. "content_type": row["content_type"],
  423. "metadata": json.loads(row["metadata"] or "{}"),
  424. "created_at": row["created_at"],
  425. })
  426. return {"results": results, "count": len(results)}
  427. finally:
  428. conn.close()
  429. # --- Knowledge API ---
  430. # ===== Knowledge API =====
  431. async def _llm_rerank(query: str, candidates: list[dict], top_k: int) -> list[str]:
  432. """
  433. 使用 LLM 对候选知识进行精排
  434. Args:
  435. query: 查询文本
  436. candidates: 候选知识列表
  437. top_k: 返回数量
  438. Returns:
  439. 排序后的知识 ID 列表
  440. """
  441. if not candidates:
  442. return []
  443. # 构造 prompt
  444. candidates_text = "\n".join([
  445. f"[{i+1}] ID: {c['id']}\nTask: {c['task']}\nContent: {c['content'][:200]}..."
  446. for i, c in enumerate(candidates)
  447. ])
  448. prompt = f"""你是知识检索专家。根据用户查询,从候选知识中选出最相关的 {top_k} 条。
  449. 用户查询:"{query}"
  450. 候选知识:
  451. {candidates_text}
  452. 请输出最相关的 {top_k} 个知识 ID,按相关性从高到低排序,用逗号分隔。
  453. 只输出 ID,不要其他内容。"""
  454. try:
  455. response = await openrouter_llm_call(
  456. messages=[{"role": "user", "content": prompt}],
  457. model="google/gemini-2.5-flash-lite"
  458. )
  459. content = response.get("content", "").strip()
  460. # 解析 ID 列表
  461. selected_ids = [
  462. idx.strip()
  463. for idx in re.split(r'[,\s]+', content)
  464. if idx.strip().startswith(("knowledge-", "research-"))
  465. ]
  466. return selected_ids[:top_k]
  467. except Exception as e:
  468. print(f"[LLM Rerank] 失败: {e}")
  469. return []
  470. @app.get("/api/knowledge/search")
  471. async def search_knowledge_api(
  472. q: str = Query(..., description="查询文本"),
  473. top_k: int = Query(default=5, ge=1, le=20),
  474. min_score: int = Query(default=3, ge=1, le=5),
  475. types: Optional[str] = None,
  476. owner: Optional[str] = None
  477. ):
  478. """检索知识(向量召回 + LLM 精排)"""
  479. try:
  480. # 1. 生成查询向量
  481. query_embedding = await get_embedding(q)
  482. # 2. 构建过滤表达式
  483. filters = []
  484. if types:
  485. type_list = [t.strip() for t in types.split(',') if t.strip()]
  486. for t in type_list:
  487. filters.append(f'array_contains(types, "{t}")')
  488. if owner:
  489. filters.append(f'owner == "{owner}"')
  490. # 添加 min_score 过滤
  491. filters.append(f'eval["score"] >= {min_score}')
  492. filter_expr = ' and '.join(filters) if filters else None
  493. # 3. 向量召回(3*k 个候选)
  494. recall_limit = top_k * 3
  495. candidates = milvus_store.search(
  496. query_embedding=query_embedding,
  497. filters=filter_expr,
  498. limit=recall_limit
  499. )
  500. if not candidates:
  501. return {"results": [], "count": 0, "reranked": False}
  502. # 转换为可序列化的格式
  503. serialized_candidates = [serialize_milvus_result(c) for c in candidates]
  504. # 4. LLM 精排
  505. reranked_ids = await _llm_rerank(q, serialized_candidates, top_k)
  506. if reranked_ids:
  507. # 按 LLM 排序返回
  508. id_to_candidate = {c["id"]: c for c in serialized_candidates}
  509. results = [id_to_candidate[id] for id in reranked_ids if id in id_to_candidate]
  510. return {"results": results, "count": len(results), "reranked": True}
  511. else:
  512. # Fallback:直接返回向量召回的 top k
  513. print(f"[Knowledge Search] LLM 精排失败,fallback 到向量 top-{top_k}")
  514. return {"results": serialized_candidates[:top_k], "count": len(serialized_candidates[:top_k]), "reranked": False}
  515. except Exception as e:
  516. print(f"[Knowledge Search] 错误: {e}")
  517. raise HTTPException(status_code=500, detail=str(e))
  518. @app.post("/api/knowledge", status_code=201)
  519. async def save_knowledge(knowledge: KnowledgeIn):
  520. """保存新知识"""
  521. try:
  522. # 生成 ID
  523. timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
  524. random_suffix = uuid.uuid4().hex[:4]
  525. knowledge_id = f"knowledge-{timestamp}-{random_suffix}"
  526. now = int(time.time())
  527. # 设置默认值
  528. owner = knowledge.owner or f"agent:{knowledge.source.get('agent_id', 'unknown')}"
  529. # 准备 source
  530. source = {
  531. "name": knowledge.source.get("name", ""),
  532. "category": knowledge.source.get("category", ""),
  533. "urls": knowledge.source.get("urls", []),
  534. "agent_id": knowledge.source.get("agent_id", "unknown"),
  535. "submitted_by": knowledge.source.get("submitted_by", ""),
  536. "timestamp": datetime.now(timezone.utc).isoformat(),
  537. "message_id": knowledge.message_id
  538. }
  539. # 准备 eval
  540. eval_data = {
  541. "score": knowledge.eval.get("score", 3),
  542. "helpful": knowledge.eval.get("helpful", 1),
  543. "harmful": knowledge.eval.get("harmful", 0),
  544. "confidence": knowledge.eval.get("confidence", 0.5),
  545. "helpful_history": [],
  546. "harmful_history": []
  547. }
  548. # 生成向量(只基于 task,因为搜索时用户描述的是任务场景)
  549. embedding = await get_embedding(knowledge.task)
  550. # 准备插入数据
  551. insert_data = {
  552. "id": knowledge_id,
  553. "embedding": embedding,
  554. "message_id": knowledge.message_id,
  555. "task": knowledge.task,
  556. "content": knowledge.content,
  557. "types": knowledge.types,
  558. "tags": knowledge.tags,
  559. "scopes": knowledge.scopes,
  560. "owner": owner,
  561. "resource_ids": knowledge.resource_ids,
  562. "source": source,
  563. "eval": eval_data,
  564. "created_at": now,
  565. "updated_at": now,
  566. }
  567. print(f"[Save Knowledge] 插入数据: {json.dumps({k: v for k, v in insert_data.items() if k != 'embedding'}, ensure_ascii=False)}")
  568. # 插入 Milvus
  569. milvus_store.insert(insert_data)
  570. return {"status": "ok", "knowledge_id": knowledge_id}
  571. except Exception as e:
  572. print(f"[Save Knowledge] 错误: {e}")
  573. raise HTTPException(status_code=500, detail=str(e))
  574. @app.get("/api/knowledge")
  575. def list_knowledge(
  576. limit: int = Query(default=100, ge=1, le=1000),
  577. types: Optional[str] = None,
  578. scopes: Optional[str] = None,
  579. owner: Optional[str] = None,
  580. tags: Optional[str] = None
  581. ):
  582. """列出知识(支持后端筛选)"""
  583. try:
  584. # 构建过滤表达式
  585. filters = []
  586. # types 支持多个,用 AND 连接(交集:必须同时包含所有选中的type)
  587. if types:
  588. type_list = [t.strip() for t in types.split(',') if t.strip()]
  589. for t in type_list:
  590. filters.append(f'array_contains(types, "{t}")')
  591. if scopes:
  592. filters.append(f'array_contains(scopes, "{scopes}")')
  593. if owner:
  594. filters.append(f'owner like "%{owner}%"')
  595. # tags 支持多个,用 AND 连接(交集:必须同时包含所有选中的tag)
  596. if tags:
  597. tag_list = [t.strip() for t in tags.split(',') if t.strip()]
  598. for t in tag_list:
  599. filters.append(f'json_contains(tags, \'"{t}"\')')
  600. # 如果没有过滤条件,查询所有
  601. filter_expr = ' and '.join(filters) if filters else 'id != ""'
  602. # 查询 Milvus
  603. results = milvus_store.query(filter_expr, limit=limit)
  604. # 转换为可序列化的格式
  605. serialized_results = [serialize_milvus_result(r) for r in results]
  606. return {"results": serialized_results, "count": len(serialized_results)}
  607. except Exception as e:
  608. print(f"[List Knowledge] 错误: {e}")
  609. raise HTTPException(status_code=500, detail=str(e))
  610. @app.get("/api/knowledge/meta/tags")
  611. def get_all_tags():
  612. """获取所有已有的 tags"""
  613. try:
  614. # 查询所有知识
  615. results = milvus_store.query('id != ""', limit=10000)
  616. all_tags = set()
  617. for item in results:
  618. # 转换为标准字典
  619. serialized_item = serialize_milvus_result(item)
  620. tags_dict = serialized_item.get("tags", {})
  621. if isinstance(tags_dict, dict):
  622. for key in tags_dict.keys():
  623. all_tags.add(key)
  624. return {"tags": sorted(list(all_tags))}
  625. except Exception as e:
  626. print(f"[Get Tags] 错误: {e}")
  627. raise HTTPException(status_code=500, detail=str(e))
  628. @app.get("/api/knowledge/{knowledge_id}")
  629. def get_knowledge(knowledge_id: str):
  630. """获取单条知识"""
  631. try:
  632. result = milvus_store.get_by_id(knowledge_id)
  633. if not result:
  634. raise HTTPException(status_code=404, detail=f"Knowledge not found: {knowledge_id}")
  635. return serialize_milvus_result(result)
  636. except HTTPException:
  637. raise
  638. except Exception as e:
  639. print(f"[Get Knowledge] 错误: {e}")
  640. raise HTTPException(status_code=500, detail=str(e))
  641. async def _evolve_knowledge_with_llm(old_content: str, feedback: str) -> str:
  642. """使用 LLM 进行知识进化重写"""
  643. prompt = f"""你是一个 AI Agent 知识库管理员。请根据反馈建议,对现有的知识内容进行重写进化。
  644. 【原知识内容】:
  645. {old_content}
  646. 【实战反馈建议】:
  647. {feedback}
  648. 【重写要求】:
  649. 1. 融合知识:将反馈中的避坑指南、新参数或修正后的选择逻辑融入原知识,使其更具通用性和准确性。
  650. 2. 保持结构:如果原内容有特定格式(如 Markdown、代码示例等),请保持该格式。
  651. 3. 语言:简洁直接,使用中文。
  652. 4. 禁止:严禁输出任何开场白、解释语或额外的 Markdown 标题,直接返回重写后的正文。
  653. """
  654. try:
  655. response = await openrouter_llm_call(
  656. messages=[{"role": "user", "content": prompt}],
  657. model="google/gemini-2.5-flash-lite"
  658. )
  659. evolved = response.get("content", "").strip()
  660. if len(evolved) < 5:
  661. raise ValueError("LLM output too short")
  662. return evolved
  663. except Exception as e:
  664. print(f"知识进化失败,采用追加模式回退: {e}")
  665. return f"{old_content}\n\n---\n[Update {datetime.now().strftime('%Y-%m-%d')}]: {feedback}"
  666. @app.put("/api/knowledge/{knowledge_id}")
  667. async def update_knowledge(knowledge_id: str, update: KnowledgeUpdateIn):
  668. """更新知识评估,支持知识进化"""
  669. try:
  670. # 获取现有知识
  671. existing = milvus_store.get_by_id(knowledge_id)
  672. if not existing:
  673. raise HTTPException(status_code=404, detail=f"Knowledge not found: {knowledge_id}")
  674. eval_data = existing.get("eval", {})
  675. # 更新评分
  676. if update.update_score is not None:
  677. eval_data["score"] = update.update_score
  678. # 添加有效案例
  679. if update.add_helpful_case:
  680. eval_data["helpful"] = eval_data.get("helpful", 0) + 1
  681. if "helpful_history" not in eval_data:
  682. eval_data["helpful_history"] = []
  683. eval_data["helpful_history"].append(update.add_helpful_case)
  684. # 添加有害案例
  685. if update.add_harmful_case:
  686. eval_data["harmful"] = eval_data.get("harmful", 0) + 1
  687. if "harmful_history" not in eval_data:
  688. eval_data["harmful_history"] = []
  689. eval_data["harmful_history"].append(update.add_harmful_case)
  690. # 知识进化
  691. content = existing["content"]
  692. need_reembed = False
  693. if update.evolve_feedback:
  694. content = await _evolve_knowledge_with_llm(content, update.evolve_feedback)
  695. eval_data["helpful"] = eval_data.get("helpful", 0) + 1
  696. need_reembed = True
  697. # 准备更新数据
  698. updates = {
  699. "content": content,
  700. "eval": eval_data,
  701. }
  702. # 如果内容变化,重新生成向量
  703. if need_reembed:
  704. embedding = await get_embedding(existing['task'])
  705. updates["embedding"] = embedding
  706. # 更新 Milvus
  707. milvus_store.update(knowledge_id, updates)
  708. return {"status": "ok", "knowledge_id": knowledge_id}
  709. except HTTPException:
  710. raise
  711. except Exception as e:
  712. print(f"[Update Knowledge] 错误: {e}")
  713. raise HTTPException(status_code=500, detail=str(e))
  714. @app.patch("/api/knowledge/{knowledge_id}")
  715. async def patch_knowledge(knowledge_id: str, patch: KnowledgePatchIn):
  716. """直接编辑知识字段"""
  717. try:
  718. # 获取现有知识
  719. existing = milvus_store.get_by_id(knowledge_id)
  720. if not existing:
  721. raise HTTPException(status_code=404, detail=f"Knowledge not found: {knowledge_id}")
  722. updates = {}
  723. need_reembed = False
  724. if patch.task is not None:
  725. updates["task"] = patch.task
  726. need_reembed = True
  727. if patch.content is not None:
  728. updates["content"] = patch.content
  729. # content 变化不需要重新生成 embedding(只基于 task)
  730. if patch.types is not None:
  731. updates["types"] = patch.types
  732. if patch.tags is not None:
  733. updates["tags"] = patch.tags
  734. if patch.scopes is not None:
  735. updates["scopes"] = patch.scopes
  736. if patch.owner is not None:
  737. updates["owner"] = patch.owner
  738. if not updates:
  739. return {"status": "ok", "knowledge_id": knowledge_id}
  740. # 如果 task 变化,重新生成向量
  741. if need_reembed:
  742. task = updates.get("task", existing["task"])
  743. embedding = await get_embedding(task)
  744. updates["embedding"] = embedding
  745. # 更新 Milvus
  746. milvus_store.update(knowledge_id, updates)
  747. return {"status": "ok", "knowledge_id": knowledge_id}
  748. except HTTPException:
  749. raise
  750. except Exception as e:
  751. print(f"[Patch Knowledge] 错误: {e}")
  752. raise HTTPException(status_code=500, detail=str(e))
  753. @app.post("/api/knowledge/batch_update")
  754. async def batch_update_knowledge(batch: KnowledgeBatchUpdateIn):
  755. """批量反馈知识有效性"""
  756. if not batch.feedback_list:
  757. return {"status": "ok", "updated": 0}
  758. try:
  759. # 先处理无需进化的,收集需要进化的
  760. evolution_tasks = [] # [(knowledge_id, old_content, feedback, eval_data)]
  761. simple_updates = [] # [(knowledge_id, is_effective, eval_data)]
  762. for item in batch.feedback_list:
  763. knowledge_id = item.get("knowledge_id")
  764. is_effective = item.get("is_effective")
  765. feedback = item.get("feedback", "")
  766. if not knowledge_id:
  767. continue
  768. existing = milvus_store.get_by_id(knowledge_id)
  769. if not existing:
  770. continue
  771. eval_data = existing.get("eval", {})
  772. if is_effective and feedback:
  773. evolution_tasks.append((knowledge_id, existing["content"], feedback, eval_data, existing["task"]))
  774. else:
  775. simple_updates.append((knowledge_id, is_effective, eval_data))
  776. # 执行简单更新
  777. for knowledge_id, is_effective, eval_data in simple_updates:
  778. if is_effective:
  779. eval_data["helpful"] = eval_data.get("helpful", 0) + 1
  780. else:
  781. eval_data["harmful"] = eval_data.get("harmful", 0) + 1
  782. milvus_store.update(knowledge_id, {"eval": eval_data})
  783. # 并发执行知识进化
  784. if evolution_tasks:
  785. print(f"🧬 并发处理 {len(evolution_tasks)} 条知识进化...")
  786. evolved_results = await asyncio.gather(
  787. *[_evolve_knowledge_with_llm(old, fb) for _, old, fb, _, _ in evolution_tasks]
  788. )
  789. for (knowledge_id, _, _, eval_data, task), evolved_content in zip(evolution_tasks, evolved_results):
  790. eval_data["helpful"] = eval_data.get("helpful", 0) + 1
  791. # 重新生成向量(只基于 task)
  792. embedding = await get_embedding(task)
  793. milvus_store.update(knowledge_id, {
  794. "content": evolved_content,
  795. "eval": eval_data,
  796. "embedding": embedding
  797. })
  798. return {"status": "ok", "updated": len(simple_updates) + len(evolution_tasks)}
  799. except Exception as e:
  800. print(f"[Batch Update] 错误: {e}")
  801. raise HTTPException(status_code=500, detail=str(e))
  802. @app.post("/api/knowledge/slim")
  803. async def slim_knowledge(model: str = "google/gemini-2.5-flash-lite"):
  804. """知识库瘦身:合并语义相似知识"""
  805. try:
  806. # 获取所有知识
  807. all_knowledge = milvus_store.query('id != ""', limit=10000)
  808. # 转换为可序列化的格式
  809. all_knowledge = [serialize_milvus_result(item) for item in all_knowledge]
  810. if len(all_knowledge) < 2:
  811. return {"status": "ok", "message": f"知识库仅有 {len(all_knowledge)} 条,无需瘦身"}
  812. # 构造发给大模型的内容
  813. entries_text = ""
  814. for item in all_knowledge:
  815. eval_data = item.get("eval", {})
  816. types = item.get("types", [])
  817. entries_text += f"[ID: {item['id']}] [Types: {','.join(types)}] "
  818. entries_text += f"[Helpful: {eval_data.get('helpful', 0)}, Harmful: {eval_data.get('harmful', 0)}] [Score: {eval_data.get('score', 3)}]\n"
  819. entries_text += f"Task: {item['task']}\n"
  820. entries_text += f"Content: {item['content'][:200]}...\n\n"
  821. prompt = f"""你是一个 AI Agent 知识库管理员。以下是当前知识库的全部条目,请执行瘦身操作:
  822. 【任务】:
  823. 1. 识别语义高度相似或重复的知识,将它们合并为一条更精炼、更通用的知识。
  824. 2. 合并时保留 helpful 最高的那条的 ID(helpful 取各条之和)。
  825. 3. 对于独立的、无重复的知识,保持原样不动。
  826. 【当前知识库】:
  827. {entries_text}
  828. 【输出格式要求】:
  829. 严格按以下格式输出每条知识,条目之间用 === 分隔:
  830. ID: <保留的id>
  831. TYPES: <逗号分隔的type列表>
  832. HELPFUL: <合并后的helpful计数>
  833. HARMFUL: <合并后的harmful计数>
  834. SCORE: <评分>
  835. TASK: <任务描述>
  836. CONTENT: <合并后的知识内容>
  837. ===
  838. 最后输出合并报告:
  839. REPORT: 原有 X 条,合并后 Y 条,精简了 Z 条。
  840. 禁止输出任何开场白或解释。"""
  841. print(f"\n[知识瘦身] 正在调用 {model} 分析 {len(all_knowledge)} 条知识...")
  842. response = await openrouter_llm_call(
  843. messages=[{"role": "user", "content": prompt}],
  844. model=model
  845. )
  846. content = response.get("content", "").strip()
  847. if not content:
  848. raise HTTPException(status_code=500, detail="LLM 返回为空")
  849. # 解析大模型输出
  850. report_line = ""
  851. new_entries = []
  852. blocks = [b.strip() for b in content.split("===") if b.strip()]
  853. for block in blocks:
  854. if block.startswith("REPORT:"):
  855. report_line = block
  856. continue
  857. lines = block.split("\n")
  858. kid, types, helpful, harmful, score, task, content_lines = None, [], 0, 0, 3, "", []
  859. current_field = None
  860. for line in lines:
  861. if line.startswith("ID:"):
  862. kid = line[3:].strip()
  863. current_field = None
  864. elif line.startswith("TYPES:"):
  865. types_str = line[6:].strip()
  866. types = [t.strip() for t in types_str.split(",") if t.strip()]
  867. current_field = None
  868. elif line.startswith("HELPFUL:"):
  869. try:
  870. helpful = int(line[8:].strip())
  871. except Exception:
  872. helpful = 0
  873. current_field = None
  874. elif line.startswith("HARMFUL:"):
  875. try:
  876. harmful = int(line[8:].strip())
  877. except Exception:
  878. harmful = 0
  879. current_field = None
  880. elif line.startswith("SCORE:"):
  881. try:
  882. score = int(line[6:].strip())
  883. except Exception:
  884. score = 3
  885. current_field = None
  886. elif line.startswith("TASK:"):
  887. task = line[5:].strip()
  888. current_field = "task"
  889. elif line.startswith("CONTENT:"):
  890. content_lines.append(line[8:].strip())
  891. current_field = "content"
  892. elif current_field == "task":
  893. task += "\n" + line
  894. elif current_field == "content":
  895. content_lines.append(line)
  896. if kid and content_lines:
  897. new_entries.append({
  898. "id": kid,
  899. "types": types if types else ["strategy"],
  900. "helpful": helpful,
  901. "harmful": harmful,
  902. "score": score,
  903. "task": task.strip(),
  904. "content": "\n".join(content_lines).strip()
  905. })
  906. if not new_entries:
  907. raise HTTPException(status_code=500, detail="解析大模型输出失败")
  908. # 生成向量并重建知识库
  909. print(f"[知识瘦身] 正在为 {len(new_entries)} 条知识生成向量...")
  910. # 批量生成向量(只基于 task)
  911. texts = [e['task'] for e in new_entries]
  912. embeddings = await get_embeddings_batch(texts)
  913. # 清空并重建
  914. now = int(time.time())
  915. milvus_store.drop_collection()
  916. milvus_store._init_collection()
  917. knowledge_list = []
  918. for e, embedding in zip(new_entries, embeddings):
  919. eval_data = {
  920. "score": e["score"],
  921. "helpful": e["helpful"],
  922. "harmful": e["harmful"],
  923. "confidence": 0.9,
  924. "helpful_history": [],
  925. "harmful_history": []
  926. }
  927. source = {
  928. "name": "slim",
  929. "category": "exp",
  930. "urls": [],
  931. "agent_id": "slim",
  932. "submitted_by": "system",
  933. "timestamp": datetime.now(timezone.utc).isoformat()
  934. }
  935. knowledge_list.append({
  936. "id": e["id"],
  937. "embedding": embedding,
  938. "message_id": "",
  939. "task": e["task"],
  940. "content": e["content"],
  941. "types": e["types"],
  942. "tags": {},
  943. "scopes": ["org:cybertogether"],
  944. "owner": "agent:slim",
  945. "resource_ids": [],
  946. "source": source,
  947. "eval": eval_data,
  948. "created_at": now,
  949. "updated_at": now
  950. })
  951. milvus_store.insert_batch(knowledge_list)
  952. result_msg = f"瘦身完成:{len(all_knowledge)} → {len(new_entries)} 条知识"
  953. if report_line:
  954. result_msg += f"\n{report_line}"
  955. print(f"[知识瘦身] {result_msg}")
  956. return {"status": "ok", "before": len(all_knowledge), "after": len(new_entries), "report": report_line}
  957. except HTTPException:
  958. raise
  959. except Exception as e:
  960. print(f"[Slim Knowledge] 错误: {e}")
  961. raise HTTPException(status_code=500, detail=str(e))
  962. @app.post("/api/extract")
  963. async def extract_knowledge_from_messages(extract_req: MessageExtractIn):
  964. """从消息历史中提取知识(LLM 分析)"""
  965. if not extract_req.submitted_by:
  966. raise HTTPException(status_code=400, detail="submitted_by is required")
  967. messages = extract_req.messages
  968. if not messages or len(messages) == 0:
  969. return {"status": "ok", "extracted_count": 0, "knowledge_ids": []}
  970. # 构造消息历史文本
  971. messages_text = ""
  972. for msg in messages:
  973. role = msg.get("role", "unknown")
  974. content = msg.get("content", "")
  975. messages_text += f"[{role}]: {content}\n\n"
  976. # LLM 提取知识
  977. prompt = f"""你是一个知识提取专家。请从以下 Agent 对话历史中提取有价值的知识。
  978. 【对话历史】:
  979. {messages_text}
  980. 【提取要求】:
  981. 1. 识别对话中的关键知识点(工具使用经验、问题解决方案、最佳实践、踩坑经验等)
  982. 2. 每条知识必须包含:
  983. - task: 任务场景描述(在什么情况下,要完成什么目标)
  984. - content: 核心知识内容(具体可操作的方法、注意事项)
  985. - types: 知识类型(从 strategy/tool/user_profile/usecase/definition/plan 中选择)
  986. - score: 评分 1-5(根据知识的价值和可操作性)
  987. 3. 只提取有实际价值的知识,不要提取泛泛而谈的内容,一次就成功或比较简单的经验就不要记录了。
  988. 4. 如果没有值得提取的知识,返回空列表
  989. 【输出格式】:
  990. 严格按以下 JSON 格式输出,每条知识之间用逗号分隔:
  991. [
  992. {{
  993. "task": "任务场景描述",
  994. "content": "核心知识内容",
  995. "types": ["strategy"],
  996. "score": 4
  997. }},
  998. {{
  999. "task": "另一个任务场景",
  1000. "content": "另一个知识内容",
  1001. "types": ["tool"],
  1002. "score": 5
  1003. }}
  1004. ]
  1005. 如果没有知识,输出: []
  1006. **注意**:只记录经过多次尝试、或经过用户指导才成功的知识,一次就成功或比较简单的经验就不要记录了。
  1007. 禁止输出任何解释或额外文本,只输出 JSON 数组。"""
  1008. try:
  1009. print(f"\n[Extract] 正在从 {len(messages)} 条消息中提取知识...")
  1010. response = await openrouter_llm_call(
  1011. messages=[{"role": "user", "content": prompt}],
  1012. model="google/gemini-2.5-flash-lite"
  1013. )
  1014. content = response.get("content", "").strip()
  1015. # 尝试解析 JSON
  1016. # 移除可能的 markdown 代码块标记
  1017. if content.startswith("```json"):
  1018. content = content[7:]
  1019. if content.startswith("```"):
  1020. content = content[3:]
  1021. if content.endswith("```"):
  1022. content = content[:-3]
  1023. content = content.strip()
  1024. extracted_knowledge = json.loads(content)
  1025. if not isinstance(extracted_knowledge, list):
  1026. raise ValueError("LLM output is not a list")
  1027. if not extracted_knowledge:
  1028. return {"status": "ok", "extracted_count": 0, "knowledge_ids": []}
  1029. # 批量生成向量(只基于 task)
  1030. texts = [item.get('task', '') for item in extracted_knowledge]
  1031. embeddings = await get_embeddings_batch(texts)
  1032. # 保存提取的知识
  1033. knowledge_ids = []
  1034. now = int(time.time())
  1035. knowledge_list = []
  1036. for item, embedding in zip(extracted_knowledge, embeddings):
  1037. task = item.get("task", "")
  1038. knowledge_content = item.get("content", "")
  1039. types = item.get("types", ["strategy"])
  1040. score = item.get("score", 3)
  1041. if not task or not knowledge_content:
  1042. continue
  1043. # 生成 ID
  1044. timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
  1045. random_suffix = uuid.uuid4().hex[:4]
  1046. knowledge_id = f"knowledge-{timestamp}-{random_suffix}"
  1047. # 准备数据
  1048. source = {
  1049. "name": "message_extraction",
  1050. "category": "exp",
  1051. "urls": [],
  1052. "agent_id": extract_req.agent_id,
  1053. "submitted_by": extract_req.submitted_by,
  1054. "timestamp": datetime.now(timezone.utc).isoformat(),
  1055. "session_key": extract_req.session_key
  1056. }
  1057. eval_data = {
  1058. "score": score,
  1059. "helpful": 1,
  1060. "harmful": 0,
  1061. "confidence": 0.7,
  1062. "helpful_history": [],
  1063. "harmful_history": []
  1064. }
  1065. knowledge_list.append({
  1066. "id": knowledge_id,
  1067. "embedding": embedding,
  1068. "message_id": "",
  1069. "task": task,
  1070. "content": knowledge_content,
  1071. "types": types,
  1072. "tags": {},
  1073. "scopes": ["org:cybertogether"],
  1074. "owner": extract_req.submitted_by,
  1075. "resource_ids": [],
  1076. "source": source,
  1077. "eval": eval_data,
  1078. "created_at": now,
  1079. "updated_at": now,
  1080. })
  1081. knowledge_ids.append(knowledge_id)
  1082. # 批量插入
  1083. if knowledge_list:
  1084. milvus_store.insert_batch(knowledge_list)
  1085. print(f"[Extract] 成功提取并保存 {len(knowledge_ids)} 条知识")
  1086. return {
  1087. "status": "ok",
  1088. "extracted_count": len(knowledge_ids),
  1089. "knowledge_ids": knowledge_ids
  1090. }
  1091. except json.JSONDecodeError as e:
  1092. print(f"[Extract] JSON 解析失败: {e}")
  1093. print(f"[Extract] LLM 输出: {content[:500]}")
  1094. return {"status": "error", "error": "Failed to parse LLM output", "extracted_count": 0}
  1095. except Exception as e:
  1096. print(f"[Extract] 提取失败: {e}")
  1097. return {"status": "error", "error": str(e), "extracted_count": 0}
  1098. @app.get("/", response_class=HTMLResponse)
  1099. def frontend():
  1100. """KnowHub 管理前端"""
  1101. return """<!DOCTYPE html>
  1102. <html lang="zh-CN">
  1103. <head>
  1104. <meta charset="UTF-8">
  1105. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  1106. <title>KnowHub 管理</title>
  1107. <script src="https://cdn.tailwindcss.com"></script>
  1108. </head>
  1109. <body class="bg-gray-50">
  1110. <div class="container mx-auto px-4 py-8 max-w-7xl">
  1111. <div class="flex justify-between items-center mb-8">
  1112. <h1 class="text-3xl font-bold text-gray-800">KnowHub 全局知识库</h1>
  1113. <button onclick="openAddModal()" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg">
  1114. + 新增知识
  1115. </button>
  1116. </div>
  1117. <!-- 筛选栏 -->
  1118. <div class="bg-white rounded-lg shadow p-6 mb-6">
  1119. <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
  1120. <div>
  1121. <label class="block text-sm font-medium text-gray-700 mb-2">类型 (Types)</label>
  1122. <div class="space-y-2">
  1123. <label class="flex items-center"><input type="checkbox" value="strategy" class="mr-2 type-filter"> Strategy</label>
  1124. <label class="flex items-center"><input type="checkbox" value="tool" class="mr-2 type-filter"> Tool</label>
  1125. <label class="flex items-center"><input type="checkbox" value="user_profile" class="mr-2 type-filter"> User Profile</label>
  1126. <label class="flex items-center"><input type="checkbox" value="usecase" class="mr-2 type-filter"> Usecase</label>
  1127. <label class="flex items-center"><input type="checkbox" value="definition" class="mr-2 type-filter"> Definition</label>
  1128. <label class="flex items-center"><input type="checkbox" value="plan" class="mr-2 type-filter"> Plan</label>
  1129. </div>
  1130. </div>
  1131. <div>
  1132. <label class="block text-sm font-medium text-gray-700 mb-2">Tags</label>
  1133. <div id="tagsFilterContainer" class="space-y-2 max-h-40 overflow-y-auto">
  1134. <p class="text-sm text-gray-500">加载中...</p>
  1135. </div>
  1136. </div>
  1137. <div>
  1138. <label class="block text-sm font-medium text-gray-700 mb-2">Owner</label>
  1139. <input type="text" id="ownerFilter" placeholder="输入 owner" class="w-full border rounded px-3 py-2">
  1140. </div>
  1141. <div>
  1142. <label class="block text-sm font-medium text-gray-700 mb-2">Scopes</label>
  1143. <input type="text" id="scopesFilter" placeholder="输入 scope" class="w-full border rounded px-3 py-2">
  1144. </div>
  1145. </div>
  1146. <button onclick="applyFilters()" class="mt-4 bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded">
  1147. 应用筛选
  1148. </button>
  1149. </div>
  1150. <!-- 知识列表 -->
  1151. <div id="knowledgeList" class="space-y-4"></div>
  1152. </div>
  1153. <!-- 新增/编辑 Modal -->
  1154. <div id="modal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4">
  1155. <div class="bg-white rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto p-6">
  1156. <h2 id="modalTitle" class="text-2xl font-bold mb-4">新增知识</h2>
  1157. <form id="knowledgeForm" class="space-y-4">
  1158. <input type="hidden" id="editId">
  1159. <div>
  1160. <label class="block text-sm font-medium mb-1">Task *</label>
  1161. <input type="text" id="taskInput" required class="w-full border rounded px-3 py-2">
  1162. </div>
  1163. <div>
  1164. <label class="block text-sm font-medium mb-1">Content *</label>
  1165. <textarea id="contentInput" required rows="6" class="w-full border rounded px-3 py-2"></textarea>
  1166. </div>
  1167. <div>
  1168. <label class="block text-sm font-medium mb-1">Types (多选)</label>
  1169. <div class="space-y-1">
  1170. <label class="flex items-center"><input type="checkbox" value="strategy" class="mr-2 type-checkbox"> Strategy</label>
  1171. <label class="flex items-center"><input type="checkbox" value="tool" class="mr-2 type-checkbox"> Tool</label>
  1172. <label class="flex items-center"><input type="checkbox" value="user_profile" class="mr-2 type-checkbox"> User Profile</label>
  1173. <label class="flex items-center"><input type="checkbox" value="usecase" class="mr-2 type-checkbox"> Usecase</label>
  1174. <label class="flex items-center"><input type="checkbox" value="definition" class="mr-2 type-checkbox"> Definition</label>
  1175. <label class="flex items-center"><input type="checkbox" value="plan" class="mr-2 type-checkbox"> Plan</label>
  1176. </div>
  1177. </div>
  1178. <div>
  1179. <label class="block text-sm font-medium mb-1">Tags (JSON)</label>
  1180. <textarea id="tagsInput" rows="2" placeholder='{"key": "value"}' class="w-full border rounded px-3 py-2"></textarea>
  1181. </div>
  1182. <div>
  1183. <label class="block text-sm font-medium mb-1">Scopes (逗号分隔)</label>
  1184. <input type="text" id="scopesInput" placeholder="org:cybertogether" class="w-full border rounded px-3 py-2">
  1185. </div>
  1186. <div>
  1187. <label class="block text-sm font-medium mb-1">Owner</label>
  1188. <input type="text" id="ownerInput" class="w-full border rounded px-3 py-2">
  1189. </div>
  1190. <div class="flex gap-2 pt-4">
  1191. <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded">保存</button>
  1192. <button type="button" onclick="closeModal()" class="bg-gray-300 hover:bg-gray-400 px-6 py-2 rounded">取消</button>
  1193. </div>
  1194. </form>
  1195. </div>
  1196. </div>
  1197. <script>
  1198. let allKnowledge = [];
  1199. let availableTags = [];
  1200. async function loadTags() {
  1201. const res = await fetch('/api/knowledge/meta/tags');
  1202. const data = await res.json();
  1203. availableTags = data.tags;
  1204. renderTagsFilter();
  1205. }
  1206. function renderTagsFilter() {
  1207. const container = document.getElementById('tagsFilterContainer');
  1208. if (availableTags.length === 0) {
  1209. container.innerHTML = '<p class="text-sm text-gray-500">暂无 tags</p>';
  1210. return;
  1211. }
  1212. container.innerHTML = availableTags.map(tag =>
  1213. `<label class="flex items-center"><input type="checkbox" value="${escapeHtml(tag)}" class="mr-2 tag-filter"> ${escapeHtml(tag)}</label>`
  1214. ).join('');
  1215. }
  1216. async function loadKnowledge() {
  1217. const params = new URLSearchParams();
  1218. params.append('limit', '1000');
  1219. const selectedTypes = Array.from(document.querySelectorAll('.type-filter:checked')).map(el => el.value);
  1220. if (selectedTypes.length > 0) {
  1221. params.append('types', selectedTypes.join(','));
  1222. }
  1223. const selectedTags = Array.from(document.querySelectorAll('.tag-filter:checked')).map(el => el.value);
  1224. if (selectedTags.length > 0) {
  1225. params.append('tags', selectedTags.join(','));
  1226. }
  1227. const ownerFilter = document.getElementById('ownerFilter').value.trim();
  1228. if (ownerFilter) {
  1229. params.append('owner', ownerFilter);
  1230. }
  1231. const scopesFilter = document.getElementById('scopesFilter').value.trim();
  1232. if (scopesFilter) {
  1233. params.append('scopes', scopesFilter);
  1234. }
  1235. try {
  1236. const res = await fetch(`/api/knowledge?${params.toString()}`);
  1237. if (!res.ok) {
  1238. console.error('加载失败:', res.status, res.statusText);
  1239. document.getElementById('knowledgeList').innerHTML = '<p class="text-red-500 text-center py-8">加载失败,请刷新页面重试</p>';
  1240. return;
  1241. }
  1242. const data = await res.json();
  1243. allKnowledge = data.results || [];
  1244. renderKnowledge(allKnowledge);
  1245. } catch (error) {
  1246. console.error('加载错误:', error);
  1247. document.getElementById('knowledgeList').innerHTML = '<p class="text-red-500 text-center py-8">加载错误: ' + error.message + '</p>';
  1248. }
  1249. }
  1250. function applyFilters() {
  1251. loadKnowledge();
  1252. }
  1253. function renderKnowledge(list) {
  1254. const container = document.getElementById('knowledgeList');
  1255. if (list.length === 0) {
  1256. container.innerHTML = '<p class="text-gray-500 text-center py-8">暂无知识</p>';
  1257. return;
  1258. }
  1259. container.innerHTML = list.map(k => {
  1260. // 确保types是数组
  1261. let types = [];
  1262. if (Array.isArray(k.types)) {
  1263. types = k.types;
  1264. } else if (typeof k.types === 'string') {
  1265. // 如果是JSON字符串(以[开头),尝试解析
  1266. if (k.types.startsWith('[')) {
  1267. try {
  1268. types = JSON.parse(k.types);
  1269. } catch (e) {
  1270. console.error('解析types失败:', k.types, e);
  1271. types = [k.types];
  1272. }
  1273. } else {
  1274. // 如果是普通字符串,包装成数组
  1275. types = [k.types];
  1276. }
  1277. }
  1278. const eval_data = k.eval || {};
  1279. return `
  1280. <div class="bg-white rounded-lg shadow p-6 hover:shadow-lg transition cursor-pointer" onclick="openEditModal('${k.id}')">
  1281. <div class="flex justify-between items-start mb-2">
  1282. <div class="flex gap-2 flex-wrap">
  1283. ${types.map(t => `<span class="bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">${t}</span>`).join('')}
  1284. </div>
  1285. <span class="text-sm text-gray-500">${eval_data.score || 3}/5</span>
  1286. </div>
  1287. <h3 class="text-lg font-semibold text-gray-800 mb-2">${escapeHtml(k.task)}</h3>
  1288. <p class="text-sm text-gray-600 mb-2">${escapeHtml(k.content.substring(0, 150))}${k.content.length > 150 ? '...' : ''}</p>
  1289. <div class="flex justify-between text-xs text-gray-500">
  1290. <span>Owner: ${k.owner || 'N/A'}</span>
  1291. <span>${new Date(k.created_at).toLocaleDateString()}</span>
  1292. </div>
  1293. </div>
  1294. `;
  1295. }).join('');
  1296. }
  1297. function openAddModal() {
  1298. document.getElementById('modalTitle').textContent = '新增知识';
  1299. document.getElementById('knowledgeForm').reset();
  1300. document.getElementById('editId').value = '';
  1301. document.querySelectorAll('.type-checkbox').forEach(el => el.checked = false);
  1302. document.getElementById('modal').classList.remove('hidden');
  1303. }
  1304. async function openEditModal(id) {
  1305. const k = allKnowledge.find(item => item.id === id);
  1306. if (!k) return;
  1307. document.getElementById('modalTitle').textContent = '编辑知识';
  1308. document.getElementById('editId').value = k.id;
  1309. document.getElementById('taskInput').value = k.task || '';
  1310. document.getElementById('contentInput').value = k.content || '';
  1311. document.getElementById('tagsInput').value = JSON.stringify(k.tags || {});
  1312. // 防御性检查:确保 scopes 是数组
  1313. const scopes = Array.isArray(k.scopes) ? k.scopes : [];
  1314. document.getElementById('scopesInput').value = scopes.join(', ');
  1315. document.getElementById('ownerInput').value = k.owner || '';
  1316. // 防御性检查:确保 types 是数组
  1317. const types = Array.isArray(k.types) ? k.types : [];
  1318. document.querySelectorAll('.type-checkbox').forEach(el => {
  1319. el.checked = types.includes(el.value);
  1320. });
  1321. document.getElementById('modal').classList.remove('hidden');
  1322. }
  1323. function closeModal() {
  1324. document.getElementById('modal').classList.add('hidden');
  1325. }
  1326. document.getElementById('knowledgeForm').addEventListener('submit', async (e) => {
  1327. e.preventDefault();
  1328. const editId = document.getElementById('editId').value;
  1329. const task = document.getElementById('taskInput').value;
  1330. const content = document.getElementById('contentInput').value;
  1331. const types = Array.from(document.querySelectorAll('.type-checkbox:checked')).map(el => el.value);
  1332. const tagsText = document.getElementById('tagsInput').value.trim();
  1333. const scopesText = document.getElementById('scopesInput').value.trim();
  1334. const owner = document.getElementById('ownerInput').value.trim();
  1335. let tags = {};
  1336. if (tagsText) {
  1337. try {
  1338. tags = JSON.parse(tagsText);
  1339. } catch (e) {
  1340. alert('Tags JSON 格式错误');
  1341. return;
  1342. }
  1343. }
  1344. const scopes = scopesText ? scopesText.split(',').map(s => s.trim()).filter(s => s) : ['org:cybertogether'];
  1345. if (editId) {
  1346. // 编辑
  1347. const res = await fetch(`/api/knowledge/${editId}`, {
  1348. method: 'PATCH',
  1349. headers: {'Content-Type': 'application/json'},
  1350. body: JSON.stringify({task, content, types, tags, scopes, owner})
  1351. });
  1352. if (!res.ok) {
  1353. alert('更新失败');
  1354. return;
  1355. }
  1356. } else {
  1357. // 新增
  1358. const res = await fetch('/api/knowledge', {
  1359. method: 'POST',
  1360. headers: {'Content-Type': 'application/json'},
  1361. body: JSON.stringify({task, content, types, tags, scopes, owner})
  1362. });
  1363. if (!res.ok) {
  1364. alert('新增失败');
  1365. return;
  1366. }
  1367. }
  1368. closeModal();
  1369. await loadKnowledge();
  1370. });
  1371. function escapeHtml(text) {
  1372. const div = document.createElement('div');
  1373. div.textContent = text;
  1374. return div.innerHTML;
  1375. }
  1376. loadTags();
  1377. loadKnowledge();
  1378. </script>
  1379. </body>
  1380. </html>"""
  1381. if __name__ == "__main__":
  1382. import uvicorn
  1383. uvicorn.run(app, host="0.0.0.0", port=9999)