开始之前
各位铁子需要先自行注册百度千帆大模型账号,注意任何免费服务都有局限
一、创建应用
1、访问 应用管理页,点击创建应用
2、输入名称和描述,点击确定,然后复制你的 API Key 和 Secret Key备用
二、开通服务
1、访问 计费管理页,点击开通服务
2、勾选模型
3、提交订单
4、开通成功
三、配置oneapi
1、模型
- eb-instant
- ernie-lite-8k
- ernie_speed
- ernie-speed-128k
2、Key
填入 API KEY|API SECRECT,注意两个值需要用管道分隔
3、代理
可选,老版本oneapi必须填写,worker反代直接填worker地址即可,worker代码下方自取
注意:官方虽然宣布支持speed和lite的8k以及128k模型,但是作者在默认列表没找到lite的128k
四、Worker反代(可选)
旧版oneapi并不支持百度的模型,可通过下面的代码用cloudflare的worker反代解决,另外通过反代可实现客户端可直接使用,渠道选择 OpenAI 自定义模型名即可
worker.js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const authHeader = request.headers.get('authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return new Response('Missing or invalid authorization header', { status: 401 });
}
const token = authHeader.substring(7);
const [client_id, client_secret] = token.split('|');
if (!client_id || !client_secret) {
return new Response('Invalid token format', { status: 400 });
}
const tokenResponse = await fetch(`https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}`);
const tokenData = await tokenResponse.json();
if (!tokenResponse.ok || !tokenData.access_token) {
return new Response('Failed to fetch access token', { status: 500 });
}
const access_token = tokenData.access_token;
const requestBody = await request.json();
const model = requestBody.model;
if (!model) {
return new Response('Missing model in request body', { status: 400 });
}
const newRequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
};
const apiUrl = `https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/${model}?access_token=${access_token}`;
const newRequest = new Request(apiUrl, newRequestInit);
const response = await fetch(newRequest);
const data = await response.text();
const formattedResponse = streamResponse(data, model);
return new Response(formattedResponse, {
status: response.status,
headers: {
...response.headers,
'Content-Type': 'text/event-stream'
}
});
}
function streamResponse(data, model) {
const encoder = new TextEncoder();
const currentTimestamp = Math.floor(Date.now() / 1000);
const lines = data.split('\n').filter(line => line.trim());
return new ReadableStream({
async start(controller) {
for (const line of lines) {
const json = JSON.parse(line.substring(6)); // Remove 'data: ' prefix
const result = {
id: `chatcmpl-${currentTimestamp}`,
object: "chat.completion.chunk",
created: currentTimestamp,
model: model,
choices: [
{
index: 0,
delta: { content: json.result },
finish_reason: null
}
]
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(result)}\n\n`));
// Check if this is the last chunk
if (json.is_end) {
const endResult = {
id: `chatcmpl-${currentTimestamp}`,
object: "chat.completion.chunk",
created: currentTimestamp,
model: model,
choices: [
{
index: 0,
delta: {},
finish_reason: "stop"
}
]
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(endResult)}\n\n`));
break;
}
}
controller.close();
}
});
}
参数
base_url:https://localhost/v1/chat/completions
model:eb-instant、ernie-lite-8k、ernie_speed、ernie-speed-128k
key:API KEY | API SECRECT
POST示例
curl https://localhost/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <API KEY>|<API SECRECT> \
-d '{
"model": "ernie_speed",
"messages": [
{
"role": "user",
"content": "写一篇1k字的小说"
}
],
"stream":true
}'