Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
本文介绍如何使用 AssemblyAI API 从文件加载音频(和视频)转录内容作为文档对象。
使用方法
首先,你需要安装官方的 AssemblyAI 包:
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";
// 你也可以使用本地文件路径,加载器会为你将其上传到 AssemblyAI。
const audioUrl = "https://storage.googleapis.com/aai-docs-samples/espn.m4a";
// 使用 `AudioTranscriptParagraphsLoader` 或 `AudioTranscriptSentencesLoader` 可以将转录内容分割为段落或句子
const loader = new AudioTranscriptLoader(
{
audio: audioUrl,
// 其他参数请参考文档:https://www.assemblyai.com/docs/api-reference/transcripts/submit
},
{
apiKey: "<ASSEMBLYAI_API_KEY>", // 或者设置 `ASSEMBLYAI_API_KEY` 环境变量
}
);
const docs = await loader.load();
console.dir(docs, { depth: Infinity });
** 提示 **
- 你可以使用
AudioTranscriptParagraphsLoader 或 AudioTranscriptSentencesLoader 将转录内容分割为段落或句子。
audio 参数可以是 URL、本地文件路径、缓冲区或流。
audio 也可以是视频文件。请参阅 FAQ 文档中支持的文件类型列表。
- 如果你不传入
apiKey 选项,加载器将使用 ASSEMBLYAI_API_KEY 环境变量。
- 除了
audio 之外,你还可以添加更多属性。完整的请求参数列表请参考 AssemblyAI API 文档。
你也可以使用 AudioSubtitleLoader 来获取 srt 或 vtt 格式的字幕作为文档。
import { AudioSubtitleLoader } from "@langchain/community/document_loaders/web/assemblyai";
// 你也可以使用本地文件路径,加载器会为你将其上传到 AssemblyAI。
const audioUrl = "https://storage.googleapis.com/aai-docs-samples/espn.m4a";
const loader = new AudioSubtitleLoader(
{
audio: audioUrl,
// 其他参数请参考文档:https://www.assemblyai.com/docs/api-reference/transcripts/submit
},
"srt", // srt 或 vtt
{
apiKey: "<ASSEMBLYAI_API_KEY>", // 或者设置 `ASSEMBLYAI_API_KEY` 环境变量
}
);
const docs = await loader.load();
console.dir(docs, { depth: Infinity });