格拉地亚 Java 脚本 SDK
类型脚本/ JavaScript SDK 格拉迪亚 API (英语).
所需资源
对于非浏览器环境,你都需要 节点.js 20+ 或者说 阿宝 。 。 。 .
安装
npm install @gladiaio/sdk
如果您使用Node.js < 22, 您也需要安装 ws 软件包 :
npm install ws
使用量
导入 GladiaClient 并创造实例.
提供 API 密钥 apiKey 或 GLADIA_API_KEY 环境变量. 把你的API 密钥拿来 一分钟内.
您也可以设置 GLADIA_API_URL对于现场会议, GLADIA_REGION (单位:千美元)eu-west 编号 : us-west)仅通过会话创建(POST /v2/live) (中文(简体) ).
节点.js/浏览器(ESM)
import { GladiaClient } from '@gladiaio/sdk'
const gladiaClient = new GladiaClient({
apiKey: 'your-api-key',
})
节点.js(共同JS)
const { GladiaClient } = require('@gladiaio/sdk')
const gladiaClient = new GladiaClient({
apiKey: 'your-api-key',
})
浏览器( 标记)
<script src="https://unpkg.com/@gladiaio/sdk"></script>
<script>
const gladiaClient = new Gladia.GladiaClient({
apiKey: 'your-api-key',
})
</script>
预先记录的抄录
transcribe() 接受文件路径(节点), http(s) 網址, (中文(简体) ), File,或 Blob。它在需要时上传,然后是民意调查,直到工作完成.
import { GladiaClient } from '@gladiaio/sdk'
import 'dotenv/config'
const gladiaClient = new GladiaClient()
const audioPath = '../data/online-meeting-example.mp4'
const result = await gladiaClient.preRecorded().transcribe(audioPath, {
language_config: { languages: ['en'] },
})
console.log(result.result?.transcription?.full_transcript ?? '')
见所有 这里支持的语言 来啊!
通过 选项 参数以启用从 音频智能 例如分化、翻译、PII编辑等.
Async 预录
如果你的运行时间没有顶级 await,将电话打入 async 函数 :
async function main() {
const gladiaClient = new GladiaClient()
const result = await gladiaClient.preRecorded().transcribe('./audio.mp3')
console.log(result.result?.transcription?.full_transcript ?? '')
}
main().catch(console.error)
现场录音
const liveSession = gladiaClient.liveV2().startSession({
model: 'solaria-1',
encoding: 'wav/pcm',
sample_rate: 16000,
bit_depth: 16,
channels: 1,
language_config: {
languages: ['en'],
},
messages_config: {
receive_partial_transcripts: true,
},
})
liveSession.on('message', (message) => {
if (message.type === 'transcript') {
console.log(`${message.data.is_final ? 'F' : 'P'} | ${message.data.utterance.text.trim()}`)
}
})
liveSession.once('started', () => {
console.log(`Session ${liveSession.sessionId} started`)
})
liveSession.on('error', (err) => {
console.error('An error occurred during live session:', err)
})
liveSession.once('ended', () => {
console.log(`Session ${liveSession.sessionId} ended`)
})
liveSession.sendAudio(/* <audio_chunk> */)
liveSession.stopRecording()
见所有 这里支持的语言 来啊!
通过 选项 参数以启用从 音频智能 例如分化、翻译、PII编辑等.
等待会话结束
改为: await 关闭后 stopRecording(),请等待 ended 事件:
import { GladiaClient } from '@gladiaio/sdk'
async function runLive() {
const gladiaClient = new GladiaClient({ apiKey: 'your-api-key' })
const liveSession = gladiaClient.liveV2().startSession({
language_config: { languages: ['en'] },
})
const ended = new Promise<void>((resolve) => {
liveSession.once('ended', () => resolve())
})
liveSession.stopRecording()
await ended
}
runLive().catch(console.error)
当您需要会话编号时, 当后端创建会话时 :
const sessionId = await liveSession.getSessionId()
文档
许可证
麻省理工学院