第三方通用知识库接入规范-j9九游会登录
前言
当您拥有企业内部已有的知识库系统(如内部wiki、私有文档库),且不希望将数据迁移至agentarts平台时,可遵循本规范开发适配接口。
典型场景
企业智能体需要检索内部保密文档,通过实现本规范定义的api接口,agentarts可直接调用您的知识库服务进行问答增强。
工作原理
您需开发并部署两个标准http接口,agentarts智能体在对话过程中,当需要检索外部知识时,将依次调用您的接口:
费用说明
通过agentarts调用第三方知识库接口,可能会产生公网流量费用,具体取决于您的第三方服务部署环境。该调用过程将消耗智能体的运行时长资源。
api接口规范
对于api接口适配,开发者需要新增两个接口,一个是获取知识库列表,一个是检索知识库。开发者按照接口:获取知识库列表、接口:检索知识库规范提供对外接口,并在接口内实现密钥鉴权、数据查询、知识检索等业务逻辑,最后按照规范返回对应数据。
- uri
get /knowledge-bases
- 查询query参数
表1 查询query参数 参数
是否必选
参数类型
描述
name
否
string
参数解释:
知识库名称,用于获取知识库列表时筛选知识库名称。
取值范围:
长度不超过64个字符。
默认取值:
不涉及。
offset
否
integer
参数解释:
当前请求的分页偏移量,表示从第几条开始获取数据。默认值为0,表示从第0条开始获取数据。
取值范围:
0~65535。
默认取值:
0。
limit
否
integer
参数解释:
当前请求返回的分页最大数据条数,表示一次请求返回的数据条数。默认值为10,表示一页最多展示10条数据。
取值范围:
1~100。
默认取值:
10。
- 请求header参数
表2 请求header参数 参数
是否必选
参数类型
描述
authorization
是
string
参数解释:
认证鉴权使用的api-key密钥,拼接“bearer ”后的字符串。
示例:bearer d59******9c3,用于第三方知识库的鉴权。
取值范围:
不涉及。
默认取值:
不涉及。
- 响应参数
表3 响应参数200 参数
参数类型
描述
total
integer
参数解释:
知识库数据总数。
取值范围:
不涉及。
knowledge_base_list
array of 表4
参数解释:
知识库数据信息列表。
注意:第三方知识库只返回可用的知识库,即在当前时刻可被外部引用并支持检索的知识库数据。
取值范围:
不涉及。
表4 knowledgebaseinfo 参数
参数类型
描述
knowledge_base_id
string
参数解释:
知识库id。
取值范围:
不涉及。
name
string
参数解释:
知识库名称。
取值范围:
不涉及。
description
string
参数解释:
知识库描述。
取值范围:
不涉及。
表5 响应参数400、500等 参数
参数类型
描述
error_code
string
参数解释:
错误码。
取值范围:
不涉及。
error_msg
string
参数解释:
错误描述。
取值范围:
不涉及。
- 请求示例
get http://10.0.0.0:8090/knowledge-bases?name=123&offset=0&limit=10 - 响应示例
{ "total": 1, "knowledge_base_list": [ { "knowledge_base_id": "001", "name": "一个通用第三方知识库的名称 / this is a knowledgebase name", "description": "一个描述 / a description" } ] }
- uri
post /knowledge-bases/retrieve
- 请求header参数
表6 请求header参数 参数
是否必选
参数类型
描述
authorization
是
string
参数解释:
认证鉴权使用的api-key密钥,拼接“bearer ”后的字符串。
示例:bearer d59******9c3,用于第三方知识库的鉴权。
取值范围:
不涉及。
默认取值:
不涉及。
- body参数
表7 body参数 参数
是否必选
参数类型
描述
knowledge_base_ids
是
array of string
参数解释:
知识库id的列表,用于指定搜索时需要检索的知识库。
取值范围:
列表长度大于等于1,即至少指定检索一个知识库。
默认取值:
不涉及。
query
是
string
参数解释:
用户的搜索内容,即需要在知识库中检索的问题或关键信息。
取值范围:
长度不超过4096个字符。
默认取值:
不涉及。
method
是
string
参数解释:
搜索方式。语义检索:“doc”。
取值范围:
不涉及。
默认取值:
不涉及。
offset
否
integer
参数解释:
当前请求的分页偏移量,表示从第几条开始获取数据。默认值为0,表示从第0条开始获取数据。
取值范围:
0~65535。
默认取值:
0。
limit
否
integer
参数解释:
当前请求返回的分页最大数据数量,表示一次请求返回的数据条数。默认值为10,表示一页最多展示10条数据。
取值范围:
1-100。
默认取值:
10。
top_k
否
integer
参数解释:
检索返回结果的最大召回数量。
取值范围:
1-1024。
默认取值:
50。
search_threshold
否
number
参数解释:
检索时的相关度得分阈值,低于该阈值的结果将被过滤。默认值0.0时表示不设置相关度得分阈值。
取值范围:
0.0 - 1.0。
默认取值:
0.0。
extra_params
否
array of 表8
参数解释:
额外的自定义参数,用于传递第三方知识库所需的其他业务参数。
示例:
"extra_params": [ { "key": "tenant_id", "value": "enterprise-001" }, { "key": "data_region", "value": "cn-north" } ]取值范围:
不涉及。
默认取值:
不涉及。
表8 knowledgebaseextraparam 参数
是否必选
参数类型
描述
key
是
string
参数解释:
额外参数主键名。
取值范围:
不涉及。
value
是
string
参数解释:
额外参数的值。
取值范围:
不涉及。
- 响应参数
表9 响应参数200 参数
参数类型
描述
total
integer
参数解释:
检索结果总数。
取值范围:
不涉及。
search_result_list
array of 表10
参数解释:
检索结果的信息列表。
取值范围:
不涉及。
表10 searchchunkinfo 参数
参数类型
描述
knowledge_base_id
string
参数解释:
该切片所属的知识库id。
取值范围:
不涉及。
file_id
string
参数解释:
该切片所属的文件id。
取值范围:
不涉及。
chunk_id
string
参数解释:
切片的唯一标识id。
取值范围:
不涉及。
content
string
参数解释:
检索返回的文本内容,即实际注入提示词的知识片段。
取值范围:
不涉及。
score
number
参数解释:
该切片与检索query的相关度得分,将根据此得分决定是否使用该切片。
取值范围:
0.0 - 1.0。
title
string
参数解释:
文本标题,推荐返回文件名称,用于在引用来源时展示给用户。
取值范围:
不涉及。
表11 响应参数400、500等 参数
参数类型
描述
error_code
string
参数解释:
错误码。
取值范围:
不涉及。
error_msg
string
参数解释:
错误描述。
取值范围:
不涉及。
- 请求示例
post http://10.0.0.0:8090/knowledge-bases/retrieve - 响应示例
{ "knowledge_base_ids": [ "001" ], "query": "检索", "method": "doc", "offset": 0, "limit": 10, "top_k": 50, "search_threshold": 0.01 } 响应示例: { "total": 1, "search_result_list": [ { "knowledge_base_id": "001", "file_id": "101", "chunk_id": "201", "content": "一个通用第三方知识库的检索切片内容 / this is a retrieve chunk content", "score": 0.8, "title": "检索切片的标题 / retrieve chunk title" } ] }
api接口实现案例(java)
@slf4j
@restcontroller
public class commonthirdpartyknowledgebasecontroller {
/**
* list knowledgebase function
*
* @param name
* @param offset
* @param limit
* @param authorization
* @return listknowledgebaseresponse
*/
@getmapping("/knowledge-bases")
public listknowledgebaseresponse listknowledgebase(
@requestheader("authorization") string authorization,
@requestparam(required = false) string name,
@requestparam(required = false) integer offset,
@requestparam(required = false) integer limit) {
/**
* 对密钥认证鉴权
* verify and authenticate the key
*/
if (!authorization.startswith("bearer ")) {
log.error("invalid authorization");
throw new runtimeexception("invalid authorization");
}
string apikey = authorization.substring("bearer ".length());
/**
* then do something for authorization
*
*/
/**
* 根据业务实现代码逻辑,构造返回结果
* construct and return results based on your business implementation and code logic
*/
listknowledgebaseresponse response = new listknowledgebaseresponse();
response.total = 1;
knowledgebaseinfo knowledgebaseinfo = new knowledgebaseinfo();
knowledgebaseinfo.knowledge_base_id = "001";
knowledgebaseinfo.name = "一个通用第三方知识库的名称 / this is a knowledgebase name";
knowledgebaseinfo.description = "一个描述 / a description";
response.knowledge_base_list = list.of(knowledgebaseinfo);
return response;
}
/**
* @param authorization
* @param request
* @return retrieveknowledgebaseresponse
*/
@postmapping("/knowledge-bases/retrieve")
public retrieveknowledgebaseresponse retrieveknowledgebase(
@requestheader("authorization") string authorization,
@requestbody retrieveknowledgebaserequest request) {
/**
* 对密钥认证鉴权
* verify and authenticate the key
*/
if (!authorization.startswith("bearer ")) {
log.error("invalid authorization");
throw new runtimeexception("invalid authorization");
}
string apikey = authorization.substring("bearer ".length());
/**
* then do something for authorization
*
*/
/**
* 根据业务实现代码逻辑,构造返回结果
* construct and return results based on your business implementation and code logic
*/
retrieveknowledgebaseresponse response = new retrieveknowledgebaseresponse();
response.total = 1;
searchchunkinfo searchchunkinfo = new searchchunkinfo();
searchchunkinfo.knowledge_base_id = "001";
searchchunkinfo.file_id = "101";
searchchunkinfo.chunk_id = "201";
searchchunkinfo.content = "一个通用第三方知识库的检索切片内容 / this is a retrieve chunk content";
searchchunkinfo.title = "检索切片的标题 / retrieve chunk title";
searchchunkinfo.score = 0.8f;
response.search_result_list = list.of(searchchunkinfo);
return response;
}
/*
* the response structure of the listknowledgebase function
*/
@data
public class listknowledgebaseresponse {
integer total;
list knowledge_base_list;
}
@data
public class knowledgebaseinfo {
string knowledge_base_id;
string name;
string description;
}
/*
* the request structure of the retrieveknowledgebase function
*/
@data
public static class retrieveknowledgebaserequest {
list knowledge_base_ids;
string query;
string method;
integer offset;
integer limit;
integer top_k;
float search_threshold;
list extra_params;
}
@data
public static class knowledgebaseextraparam {
string key;
string value;
}
/*
* the response structure of the retrieveknowledgebase function
*/
@data
public class retrieveknowledgebaseresponse {
integer total;
list search_result_list;
}
@data
public class searchchunkinfo {
string knowledge_base_id;
string file_id;
string chunk_id;
string content;
float score;
string title;
}
} 常见问题
如果接口调用失败,请检查鉴权头是否正确配置。常见报错排查请参见第三方接入故障排除。
相关文档
意见反馈
文档内容是否对您有帮助?
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨