Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@baiducloud/qianfan

Package Overview
Dependencies
Maintainers
6
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@baiducloud/qianfan

针对百度智能云千帆大模型平台,我们推出了一套 JavaScript SDK(下称千帆 SDK),方便用户通过代码接入并调用千帆大模型平台的能力。

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38K
increased by8.53%
Maintainers
6
Weekly downloads
 
Created
Source

百度千帆大模型平台 JavaScript SDK

针对百度智能云千帆大模型平台,我们推出了一套 JavaScript SDK(下称千帆 SDK),方便用户通过代码接入并调用千帆大模型平台的能力。

license codecov NPM version NPM downloads docs Feedback Issue Feedback Ticket

如何安装

npm install @baiducloud/qianfan
# or
yarn add @baiducloud/qianfan

快速使用

鉴权

在使用千帆 SDK 之前,用户需要 百度智能云控制台 - 安全认证 页面获取 Access Key 与 Secret Key,并在 千帆控制台 中创建应用,选择需要启用的服务,具体流程参见平台 说明文档

SDK 支持从当前目录的 .env 中读取配置,也可以修改环境变量 QIANFAN_ACCESS_KEY 和 QIANFAN_SECRET_KEY ,同时支持初始化手动传入 AK/SK 。

env 读取
env 文件示例

在你项目的根目录中创建一个名为 .env 的文件,并添加以下内容:

QIANFAN_AK=your_access_key
QIANFAN_SK=your_secret_key
QIANFAN_ACCESS_KEY=another_access_key
QIANFAN_SECRET_KEY=another_secret_key
修改 env 的配置
import {setEnvVariable} from "@baiducloud/qianfan";
setEnvVariable('QIANFAN_AK','***');
setEnvVariable('QIANFAN_SK','***');
初始化手动传入 AK/SK
// 手动传 AK/SK 
const client = new ChatCompletion({ QIANFAN_AK: '***', QIANFAN_SK: '***'});
// 手动传 ACCESS_KEY / SECRET_KEY
const client = new ChatCompletion({ QIANFAN_ACCESS_KEY: '***', QIANFAN_SECRET_KEY: '***' });

Chat 对话

可以使用 ChatCompletion 对象完成对话相关操作

import {ChatCompletion} from "@baiducloud/qianfan";
// 直接读取 env
const client = new  ChatCompletion();
// 手动传 AK/SK 
// const client = new ChatCompletion({ QIANFAN_AK: '***', QIANFAN_SK: '***'});

async function main() {
    const resp = await client.chat({
        messages: [
            {
                role: "user",
                content: "今天深圳天气",
            },
        ],
    }, "ERNIE-Bot-turbo");
}

main();

参数传入 stream 为 true 时,返回流式结果

// 流式 测试
async function main() {
    const stream =  await client.chat({
          messages: [
              {
                  role: "user",
                  content: "等额本金和等额本息有什么区别?"
              },
          ],
          stream: true,
      }, "ERNIE-Bot-turbo");
      for await (const chunk of stream as AsyncIterableIterator<any>) {
           // 返回结果
        }
}

Completion 续写

对于不需要对话,仅需要根据 prompt 进行补全的场景来说,用户可以使用 Completions 来完成这一任务。

import {Completions} from "@baiducloud/qianfan";
// 直接读取 env  
const client = new Completions();

// 手动传 AK/SK
// const client = new Completions({ QIANFAN_AK: '***', QIANFAN_SK: '***'});

async function main() {
    const resp = await client.completions({
        prompt: 'Introduce the city Beijing',
    }, "SQLCoder-7B");
}

main();

参数传入 stream 为 true 时,返回流式结果

// 流式 
async function main() {
    const stream =  await client.completions({
        prompt: 'Introduce the city Beijing',
        stream: true,
      }, "SQLCoder-7B");
      for await (const chunk of stream as AsyncIterableIterator<any>) {
          // 返回结果
        }
}
main();

Embedding 向量化

千帆 SDK 同样支持调用千帆大模型平台中的模型,将输入文本转化为用浮点数表示的向量形式。转化得到的语义向量可应用于文本检索、信息推荐、知识挖掘等场景。

import {Eembedding} from "@baiducloud/qianfan";
// 直接读取 env  
const client = new Eembedding();

// 手动传 AK/SK 测试
// const client = new Eembedding({ QIANFAN_AK: '***', QIANFAN_SK: '***'});
async function main() {
    const resp = await client.embedding({
        input: [ 'Introduce the city Beijing'],
    }, "Embedding-V1");
}

main();

FAQs

Package last updated on 29 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc