Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 6eb56f9

Browse files
committed
Add knowledge base management feature and update configurations
- Introduced a new knowledge base management system that allows users to create, edit, and delete knowledge bases via a web interface. - Implemented vector similarity search for intelligent Q&A matching, enhancing the response capabilities of the system. - Added new configuration options for embedding API integration, including API base URL, key, model name, and similarity thresholds. - Updated the CHANGELOG to reflect the new version 2.2.0 and document major updates and added features. - Enhanced the README files to include detailed descriptions of the knowledge base functionality and its use cases. - Incremented version numbers in both backend and frontend to 2.2.0 to signify the introduction of these features.
1 parent dc0e00c commit 6eb56f9

31 files changed

+2553
-287
lines changed

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,47 @@ All notable changes to Xiangxin AI Guardrails platform are documented in this fi
1010
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1212

13+
## [2.2.0] - 2025-01-15
14+
15+
### 🚀 重大更新 Major Updates
16+
- 🧠 **代答知识库功能**
17+
- 全新的智能代答系统,基于向量相似度搜索
18+
- 支持上传问答对文件,自动构建知识库向量索引
19+
- 风险检测时优先匹配知识库中的相似问题,返回对应答案
20+
- 支持全局知识库和用户级知识库,管理员可配置全局生效的知识库
21+
22+
### 新增 Added
23+
- 📚 **代答知识库管理**
24+
- Web界面支持知识库创建、编辑、删除
25+
- JSONL格式问答对文件上传和验证
26+
- 向量索引自动生成和管理
27+
- 知识库搜索测试功能
28+
- 支持文件替换和重新索引
29+
30+
- 🎯 **智能代答策略**
31+
- 风险检测触发时,优先搜索知识库相似问题
32+
- 基于余弦相似度的问题匹配算法
33+
- 可配置相似度阈值和返回结果数量
34+
- 未找到相似问题时,回退到传统拒答模板
35+
36+
### 新增配置 New Configuration
37+
- `EMBEDDING_API_BASE_URL` - Embedding API服务地址
38+
- `EMBEDDING_API_KEY` - Embedding API密钥
39+
- `EMBEDDING_MODEL_NAME` - Embedding模型名称
40+
- `EMBEDDING_MODEL_DIMENSION` - 向量维度配置
41+
- `EMBEDDING_SIMILARITY_THRESHOLD` - 相似度阈值
42+
- `EMBEDDING_MAX_RESULTS` - 最大返回结果数
43+
44+
45+
#### 知识库文件格式
46+
```jsonl
47+
{"questionid": "q1", "question": "什么是人工智能?", "answer": "人工智能是模拟人类智能的技术。"}
48+
{"questionid": "q2", "question": "如何使用机器学习?", "answer": "机器学习是AI的一个重要分支..."}
49+
```
50+
51+
## [2.1.0] - 2025-09-29
52+
增加敏感度阈值配置功能,应对特殊场景和全自动流水线。
53+
1354
## [2.0.0] - 2025-01-01
1455

1556
### 🚀 重大更新 Major Updates

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ English | [中文](./README_ZH.md)
2828
- 🧠 **Context Awareness** - Intelligent safety detection based on conversation context
2929
- 📋 **Compliance Standards** - Compliant with "GB/T45654—2025 Basic Security Requirements for Generative AI Services"
3030
- 🔧 **Flexible Configuration** - Blacklist/whitelist, response templates, rate limiting and other personalized configurations
31+
- 🧠 **Knowledge Base Responses** - Vector similarity-based intelligent Q&A matching with custom knowledge bases 🆕
3132
- 🏢 **Private Deployment** - Support for complete local deployment, controllable data security
3233
- 🔌 **Customer System Integration** - Deep integration with existing customer user systems, API-level configuration management
3334
- 📊 **Visual Management** - Intuitive web management interface and real-time monitoring
@@ -393,6 +394,57 @@ User Request → Security Gateway(5002) → Input Safety Detection
393394
- **Smart Recognition**: Automatic detection of reasoning_content, thinking and other reasoning fields
394395
- **Transparent Proxy**: Full OpenAI API compatibility, supports all reasoning models
395396

397+
## 🧠 Knowledge Base Responses Feature 🆕
398+
399+
Xiangxin AI Guardrails v2.2.0 introduces powerful knowledge base response functionality with vector similarity-based intelligent Q&A matching.
400+
401+
### 📚 Key Features
402+
403+
- **Intelligent Matching**: Vector similarity search for most relevant questions using embeddings
404+
- **Automatic Responses**: Priority responses from knowledge base when risks are detected
405+
- **Flexible Management**: Web interface for uploading, editing, and deleting knowledge bases
406+
- **Tiered Permissions**: Support for user-level and global knowledge bases, admin-configurable global knowledge bases
407+
- **File Format**: Support for JSONL format Q&A pair file uploads
408+
409+
### 🔄 Workflow
410+
411+
```
412+
User Input → Security Detection → [Risk Detected] → Search Knowledge Base → Similar Question Found?
413+
414+
Yes → Return Knowledge Base Answer
415+
416+
No → Return Traditional Rejection Template
417+
```
418+
419+
### 📝 Knowledge Base File Format
420+
421+
```jsonl
422+
{"questionid": "q1", "question": "What is artificial intelligence?", "answer": "Artificial intelligence is technology that simulates human intelligence, including machine learning and deep learning branches."}
423+
{"questionid": "q2", "question": "How to protect data privacy?", "answer": "Data privacy protection requires multiple technical measures including encryption, access control, and data anonymization."}
424+
{"questionid": "q3", "question": "What are the uses of blockchain?", "answer": "Blockchain technology can be used in digital currency, supply chain management, identity authentication and many other fields."}
425+
```
426+
427+
### 🔧 Embedding Service Configuration
428+
429+
The knowledge base response feature requires embedding model service support.
430+
431+
```bash
432+
# Start embedding service using vLLM
433+
vllm serve --port your-port --host your-host-ip --task embed path/to/Qwen/Qwen3-Embedding-0.6B --served-model-name Xiangxin-Embedding-1024
434+
435+
# Then configure in your settings
436+
EMBEDDING_API_BASE_URL=http://your-host-ip:your-port/v1
437+
EMBEDDING_API_KEY=EMPTY
438+
EMBEDDING_MODEL_NAME=Xiangxin-Embedding-1024
439+
```
440+
441+
### 🎯 Use Cases
442+
443+
- **Customer Service**: Upload FAQ answers for automatic standard responses
444+
- **Policy Interpretation**: Configure policy-related Q&A for authoritative explanations
445+
- **Technical Support**: Build technical issue knowledge base for quick user consultation responses
446+
- **Compliance Responses**: Provide compliant standard answers for sensitive topics
447+
396448
## 🚀 Quick Start
397449

398450
### 🐳 One-Click Docker Deployment (Recommended)

README_ZH.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- 🧠 **上下文感知** - 基于对话上下文的智能安全检测
2929
- 📋 **合规标准** - 符合《GB/T45654—2025 生成式人工智能服务安全基本要求》
3030
- 🔧 **灵活配置** - 黑白名单、代答库、限速等个性化配置
31+
- 🧠 **代答知识库** - 基于向量相似度的智能问答匹配,支持自定义问答对知识库 🆕
3132
- 🎯 **敏感度阈值配置** - 三档敏感度阈值配置,适应自动化流水线等不同使用场景 🆕
3233
- 🏢 **私有化部署** - 支持完全本地化部署,数据安全可控
3334
- 🔌 **客户系统集成** - 支持与客户现有用户系统深度集成,API级别的配置管理
@@ -391,6 +392,55 @@ response = client.chat.completions.create(model="local-reasoning-llm", messages=
391392
[通过检测] → 返回给用户
392393
```
393394

395+
## 🧠 代答知识库功能 🆕
396+
397+
象信AI安全护栏v2.2.0新增了强大的代答知识库功能,基于向量相似度搜索提供智能问答匹配。
398+
399+
### 📚 功能特性
400+
401+
- **智能匹配**:基于embedding向量相似度搜索最相关的问题
402+
- **自动代答**:检测到风险时,优先返回知识库中的标准答案
403+
- **灵活管理**:支持Web界面上传、编辑、删除知识库
404+
- **分级权限**:支持用户级和全局级知识库,管理员可配置全局生效的知识库
405+
- **文件格式**:支持JSONL格式的问答对文件上传
406+
407+
### 🔄 工作流程
408+
409+
```
410+
用户输入 → 安全检测 → [检测到风险] → 搜索知识库 → 找到相似问题?
411+
412+
是 → 返回知识库答案
413+
414+
否 → 返回传统拒答模板
415+
```
416+
417+
### 📝 知识库文件格式
418+
419+
```jsonl
420+
{"questionid": "q1", "question": "什么是人工智能?", "answer": "人工智能是模拟人类智能的技术,包括机器学习、深度学习等分支。"}
421+
{"questionid": "q2", "question": "如何保护数据隐私?", "answer": "数据隐私保护需要采用加密、访问控制、数据脱敏等多种技术手段。"}
422+
{"questionid": "q3", "question": "区块链有什么用途?", "answer": "区块链技术可用于数字货币、供应链管理、身份认证等多个领域。"}
423+
```
424+
425+
### 🔧 Embedding服务配置
426+
427+
代答知识库功能需要embedding模型服务支持
428+
```bash
429+
# 使用vLLM启动embedding服务
430+
vllm serve --port your-port --host your-host-ip --task embed path/to/Qwen/Qwen3-Embedding-0.6B --served-model-name Xiangxin-Embedding-1024
431+
432+
# 然后在配置文件中设置
433+
EMBEDDING_API_BASE_URL=http://your-host-ip:your-port/v1
434+
EMBEDDING_API_KEY=EMPTY
435+
EMBEDDING_MODEL_NAME=Xiangxin-Embedding-1024
436+
```
437+
438+
### 🎯 使用场景
439+
440+
- **客服问答**:上传常见问题答案,自动回复标准答案
441+
- **政策解读**:配置政策相关问答,提供权威解释
442+
- **技术支持**:建立技术问题知识库,快速响应用户咨询
443+
- **合规回复**:针对敏感话题提供合规的标准回答
394444

395445
## 🚀 快速开始
396446

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.2.0

backend/.env.example

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ DATABASE_URL=postgresql://xiangxin:your_password@localhost:54321/xiangxin_guardr
1616

1717
# 模型配置
1818
# 设置模型API地址(本地或云端)
19-
GUARDRAILS_MODEL_API_URL=http://localhost:58002/v1
20-
GUARDRAILS_MODEL_API_KEY=your-model-api-key
19+
GUARDRAILS_MODEL_API_URL=http://your-host-ip:your-port/v1
20+
GUARDRAILS_MODEL_API_KEY=your-guardrails-model-api-key
2121
GUARDRAILS_MODEL_NAME=Xiangxin-Guardrails-Text
2222

2323
# 检测最大上下文长度配置 (应该等于模型max-model-len - 1000)
@@ -35,6 +35,15 @@ SUPPORT_EMAIL=wanglei@xiangxinai.cn
3535
# HuggingFace模型
3636
HUGGINGFACE_MODEL=xiangxinai/Xiangxin-Guardrails-Text
3737

38+
# 嵌入模型API配置
39+
# 用于知识库向量化的嵌入模型API
40+
EMBEDDING_API_BASE_URL=http://your-host-ip:your-port/v1
41+
EMBEDDING_API_KEY=your-embedding-api-key
42+
EMBEDDING_MODEL_NAME=Xiangxin-Embedding-1024
43+
EMBEDDING_MODEL_DIMENSION=1024
44+
EMBEDDING_SIMILARITY_THRESHOLD=0.7
45+
EMBEDDING_MAX_RESULTS=5
46+
3847
# JWT配置
3948
# 警告:请生成一个安全的随机密钥!可以使用: openssl rand -base64 64
4049
JWT_SECRET_KEY=PLEASE-GENERATE-A-SECURE-RANDOM-KEY-USING-openssl-rand-base64-64

backend/.env.local.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ JWT_SECRET_KEY=GENERATE-A-SECURE-RANDOM-JWT-KEY-IN-PRODUCTION
1818
DATA_DIR=/mnt/data/xiangxin-guardrails-data
1919

2020
# 模型API配置
21-
GUARDRAILS_MODEL_API_URL=http://localhost:58002/v1
22-
GUARDRAILS_MODEL_API_KEY=your-model-api-key
21+
GUARDRAILS_MODEL_API_URL=http://your-host-ip:your-port/v1
22+
GUARDRAILS_MODEL_API_KEY=your-guardrails-model-api-key
2323

2424
# SMTP配置(可选)
2525
SMTP_SERVER=

backend/config.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class Settings(BaseSettings):
4444
database_url: str = "postgresql://xiangxin:your_password@localhost:54321/xiangxin_guardrails"
4545

4646
# 模型配置
47-
guardrails_model_api_url: str = "http://localhost:58002/v1"
48-
guardrails_model_api_key: str = "your-model-api-key"
47+
guardrails_model_api_url: str = "http://your-host-ip:your-port/v1"
48+
guardrails_model_api_key: str = "your-guardrails-model-api-key"
4949
guardrails_model_name: str = "Xiangxin-Guardrails-Text"
5050

5151
# 检测最大上下文长度配置 (应该等于模型max-model-len - 1000)
@@ -72,6 +72,15 @@ def detection_log_dir(self) -> str:
7272

7373
# HuggingFace模型
7474
huggingface_model: str = "xiangxinai/Xiangxin-Guardrails-Text"
75+
76+
# 嵌入模型API配置
77+
# 用于知识库向量化的嵌入模型API
78+
embedding_api_base_url: str = "http://your-host-ip:your-port/v1"
79+
embedding_api_key: str = "your-embedding-api-key"
80+
embedding_model_name: str = "Xiangxin-Embedding-1024"
81+
embedding_model_dimension: int = 1024 # 嵌入向量维度
82+
embedding_similarity_threshold: float = 0.7 # 相似度阈值
83+
embedding_max_results: int = 5 # 最大返回结果数
7584

7685
# JWT配置
7786
# 警告:请生成安全的随机密钥!使用: openssl rand -base64 64

backend/database/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ async def init_db(minimal=False):
146146
]
147147

148148
default_responses = [
149-
ResponseTemplate(category="S1", risk_level="高风险", template_content="很抱歉,我不能讨论涉及政治敏感的话题。", is_default=True),
149+
ResponseTemplate(category="S1", risk_level="高风险", template_content="很抱歉,我不能讨论涉政话题。", is_default=True),
150150
ResponseTemplate(category="S2", risk_level="高风险", template_content="很抱歉,我不能回答涉及敏感政治话题的问题。", is_default=True),
151151
ResponseTemplate(category="S3", risk_level="高风险", template_content="很抱歉,我不能回答损害国家形象的问题。", is_default=True),
152152
ResponseTemplate(category="S4", risk_level="高风险", template_content="很抱歉,我不能提供可能对未成年人造成伤害的内容。", is_default=True),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- 添加is_global列到knowledge_bases表
2+
-- 用于支持全局知识库功能(所有用户生效的知识库)
3+
4+
-- 添加is_global列
5+
ALTER TABLE knowledge_bases
6+
ADD COLUMN is_global BOOLEAN DEFAULT false NOT NULL;
7+
8+
-- 添加索引以提高查询性能
9+
CREATE INDEX idx_knowledge_bases_is_global ON knowledge_bases(is_global);
10+
11+
-- 更新现有记录,默认都不是全局知识库
12+
UPDATE knowledge_bases SET is_global = false WHERE is_global IS NULL;
13+
14+
-- 添加注释
15+
COMMENT ON COLUMN knowledge_bases.is_global IS '是否为全局知识库(所有用户生效),仅管理员可设置';

backend/database/models.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,26 @@ class ProxyRequestLog(Base):
287287
user = relationship("User")
288288
proxy_config = relationship("ProxyModelConfig")
289289

290+
class KnowledgeBase(Base):
291+
"""代答知识库表"""
292+
__tablename__ = "knowledge_bases"
293+
294+
id = Column(Integer, primary_key=True, index=True)
295+
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False, index=True)
296+
category = Column(String(50), nullable=False, index=True) # 风险类别 (S1-S12)
297+
name = Column(String(255), nullable=False) # 知识库名称
298+
description = Column(Text) # 描述
299+
file_path = Column(String(512), nullable=False) # 原始JSONL文件路径
300+
vector_file_path = Column(String(512)) # 向量化文件路径
301+
total_qa_pairs = Column(Integer, default=0) # 问答对总数
302+
is_active = Column(Boolean, default=True, index=True) # 是否启用
303+
is_global = Column(Boolean, default=False, index=True) # 是否为全局知识库(所有用户生效),仅管理员可设置
304+
created_at = Column(DateTime(timezone=True), server_default=func.now())
305+
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
306+
307+
# 关联关系
308+
user = relationship("User")
309+
290310
class OnlineTestModelSelection(Base):
291311
"""在线测试模型选择表 - 记录用户在在线测试中选择的代理模型"""
292312
__tablename__ = "online_test_model_selections"

0 commit comments

Comments
 (0)