欢迎回来

登录 EAKE AI,继续您的智能之旅

忘记密码?
还没有账号?立即注册

百度文心一言 API 使用手册

2026-05-08 · 使用手册

一、概述

文心一言(ERNIE Bot)是百度推出的大语言模型服务,提供 ERNIE 系列模型的 API 访问。支持文本生成、对话、函数调用、图像理解、文生图等能力。国内首批通过大模型备案,企业级服务成熟。

二、认证

采用 Access Token 方式认证,通过 API Key + Secret Key 获取:

# 获取 Access Token
curl https://aip.baidubce.com/oauth/2.0/token 
  -d "grant_type=client_credentials&client_id=YOUR_API_KEY&client_secret=YOUR_SECRET_KEY"

# 返回
{"access_token": "24.xxxxxxxxxxxx", "expires_in": 2592000}

Access Token 有效期30天,建议缓存复用。

API Key 和 Secret Key 在 千帆控制台 创建。

三、Base URL

https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop

四、模型列表

模型端点路径上下文说明
ERNIE 4.0/chat/completions_pro128K旗舰模型
ERNIE 3.5/chat/completions96K通用模型
ERNIE Speed/chat/ernie-speed-128k128K快速模型
ERNIE Lite/chat/ernie-lite-8k8K轻量模型
ERNIE Character/chat/ernie-char-8k8K角色扮演
ERNIE Functions/chat/ernie-func-8k8K函数调用
ERNIE-ViLG/txt2img-文生图

五、Chat 请求

POST https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=xxx

{
  "messages": [
    {"role": "user", "content": "你好"}
  ],
  "temperature": 0.7,
  "top_p": 0.9,
  "max_output_tokens": 2048,
  "stream": false,
  "system": "你是文心一言助手"
}

参数说明

参数类型必填说明
messagesarray消息数组
temperaturefloat0-1,默认0.95
top_pfloat0-1,默认0.7
max_output_tokensint最大输出
streambool流式输出
systemstring系统提示词
functionsarray函数定义(Function Calling)
penalty_scorefloat1-2,重复惩罚

响应

{
  "id": "as-xxx",
  "object": "chat.completion",
  "created": 1717100000,
  "result": "你好!我是文心一言,有什么可以帮你的?",
  "is_truncated": false,
  "need_clear_history": false,
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 15,
    "total_tokens": 20
  }
}

六、Function Calling

"functions": [{
  "name": "get_weather",
  "description": "获取天气信息",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {"type": "string", "description": "城市名"}
    },
    "required": ["city"]
  }
}]

模型返回 function_call 字段时,执行对应函数并将结果追加到 messages 中继续对话。

七、流式输出

curl "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=xxx" 
  -H "Content-Type: application/json" 
  -d '{"messages":[{"role":"user","content":"你好"}],"stream":true}'

返回 Server-Sent Events (SSE) 格式数据流。

八、文生图

POST https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/txt2img?access_token=xxx

{
  "prompt": "一只在赛博朋克城市中的白色猫",
  "n": 1,
  "size": "1024x1024",
  "style": "vivid"
}

九、OpenAI 兼容模式

千帆平台同时提供 OpenAI 兼容接口,可直接使用 OpenAI SDK:

from openai import OpenAI

client = OpenAI(
    api_key="your-api-key",
    base_url="https://qianfan.baidubce.com/v1"
)

response = client.chat.completions.create(
    model="ernie-4.0-8k",
    messages=[{"role": "user", "content": "你好"}]
)

十、定价

模型输入价格输出价格
ERNIE 4.0¥30/1M tokens¥90/1M tokens
ERNIE 3.5¥4/1M tokens¥8/1M tokens
ERNIE Speed免费免费
ERNIE Lite免费免费

ERNIE Speed 和 ERNIE Lite 免费使用,适合开发测试。

十一、速率限制

层级RPMTPM
免费3-10限制较低
付费60-300按模型不同

十二、错误码

错误码含义处理
1参数错误检查请求参数
2服务暂不可用稍后重试
3权限不足检查 Access Token
17请求频率超限退避重试
18QPS 超限降低并发
110Access Token 无效重新获取
336100内容合规拦截修改输入内容

评论区

发表评论