ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

ToolJet Cohere 插件实战:配置数据源、文本生成与 Chat 对话的高级参数全解

ToolJet Cohere 插件实战:配置数据源、文本生成与 Chat 对话的高级参数全解 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/ToolJetCohere 是 ToolJet Marketplace 中类型为ai的官方插件之一它把 Cohere 的大语言模型能力封装成标准数据源让开发者无需编写后端代码即可在内部工具、仪表盘和业务应用中完成文本生成与多轮对话。本文以 cohere.md 为主线结合仓库中 marketplace/plugins/cohere 的源码实现完整讲解接入 Token、两种核心操作、全部高级参数以及请求在插件内部的分发与错误处理机制。读完本文你将能在 ToolJet 中独立完成 Cohere 插件的安装、配置、查询创建与参数调优。一、插件定位Cohere 能给你的 ToolJet 应用带来什么Cohere 插件允许你在 ToolJet 中直接使用 Cohere 的先进 AI 模型来完成两类任务文本生成text generation和构建聊天机器人助手chatbot assistant。通过配置各类参数你可以对生成结果做细粒度优化例如控制随机性、输出长度、重复惩罚、引用生成模式和安全等级。在仓库中该插件的元数据由 manifest.json 声明其type: ai字段表明这是一个 AI 类数据源插件插件运行时由 index.ts 中的CohereService实现底层通过官方 SDKcohere-ai^7.15.4见 package.json的CohereClientV2发起请求。安装插件的前提在安装任何 Marketplace 插件之前需要先在 ToolJet 的.env中开启 Marketplace 功能详见 marketplace_overview.mdENABLE_MARKETPLACE_FEATUREtrue开启后以管理员身份登录点击仪表盘左下角的设置图标进入Marketplace页面找到 Cohere 插件卡片点击Install安装完成后在Data sources页签滚动到Plugins区域即可看到已安装的 Cohere将其配置为数据源。注意如果移除插件所有与之关联的查询都会从应用中一并删除。二、Connection用 Access Token 建立连接连接 Cohere 只需要一个凭据——Access token访问令牌。它需要你在 Cohere 的 Dashboard 中的 API Keys 页面生成登录 Cohere 账户后即可创建并可按需设置额度与过期策略。在 ToolJet 中创建 Cohere 数据源时只需填写API key一个字段。从源码看这个字段的规格在 manifest.json 中定义type: password输入框按密码形式展示避免泄露encrypted: trueAPI Key 在存储层会被加密同时source.options.apiKey同样标记为encrypted: truerequired: [apiKey]未填写 API Key 时无法保存数据源。{ title: Cohere datasource, description: A schema defining Cohere datasource, type: ai, source: { name: Cohere, kind: cohere, options: { apiKey: { type: string, encrypted: true } } }, properties: { apiKey: { label: API key, key: apiKey, type: password, description: Enter your Cohere API Key, encrypted: true } }, required: [apiKey] }连接测试的底层原理保存数据源时ToolJet 会调用插件实现的testConnection方法index.ts。其流程是校验sourceOptions.apiKey是否存在缺失则抛出QueryError(Connection could not be established, API key is missing, {})调用getConnection用new CohereClientV2({ token: apiKey })构建 SDK 客户端向模型command-r-plus-08-2024发送一条chat请求消息内容为hello world!若请求成功返回{ status: ok }若失败则携带error.response?.status抛出连接失败异常。这也解释了为什么连接测试本身会产生一次真实的模型调用——它会消耗一次 API 配额且返回结果不用于业务。三、Supported Operations文本生成与 Chat 对话插件支持两种操作Operation枚举定义见 types.tsexport enum Operation { TextGeneration text_generation, Chat chat, }创建查询时操作与模型选择由 operations.json 驱动其默认值为{ defaults: { operation: text_generation, model: command-r-plus } }即新建查询时默认选中Text Generation操作与command-r-plus模型。Text Generation创意文本生成使用该操作可根据用户输入生成创意文本内容选择目标模型并定义额外参数来优化输出。必填参数Model用于生成文本的模型。可用模型如下与 Chat 操作共享大部分模型族command-r7b-12-2024command-r-plus-08-2024command-r-plus-04-2024command-r-pluscommand-r-08-2024command-r-03-2024command-rcommandcommand-nightlycommand-lightcommand-light-nightlyc4ai-aya-expanse-8bc4ai-aya-expanse-32bMessage用于生成响应的主要用户输入。可选参数Advanced parameters用于配置模型响应的额外参数详见下文「Advanced Parameters」小节。示例参数{ 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] }底层实现query_operations.tsexport async function textGeneration(cohere: CohereClientV2, options: QueryOptions) { const { model, message, advanced_parameters } options; if (!model || !message) { return { error: Model and message are required for text generation., statusCode: 400 }; } let advancedParams {}; if (advanced_parameters) { advancedParams JSON.parse(advanced_parameters); } const response await cohere.chat({ model: model, messages: [{ role: user, content: message }], ...advancedParams, }); return response; }要点model与message任一缺失时直接返回{ error, statusCode: 400 }而非抛出异常advanced_parameters是一个 JSON字符串由插件用JSON.parse解析后通过展开运算符...advancedParams合并进 SDK 请求——这也意味着填写高级参数时必须保证是合法 JSON文本生成在底层同样走cohere.chat接口只是消息列表只有一条user消息。响应示例由 Cohere 模型生成仅用于演示返回文本的形态不代表 ToolJet 官方的功能声明ToolJet is an open-source no-code platform that allows you to build your own tools and automate your workflows in minutes. It is built on top of the powerful Airbyte open-source standard for data integration, focusing on user-friendliness and extensibility. With ToolJet, you can create custom solutions for your business without any prior coding knowledge.Heres a high-level overview of the features and capabilities of ToolJet:No-Code Builder: ToolJet offers a visual interface where you can quickly create powerful applications, workflows, and automation scripts without writing a single line of code.Data Integration: ToolJet leverages Airbyte to provide seamless data integration capabilities. You can sync data from various sources like databases, APIs, or SaaS applications to build custom dashboards, data pipelines, or extensions.Visual Automation Builder: Create automated workflows using a drag-and-drop interface. Connect various tools, apps, and APIs to automate tasks, notifications, data manipulation, and more.Open Source: Being open-source means you get full transparency over the platforms underlying code. Plus, you can contribute to the project and customize or extend it according to your needs.Extensions APIs: ToolJet provides a marketplace for sharing and discovering extensions, APIs, and pre-built workflows. You can extend the functionality of ToolJet with community-built solutions.Dashboard Reports: Create interactive dashboards and reports using the built-in charting and visualization tools. Visualize data from various sources in one place and share insights with your team.Forms UI: Easily create forms and user interfaces using ToolJets intuitive form builder. Collect data, feedback, or insights from your users or systems.Collaboration Security: Control user access and permissions with robust security features. Collaborate with team members on different projects and ensure data privacy and compliance.Integration with External Tools: ToolJet integrates with popular productivity, collaboration, and data tools, including Slack, Google Workspace, Microsoft Office, Airbyte, and more.Open API Extensibility: ToolJet has a robust application programming interface (API), which allows developers to extend its capabilities. You can customize and connect any external service or application.Chat带上下文的对话式交互使用 Chat 操作可进行类对话交互模型会基于给定的提示与指令作出回应提供相关且符合上下文的回答保持流畅的对话节奏。必填参数Model指定用于聊天响应的模型可用模型与上面 Text Generation 的列表一致。History记录先前的交互用于在对话中维持上下文。Message聊天中生成响应的主要用户输入。可选参数Advanced parameters同前用于配置模型响应参见下文「Advanced Parameters」。示例参数与 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] }底层实现query_operations.tsexport 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; }要点三个参数缺一不可缺失时直接抛出Error与文本生成的返回 400 不同这里会中断查询并进入错误处理history同样是一个 JSON 字符串解析后把当前message以user角色追加到历史末尾再整体作为messages发送——这是维持多轮上下文的关键机制一个典型的历史结构参考也是 operations.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 } ]响应示例同样为模型生成的演示输出ToolJet is a no-code platform that allows you to build custom internal tools with drag and drop functionality. You can integrate Cohere with ToolJet to enable an added advantage of AI features in your apps built on ToolJet.To integrate Cohere AI into your ToolJet app, you should have a Cohere AI API key. If you dont have one, you can sign up for a free Cohere AI account and get your API key.As a next step, you can refer to our documentation to see a step-by-step guide to integrate Cohere AI with ToolJet. If you have any further questions, please let me know!四、Advanced Parameters逐项解析高级参数以 JSON 对象形式填入下表完整列出插件支持的全部参数来自 cohere.md参数说明Response Format配置模型以指定格式输出。Temperature控制输出结果的随机程度。Max Tokens模型在响应中生成的最大 token 数量。Seed通过初始化生成器确保结果一致。P通过设置概率阈值来限制随机性。K每一步生成时只考虑概率最高的前 k 个 token。Frequency Penalty抑制高频词的使用使响应更多样化。Presence Penalty减少词或短语的重复出现。Citation Options控制引用生成的选项。Safety Mode选择插入到提示词中的安全指令。允许值CONTEXTUAL、STRICT、OFF。Stop Sequences定义最多 5 个字符串当模型生成到这些字符串时停止生成并返回已生成文本。结合源码可以进一步理解几个关键参数的落地方式Response Format示例值为{type: text}即要求纯文本输出Safety Mode不同模型族在 operations.json 中的占位默认值不同——较新的 command-r 系列与 command-nightly 使用CONTEXTUAL而 command-light 系列与 c4ai-aya-expanse 系列使用NONE文档表格中给出的允许值为 CONTEXTUAL、STRICT、OFF填写时以你选择的模型实际支持的枚举为准Stop Sequences最多 5 个终止字符串示例[spam, fraud]表示当模型开始生成这两个词之一时立即截断输出P 与 K二者配合使用p通过累积概率截断采样nucleus samplingk限定每步候选 token 数top-k sampling在示例中p: 0.3, k: 1意味着采样空间被严格约束输出更确定Seed固定随机种子让相同输入可复现相同输出便于测试与回归对比。注意这些参数是透传给cohere-aiSDK 的通过...advancedParams展开因此凡是 Cohere Chat API 支持的请求体字段均可在此填写超出文档表格所列的参数也可尝试但需以 SDK 与模型实际接受的范围为准。五、源码视角查询如何被分发与执行整个查询生命周期由CohereService驱动index.tsasync run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): PromiseQueryResult { const operation queryOptions.operation; const cohere await this.getConnection(sourceOptions); let result {}; try { switch (operation) { case Operation.TextGeneration: result await textGeneration(cohere, queryOptions); break; case Operation.Chat: result await chat(cohere, queryOptions); break; default: throw new QueryError(Query could not be completed, Invalid operation, {}); } } catch (error: any) { console.error(Error in Cohere query:, error); // ...错误解析与包装 throw new QueryError(Query could not be completed, errorMessage, errorDetails); } return { status: ok, data: result }; }值得注意的实现细节分发逻辑run依据queryOptions.operation走 switch 分支把真实调用委托给query_operations.ts中的两个函数未知操作抛出Invalid operation错误包装catch 块会尽力从 SDK 错误对象中提取error.body.message错误消息、error.req.idrequestId、error.error.type错误类型以及statusCode统一包装成QueryError抛出便于查询面板展示可读错误信息返回值结构成功时返回{ status: ok, data: result }。结合 manifest.json 中声明的exposedVariablesisLoading、data、rawData查询结果可在应用组件中通过queries.queryName.data直接取用运行环境插件为独立包tooljet-marketplace/cohere构建命令为ncc build lib/index.ts -o dist见 package.json说明该插件会以打包后的产物形式被 ToolJet 加载执行。六、实战在应用中使用查询结果完成数据源配置后在应用的 Query Panel 中新建查询并选择 Cohere 数据源随后按需选择操作Text Generation 或 Chat、模型并填写消息与高级参数。运行查询后结果对象可以通过{{queries.query_name.data}}绑定到 Text、Markdown、Table 等组件上常见用法包括把模型生成的文本展示在 Text 组件中实现一键生成文案/报告类内部工具把 Chat 查询的history用代码构建并维护多轮上下文配合按钮组件实现页面内的对话助手用stop_sequences约束输出边界避免模型跑题生成无关内容。如果需要为多个团队环境复用配置可将 API Key 通过组织环境变量注入插件字段支持加密存储避免凭据硬编码在应用定义中。小结Cohere 插件是 ToolJet Marketplace 中接入 LLM 能力的一条捷径只需一个 Access Token即可在查询面板中完成文本生成与多轮对话并通过temperature、seed、p/k、两类惩罚系数、引用模式、安全模式与终止序列等高级参数精细调控输出。其实现index.ts、query_operations.ts与元数据manifest.json、operations.json全部开源在仓库的 marketplace/plugins/cohere 目录下你可以对照源码理解每一步参数如何透传到 Cohere Chat API也可以参考 marketplace_overview.md 了解插件安装、使用与移除的完整流程。【免费下载链接】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),仅供参考
返回列表