当前位置:首页点滴记忆百度免费模型食用指南

百度免费模型食用指南

 

开始之前

各位铁子需要先自行注册百度千帆大模型账号,注意任何免费服务都有局限

一、创建应用

1、访问 应用管理页,点击创建应用

百度免费模型食用指南

2、输入名称和描述,点击确定,然后复制你的 API Key 和 Secret Key备用

百度免费模型食用指南

二、开通服务

1、访问 计费管理页,点击开通服务

百度免费模型食用指南

2、勾选模型

百度免费模型食用指南

3、提交订单

百度免费模型食用指南

4、开通成功

百度免费模型食用指南

 

 

三、配置oneapi

开源地址:One API (justsong.cn)

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
  }'

参考链接

给TA打赏
共{{data.count}}人
人已打赏
资源分享

[PC/安卓]网易云音乐绿化版

2022-10-19 11:49:23

一些小笔记

软件体系结构整理分享

2020-2-23 13:55:43

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索

Warning: Undefined array key "avatar" in /www/wwwroot/blog.xzbzq.com/wp-content/themes/b2Jitheme/jitheme_functions.php on line 397

Warning: Undefined array key "name" in /www/wwwroot/blog.xzbzq.com/wp-content/themes/b2Jitheme/jitheme_functions.php on line 397

Warning: Undefined array key "avatar" in /www/wwwroot/blog.xzbzq.com/wp-content/themes/b2Jitheme/jitheme_functions.php on line 397

Warning: Undefined array key "name" in /www/wwwroot/blog.xzbzq.com/wp-content/themes/b2Jitheme/jitheme_functions.php on line 397
  • 纯七对文章单身三年现状发布评论!
  • 漂流的风对文章复杂世界的明白人发布评论!
  • 漂流的风对文章单身三年现状发布评论!
  • 漂流的风对文章讲个故事发布评论!
  • 漂流的风点赞了文章单身三年现状超级牛B!
  • 漂流的风点赞了文章复杂世界的明白人超级牛B!
  • 纯七对文章Chevereto V4.0.7 开心版发布评论!
  • 对文章Chevereto V4.0.7 开心版发布评论!