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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ernie-ai-provider

v1.1.2

Published

Community-built ERNIE AI Provider for Vercel AI SDK - Integrate Baidu's ERNIE models with Vercel's AI application framework

Readme

ERNIE AI Provider for Vercel AI SDK

npm version npm downloads

百度千帆大模型的 Vercel AI SDK 简易版社区provider,支持 ERNIE 系列模型与 Vercel AI 的集成。

特性

  • 🚀 支持 ERNIE
  • 🎨 与 Vercel AI SDK 集成
  • 🌊 流式响应支持
  • 🎯 结构化数据生成(JSON 对象)
  • 📊 流式结构化对象生成(实时生成结构化数据)
  • 🛠️ 工具调用(Function Calling)
  • 🔧 TypeScript 支持
  • 🔍 网络搜索功能(实时信息获取)
  • 👤 系统人设和角色定制
  • 🎯 搜索过滤和域名限制
  • 📈 流式响应增强选项

安装

# 使用 npm
npm install ernie-ai-provider

# 使用 yarn
yarn add ernie-ai-provider

# 使用 pnpm
pnpm add ernie-ai-provider

环境变量

QIANFAN_API_KEY="your-api-key"

快速开始

基础用法

import { ernie } from 'ernie-ai-provider';
import { generateText } from 'ai';

const { text } = await generateText({
  model: ernie('ernie-4.0-8k'),
  prompt: '请写一个关于人工智能的简短介绍',
});

console.log(text);

自定义配置

import { createErnie } from 'ernie-ai-provider';
import { generateText } from 'ai';

const ernie = createErnie({
  apiKey: 'your-api-key',
  baseURL: 'https://qianfan.baidubce.com/v2', // 可选
});

const { text } = await generateText({
  model: ernie('ernie-4.0-8k'),
  prompt: '请解释什么是机器学习',
});

流式响应

import { ernie } from 'ernie-ai-provider';
import { streamText } from 'ai';

const { textStream } = await streamText({
  model: ernie('ernie-4.0-8k'),
  prompt: '请详细解释机器学习的基本概念',
});

for await (const delta of textStream) {
  process.stdout.write(delta);
}

结构化数据生成

import { ernie } from 'ernie-ai-provider';
import { generateObject } from 'ai';
import { z } from 'zod';

const result = await generateObject({
  model: ernie('ernie-4.0-8k'),
  schema: z.object({
    recipe: z.object({
      name: z.string(),
      ingredients: z.array(
        z.object({
          name: z.string(),
          amount: z.string(),
        })
      ),
      steps: z.array(z.string()),
    }),
  }),
  prompt: '生成一个宫保鸡丁的菜谱',
});

console.log(JSON.stringify(result.object.recipe, null, 2));

流式结构化数据生成

import { ernie } from 'ernie-ai-provider';
import { streamObject } from 'ai';
import { z } from 'zod';

const productSchema = z.object({
  name: z.string().describe('产品名称'),
  category: z.string().describe('产品分类'),
  price: z.number().describe('价格'),
  features: z.array(z.string()).describe('产品特性'),
  specifications: z.object({
    weight: z.string(),
    dimensions: z.string(),
    color: z.string(),
  }),
});

const { partialObjectStream, object } = await streamObject({
  model: ernie('ernie-4.0-8k'),
  schema: productSchema,
  prompt: '生成一个智能手机的产品信息',
});

// 实时显示生成过程
for await (const partialObject of partialObjectStream) {
  console.log('生成中:', JSON.stringify(partialObject, null, 2));
}

// 获取最终结果
const finalProduct = await object;
console.log('完成:', JSON.stringify(finalProduct, null, 2));

工具调用

import { ernie } from 'ernie-ai-provider';
import { generateText, tool } from 'ai';
import { z } from 'zod';

const getWeatherTool = tool({
  description: '获取指定城市的天气信息',
  parameters: z.object({
    city: z.string().describe('城市名称'),
  }),
  execute: async ({ city }) => {
    // 这里应该调用真实的天气API
    return `${city}的天气:晴天,温度25°C`;
  },
});

const result = await generateText({
  model: ernie('ernie-4.0-8k'),
  prompt: '北京今天天气怎么样?',
  tools: {
    getWeather: getWeatherTool,
  },
});

console.log(result.text);

网络搜索功能

import { ernie } from 'ernie-ai-provider';
import { generateText } from 'ai';

const result = await generateText({
  model: ernie('ernie-4.0-8k', {
    web_search: {
      enable_citation: true,    // 启用引用信息
      enable_trace: true,       // 启用搜索轨迹
      enable_status: true,      // 启用状态信息
    }
  }),
  prompt: '请告诉我最新的AI技术发展动态',
});

console.log(result.text);

系统人设和角色定制

import { ernie } from 'ernie-ai-provider';
import { generateText } from 'ai';

const result = await generateText({
  model: ernie('ernie-4.0-8k'),
  prompt: '请分析一下当前的股市走势',
  // 设置系统人设
  system: '你是一位资深的金融分析师,拥有15年的投资经验',
  // 设置详细指令
  instruction: '请用专业术语分析,并提供具体的数据支撑',
  userId: 'analyst_001',
  maxOutputTokens: 600,
});

console.log(result.text);

API 参考

createErnie(options)

创建一个自定义的 ERNIE 提供商实例。

参数:

  • options.apiKey (string, 可选): API 密钥,默认从 QIANFAN_API_KEY 环境变量读取
  • options.baseURL (string, 可选): 自定义 API 基础 URL
  • options.headers (Record<string, string>, 可选): 自定义请求头

ernie(modelId, settings?)

创建一个 ERNIE 语言模型实例。

参数:

  • modelId (string): 模型标识符
  • settings (object, 可选): 模型设置
    • temperature (number): 控制输出随机性,范围 0-1
    • topP (number): 核采样参数,范围 0-1
    • maxTokens (number): 最大输出令牌数
    • penaltyScore (number): 重复惩罚分数,范围 1.0-2.0

许可证

MIT

贡献

欢迎提交 Issue 和 Pull Request!

相关链接