开始使用 Soniox LangChain中的音频转录加载器
设置
安装包:
npm install @soniox/langchain
凭证
从以下位置获取您的Soniox API密钥 Soniox控制台 并将其设置为环境变量:
用法
基本转录
使用以下方式转录音频文件的示例 SonioxAudioTranscriptLoader 并使用LLM生成摘要
const audioFileUrl = "https://soniox.com/media/examples/coffee_shop.mp3";
const loader = new SonioxAudioTranscriptLoader(
{
audio: audioFileUrl,
},
{
language_hints: ["en"],
// Any other transcription parameters you find here
// https://soniox.com/docs/stt/api-reference/transcriptions/create_transcription
}
);
console.log(`Transcribing ${audioFileUrl}...`);
const docs = await loader.load();
const transcriptText = docs[0].pageContent;
console.log(`Transcript: ${transcriptText}`);
// Create a chain to summarize the transcript
const prompt = ChatPromptTemplate.fromTemplate(
"Write a concise summary of the following speech:\n\n{transcript}"
);
const chain = prompt
.pipe(new ChatOpenAI({ model: "gpt-5-mini" }))
.pipe(new StringOutputParser());
const summary = await chain.invoke({ transcript: transcriptText });
console.log(summary);
您也可以从二进制数据转录音频:
// Fetch the file
const response = await fetch("https://github.com/soniox/soniox_examples/raw/refs/heads/master/speech_to_text/assets/coffee_shop.mp3");
const audioBuffer = await response.bytes(); // Uint8Array
const loader = new SonioxAudioTranscriptLoader({
audio: audioBuffer,
})
const docs = await loader.load();
console.log(docs[0].pageContent); // Transcribed text
翻译
从任何检测到的语言翻译到目标语言:
const loader = new SonioxAudioTranscriptLoader(
{
audio: audioFileUrl,
},
{
translation: {
type: "one_way",
target_language: "fr",
},
language_hints: ["en"],
}
);
const docs = await loader.load();
let originalText = "";
let translatedText = "";
for (const token of docs[0].metadata.tokens) {
if (token.translation_status === "translation") {
translatedText += token.text;
} else {
originalText += token.text;
}
}
console.log(originalText);
console.log(translatedText);
您也可以使用以下方式同时在两种语言之间转录和翻译 two_way 翻译类型。了解更多关于 Soniox翻译.
语言提示
Soniox自动检测并转录语音 **60多种语言**。当您知道音频中可能出现哪些语言时,请提供 language_hints 以通过将识别偏向这些语言来提高准确性
语言提示 **不会限制** 识别——它们仅 **偏向** 模型向指定语言,同时仍允许检测到其他语言(如果存在)
const loader = new SonioxAudioTranscriptLoader(
{
audio: audioFileUrl,
},
{
language_hints: ["en", "es"],
}
);
const docs = await loader.load();
更多详细信息,请参阅 Soniox语言提示文档.
说话人分割
启用说话人识别以区分不同说话人:
const loader = new SonioxAudioTranscriptLoader(
{
audio: audioFileUrl,
},
{
enable_speaker_diarization: true,
}
);
const docs = await loader.load();
// Access speaker information in the metadata
let currentSpeaker = null;
let output = "";
for (const token of docs[0].metadata.tokens) {
if (currentSpeaker !== token.speaker) {
currentSpeaker = token.speaker;
output += `\nSpeaker ${currentSpeaker}: ${token.text.trimStart()}`;
} else {
output += token.text;
}
}
console.log(output);
// Analyze the conversation
const prompt = ChatPromptTemplate.fromTemplate(
`Analyze the following conversation between speakers.
Identify the intent of each speaker.
Conversation:
{conversation}`
);
const chain = prompt
.pipe(new ChatOpenAI({ model: "gpt-5-mini" }))
.pipe(new StringOutputParser());
const analysis = await chain.invoke({ conversation: output });
console.log(analysis);
语言识别
启用自动语言检测和识别:
const loader = new SonioxAudioTranscriptLoader(
{
audio: audioFileUrl,
},
{
enable_language_identification: true,
}
);
提高准确性的上下文
提供特定领域 上下文 以提高转录准确性:
const loader = new SonioxAudioTranscriptLoader(
{
audio: audioBuffer,
},
{
context: {
general: [
{ key: "industry", value: "healthcare" },
{ key: "meeting_type", value: "consultation" }
],
terms: ["hypertension", "cardiology", "metformin"],
translation_terms: [
{ source: "blood pressure", target: "presión arterial" },
{ source: "medication", target: "medicamento" }
]
}
}
);
更多详细信息,请参阅 Soniox上下文文档.
API参考
构造函数参数
SonioxLoaderParams(必需)
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
audio | `Uint8Array \ | string` | 是 |
audioFormat | SonioxAudioFormat | 否 | 音频文件格式 |
apiKey | string | 否 | Soniox API密钥(默认为 SONIOX_API_KEY 环境变量) |
apiBaseUrl | string | 否 | API基础URL(默认为 https://api.soniox.com/v1) |
pollingIntervalMs | number | 否 | 轮询间隔(毫秒,最小值:1000,默认值:1000) |
pollingTimeoutMs | number | 否 | 轮询超时(毫秒,默认值:180000) |
SonioxLoaderOptions (可选)
| 参数 | 类型 | 描述 |
|---|---|---|
model | SonioxTranscriptionModelId | 使用的模型 (默认: "stt-async-v4") |
translation | object | 翻译配置 |
language_hints | string[] | 转录语言提示 |
language_hints_strict | boolean | 强制严格的语言提示 |
enable_speaker_diarization | boolean | 启用说话人识别 |
enable_language_identification | boolean | 启用语言检测 |
context | object | 用于提高准确性的上下文 |
浏览 文档 获取支持选项的完整列表。
支持的音频格式
- -
aac- 高级音频编码 - -
aiff- 音频交换文件格式 - -
amr- 自适应多速率 - -
asf- 高级系统格式 - -
flac- 免费无损音频编解码器 - -
mp3- MPEG 音频第三层 - -
ogg- Ogg Vorbis - -
wav- 波形音频文件格式 - -
webm- WebM 音频
返回值
该 load() 方法返回一个包含单个对象的数组 Document object:
type Document {
pageContent: string, // The transcribed text
metadata: SonioxTranscriptResponse // Full transcript with metadata
}
元数据包括转录文本、说话人信息(如果启用了说话人分离)、语言信息(如果启用了识别)、翻译数据(如果启用了翻译)以及时间信息。
type SonioxTranscriptResponse = {
id: string;
text?: string | null;
tokens?: SonioxTranscriptToken[] | null;
}
令牌类型:
type SonioxTranscriptToken = {
text: string;
start_ms?: number | null;
end_ms?: number | null;
confidence?: number | null;
speaker?: number | string | null;
language?: string | null;
translation_status?: string | null;
};
您可以了解更多关于 SonioxTranscriptResponse 类型的信息,请参阅 Soniox REST API 参考.