导入知识库
更新时间:2025-03-07
接口描述
导入到知识库,当前仅支持单个web的url导入。
权限说明
Authorization需要填写密钥
接口定义
Path | /v2/knowledgeBase?Action=CreateDocuments |
---|---|
Method | POST |
Content-Type | application/json |
Authorization | 请求签名(Bearer <AppBuilder API Key>) |
请求结构
POST /v2/knowledgeBase?action=CreateDocuments HTTP/1.1
HOST: qianfan.baidubce.com
Authorization: authorization string
Content-Type: application/json
{
"id":"knowledgeBaseID",
"source": { # 数据来源
"type": "web", # web
"urls": [],
"urlConfigs": [{
"url": "https://***tax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=pdf&Jym=9223&Fphm=25112000000023412&Czsj=1738819093326",
"updateFrequency": 1,
"urlDepth": 1
},
{
"url": "https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_8214091452491261937%22%7D&n_type=1&p_from=3",
"updateFrequency": 3,
"urlDepth": 2
}
]
},
"contentFormat": "", # rawText (允许配置后续分割策略), qa(不支持配置后续分割策略),导入知识库只支持rawText
"processOption": {
"template": "ppt" # 可选项为 ppt、paper、qaPair、resume、custom、default
"parser": { # 不是必填项
"choices": ["layoutAnalysis"] # 可选项为 layoutAnalysis、ocr
},
"chunker": {
"choices": ["separator"], # separator, pattern, onePage
"separator": { #当choices中包含 separator时需要填写这个参数
"separators": [",", "!", "?"], # separator,可以新增分页符
"targetLength": 333,
"overlapRate": 0.09
},
// "pattern": { #当choices中包含 pattern时需要填写这个参数
// "markPosition": "head",
// "regex": "正则表达式",
// "targetLength": 333,
// "overlapRate": 0.09
// }
},
"knowledgeAugmentation": {
"choices": ["faq"], # faq,spokenQuery、spo、shortSummary按需增加
}
}
}
请求头域
除公共头域外,无其它特殊头域。
请求参数
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
id | string | 是 | 知识库id |
source | dict | 是 | 数据来源 |
source \ type | string | 是 | 数据来源类型 (web) |
source \ urls | []string | 是 | url列表 |
source \ urlConfigs | [config] | 否 | url导入配置 |
contentFormat | string | 是 | 上传的数据类型,可选值为:rawText:文本文档数据。(暂不支持表格型知识数据) |
processOption | object | contentFormat为rawText时是 | 文档处理策略 |
processOption \ template | string | 是 | 配置方法,可选值为:ppt:ppt幻灯片paper:论文文档resume:简历文档custom:自定义配置,且切片策略可配置default:自定义配置(切片策略不生效)qaPair:结构化问答对 |
processOption \ parser | dict | 否 | 文档解析配置 |
processOption \ parser \ choices | []string | 否 | 解析方法,可选值为:(文字提取默认启动,参数不体现)layoutAnalysis:版面分析ocr:光学字符识别 pageImageAnalysis:文档图片解析 chartAnalysis:图表解析 tableAnalysis:表格深度解析 |
processOption \ chunker | dict | 自定义配置(template为custom)时是 | 切片策略配置 |
processOption \ chunker \ choices | []string | 是 | 使用哪些切片策略方法,可选值为:separator:分割符切片,配置切分标识符、切片最⼤⻓度等选项pattern:正则切片方法,使用正则表达式切片onePage: 整文件切片,将整篇文档内容存入单一切片 |
processOption \ chunker \ separator | dict | chunker.choices[]中包含时是 | 分隔符配置 |
processOption \ chunker \ separator \ separators | []string | chunker.choices[]中包含separator时是 | 分隔符列表,可以使用分页符 |
processOption \ chunker \ separator \ targetLength | int | chunker.choices[]中包含separator时是 | 分段最大长度 |
processOption \ chunker \ separator \ overlapRate | float | chunker.choices[]中包含separator时是 | 重叠比例 |
processOption \ chunker \ pattern | dict | chunker.choices[]中包含pattern时是 | 正则配置 |
processOption \ chunker \ pattern \ markPosition | string | chunker.choices[]中包含pattern时是 | 匹配命中时,命中内容放置策略(head | tail | drop) |
processOption \ chunker \ pattern \ regex | string | chunker.choices[]中包含pattern时是 | 正则表达式 |
processOption \ chunker \ pattern \ targetLength | int | chunker.choices[]中包含pattern时是 | 分段最大长度 |
processOption \ chunker \ pattern \ overlapRate | float | chunker.choices[]中包含pattern时是 | 重叠比例 |
processOption \ chunker \ prependInfo | []string | 否 | chunker关联元数据,可选值为:title :增加标题,filename:增加文件名 |
knowledgeAugmentation | dict | 否 | 数据增强策略 |
knowledgeAugmentation \ choices | []string | 否 | faq:问题生成。通过该字段生成的问题在段落中一定存在明确答案。 spokenQuery:问题生成。通过该字段生成的问题是和段落相关的。 spo:三元知识抽取 shortSummary:段落总结。 以上字段按需增加。 其中如果想要具备问题生成能力,则faq和spokenQuery必须都要传。 |
config对象
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
url | string | 否 | url值 |
updateFrequency | int | 否 | 更新频率,单位(天) 可选值: -1:不自动更新 1:每天更新 3:每3天更新 7:每7天更新 30:每30天更新 |
urlDepth | int | 否 | url下钻深度。 可选值: 1:只解析当前页面。 2:解析当前网页及子网页 |
响应头域
除公共头域外,无其它特殊头域。
响应参数
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
requestId | string | 是 | requestId |
documentIds | array | 是 | 存放文档id的数组 |
请求curl 示例
curl --location 'https://qianfan.baidubce.com/v2/knowledgeBase?Action=CreateDocuments' \
--header 'Authorization: Bearer authorization string' \
--header 'Content-Type: application/json' \
--data '{
"id":"4bcfc08f-0f30-4acd-95ec-2bedecc8f795",
"source": {
"type": "web",
"urlConfigs": [{
"url": "https://****.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwsj=1738819093326",
"updateFrequency": 1,
"urlDepth": 1
},
{
"url": "https://*******spage/data/landingsuper?context=%7B%22nid%22%3A%22news_8214091452491261937%22%7D&n_type=1&p_from=3",
"updateFrequency": 3,
"urlDepth": 2
}
]
},
"contentFormat": "rawText",
"processOption": {
"template": "custom",
"parser": {
"choices": ["layoutAnalysis"]
},
"chunker": {
"choices": ["separator"],
"separator": {
"separators": ["。", ",",","],
"targetLength": 300,
"overlapRate": 0.25
},
"prependInfo": ["title", "filename"]
}
}
}'
正确响应示例
HTTP/1.1 200 OK
{
"requestId": "c0650988-f263-4bb1-be4b-e5c4dc50513a",
"documentIds": [
"3447f80c-e321-4101-bb5b-9e3f82a37c6b",
"33f8a63f-fa95-4c93-abd7-3c40a881ba5b",
"8d5ebac3-90ec-4c64-a06f-539fb8753a0a"
]
}
错误响应示例
HTTP/1.1 400
{
"code": "InvalidRequestArgumentError",
"message": "Dataset Not Found",
"requestId": "9873898f-4af9-42da-b670-7cd71de8c4d2"
}