
Haystack 音频 API 详解LocalWhisperTranscriber 与 RemoteWhisperTranscriber 语音转文本组件【免费下载链接】haystackOpen-source AI orchestration framework for building context-engineered, production-ready LLM applications. Design modular pipelines and agent workflows with explicit control over retrieval, routing, memory, and generation. Built for scalable agents, RAG, multimodal applications, semantic search, and conversational systems.项目地址: https://gitcode.com/GitHub_Trending/ha/haystack本篇基于 Haystack 2.20 版音频 API 参考文档系统讲解whisper_local与whisper_remote两个模块中的两个语音转文本Speech-to-Text组件本地推理的LocalWhisperTranscriber与调用 OpenAI Whisper API 的RemoteWhisperTranscriber。读完后你可以掌握两个组件的完整初始化参数、run输入输出约定、序列化/反序列化方式并结合当前仓库的迁移说明与发布记录明确这些组件在 Haystack 3.x 中的现状与迁移路径。音频组件总览Haystack 的音频处理能力由haystack.components.audio包提供按实现方式分为两个子模块模块组件实现方式适用场景whisper_localLocalWhisperTranscriber在本地机器上运行 OpenAI 的 Whisper 模型数据不出本地、离线环境、大批量音频转写whisper_remoteRemoteWhisperTranscriber调用 OpenAI 的 Whisper API无需本地算力、快速集成、按量付费两个组件对外接口高度统一run方法都接收sources文件路径或ByteStream的二进制流列表都声明相同的输出类型documents: list[Document]并且都实现了to_dict/from_dict序列化协议因此可以在管道中互换使用、以统一的 YAML 格式落盘。LocalWhisperTranscriber本地 Whisper 转写组件功能定位与用法LocalWhisperTranscriber在本地机器上加载 OpenAI 的 Whisper 模型完成音频转写。支持的音频格式、语言列表等参数细节以 Whisper 官方文档和官方仓库的说明为准。基本用法如下from haystack.components.audio import LocalWhisperTranscriber whisper LocalWhisperTranscriber(modelsmall) whisper.warm_up() transcription whisper.run(sources[path/to/audio/file])初始化参数__init__def __init__(model: WhisperLocalModel large, device: Optional[ComponentDevice] None, whisper_params: Optional[dict[str, Any]] None)参数类型 / 默认值说明modelWhisperLocalModel默认large要使用的 Whisper 模型名称可取tiny、base、small、medium、large默认。各模型的规模、语言支持与精度差异参考 Whisper 官方文档中的可用模型与语言章节deviceOptional[ComponentDevice]默认None模型加载的设备为None时自动选择默认设备whisper_paramsOptional[dict[str, Any]]默认None透传给底层 Whisper 推理的附加参数用于覆盖转写行为从当前仓库的发布记录可以看到device参数经历过一次类型升级早期版本接受cuda:0这类字符串之后改为采用框架无关的设备管理方案。迁移方式为from haystack.utils.device import ComponentDevice, Device from haystack.components.audio import LocalWhisperTranscriber device ComponentDevice.from_single(Device.gpu(id0)) # 或 # device ComponentDevice.from_str(cuda:0) transcriber LocalWhisperTranscriber(devicedevice)该变更对应发布说明 whisper-loc-new-devices-0665a24cd92ee4b6.yaml意味着组件的设备选择逻辑与其他 Haystack 组件保持一致可直接复用统一的ComponentDevice配置。warm_up预加载模型def warm_up() - None在管道启动时把模型加载进内存。由于本地 Whisper 模型尤其是large加载耗时较长推荐在构建管道后显式调用warm_up()避免首次run时产生不可预期的延迟。run转写入口component.output_types(documentslist[Document]) def run(sources: list[Union[str, Path, ByteStream]], whisper_params: Optional[dict[str, Any]] None)参数说明sources待转写的音频文件路径str或Path或二进制流ByteStream列表whisper_params本次运行时的 Whisper 推理参数支持音频格式、语言等可选项。返回值是一个字典唯一键为documents列表中每个Document对应一个输入音频文件content字段是转写文本meta中保存 Whisper 模型返回的元数据包括对齐alignment数据和所用音频文件的路径。值得注意的是输入参数从早期的audio_files更名为sources以统一 Haystack 各组件的输入插槽命名同时新增了ByteStream支持——这一变更见 change-localwhispertranscriber-run-3b0a818060867720.yaml。如果你的管道中仍使用旧参数名需要把audio_files改为sources。transcribe底层转写方法def transcribe(sources: list[Union[str, Path, ByteStream]], **kwargs) - list[Document]这是组件内部实际执行转写的方法接收音频文件列表返回Document列表每个输入文件一个。run本质上是对transcribe的管道化封装声明了输出类型以便在Pipeline中自动连线。依赖与安装注意事项从仓库发布记录 simplify-whisper-installation-1e347e2527cbf913.yaml 可以看到早期openai-whisper的tiktoken依赖与 Haystack 存在版本冲突官方通过升级openai-whisper到20231106版本并重新引入 audio 安装可选依赖extra来解决。因此使用本地组件时建议显式安装不低于该版本的openai-whisper并确保系统具备ffmpegWhisper 的音频解码前置条件。RemoteWhisperTranscriberOpenAI Whisper API 转写组件功能定位与用法RemoteWhisperTranscriber通过 OpenAI 的 Whisper API 完成转写需要配置 OpenAI API Key认证方式见 OpenAI 官方文档。基本用法如下from haystack.components.audio import RemoteWhisperTranscriber whisper RemoteWhisperTranscriber(api_keySecret.from_token(your-api-key), modeltiny) transcription whisper.run(sources[path/to/audio/file])初始化参数__init__def __init__(api_key: Secret Secret.from_env_var(OPENAI_API_KEY), model: str whisper-1, api_base_url: Optional[str] None, organization: Optional[str] None, http_client_kwargs: Optional[dict[str, Any]] None, **kwargs)参数说明参数默认值说明api_keySecret.from_env_var(OPENAI_API_KEY)OpenAI API Key。默认从环境变量OPENAI_API_KEY读取也可在初始化时通过Secret显式传入modelwhisper-1模型名称目前仅接受whisper-1api_base_urlNone可选的 API 基础地址用于指向兼容 OpenAI 音频接口的自托管/代理端点organizationNoneOpenAI 组织 ID多组织账号建议设置用法见 OpenAI 组织配置文档http_client_kwargsNone配置底层httpx.Client/httpx.AsyncClient的参数字典可在此设置超时、代理、TLS 等传输层选项**kwargs—其余可选参数直接透传给 OpenAI 端点**kwargs透传参数中常用的有language输入音频的语言以 ISO-639-1 格式提供如zh、en。预先声明语言可提升转写准确率并降低延迟prompt引导模型风格或衔接上一段音频的可选文本需与音频语言一致response_format转写输出格式该组件仅支持jsontemperature采样温度取值 01。较高值如 0.8输出更随机较低值如 0.2更聚焦、更确定设为 0 时模型会基于 log probability 自动逐步升高温度直到触及阈值。从源码结构看RemoteWhisperTranscriber的模型参数名也经历过一次统一早期的model_name/model_name_or_path被重命名为model见 rename-model-param--transcribers-71dbe7cfb86950e0.yaml。此外该组件底层已迁移到 OpenAI SDK 实现见 migrate-remote-whisper-transcriber-to-openai-sdk-980ae6f54ddfd7df.yaml。run转写入口component.output_types(documentslist[Document]) def run(sources: list[Union[str, Path, ByteStream]])sources文件路径或ByteStream对象列表包含待转写的音频文件。返回值同样是包含documents键的字典每个输入文件对应一个Documentcontent为转写文本。与本地组件相同远端组件的输入类型也从早期的list[ByteStream]扩展为list[Union[str, Path, ByteStream]]见 remotetranscriber-input-type-aae9a255435a3507.yaml这样它可以直接连接FileTypeRouter等按文件类型分流的路由组件也支持先落盘再传路径的管道写法接入方式更灵活。序列化与反序列化to_dict / from_dict两个组件都实现了 Haystack 标准的组件序列化协议这是它们能作为 YAML 管道的一部分被保存和加载的前提# LocalWhisperTranscriber / RemoteWhisperTranscriber 通用 def to_dict() - dict[str, Any] # 序列化为字典 classmethod def from_dict(cls, data: dict[str, Any]) # 从字典反序列化为组件实例to_dict()把组件当前配置模型名、设备、API 端点、透传参数等序列化为字典远端组件中的Secret按 Haystack 的密钥约定序列化避免明文 API Key 泄漏到配置文件from_dict(data)类方法接收to_dict()产出的字典并重建组件。在Pipeline层面这两个方法由Pipeline.dumps()/Pipeline.loads()统一调度你通常不需要手动调用但当需要把单个组件配置嵌入外部系统如自定义配置中心时可以直接使用。选型建议本地还是远端基于上述 API 设计可以归纳出两者的选型要点数据合规 / 离线 / 大批量选LocalWhisperTranscriber。音频不出本地且可通过model参数在tinylarge之间权衡精度与吞吐用device指定 GPU如ComponentDevice.from_single(Device.gpu(id0))加速推理代价是需要本地算力、openai-whisper依赖和ffmpeg。免运维 / 低延迟起步 / 峰值弹性选RemoteWhisperTranscriber。无需本地模型http_client_kwargs支持传输层定制language、temperature、prompt等参数直接透传 API代价是需要 API Key 与网络访问且受 API 计费约束。两者输出契约一致documents: list[Document]每个音频一个 Document因此可以在不改动下游组件如文本预处理、写入 Document Store的情况下互换便于先以远端 API 验证流程、再切换本地模型降本。重要变更组件已迁出 Haystack 核心包阅读本参考文档时需要注意其版本背景该 API 文档对应 Haystack 2.20。在当前的 Haystack 3.x 中LocalWhisperTranscriber与RemoteWhisperTranscriber已从核心包移除迁移到独立的whisper-haystack集成包中相关发布说明见 deprecate-whisper-components-95822a86cd87fdc0.yaml 与 remove-whisper-components-30108535da20e41f.yaml。当前仓库根目录的 MIGRATION.md 中的迁移表明确给出了新旧导入路径对照旧导入haystack-ai 3.0.0新包新导入from haystack.components.audio import LocalWhisperTranscriberwhisper-haystackfrom haystack_integrations.components.audio.whisper import LocalWhisperTranscriberfrom haystack.components.audio import RemoteWhisperTranscriberwhisper-haystackfrom haystack_integrations.components.audio.whisper import RemoteWhisperTranscriber实际迁移步骤为安装新包pip install whisper-haystack然后更新导入路径本地组件还需要额外安装openai-whisper20231106与ffmpeg。迁移的官方理由是把这类组件拆到独立包中以便隔离测试、独立于 Haystack 主发布周期地修复问题同时让核心包的开发和 CI 更轻量。小结Haystack 2.20 音频 API 的核心内容可归纳为三点其一LocalWhisperTranscriber与RemoteWhisperTranscriber提供了本地推理与云端 API 两条音频转写路径参数与输入输出契约sources进、documents出高度对称便于在管道中互换其二两个组件均支持str/Path/ByteStream混合输入和标准to_dict/from_dict序列化可直接接入管道并持久化为 YAML其三在 Haystack 3.x 中这两个组件已迁移至whisper-haystack集成包升级项目时应按 MIGRATION.md 的对照表更新导入路径并补齐openai-whisper依赖。【免费下载链接】haystackOpen-source AI orchestration framework for building context-engineered, production-ready LLM applications. Design modular pipelines and agent workflows with explicit control over retrieval, routing, memory, and generation. Built for scalable agents, RAG, multimodal applications, semantic search, and conversational systems.项目地址: https://gitcode.com/GitHub_Trending/ha/haystack创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考