# @botpress/cognitive 中文文档

规范地址：https://npmcn.com/package/%40botpress/cognitive/

版本：1\.3\.0
npm 元数据更新时间：2026-11-05T00:46:45.760Z
中文译文更新时间：2026-11-08T12:24:18.655Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40botpress/cognitive
原文对照：https://npmcn.com/package/%40botpress/cognitive/?lang=original
项目主页：https://github.com/botpress/botpress#readme
源码仓库：https://github.com/botpress/botpress

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：942 kB，7 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=18\.0\.0
- 运行依赖：2 个
- 首次发布：2025-01-22T21:20:50.500Z
- 最新版本发布：2026-11-05T00:46:45.450Z
- 近 30 天下载量：30915（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：97（近 12 个月 45 个）

## 中文简介

环绕 Botpress 客户端以调用 LLMS

## 安装

```sh
npm install @botpress/cognitive
```

也可以使用：`pnpm add @botpress/cognitive`、`yarn add @botpress/cognitive`、`bun add @botpress/cognitive`

## README

# Botpress 认知客户端

顶部建有的公用客户端 `@botpress/client` 以调用 TypeScript 的 LLMs 。 在浏览器和NodeJS中工作.

## 安装

```bash
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
```

## 基本用途

```ts
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!' }] })
```

## 高级

### 模式首选项

默认情况下,认知会尝试从文件 API 获取您的模型偏好. 模式首选项是用于 `best` 财务报告和审定财务报表 `fast` 预设. 请求时可以指定预设 `best` (违约), `fast` 或 模型参考文件(`integration:model-id`) (中文(简体) ).

当一个模型或提供者倒闭时,认知会:

1.  将模型标为已退化(如果尚未标为已退化)
2.  如果允许重试, 请重试首选项中的下一个模型
3.  除偏好( 降级)

该模型被标记为退化期为5分钟.

### 排行榜

当模型偏好没有通过时,默认的排名会自动用于对可用的模型进行排序 `best` 财务报告和审定财务报表 `fast` 预设. 它将检查标签、价格和销售商,以给每个模型打分并进行排名.

见 `src/models.ts` 需要更多关于这个热度的信息.

### 中止请求

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

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

### 覆盖首选项提供者

如果没有提供商被传递给认知,我们默认 `RemoteModelProvider`,它使用文件 API 存储首选项,并使用 Botpress 客户端获取模型(`getBot` 财务报告和审定财务报表 `listLanguageModels` 每个安装的集成物).

你可以通过自己的提供商, 简单创建一个类 扩展我们的基地提供商.

如果您需要频繁地(在没有服务器的情况下说)对认知客户端进行安装,则建议使用这种方法,因为检索首选需要先到 ~ 500ms 的空闲时间. 对于没有服务器的环境,我们建议您提供一个模型的静态列表或使用缓存.

```ts
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 })
```

## 活动

## 扩展

我们为认知提供两个扩展点(hoks),允许您更改请求的输入或输出. 钩子在呼叫时可以同步运行 `next(err, value)`。 。 。 . 您也可以通过调用来缩短执行 `done(err, value)`。 。 。 .

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

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

## 常见问题

### @botpress/cognitive 是什么？

环绕 Botpress 客户端以调用 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 和提交记录判断。

