AnythingLLM 是一个功能强大的知识库管理工具,提供了丰富的 API 接口,方便开发者进行集成和扩展。本文档将详细介绍 AnythingLLM 的 API 接口及其使用方法。
一、API 基础信息
-
API 地址:
http://localhost:5000/api
(默认地址) -
认证方式: API Key
-
请求格式: JSON
-
响应格式: JSON
二、API 接口列表
1. 获取知识库列表
-
URL:
/api/knowledge_bases
-
方法:
GET
-
描述: 获取所有知识库的列表。
-
请求示例:
curl -X GET "http://localhost:5000/api/knowledge_bases" -H "Authorization: Bearer YOUR_API_KEY"
-
响应示例:
{ "knowledge_bases": [ { "id": 1, "name": "Knowledge Base 1", "description": "This is the first knowledge base", "created_at": "2023-10-01T12:00:00Z" }, { "id": 2, "name": "Knowledge Base 2", "description": "This is the second knowledge base", "created_at": "2023-10-02T12:00:00Z" } ] }
2. 创建知识库
-
URL:
/api/knowledge_bases
-
方法:
POST
-
描述: 创建一个新的知识库。
-
请求参数:
参数名 类型 必填 描述 name string 是 知识库名称 description string 否 知识库描述 -
请求示例:
curl -X POST "http://localhost:5000/api/knowledge_bases" -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json" -d '{ "name": "New Knowledge Base", "description": "This is a new knowledge base" }'
-
响应示例:
{ "id": 3, "name": "New Knowledge Base", "description": "This is a new knowledge base", "created_at": "2023-10-03T12:00:00Z" }
3. 获取知识库详情
-
URL:
/api/knowledge_bases/{knowledge_base_id}
-
方法:
GET
-
描述: 获取指定知识库的详细信息。
-
请求示例:
curl -X GET "http://localhost:5000/api/knowledge_bases/1" -H "Authorization: Bearer YOUR_API_KEY"
-
响应示例:
{ "id": 1, "name": "Knowledge Base 1", "description": "This is the first knowledge base", "created_at": "2023-10-01T12:00:00Z", "documents": [ { "id": 1, "title": "Document 1", "content": "This is the content of document 1", "created_at": "2023-10-01T12:00:00Z" }, { "id": 2, "title": "Document 2", "content": "This is the content of document 2", "created_at": "2023-10-02T12:00:00Z" } ] }
4. 添加文档到知识库
-
URL:
/api/knowledge_bases/{knowledge_base_id}/documents
-
方法:
POST
-
描述: 向指定知识库添加文档。
-
请求参数:
参数名 类型 必填 描述 title string 是 文档标题 content string 是 文档内容 -
请求示例:
curl -X POST "http://localhost:5000/api/knowledge_bases/1/documents" -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json" -d '{ "title": "New Document", "content": "This is the content of the new document" }'
-
响应示例:
{ "id": 3, "title": "New Document", "content": "This is the content of the new document", "created_at": "2023-10-03T12:00:00Z" }
5. 查询知识库
-
URL:
/api/knowledge_bases/{knowledge_base_id}/query
-
方法:
POST
-
描述: 在指定知识库中查询相关内容。
-
请求参数:
参数名 类型 必填 描述 query string 是 查询语句 -
请求示例:
curl -X POST "http://localhost:5000/api/knowledge_bases/1/query" -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json" -d '{ "query": "How to use AnythingLLM?" }'
-
响应示例:
{ "results": [ { "id": 1, "title": "Document 1", "content": "This is the content of document 1", "score": 0.95 }, { "id": 2, "title": "Document 2", "content": "This is the content of document 2", "score": 0.85 } ] }
三、错误处理
API 请求可能会返回以下错误码:
错误码 | 描述 |
---|---|
400 | 请求参数错误 |
401 | 未授权 |
404 | 资源未找到 |
500 | 服务器内部错误 |
错误响应示例:
{ "error": "Unauthorized", "message": "API key is missing or invalid" }
四、总结
AnythingLLM 提供了丰富的 API 接口,方便开发者进行知识库的管理和查询。通过本文档,您可以快速上手并使用这些接口,实现知识库的集成和扩展。如果您有任何问题或建议,欢迎联系我们的支持团队。