导入知识库
更新时间:2025-04-01
接口描述
导入到知识库,当前仅支持单个web的url导入。
权限说明
调用本API,需使用API Key鉴权方式。Authorization的值为Bearer <AppBuilder API Key>。获取API Key流程,请查看授权
接口定义
Path | /v2/knowledgeBase?Action=CreateDocuments |
---|---|
Method | POST |
Content-Type | application/json |
Authorization | 请求签名(Bearer <AppBuilder API Key>) |
请求结构
Plain Text
1POST /v2/knowledgeBase?action=CreateDocuments HTTP/1.1
2HOST: qianfan.baidubce.com
3Authorization: authorization string
4Content-Type: application/json
5
6{
7 "id":"knowledgeBaseID",
8 "source": { # 数据来源
9 "type": "web", # web
10 "urls": [],
11 "urlConfigs": [{
12 "url": "https://***tax.gov.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwm?Wjgs=pdf&Jym=9223&Fphm=25112000000023412&Czsj=1738819093326",
13 "updateFrequency": 1,
14 "urlDepth": 1
15 },
16 {
17 "url": "https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_8214091452491261937%22%7D&n_type=1&p_from=3",
18 "updateFrequency": 3,
19 "urlDepth": 2
20 }
21 ]
22 },
23 "contentFormat": "", # rawText (允许配置后续分割策略), qa(不支持配置后续分割策略),导入知识库只支持rawText
24 "processOption": {
25 "template": "ppt" # 可选项为 ppt、paper、qaPair、resume、custom、default
26 "parser": { # 不是必填项
27 "choices": ["layoutAnalysis"] # 可选项为 layoutAnalysis、ocr
28 },
29 "chunker": {
30 "choices": ["separator"], # separator, pattern, onePage
31 "separator": { #当choices中包含 separator时需要填写这个参数
32 "separators": [",", "!", "?"], # separator,可以新增分页符
33 "targetLength": 333,
34 "overlapRate": 0.09
35 },
36 // "pattern": { #当choices中包含 pattern时需要填写这个参数
37 // "markPosition": "head",
38 // "regex": "正则表达式",
39 // "targetLength": 333,
40 // "overlapRate": 0.09
41 // }
42 },
43 "knowledgeAugmentation": {
44 "choices": ["faq"], # faq,spokenQuery、spo、shortSummary按需增加
45 }
46 }
47}
请求头域
除公共头域外,无其它特殊头域。
请求参数
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
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:增加文件名 |
processOption \ knowledgeAugmentation | dict | 否 | 数据增强策略 |
processOption \ knowledgeAugmentation \ choices | []string | 否 | faq:问题生成。通过该字段生成的问题在段落中一定存在明确答案。 spokenQuery:问题生成。通过该字段生成的问题是和段落相关的。 spo:三元知识抽取 shortSummary:段落总结。 以上字段按需增加。 其中如果想要具备问题生成能力,则faq和spokenQuery必须都要传。 |
tags | []dict | 否 | 配置标签 |
tag\key | string | 是 | 标签 key 可包含大小写字母、数字、中文以及-_ /.特殊字符,长度1-50 |
tag\values | []string | 是 | 标签value 可包含大小写字母、数字、中文以及-_ /.特殊字符,长度1-50 |
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 示例
Plain Text
1curl --location 'https://qianfan.baidubce.com/v2/knowledgeBase?Action=CreateDocuments' \
2--header 'Authorization: Bearer authorization string' \
3--header 'Content-Type: application/json' \
4--data '{
5 "id":"4bcfc08f-0f30-4acd-95ec-2bedecc8f795",
6 "source": {
7 "type": "web",
8 "urlConfigs": [{
9 "url": "https://****.cn:8443/kpfw/fpjfzz/v1/exportDzfpwjEwsj=1738819093326",
10 "updateFrequency": 1,
11 "urlDepth": 1
12 },
13 {
14 "url": "https://*******spage/data/landingsuper?context=%7B%22nid%22%3A%22news_8214091452491261937%22%7D&n_type=1&p_from=3",
15 "updateFrequency": 3,
16 "urlDepth": 2
17 }
18 ]
19 },
20 "contentFormat": "rawText",
21 "processOption": {
22 "template": "custom",
23 "parser": {
24 "choices": ["layoutAnalysis"]
25 },
26 "chunker": {
27 "choices": ["separator"],
28 "separator": {
29 "separators": ["。", ",",","],
30 "targetLength": 300,
31 "overlapRate": 0.25
32 },
33 "prependInfo": ["title", "filename"]
34 }
35 },
36 "tags": [
37 {
38 "key": "娱乐",
39 "values": [
40 "电影"
41 ]
42 },
43 {
44 "key": "动物",
45 "values": [
46 "爬行动物"
47 ]
48 },
49 {
50 "key": "实时新闻",
51 "values": [
52 "实时", "过时"
53 ]
54 }
55 ]
56}'
正确响应示例
Plain Text
1HTTP/1.1 200 OK
2{
3 "requestId": "c0650988-f263-4bb1-be4b-e5c4dc50513a",
4 "documentIds": [
5 "3447f80c-e321-4101-bb5b-9e3f82a37c6b",
6 "33f8a63f-fa95-4c93-abd7-3c40a881ba5b",
7 "8d5ebac3-90ec-4c64-a06f-539fb8753a0a"
8 ]
9}
错误响应示例
Plain Text
1HTTP/1.1 400
2{
3 "code": "InvalidRequestArgumentError",
4 "message": "Dataset Not Found",
5 "requestId": "9873898f-4af9-42da-b670-7cd71de8c4d2"
6}