通用文档抽取 (算法编码:8000400029)
协议类型:HTTPS
一、接入点
| 环境 |
地址 |
| 公网 |
https://api-maas.teleai.com.cn/aipaas/ocr/v1/kie/ocr |
- API Path :
/aipaas/ocr/v1/kie/ocr
- 请求方法 :
POST
- Content-Type :
application/json
二、鉴权
采用请求头携带鉴权信息:
2.1 标准鉴权
Authorization: <鉴权信息>
| 请求头 |
必填 |
说明 |
Content-Type |
否 |
内容类型,固定 application/json |
X-APP-ID |
否 |
取值为控制台 → 应用及API Key → 创建应用 → AppID |
Authorization |
是 |
鉴权信息,生成方式见上方链接 |
2.2 Bearer Token 鉴权
Authorization: Bearer <用户申请的API-KEY>
| 请求头 |
必填 |
说明 |
Content-Type |
否 |
内容类型,固定 application/json |
Authorization |
是 |
Bearer <API_KEY> |
三、能力简介
自定义字段抽取(KIE,Key Information Extraction)接口根据用户自定义的 schema 字段定义,对 jpg/png/pdf/webp 格式的输入文件进行信息提取,返回"字段-值"的 JSON 结构化输出,支持 HTML 转义保护。
- 自定义字段抽取 :根据
schema 中定义的字段,从图片或 PDF 中提取对应信息
- 结构化输出 :返回"字段-值" JSON 结构
- HTML 转义保护 :
html_escape=true 时对识别文本做 HTML 转义,防止 XSS
- 支持格式 :jpg/jpeg/png/webp
- PDF 处理 :当前仅支持 PDF 首页处理
- 图片限制 :base64 编码后不超过 10MB,最短边至少 20px
- schema 字段类型 :V1.0 仅支持 String 类型字段
3.1 修订历史
| 修订日期 |
算法版本号 |
修订内容 |
| 2026-10-20 |
V1.0 |
初始版本发布,支持自定义字段抽取(String 类型) |
四、请求与响应
4.1 请求参数
4.1.1 顶层字段
| 字段 |
类型 |
必填 |
说明 |
seqid |
string |
是 |
接口跟踪 ID,每次请求需不同,可用 UUID 生成 |
image |
string |
是 |
图片或文件的 Base64 编码或 URL 地址,由 image_type 决定 |
image_type |
int |
是 |
图片来源:0=URL,1=base64,默认 0 |
html_escape |
boolean |
否 |
是否对识别文本进行 HTML 转义,默认 true |
schema |
object |
是 |
抽取字段及定义,标准 JSON Schema 格式,V1.0 仅支持 String 类型字段 |
4.1.2 schema
标准 JSON Schema 对象,定义需要抽取的字段名称与类型。V1.0 仅支持 type: "string" 字段。
| 字段 |
类型 |
必填 |
说明 |
type |
string |
是 |
固定为 "object" |
properties |
object |
是 |
字段定义映射,key 为字段名,value 为字段类型定义对象,需包含 {"type": "string"} |
schema 示例:
{
"type": "object",
"properties": {
"姓名": { "type": "string" },
"编号": { "type": "string" },
"地址": { "type": "string" }
}
}
4.2 响应体
4.2.1 顶层字段
| 字段 |
类型 |
说明 |
seqid |
string |
调用时传入的跟踪 ID |
code |
string |
状态码,10000 为成功 |
message |
string |
调用结果描述 |
flag |
int |
调用状态:1=成功,0=失败 |
data |
object |
返回数据对象,见 4.2.2 |
4.2.2 data
| 字段 |
类型 |
说明 |
time_second |
double |
识别耗时(秒) |
words_result |
object |
提取结果,key 为 schema 中定义的字段名,value 为提取到的文本值(未提取到时为空字符串) |
usage |
object |
token 用量信息,见 4.2.3 |
total_page_number |
int |
处理文档页数 |
4.2.3 data > usage
| 字段 |
类型 |
说明 |
completion_tokens |
int |
生成 tokens 数 |
prompt_tokens |
int |
prompt tokens 数 |
total_tokens |
int |
总 tokens 数 |
五、状态码说明
5.1 算法状态码
| 状态码 |
说明 |
描述 |
10000 |
请求成功 |
服务执行成功 |
10301 |
服务必填参数缺失 |
缺少必填参数,请检查请求体字段 |
10304 |
服务参数错误 |
参数类型或取值错误,请检查 image_type、schema 等字段 |
10305 |
图片相关错误 |
图片大小超限、格式不支持或图片获取失败 |
10903 |
服务执行失败 |
推理异常或服务内部执行失败,请重试或联系技术支持 |
5.2 公共状态码
- 接口响应的公共状态码由网关统一定义,一般用于标识鉴权失败、限流或服务异常,具体业务错误码、错误信息及排查建议请参考:开发指南-公共信息及状态码。
六、调用示例
6.1 请求示例
{
"seqid": "test_seq0",
"image": "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgIC...",
"image_type": 1,
"html_escape": true,
"schema": {
"type": "object",
"properties": {
"姓名": { "type": "string" },
"编号": { "type": "string" },
"地址": { "type": "string" }
}
}
}
6.2 成功响应示例
{
"seqid": "req-20260310-7b8c9d2e",
"code": "10000",
"message": "成功",
"flag": 1,
"data": {
"time_second": 0.452,
"words_result": {
"姓名": "张三",
"编号": "",
"地址": "北京市朝阳区xx路xx号"
},
"usage": {
"completion_tokens": 40,
"prompt_tokens": 23,
"total_tokens": 63
},
"total_page_number": 1
}
}
6.3 CURL
curl -X POST 'https://api-maas.teleai.com.cn/aipaas/ocr/v1/kie/ocr' \
-H 'Content-Type: application/json' \
-H 'X-APP-ID: <your_app_id>' \
-H 'Authorization: <your_authorization>' \
-d '{
"seqid": "test_seq0",
"image": "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgIC...",
"image_type": 1,
"html_escape": true,
"schema": {
"type": "object",
"properties": {
"姓名": { "type": "string" },
"编号": { "type": "string" },
"地址": { "type": "string" }
}
}
}'
6.4 Python
import base64
import hashlib
import hmac
import json
import time
import urllib.parse
import uuid
import requests
# ===== 配置 =====
X_APP_ID = "your_app_id"
X_APP_KEY = "your_app_key"
REGION = "BJ"
EXPIRATION_IN_SECONDS = "43200"
SIGNED_HEADERS = "x-app-id"
SIGNING_HEADER = "teleai-cloud-auth-v1"
API_URL = "https://api-maas.teleai.com.cn/aipaas/ocr/v1/kie/ocr"
def normalize(path):
"""对 URI 路径做 RFC 3986 规范化编码"""
return urllib.parse.quote(path, safe="/")
def generate_canonical_uri(url):
"""从完整 URL 提取并规范化 canonical URI"""
parsed = urllib.parse.urlparse(url)
path = parsed.path if parsed.path else "/"
return normalize(path)
def generate_canonical_headers(signed_headers_dict):
"""生成 canonical headers 字符串"""
header_list = []
for key in sorted(signed_headers_dict.keys()):
value = signed_headers_dict[key].strip()
header_list.append(f"{key}:{value}\n")
return "".join(header_list)
def get_authorization(app_id, app_key, method, uri, headers, region,
expiration, signed_headers):
"""生成 Authorization 鉴权头"""
timestamp = int(time.time())
date = time.strftime("%Y-%m-%d", time.localtime(timestamp))
# 组装 signed headers 字典
signed_header_keys = signed_headers.split(";")
signed_headers_dict = {}
for k in signed_header_keys:
k = k.strip().lower()
if k in headers:
signed_headers_dict[k] = headers[k]
canonical_headers = generate_canonical_headers(signed_headers_dict)
signed_header_list = ";".join(sorted(signed_headers_dict.keys()))
# 构造签名字符串
canonical_request = (
f"{method.upper()}\n"
f"{uri}\n\n"
f"{canonical_headers}\n"
f"{signed_header_list}"
)
# 派生签名密钥
k_date = hmac.new(
app_key.encode("utf-8"), date.encode("utf-8"), hashlib.sha256
).hexdigest()
k_region = hmac.new(
k_date.encode("utf-8"), region.encode("utf-8"), hashlib.sha256
).hexdigest()
k_signing = hmac.new(
k_region.encode("utf-8"),
SIGNING_HEADER.encode("utf-8"),
hashlib.sha256,
).hexdigest()
# 计算签名
signature = hmac.new(
k_signing.encode("utf-8"),
canonical_request.encode("utf-8"),
hashlib.sha256,
).hexdigest()
authorization = (
f"{SIGNING_HEADER} "
f"Credential={app_id}/{date}/{region}/{SIGNING_HEADER}, "
f"SignedHeaders={signed_header_list}, "
f"Signature={signature}, "
f"Expires={expiration}"
)
return authorization
def encode_image(image_path):
"""将本地图片编码为 base64 字符串"""
with open(image_path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
def main():
seqid = str(uuid.uuid4()).replace("-", "")[:32]
image_base64 = encode_image("test.jpg")
body = {
"seqid": seqid,
"image": image_base64,
"image_type": 1,
"html_escape": True,
"schema": {
"type": "object",
"properties": {
"姓名": {"type": "string"},
"编号": {"type": "string"},
"地址": {"type": "string"},
},
},
}
uri = generate_canonical_uri(API_URL)
headers = {
"Content-Type": "application/json",
"X-APP-ID": X_APP_ID,
}
authorization = get_authorization(
X_APP_ID, X_APP_KEY, "POST", uri, headers,
REGION, EXPIRATION_IN_SECONDS, SIGNED_HEADERS,
)
headers["Authorization"] = authorization
response = requests.post(API_URL, headers=headers, json=body)
result = response.json()
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
6.5 Java
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class KieOcrExample {
public static void main(String[] args) {
String appId = "your_app_id";
String authorization = "your_authorization";
String url = "https://api-maas.teleai.com.cn/aipaas/ocr/v1/kie/ocr";
// 构建 schema
JSONObject schema = new JSONObject();
schema.put("type", "object");
JSONObject properties = new JSONObject();
properties.put("姓名", new JSONObject().fluentPut("type", "string"));
properties.put("编号", new JSONObject().fluentPut("type", "string"));
properties.put("地址", new JSONObject().fluentPut("type", "string"));
schema.put("properties", properties);
// 读取图片并 base64 编码
byte[] imageBytes = java.nio.file.Files.readAllBytes(
java.nio.file.Paths.get("test.jpg")
);
String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
// 构建请求体
JSONObject body = new JSONObject();
body.put("seqid", "test_seq0");
body.put("image", imageBase64);
body.put("image_type", 1);
body.put("html_escape", true);
body.put("schema", schema);
// 构建请求头
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("X-APP-ID", appId);
headers.put("Authorization", authorization);
// 发送请求
HttpResponse response = HttpRequest.post(url)
.addHeaders(headers)
.body(body.toJSONString())
.execute();
System.out.println(response.body());
}
}