上传文件到知识库
更新时间:2025-04-22
接口描述
上传文件到知识库
权限说明
调用本API,需使用API Key鉴权方式。Authorization的值为Bearer <AppBuilder API Key>。获取API Key流程,请查看授权
接口定义
Path | /v2/knowledgeBase?Action=UploadDocuments |
---|---|
Method | POST |
Content-Type | multipart/form-data |
Authorization | 请求签名(Bearer <AppBuilder API Key>) |
请求结构
Plain Text
1POST /v2/knowledgeBase?Action=UploadDocuments HTTP/1.1
2HOST: qianfan.baidubce.com
3Authorization: Bearer <AppBuilder API Key>
4Content-Type: multipart/form-data
5
6form-data参数:
7file:待上传的文件
8payload:json字符串,结构如下:
9{
10 "id":"knowledgeBaseID",
11 "source": { # 数据来源
12 "type": "file" # file
13 },
14 "contentFormat": "", # rawText (允许配置后续分割策略)
15 "processOption": {
16 "template": "ppt", # 可选项为 ppt、paper、qaPair、resume、custom、default
17 "parser": {
18 "choices": ["layoutAnalysis", "ocr"]
19 },
20 "chunker": {
21 "choices": ["separator"], # separator, pattern, onePage
22 "separator": { #当choices中包含 separator时需要填写这个参数
23 "separators": [",", "!", "?"], # separator,可以新增分页符
24 "targetLength": 333,
25 "overlapRate": 0.09
26 },
27 // "pattern": { #当choices中包含 pattern时需要填写这个参数
28 // "markPosition": "head",
29 // "regex": "正则表达式",
30 // "targetLength": 333,
31 // "overlapRate": 0.09
32 //},
33 "prependInfo": ["title", "filename"]
34 },
35 "knowledgeAugmentation": {
36 "choices": ["faq"], # faq,spokenQuery、spo、shortSummary按需增加
37 }
38 }
39}
请求头域
除公共头域外,无其它特殊头域。
请求参数
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
file | file | 是 | 待上传的文件。 支持.doc/.txt/.docx/.pdf/.ppt/.pptx 六种格式 .txt文件不能超过10MB .pdf文件不能超过500MB且不能超过3000页 其他类型文件不能超过50MB且不能超过1000页 账户共享资源知识文件上限为100GB |
payload | string | 是 | 知识库配置 |
payload参数的结构说明如下:
字段 | 类型 | 是否必须 | 说明 |
---|---|---|---|
id | string | 是 | 知识库id |
source | dict | 是 | 数据来源 |
source \ type | string | 是 | 数据来源类型 (file) |
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[]中包含时是 | 分隔符配置 可选分割符: "by_page": 分页符 "。":中文句号 ",":中文逗号 "?":中文问号 "!":中文叹号 ".":英文句号 "!":英文叹号 "?":英文问号 "……":英文省略号 |
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[]中包含时是 | 正则配置 |
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 |
响应头域
除公共头域外,无其它特殊头域。
响应参数
字段 | 类型 | 必然存在 | 说明 |
---|---|---|---|
requestId | string | 是 | requestId |
documentId | string | 是 | 文档id |
请求curl 示例
Plain Text
1curl --location --request POST 'https://qianfan.baidubce.com/v2/knowledgeBase?Action=UploadDocuments' \
2--header 'Authorization: Bearer authorization string' \
3--form 'file=@"/Users/Desktop/xxx.pdf"' \
4--form 'payload="{
5 \"id\": \"0f684052-2145-412c-8e9c-6f0e4736a509\",
6 \"source\": {
7 \"type\": \"file\"
8 },
9 \"contentFormat\": \"rawText\",
10 \"processOption\": {
11 \"template\": \"custom\",
12 \"parser\": {
13 \"choices\": [
14 \"layoutAnalysis\"
15 ]
16 },
17 \"chunker\": {
18 \"choices\": [
19 \"separator\"
20 ],
21 \"separator\": {
22 \"separators\": [
23 \"。\"
24 ],
25 \"targetLength\": 400,
26 \"overlapRate\": 0.25
27 },
28 \"prependInfo\": [
29 \"title\",
30 \"filename\"
31 ]
32 },
33 \"knowledgeAugmentation\": {
34 \"choices\": [
35 \"faq\"
36 ]
37 }
38 },
39 \"tags\": [
40 {
41 \"key\": \"娱乐\",
42 \"values\": [
43 \"电影\"
44 ]
45 },
46 {
47 \"key\": \"动物\",
48 \"values\": [
49 \"爬行动物\"
50 ]
51 }
52 ]
53 }
54"'
正确响应示例
Plain Text
1HTTP/1.1 200 OK
2{
3 "requestId": "46017562-637f-402a-9000-cfe535986d5d",
4 "documentId": "ee50852a-57ed-44aa-9b8a-6cb7156cf79c"
5}
错误响应示例
Plain Text
1HTTP/1.1 400
2{
3 "code": "InvalidRequestArgumentError",
4 "message": "Dataset Not Found",
5 "requestId": "9873898f-4af9-42da-b670-7cd71de8c4d2"
6}