Botpress 认知客户端
顶部建有的公用客户端 @botpress/client 以调用 TypeScript 的 LLMs 。 在浏览器和NodeJS中工作.
安装
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
基本用途
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) (中文(简体) ).
当一个模型或提供者倒闭时,认知会:
- 将模型标为已退化(如果尚未标为已退化)
- 如果允许重试, 请重试首选项中的下一个模型
- 除偏好( 降级)
该模型被标记为退化期为5分钟.
排行榜
当模型偏好没有通过时,默认的排名会自动用于对可用的模型进行排序 best 财务报告和审定财务报表 fast 预设.
它将检查标签、价格和销售商,以给每个模型打分并进行排名.
见 src/models.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 的空闲时间. 对于没有服务器的环境,我们建议您提供一个模型的静态列表或使用缓存.
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)。 。 。 .
const cognitive = new Cognitive({ client: new Client() })
cognitive.interceptors.request.use(async (err, req, next, done) => {
// do whatever here
next(null, req)
})