# 知识库API

知识库API位于 `backend/app/api/knowledge.py`

## 端点列表

| 方法 | 路径 | 说明 |
|------|------|------|
| GET | /api/v1/knowledge/bases | 获取知识库列表 |
| POST | /api/v1/knowledge/bases | 创建知识库 |
| GET | /api/v1/knowledge/bases/{id} | 获取知识库详情 |
| PUT | /api/v1/knowledge/bases/{id} | 更新知识库 |
| DELETE | /api/v1/knowledge/bases/{id} | 删除知识库 |
| POST | /api/v1/knowledge/bases/{id}/documents | 上传文档 |
| GET | /api/v1/knowledge/bases/{id}/documents | 获取文档列表 |
| DELETE | /api/v1/knowledge/bases/{id}/documents/{doc_id} | 删除文档 |
| POST | /api/v1/knowledge/search | 搜索知识 |
| GET | /api/v1/knowledge/entities | 获取实体列表 |
| GET | /api/v1/knowledge/relations | 获取关系列表 |

## 获取知识库列表

```
GET /api/v1/knowledge/bases
```

### 响应

```json
{
  "success": true,
  "data": {
    "items": [
      {
        "id": "uuid",
        "name": "知识库名称",
        "description": "知识库描述",
        "document_count": 100,
        "created_at": "2024-01-01T00:00:00Z"
      }
    ],
    "total": 10
  }
}
```

## 创建知识库

```
POST /api/v1/knowledge/bases
```

### 请求体

```json
{
  "name": "知识库名称",
  "description": "知识库描述",
  "embedding_model": "text-embedding-ada-002"
}
```

## 上传文档

```
POST /api/v1/knowledge/bases/{id}/documents
```

### 请求体 (multipart/form-data)

| 字段 | 类型 | 说明 |
|------|------|------|
| file | file | 文档文件 |
| title | string | 文档标题 |
| tags | string[] | 标签 |

### 支持格式

- PDF
- TXT
- Markdown
- DOCX

## 搜索知识

```
POST /api/v1/knowledge/search
```

### 请求体

```json
{
  "query": "搜索关键词",
  "knowledge_base_ids": ["uuid1", "uuid2"],
  "top_k": 10
}
```

### 响应

```json
{
  "success": true,
  "data": {
    "results": [
      {
        "content": "相关内容片段",
        "document_id": "uuid",
        "document_title": "文档标题",
        "similarity": 0.95,
        "metadata": {}
      }
    ]
  }
}
```

## 知识图谱API

知识图谱API位于 `backend/app/api/knowledge_graph.py`

### 获取实体列表

```
GET /api/v1/knowledge/entities
```

### 查询参数

| 参数 | 类型 | 说明 |
|------|------|------|
| entity_type | string | 实体类型 |
| brand_id | uuid | 品牌ID |

### 响应

```json
{
  "success": true,
  "data": {
    "items": [
      {
        "id": "uuid",
        "name": "实体名称",
        "type": "brand",
        "properties": {},
        "created_at": "2024-01-01T00:00:00Z"
      }
    ]
  }
}
```

### 获取关系列表

```
GET /api/v1/knowledge/relations
```

### 响应

```json
{
  "success": true,
  "data": {
    "items": [
      {
        "id": "uuid",
        "source_id": "uuid",
        "target_id": "uuid",
        "relation_type": "competes_with",
        "properties": {},
        "created_at": "2024-01-01T00:00:00Z"
      }
    ]
  }
}
```
