摘要:本文梳理百度文心的三种使用方式------普通用户直接通过文心 APP / 官网体验,开发者则可通过百度智能云千帆平台或百度 AI Studio 调用 API。文中重点推荐千帆平台的 OpenAI 兼容模式,给出环境准备、凭证获取、核心配置与 Python 调用示例,并对比千帆与 AI Studio 在接入门槛、计费方式、模型范围、OpenAI SDK 兼容性和适用场景上的差异,最后补充文生图调用与密钥安全等注意事项,帮助读者快速选型并跑通第一个调用。
百度文心目前主要通过三种方式使用:面向普通用户的文心 APP / 官网 、面向开发者的千帆平台 API ,以及通过百度 AI Studio 调用。
🖥️ 普通用户:直接体验
如果你只是想直接和文心对话或使用其能力,最直接的方式是访问文心一言官网 或下载文心 APP。在这里可以直接使用文心 5.0 等最新模型进行对话、创作、分析文档等。
💻 开发者:通过 API 调用
对于需要将文心集成到自己应用中的开发者,主要有两个平台入口:
1. 百度智能云千帆平台(企业级/主流选择)
这是百度官方面向企业和开发者提供的大模型平台,支持文心全系模型(如文心 5.5 Flash Preview、文心 5.0、ERNIE 系列等)的 API 调用。
- 接入方式 :使用标准的 Chat Completions 接口。你需要获取千帆的 API Key (通常以
bce-v3/开头),并将请求地址指向https://qianfan.baidubce.com/v2。 - 计费参考:不同模型定价不同。例如文心 5.5 Flash Preview 的 API 定价为每百万 Token 输入忙时 2 元、闲时 1 元,输出忙时 8 元、闲时 4 元(常有折扣活动)。
2. 百度 AI Studio(适合个人开发者/快速验证)
如果习惯使用 OpenAI 的 SDK,AI Studio 提供了兼容的接口。
- 接入方式 :需要获取 AI Studio 的访问令牌(API Key),并将
base_url设置为https://aistudio.baidu.com/llm/lmapi/v3。之后即可用 Python 的openai库调用文生图、向量化或对话模型。
代码示例(千帆平台调用):
bash
curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: your-api-key' \
--data '{
"model": "ernie-5.5-flash-preview",
"messages": [{"role": "user", "content": "hello"}]
}'
针对开发接入场景,目前最推荐的方式是走百度智能云千帆平台 ,并且优先使用其 OpenAI 兼容接口,这样可以直接复用现有的 OpenAI SDK 代码,迁移成本最低。
推荐路径:千帆 OpenAI 兼容模式
这是目前最省事的接入方式。不需要处理复杂的鉴权签名,只需一个以 bce-v3/ 开头的 API Key。
0. 环境准备
在开始调用之前,先把本地环境准备好:
bash
pip install openai
- openai 库版本 :建议使用
1.0.0及以上版本(如pip install "openai>=1.0")。1.x 系列采用OpenAI客户端类写法,与下文示例一致;0.x 旧版的openai.ChatCompletion接口已废弃,不建议使用。 - Python 版本 :建议 Python 3.8 及以上(推荐 3.10+),以保证与最新版
openai库兼容。
1. 获取凭证
登录百度智能云千帆控制台,创建应用后获取 API Key(格式通常为 bce-v3/ALTAK-xxxx/xxxx)。
2. 核心配置
在代码中,将 base_url 指向千帆的兼容端点,并传入你的 API Key。
| 配置项 | 值 |
|---|---|
| Base URL | https://qianfan.baidubce.com/v2 |
| API Key | 你的千帆 API Key(bce-v3/...) |
| 模型名称 | 例如 ernie-4.0-turbo-8k、ernie-3.5-8k 等 |
3. Python 代码示例
python
from openai import OpenAI
client = OpenAI(
api_key="bce-v3/你的API_KEY",
base_url="https://qianfan.baidubce.com/v2"
)
response = client.chat.completions.create(
model="ernie-4.0-turbo-8k",
messages=[{"role": "user", "content": "你好,请介绍一下你自己"}]
)
print(response.choices[0].message.content)
备选路径:AI Studio(适合个人快速验证)
如果只是想低成本试用或做原型验证,可以用百度 AI Studio 的令牌。它同样兼容 OpenAI 格式,且新用户通常有免费额度。
- Base URL :
https://aistudio.baidu.com/llm/lmapi/v3 - API Key:在 AI Studio 个人中心的"访问令牌"页面获取 。
- 调用方式 :同样使用
openai库,仅需替换base_url和api_key。
两条接入路径对比
为方便快速选型,下面从接入门槛、计费方式、适用模型范围、是否兼容 OpenAI SDK、适用场景五个维度,对千帆平台与百度 AI Studio 做一次横向对比:
| 对比维度 | 千帆平台(OpenAI 兼容模式) | 百度 AI Studio |
|---|---|---|
| 接入门槛 | 需注册百度智能云账号、实名认证并创建应用,获取 bce-v3/ 开头的 API Key,流程偏企业级 |
注册 AI Studio 账号即可在个人中心获取访问令牌,门槛更低,适合个人快速上手 |
| 计费方式 | 按 Token 计费,不同模型定价不同,区分忙时/闲时,常有折扣活动 | 新用户通常有免费额度,超出后按平台规则计费,适合低成本试用 |
| 适用模型范围 | 覆盖文心全系模型(文心 5.5 Flash Preview、文心 5.0、ERNIE 系列等),模型选择更全 | 以对话、向量化、文生图等常用模型为主,覆盖面相对有限 |
| 是否兼容 OpenAI SDK | 兼容,base_url 指向 https://qianfan.baidubce.com/v2,可直接复用 OpenAI SDK |
兼容,base_url 指向 https://aistudio.baidu.com/llm/lmapi/v3,同样使用 openai 库 |
| 适用场景 | 企业级生产环境、需要稳定 SLA 与全系模型、长期规模化调用 | 个人学习、原型验证、低成本快速试用 |
| 下面用一张流程图帮你快速判断该走哪条路径: |
#mermaid-svg-yvDRIuNNG7tNeZjZ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yvDRIuNNG7tNeZjZ .error-icon{fill:#552222;}#mermaid-svg-yvDRIuNNG7tNeZjZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yvDRIuNNG7tNeZjZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .marker.cross{stroke:#333333;}#mermaid-svg-yvDRIuNNG7tNeZjZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yvDRIuNNG7tNeZjZ p{margin:0;}#mermaid-svg-yvDRIuNNG7tNeZjZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .cluster-label text{fill:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .cluster-label span{color:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .cluster-label span p{background-color:transparent;}#mermaid-svg-yvDRIuNNG7tNeZjZ .label text,#mermaid-svg-yvDRIuNNG7tNeZjZ span{fill:#333;color:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .node rect,#mermaid-svg-yvDRIuNNG7tNeZjZ .node circle,#mermaid-svg-yvDRIuNNG7tNeZjZ .node ellipse,#mermaid-svg-yvDRIuNNG7tNeZjZ .node polygon,#mermaid-svg-yvDRIuNNG7tNeZjZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .rough-node .label text,#mermaid-svg-yvDRIuNNG7tNeZjZ .node .label text,#mermaid-svg-yvDRIuNNG7tNeZjZ .image-shape .label,#mermaid-svg-yvDRIuNNG7tNeZjZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-yvDRIuNNG7tNeZjZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .rough-node .label,#mermaid-svg-yvDRIuNNG7tNeZjZ .node .label,#mermaid-svg-yvDRIuNNG7tNeZjZ .image-shape .label,#mermaid-svg-yvDRIuNNG7tNeZjZ .icon-shape .label{text-align:center;}#mermaid-svg-yvDRIuNNG7tNeZjZ .node.clickable{cursor:pointer;}#mermaid-svg-yvDRIuNNG7tNeZjZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .arrowheadPath{fill:#333333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yvDRIuNNG7tNeZjZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yvDRIuNNG7tNeZjZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yvDRIuNNG7tNeZjZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yvDRIuNNG7tNeZjZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .cluster text{fill:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ .cluster span{color:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yvDRIuNNG7tNeZjZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yvDRIuNNG7tNeZjZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-yvDRIuNNG7tNeZjZ .icon-shape,#mermaid-svg-yvDRIuNNG7tNeZjZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yvDRIuNNG7tNeZjZ .icon-shape p,#mermaid-svg-yvDRIuNNG7tNeZjZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yvDRIuNNG7tNeZjZ .icon-shape .label rect,#mermaid-svg-yvDRIuNNG7tNeZjZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yvDRIuNNG7tNeZjZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yvDRIuNNG7tNeZjZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yvDRIuNNG7tNeZjZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
是
否
开始选型
是否需要企业级 SLA 与全系模型?
走千帆平台(OpenAI 兼容模式)
base_url: https://qianfan.baidubce.com/v2
是否只需快速验证 / 低成本?
走百度 AI Studio
base_url: https://aistudio.baidu.com/llm/lmapi/v3
重新评估需求,或先按千帆方案做原型
一句话总结:要上生产、要全系模型选千帆;只想快速验证、控制成本选 AI Studio。
需要注意
如果后续要接入文生图(如 Stable-Diffusion-XL),在 AI Studio 兼容接口下也可以直接通过 client.images.generate 调用。下面是一段完整的 Python 示例,包含 base_url、api_key 配置、prompt 参数,以及把返回结果保存为图片文件的写法:
python
import base64
from openai import OpenAI
# 1. 配置客户端:AI Studio 兼容 OpenAI 格式
client = OpenAI(
api_key="你的 AI Studio 访问令牌",
base_url="https://aistudio.baidu.com/llm/lmapi/v3"
)
# 2. 发起文生图请求
response = client.images.generate(
model="Stable-Diffusion-XL",
prompt="一只坐在窗台上的橘猫,午后阳光,写实风格,高清细节",
size="1024x1024",
n=1,
response_format="b64_json" # 要求返回 base64 编码的图片数据
)
# 3. 将返回结果保存为图片文件
image_b64 = response.data[0].b64_json
with open("output.png", "wb") as f:
f.write(base64.b64decode(image_b64))
print("图片已保存为 output.png")
几点说明:
response_format:设为b64_json时返回 base64 数据,便于直接落盘;若平台支持返回 URL,也可改为url后用requests下载。size与n:按模型支持的分辨率和数量填写,不同模型可选项可能不同。- 异常处理 :生产环境建议用
try/except包裹请求,并注意文生图通常按张计费。
另外,生产环境记得妥善保管 Key,不要硬编码在前端代码里。
。