本文作者:程序员晚枫 | AI编程布道者 | 专注AI工具测评与教学

全网40万+粉丝,6年Python开发经验,开源项目python-office作者

💡 想系统了解各大厂商 Coding Plan? 👉 点击查看 Coding Plan 对比汇总

大家好,这里是程序员晚枫。

今天带来百度智能云 Coding Plan 的实战教程,重点讲讲怎么用文心一言和飞桨进行 AI 编程开发。

一、文心一言 API 调用

第一步:获取 API Key

  1. 访问 👉 百度智能云 Coding Plan 详情
  2. 注册百度智能云账号
  3. 开通文心一言 API 服务
  4. 获取 API Key 和 Secret Key

第二步:代码调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
import json

# 百度 API 配置
ak = "你的API Key"
sk = "你的Secret Key"

# 获取 Access Token
def get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {
"grant_type": "client_credentials",
"client_id": ak,
"client_secret": sk
}
response = requests.post(url, params=params)
return response.json().get("access_token")

# 调用文心一言
def call_wenxin(prompt):
token = get_access_token()
url = f"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token={token}"

payload = {
"messages": [{"role": "user", "content": prompt}]
}

response = requests.post(url, json=payload)
return response.json()

# 使用示例
result = call_wenxin("帮我写一个Python的快速排序")
print(result)

二、飞桨开发实战

飞桨(PaddlePaddle)是百度自家的深度学习框架,如果你在用飞桨,百度 Coding Plan 可以提供更好的集成体验。

环境安装

1
2
pip install paddlepaddle
pip install paddlenlp

飞桨+文心代码示例

1
2
3
4
5
6
7
8
from paddlenlp import Taskflow

# 代码助手
code_helper = Taskflow("code_dialogue")

# 使用文心能力进行代码对话
result = code_helper("帮我写一个快速排序")
print(result)

三、IDE 插件接入

百度也提供了 VS Code 插件:

  1. 在 VS Code 扩展市场搜索「百度」
  2. 安装「百度 AI 代码助手」
  3. 填入 API Key
  4. 开始使用

四、常见问题

Q1:文心一言免费吗?

有新用户免费额度,具体以官方为准。

Q2:飞桨适合什么场景?

飞桨适合深度学习相关的开发,和百度 Coding Plan 结合更好。

Q3:百度服务稳定吗?

百度智能云服务稳定性在国内属于第一梯队。


相关阅读


📢 更多 Coding Plan 对比:👉 点击查看所有厂商的 Coding Plan


作者:程序员晚枫,全网同名,专注 AI 工具测评与 Python 自动化办公教学。


🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!


🤖 开发者效率工具推荐

👉 想体验 MiniMax Token Plan点击这里享受 9 折优惠

💡 按次计费,非常划算! 想象成去菜市场买菜——买张门票进去,菜随便拿。按使用次数收费,不限额度,用多少付多少,特别适合开发者!