
Langflow Cohere 扩展 Bundle 详解lfx-cohere 的安装、组件参数与旧工作流迁移机制【免费下载链接】langflowLangflow is a powerful tool for building and deploying AI-powered agents and workflows.项目地址: https://gitcode.com/GitHub_Trending/la/langflowlfx-cohere 是 Langflow 将 Cohere 系列组件大语言模型、向量嵌入、文档重排序从核心包拆分出来的独立扩展 Bundle。本文基于 src/bundles/cohere/README.md 展开结合包内清单、组件源码与迁移表说明该 Bundle 的安装注册机制、三个组件的完整参数与实现细节以及旧版已保存工作流如何被自动改写为新命名空间 ID。一、Bundle 结构一份清单与一组组件lfx-cohere 遵循 Langflow 扩展 Bundle 的标准布局一份extension.json清单加上一组组件模块。清单文件 extension.json 声明了 Bundle 的核心元数据{ $schema: https://schemas.langflow.org/extension/v1.json, id: lfx-cohere, version: 0.1.1, name: Cohere, description: Cohere component(s) as a standalone Langflow Extension Bundle., lfx: { compat: [1] }, bundles: [ { name: cohere, path: components/cohere } ] }几个关键字段id/versionBundle 的唯一标识与版本当前版本为0.1.1lfx.compat: [1]声明该 Bundle 兼容的 BUNDLE_API 主版本线加载器会据此校验 API 兼容性bundles[].path相对清单所在目录的组件目录路径即 src/bundles/cohere/src/lfx_cohere/components/cohere其中包含三个组件模块组件类模块命名空间 IDCohereComponentcohere_models.pyext:cohere:CohereComponentofficialCohereEmbeddingsComponentcohere_embeddings.pyext:cohere:CohereEmbeddingsComponentofficialCohereRerankComponentcohere_rerank.pyext:cohere:CohereRerankComponentofficial组件包的init.py 采用惰性重导出通过_dynamic_imports映射表与模块级__getattr__只在真正访问CohereComponent等符号时才去导入对应子模块。源码注释说明这是为了对齐拆分前lfx.components.cohere的布局保证旧工作流引用的模块级类在迁移改写后仍能正常解析。二、安装与自动注册安装命令见 READMEpip install lfx-cohere注册机制的关键在于 pyproject.toml 中的 entry-point 声明[project.entry-points.langflow.extensions] lfx-cohere lfx_cohereLangflow 服务端启动时会通过langflow.extensions这一 entry-point 自动发现已安装的扩展分布distribution读取其extension.json清单并加载组件。正如 README 所述安装后重启 Langflow 服务组件就会以命名空间 IDext:cohere:Classofficial出现在调色板palette的cohere分组下。从构建配置看extension.json与组件源码都被打包进lfx_cohere包内[tool.hatch.build.targets.wheel] packages [src/lfx_cohere] include [ src/lfx_cohere/extension.json, src/lfx_cohere/components/**/*.py, ]这样importlib.metadata.files(dist)才能找到清单加载器再把bundles[].path解析为相对清单目录的路径。运行时依赖约束pyproject.toml 中声明了该 Bundle 的依赖requires-python 3.10,3.15 dependencies [ lfx1.12.0.dev0,2.0.0, langchain-cohere~0.5.0, ]lfx的下界锚定在当前major.minor线、上界卡在下一个大版本之下文件内注释说明该下界在拆分时从src/lfx/pyproject.toml读取并会通过make patch流程由scripts/ci/sync_bundle_lfx_pin.py重新同步组件实际调用 Cohere 能力依赖langchain-cohere约 0.5 版提供的ChatCohere、CohereEmbeddings、CohereRerank封装细粒度的 BUNDLE_API 兼容性则依靠清单中lfx.compat与BUNDLE_API_VERSION的比对来强制执行。三、开发验证流程README 给出的开发流程为cd src/bundles/cohere pip install -e . lfx extension validate src/lfx_coherepip install -e .以可编辑方式安装本 Bundle方便在 components/cohere 下直接改动组件代码并即时生效lfx extension validate对清单目录做静态校验清单结构、组件声明合法性等是在发布前确认 Bundle 可被加载的第一道检查。pyproject 中的注释还提到一个细节可编辑安装若其dist.files只暴露 dist-info 条目加载器会回退到 entry-point 来定位清单——这正是上面langflow.extensionsentry-point 同时存在的意义。四、组件详解4.1 Cohere Language ModelsCohereModelCohereComponent 继承LCModelComponent用于把 Cohere 大语言模型接入工作流中的 LLM 插槽。组件参数参数类型说明基础输入LCModelComponent.get_base_inputs()继承自通用 LLM 组件基类的公共参数cohere_api_keySecretStrInputCohere API Key必填requiredTrue默认值为环境变量名COHERE_API_KEYtemperatureSliderInput采样温度范围 0–2、步长 0.01默认 0.75值越小输出越确定越大越发散核心构建逻辑def build_model(self) - LanguageModel: cohere_api_key self.cohere_api_key temperature self.temperature api_key SecretStr(cohere_api_key).get_secret_value() if cohere_api_key else None return ChatCohere( temperaturetemperature or 0.75, cohere_api_keyapi_key, )从源码结构看密钥先经过SecretStr解包再以cohere_api_key传入ChatCoheretemperature 缺省时回退到 0.75。display_name为 Cohere Language Models图标为Cohere。4.2 Cohere EmbeddingsCohereEmbeddingsCohereEmbeddingsComponent 用于生成文本向量输出单个Embeddings类型的embeddings输出构建方法build_embeddings。完整参数表参数类型默认值 / 取值说明api_keySecretStrInput必填real_time_refreshTrueCohere API Key变更后触发实时刷新model_nameDropdownInput默认embed-english-v2.0选项含embed-multilingual-v2.0、embed-english-light-v2.0、embed-multilingual-light-v2.0嵌入模型comboboxTrue允许手动输入模型名truncateMessageTextInput高级项输入超长时的截断策略max_retriesIntInput3请求失败重试次数user_agentMessageTextInputlangchain请求 User-Agent 标识request_timeoutFloatInput空请求超时秒空则不设置构建时参数被整体透传给langchain_cohere.CohereEmbeddings若构造失败通常是 API Key 或模型参数无效会抛出带提示的ValueErrorPlease verify the API key and model parameters, and try again.该组件还有两处值得注意的动态行为模型列表拉取get_model()使用cohere.ClientV2(api_key)调用models.list(endpointembed)返回当前账号可用的嵌入模型名列表选项联动刷新update_build_config钩子在api_key或model_name字段变化时若已填写 API Key则用get_model()的结果刷新下拉框选项未填 Key 时则直接采用传入的字段值。因此在前端编辑表单时填入密钥后模型下拉列表会自动更新为实时可用列表。4.3 Cohere RerankCohereRerankCohereRerankComponent 继承LCCompressorComponent作为文档压缩器接入 RAG 检索链路输出reranked_documents方法compress_documents。参数包括继承自压缩器基类的通用输入其中top_n在 src/lfx/src/lfx/base/compressors/model.py 中定义为 IntInput、默认 3 的高级项加上本组件特有的两个字段参数类型默认值 / 取值说明api_keySecretStrInput—Cohere API KeymodelDropdownInput默认rerank-english-v3.0选项含rerank-multilingual-v3.0、rerank-english-v2.0、rerank-multilingual-v2.0重排序模型build_compressor在运行期才延迟导入CohereRerankfrom langchain_cohere import CohereRerank未安装langchain-cohere时抛出带安装提示的ImportError正常路径上以cohere_api_key、model、top_n三个参数构造实例。五、旧工作流迁移从lfx.components.cohere.*到命名空间 IDREADME 的 Migration 一节指出引用旧类名或lfx.components.cohere.*旧导入路径的已保存工作流会被迁移表自动改写为新的命名空间 ID。迁移表位于 src/lfx/src/lfx/extension/migration/migration_table.json其中每个组件覆盖四种旧形态均标注added_in: 1.11.0裸类名bare_class_name: CohereComponent→ext:cohere:CohereComponentofficial完整旧导入路径import_path: lfx.components.cohere.cohere_models.CohereComponent包级重导出路径import_path: lfx.components.cohere.CohereComponentPhase-A 之前的槽位legacy_slot: ext:cohere:CohereComponentofficial-pre-a。三个组件 × 四种形态共 12 条迁移记录目标统一为ext:cohere:Classofficial。配套的旧路径兼容 shim 在 src/lfx/src/lfx/components/cohere/init.py它不含任何组件实现仅把lfx.components.cohere重新指向已安装的lfx_cohere.components.cohere分布若未安装 lfx-cohere则抛出带pip install lfx-cohere安装提示的ModuleNotFoundError。源码注释说明该 shim 将在弃用窗口结束M4时移除。这一迁移链路由集成测试 src/lfx/tests/integration/extension/test_pilot_cohere_upgrade.py 覆盖测试对三个组件类逐一构造模拟的已保存工作流节点调用migrate_flow_payload断言裸类名、完整导入路径、包级重导出路径、pre-Phase-A 槽位四种形态都被改写为ext:cohere:Classofficial并额外验证分布可导入性与清单可发现性。六、小结与适用前提适用版本迁移记录标注added_in: 1.11.0lfx依赖下界为1.12.0.dev0因此独立使用 lfx-cohere 需要较新的 lfx/Langflow 版本线Python 要求3.10,3.15。升级路径从旧版 Langflow 升级时无需手动改工作流 JSON——加载器会依据迁移表自动完成 ID 改写lfx.components.cohere旧导入路径由 shim 兜底一段时间。验证与调试入口开发态可用lfx extension validate校验清单加载异常时可先确认langflow.extensionsentry-point 是否被 pip 正确注册、extension.json是否随 wheel 打包。相关源码路径索引Bundle 文档src/bundles/cohere/README.md打包与依赖src/bundles/cohere/pyproject.toml清单src/bundles/cohere/src/lfx_cohere/extension.json组件实现cohere_models.py、cohere_embeddings.py、cohere_rerank.py迁移表与 shimmigration_table.json、components/cohere shim升级回归测试test_pilot_cohere_upgrade.py【免费下载链接】langflowLangflow is a powerful tool for building and deploying AI-powered agents and workflows.项目地址: https://gitcode.com/GitHub_Trending/la/langflow创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考