跳到主要内容
本文介绍如何使用 AssemblyAI API 从文件中将音频(和视频)转录作为文档对象加载。

用法

首先,您需要安装官方的 AssemblyAI 包
npm
npm install @langchain/community @langchain/core assemblyai
要使用加载器,您需要一个 AssemblyAI 账户 并从 仪表板获取您的 AssemblyAI API 密钥 然后,将 API 密钥配置为 ASSEMBLYAI_API_KEY 环境变量或 apiKey 选项参数。
import {
  AudioTranscriptLoader,
  // AudioTranscriptParagraphsLoader,
  // AudioTranscriptSentencesLoader
} from "@langchain/community/document_loaders/web/assemblyai";

// You can also use a local file path and the loader will upload it to AssemblyAI for you.
const audioUrl = "https://storage.googleapis.com/aai-docs-samples/espn.m4a";

// Use `AudioTranscriptParagraphsLoader` or `AudioTranscriptSentencesLoader` for splitting the transcript into paragraphs or sentences
const loader = new AudioTranscriptLoader(
  {
    audio: audioUrl,
    // any other parameters as documented here: https://www.assemblyai.com/docs/api-reference/transcripts/submit
  },
  {
    apiKey: "<ASSEMBLYAI_API_KEY>", // or set the `ASSEMBLYAI_API_KEY` env variable
  }
);
const docs = await loader.load();
console.dir(docs, { depth: Infinity });
** 信息 **
  • 您可以使用 AudioTranscriptParagraphsLoaderAudioTranscriptSentencesLoader 将转录文本分割成段落或句子。
  • audio 参数可以是 URL、本地文件路径、缓冲区或流。
  • audio 也可以是视频文件。请参阅 FAQ 文档中支持的文件类型列表
  • 如果您没有传入 apiKey 选项,加载器将使用 ASSEMBLYAI_API_KEY 环境变量。
  • 除了 audio,您还可以添加更多属性。请在 AssemblyAI API 文档中找到完整的请求参数列表。
您还可以使用 AudioSubtitleLoadersrtvtt 字幕作为文档获取。
import { AudioSubtitleLoader } from "@langchain/community/document_loaders/web/assemblyai";

// You can also use a local file path and the loader will upload it to AssemblyAI for you.
const audioUrl = "https://storage.googleapis.com/aai-docs-samples/espn.m4a";

const loader = new AudioSubtitleLoader(
  {
    audio: audioUrl,
    // any other parameters as documented here: https://www.assemblyai.com/docs/api-reference/transcripts/submit
  },
  "srt", // srt or vtt
  {
    apiKey: "<ASSEMBLYAI_API_KEY>", // or set the `ASSEMBLYAI_API_KEY` env variable
  }
);

const docs = await loader.load();
console.dir(docs, { depth: Infinity });

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.