npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qiandan-wt

v0.0.1-beta.1

Published

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

Downloads

3

Readme

百度千帆大模型平台 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();

图像

文生图

根据用户输入的文本生成图片。

模型支持列表 Stable-Diffusion-XL

import * as http from 'http';
import {Text2Image} from "@baiducloud/qianfan";
// 直接读取 env  
const client = new Text2Image();

// 手动传 AK/SK 测试
// const client = new Text2Image({ QIANFAN_AK: '***', QIANFAN_SK: '***'});
async function main() {
    const resp = await client.text2Image({
        prompt: '生成爱莎公主的图片',
        size: '768x768',
        n: 1,
        steps: 20,
        sampler_index: 'Euler a',
    }, 'Stable-Diffusion-XL');

    const base64Image = resp.data[0].b64_image;
    // 注意 base64Image没有带ata:image/jpeg;base64 前缀,要直接使用的话,需要加上
    // 创建一个简单的服务器
    const server = http.createServer((req, res) => {
        res.writeHead(200, {'Content-Type': 'text/html'});
        let html = `<html><body><img src="data:image/jpeg;base64,${base64Image}" /><br/></body></html>`;
        res.end(html);
    });
    const port = 3001;
    server.listen(port, () => {
        console.log(`服务器运行在 http://localhost:${port}`);
    });
}

main();

Plugin 插件

SDK支持使用平台插件能力,以帮助用户快速构建 LLM 应用或将 LLM 应用到自建程序中。支持知识库、智慧图问、天气等插件。

// 天气插件
async function main() {
    const resp = await client.plugins({
        query: '深圳今天天气如何',
        /** 
         *  插件名称
         * 知识库插件固定值为["uuid-zhishiku"] 
         * 智慧图问插件固定值为["uuid-chatocr"]
         * 天气插件固定值为["uuid-weatherforecast"]
         */ 
        plugins: [
            'uuid-weatherforecast',
        ],
    });
}

// 智慧图问
async function chatocrMain() {
    const resp = await client.plugins({
        query: '请解析这张图片, 告诉我怎么画这张图的简笔画',
        plugins: [
            'uuid-chatocr',
        ],
        fileurl: 'https://xxx.bcebos.com/xxx/xxx.jpeg',
    });
}

// 知识库
async function zhishikuMain() {
    const reps = await client.plugins({
        query: '你好什么时候飞行员需要负法律责任?',
        plugins: [
            'uuid-zhishiku',
        ],
    });
}

main();

// chatocrMain();

// zhishikuMain();

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

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

// 手动传 AK/SK 测试
// const client = new Plugins({ QIANFAN_AK: '***', QIANFAN_SK: '***'});
async function main() {
    const stream = await client.plugins({
        query: '深圳今天天气如何',
        /** 
         *  插件名称
         * 知识库插件固定值为["uuid-zhishiku"] 
         * 智慧图问插件固定值为["uuid-chatocr"]
         * 天气插件固定值为["uuid-weatherforecast"]
         */ 
        plugins: [
            'uuid-weatherforecast',
        ],
        stream: true,
    });
    for await (const chunk of stream as AsyncIterableIterator<any>) {
        // 返回结果
    }
}

main();