ToolJet 接入 Cohere 大模型实战指南:文本生成与 Chat 对话插件的完整配置
2026/9/13 19:17:13 网站建设 项目流程

ToolJet 接入 Cohere 大模型实战指南:文本生成与 Chat 对话插件的完整配置

【免费下载链接】ToolJetOpen-source foundation of ToolJet AI - the enterprise app generation platform for internal tools, dashboards, business applications, workflows and AI agents. Build visually, from a prompt, or from Claude Code, Codex and Cursor over MCP 🚀项目地址: https://gitcode.com/GitHub_Trending/to/ToolJet

本文围绕 ToolJet 官方 Marketplace 中的 Cohere 插件展开,系统讲解如何通过 Access token 建立连接、使用 Text Generation(文本生成)与 Chat(对话)两种操作,以及全部高级参数的取值与作用。读完本文,你将能够在 ToolJet 的低代码应用中直接调用 Cohere 的 command 系列与 Aya 系列模型,完成文案生成、智能客服、业务助手等 AI 场景,并能从源码层面理解插件的工作机制。

插件概览:Cohere 能做什么

Cohere 插件是 ToolJet Marketplace 中类型为ai的数据源插件,其完整实现位于 marketplace/plugins/cohere。插件底层通过官方cohere-aiSDK 中的CohereClientV2客户端发起请求,目前对外暴露两种核心操作:

  • Text Generation(文本生成):给定模型与提示词,生成创造性文本内容;
  • Chat(对话):基于历史消息与当前用户输入进行多轮对话,模型会参考上下文维持流畅的会话。

插件的操作分发逻辑集中在 marketplace/plugins/cohere/lib/index.ts,其中CohereService.run()根据queryOptions.operation将请求路由到textGenerationchat两个处理函数,二者定义在 marketplace/plugins/cohere/lib/query_operations.ts。

建立连接:获取 Access Token 并配置 API Key

连接 Cohere 只需要一个Access token(即 API Key),可以在 Cohere Dashboard 的 API Keys 页面生成。在 ToolJet 中新建数据源并选择 Cohere 后,将生成的 token 填入API key字段即可。

从插件源码看,连接过程非常简洁。在 marketplace/plugins/cohere/lib/index.ts 的getConnection方法中:

async getConnection(sourceOptions: SourceOptions): Promise<CohereClientV2> { const { apiKey } = sourceOptions; if (!apiKey) { throw new Error('API key missing: No API key provided in source options.'); } const cohere = new CohereClientV2({ token: apiKey }); return cohere; }
  • 若未提供 API key,会直接抛出 "API key missing" 错误;
  • 提供后即创建CohereClientV2实例,后续所有操作复用该客户端。

数据源的字段定义在 marketplace/plugins/cohere/lib/manifest.json 中:apiKey的类型为password,并带有"encrypted": true标记,说明该密钥在 ToolJet 中以加密形式存储,不会以明文暴露在前端。

插件还提供了连接测试能力(testConnection):它会使用command-r-plus-08-2024模型向 Cohere 发送一条hello world!测试消息,若请求失败则返回带状态码的连接错误,帮助你在正式使用前快速验证 token 的有效性。

支持的模型列表

两种操作都支持在以下模型中选择(来自 marketplace/plugins/cohere/lib/operations.json 的模型下拉列表):

模型 ID说明
command-r7b-12-20242024 年 12 月版轻量 R 系列模型
command-r-plus-08-20242024 年 8 月版 R+ 旗舰模型
command-r-plus-04-20242024 年 4 月版 R+ 模型
command-r-plusR+ 系列当前版本
command-r-08-20242024 年 8 月版 R 系列模型
command-r-03-20242024 年 3 月版 R 系列模型
command-rR 系列当前版本
commandCommand 系列当前版本
command-nightlyCommand 夜间构建版
command-light轻量版 Command 模型
command-light-nightly轻量版夜间构建
c4ai-aya-expanse-8bAya Expanse 8B 开源模型
c4ai-aya-expanse-32bAya Expanse 32B 开源模型

值得注意的是,operations.json中默认操作为text_generation,默认模型为command-r-plus。Chat 操作下command-r7b-12-2024也会出现在模型列表中,而 Text Generation 操作的下拉列表则主要面向 R 系列与 Command 系列模型,具体以 marketplace/plugins/cohere/lib/operations.json 为准。

操作一:Text Generation(文本生成)

该操作用于生成创造性文本内容。选择模型并填写提示词后,插件会将modelmessage组装为一次cohere.chat调用(底层 Chat API 兼顾了生成与对话能力),并透传所有高级参数。

必填参数

  • Model:用于生成文本的模型,见上方模型列表;
  • Message:生成响应的主要用户输入。

可选参数

  • Advanced parameters:以 JSON 形式传入的额外配置,详见下文"高级参数详解"。

参数示例(完整复制自插件表单占位符与官方文档):

{ "response_format": {"type": "text"}, "temperature": 0.3, "max_tokens": 512, "seed": 3, "p": 0.3, "k": 1, "frequency_penalty": 0.3, "presence_penalty": 0.3, "citation_options": {"mode": "fast"}, "safety_mode": "off", "stop_sequences": ["spam", "fraud"] }

响应示例(以介绍 ToolJet 的提示词为例):

ToolJet is an open-source no-code platform that allows you to build your own tools and automate your workflows in minutes... 模型会基于提示词输出一段结构化介绍,涵盖 No-Code Builder、数据集成、可视化自动化、开源、扩展与 API、仪表盘与报表、表单与 UI、协作与安全、外部工具集成、开放 API 与可扩展性等要点。

操作二:Chat(对话)

该操作用于类聊天式交互,模型根据给定的提示词与指令做出响应,并借助历史消息保持对话上下文,适合构建聊天机器人、客服助手等场景。

必填参数

  • Model:指定对话使用的模型,见上方模型列表;
  • History:之前的交互记录,用于在对话中维持上下文。它是一段 JSON 数组,例如:
[ { "role": "system", "content": "You are an SEO specialist content writer" }, { "role": "user", "content": "Write a title for a blog post about API design. Only output the title text." }, { "role": "assistant", "content": "Designing Perfect APIs" } ]
  • Message:本轮对话中用户的主要输入。

可选参数

  • Advanced parameters:JSON 形式的额外配置,同 Text Generation。

参数示例

{ "response_format": {"type": "text"}, "temperature": 0.3, "max_tokens": 512, "seed": 3, "p": 0.3, "k": 1, "frequency_penalty": 0.3, "presence_penalty": 0.3, "citation_options": {"mode": "fast"}, "safety_mode": "off", "stop_sequences": ["spam", "fraud"] }

响应示例:当用户询问如何在 ToolJet 中集成 Cohere AI 时,模型会给出引导性回答——先说明 ToolJet 是一个可通过拖拽构建内部工具的 no-code 平台、可以集成 Cohere 获得 AI 能力,然后建议用户注册 Cohere 账号获取 API Key,并提示查阅官方文档获取逐步集成指南。

Chat 的历史消息处理逻辑

从源码看,Chat 操作对历史消息的处理在 marketplace/plugins/cohere/lib/query_operations.ts 中完成:

export async function chat(cohere: CohereClientV2, options: QueryOptions) { const { model, message, advanced_parameters, history } = options; if (!model || !history || !message) { throw new Error('Model, history, and message are required for chat.'); } let parsedHistory = []; parsedHistory = JSON.parse(history); parsedHistory.push({ role: 'user', content: message, }); let advancedParams = {}; if (advanced_parameters) { advancedParams = JSON.parse(advanced_parameters); } const response = await cohere.chat({ model, messages: parsedHistory, ...advancedParams, }); return response; }

要点:

  • 三者(模型、历史、消息)缺一不可,否则直接抛错;
  • historyadvanced_parameters都是 JSON 字符串,运行时通过JSON.parse解析;
  • 插件会把用户本轮message自动追加为role: 'user'的消息,再连同历史一起发给模型,因此你只需要在 History 中维护 system / user / assistant 的历史轮次,当前问题填在 Message 中即可。

高级参数详解(Advanced Parameters)

下表完整对应插件支持的高级参数(见文档与 marketplace/plugins/cohere/lib/operations.json 中的占位符示例):

参数说明
Response Format配置模型以指定格式输出,例如{"type": "text"}表示纯文本输出。
Temperature控制输出结果的随机程度。值越高输出越发散,越低越保守、确定性越强;示例中0.3属于偏向稳定输出的取值。
Max Tokens模型在响应中最多生成的 token 数量,用于限制输出长度、控制成本;文档示例为512,插件表单占位符默认256
Seed通过初始化生成器使结果保持一致。相同 seed 下相同输入可复现相近输出,便于调试与测试。
P概率阈值(top-p),通过设定累积概率阈值来限制随机性,只从累积概率达到该阈值的 token 中采样。
KTop-k 采样,每一步生成时只考虑概率最高的前 k 个 token。
Frequency Penalty惩罚高频出现的词语,鼓励更丰富的用词,使输出更多样化。
Presence Penalty降低重复使用已出现过的词或短语的概率,减少内容复读。
Citation Options控制引用生成的选项,例如{"mode": "fast"}开启快速引用模式,适合需要标注来源的问答场景。
Safety Mode选择插入到提示词中的安全指令。允许值:CONTEXTUAL(按上下文)、STRICT(严格)、OFF(关闭);插件表单占位符中也出现过NONE取值,具体以所选模型支持范围为准。
Stop Sequences定义最多 5 个字符串,当生成内容命中其中任意一个时立即停止生成并返回已生成的文本,常用于拦截敏感词、控制输出边界。

这些参数会以展开运算符...advancedParams的方式直接合并进cohere.chat({...})请求体(见 marketplace/plugins/cohere/lib/query_operations.ts),意味着参数名需与 Cohere Chat API 的字段命名保持一致。

源码视角:请求分发与错误处理

CohereService.run()是整个查询的入口,位于 marketplace/plugins/cohere/lib/index.ts。其工作流程为:

  1. queryOptions.operation读取操作类型;
  2. 通过getConnection建立 Cohere 客户端;
  3. 依据Operation枚举(text_generation/chat,定义在 marketplace/plugins/cohere/lib/types.ts)分发到对应处理函数;
  4. 未知操作抛出Invalid operation错误。

错误处理值得一提:当 Cohere API 返回错误时,插件会尽力解析error.body.messageerror.req.iderror.statusCode等信息,组装成带有requestIderrorTypestatusCodeQueryError抛出,方便你在 ToolJet 的查询调试面板中快速定位认证失败、限流或参数错误等问题。

响应结构:成功时统一返回{ status: 'ok', data: result },其中data为 Cohere Chat API 的原始响应对象。

测试现状与使用建议

插件仓库中的测试文件 marketplace/plugins/cohere/tests/index.js 目前仅包含it.todo('needs tests')占位,说明测试用例仍在规划中。因此在实际接入时建议:

  • 先在数据源配置页执行Test connection验证 token 有效性;
  • 用较小的max_tokens(如 256)与较低的temperature(如 0.3)起步,控制成本并便于对比效果;
  • 对稳定性敏感的业务(如报表文案)设置seed以获得可复现输出;
  • 在 Chat 场景下务必维护好 History 的角色结构(system / user / assistant),当前用户输入填写在 Message 中即可自动追加。

通过以上配置,你便可以在 ToolJet 中快速搭建基于 Cohere 的文本生成与智能对话能力,并将其与表单、表格、按钮等组件联动,落地到真实的业务应用中。

【免费下载链接】ToolJetOpen-source foundation of ToolJet AI - the enterprise app generation platform for internal tools, dashboards, business applications, workflows and AI agents. Build visually, from a prompt, or from Claude Code, Codex and Cursor over MCP 🚀项目地址: https://gitcode.com/GitHub_Trending/to/ToolJet

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询