@botpress/cognitive

中文文档
1.3.0发布于 2026/11/05MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3.1万

Wrapper around the Botpress Client to call LLMs

原始使用文档
文档目录9 节
  1. Installation
  2. Basic Usage
  3. Advanced
  4. Model Preferences
  5. Ranking Heuristic
  6. Aborting the request
  7. Overriding Preferences Provider
  8. Events
  9. Extensions

Botpress Cognitive Client

A utility client built on top of @botpress/client to call LLMs for TypeScript. Works in the browser and NodeJS.

Installation

npm install --save @botpress/client @botpress/cognitive # for npm
yarn add @botpress/client @botpress/cognitive # for yarn
pnpm add @botpress/client @botpress/cognitive # for pnpm

Basic Usage

import { Client } from '@botpress/client'
import { Cognitive } from '@botpress/cognitive'

const token = 'your-token'
const botId = 'your-bot-id'

const client = new Client({ token, botId })

const cognitive = new Cognitive({ client })

const response = await cognitive.generateContent({ messages: [{ role: 'user', content: 'Hello!' }] })

Advanced

Model Preferences

By default, cognitive will try to fetch your model preferences from the File API. Model preferences is an ordered list of models for best and fast presets. When making a request, you can specify a preset best (default), fast or a ModelRef (integration:model-id).

When a model or a provider is down, cognitive will:

  1. mark the model as degraded if not already marked as such
  2. if retries allow, retry the request on the next model in the preferences
  3. save the preferences (degradation)

The model is marked as degraded for a duration of 5 minutes.

Ranking Heuristic

When no model preferences are passed, the default ranking heuristic will be used to rank available models automatically for best and fast presets. It will look at the tags, price and vendor to score each models and rank them.

See src/models.ts for more information on this heuristic.

Aborting the request

const cognitive = new Cognitive({ client: new Client() })
const controller = new AbortController()

await cognitive.generateContent({
  messages: [],
  signal: controller.signal,
})

Overriding Preferences Provider

If no provider is passed to Cognitive, we default to RemoteModelProvider, which uses the File API to store preferences and fetches models using the Botpress Client (getBot and listLanguageModels on each installed integrations).

You can pass your own Provider, simply create a class that extends our base provider.

This method is recommended if you need to instanciate the Cognitive client frequently (say in a serverless), as retrieving preferences incurs an initial ~500ms latency. For serverless environments, we recommend you provide a static list of models or use caching.

import { Cognitive, ModelProvider } from '@botpress/cognitive'

export class CustomModelProvider extends ModelProvider {
  public fetchInstalledModels(): Promise<Model[]> {
    throw new Error('Not implemented')
  }

  public fetchModelPreferences(): Promise<ModelPreferences | null> {
    throw new Error('Not implemented')
  }

  public saveModelPreferences(preferences: ModelPreferences): Promise<void> {
    throw new Error('Not implemented')
  }

  public deleteModelPreferences(): Promise<void> {
    throw new Error('Not implemented')
  }
}

const provider = new CustomModelProvider()
const cognitive = new Cognitive({ client: new Client(), provider })

Events

Extensions

We provide two extension points (hooks) for cognitive that allows you to change the input or output of requests. Hooks can be asynchronous and run sequentially when calling next(err, value). You can also shortcircuit the execution by calling done(err, value).

const cognitive = new Cognitive({ client: new Client() })

cognitive.interceptors.request.use(async (err, req, next, done) => {
  // do whatever here
  next(null, req)
})

版本与发布记录

@botpress/cognitive 共发布 97 个版本,首次发布于 2025/01/23;过去 12 个月发布了 45 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.0latest2026/11/053 天前
1.2.22026/09/171 个月前
1.2.12026/09/171 个月前
1.2.02026/09/052 个月前
1.1.02026/07/273 个月前
1.0.22026/07/243 个月前

关于 @botpress/cognitive 的常见问题

@botpress/cognitive 是什么?

Wrapper around the Botpress Client to call LLMs

如何安装 @botpress/cognitive?

在项目目录运行 npm install @botpress/cognitive,或使用 pnpm add / yarn add / bun add。

@botpress/cognitive 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@botpress/cognitive 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@botpress/cognitive 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@botpress/cognitive 可以商用吗?

@botpress/cognitive 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@botpress/cognitive 的下载量有多少?

根据 npm 官方下载统计,@botpress/cognitive 在 2026/10/09 – 2026/11/07 这 30 天内被下载 30,915 次,日均约 1,031 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@botpress/cognitive 还在维护吗?

最新版本 1.3.0 发布于 2026/11/05(3 天前);至今共发布 97 个版本,其中过去 12 个月发布了 45 个;首次发布于 2025/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。