53
packages/forge/blocks/openai/actions/createTranscription.tsx
Normal file
53
packages/forge/blocks/openai/actions/createTranscription.tsx
Normal file
@ -0,0 +1,53 @@
|
||||
import { option, createAction } from '@typebot.io/forge'
|
||||
import { defaultOpenAIOptions } from '../constants'
|
||||
import OpenAI, { ClientOptions, toFile } from 'openai'
|
||||
import { isNotEmpty } from '@typebot.io/lib'
|
||||
import { auth } from '../auth'
|
||||
import { baseOptions } from '../baseOptions'
|
||||
import ky from 'ky'
|
||||
|
||||
export const createTranscription = createAction({
|
||||
name: 'Create transcription',
|
||||
auth,
|
||||
baseOptions,
|
||||
options: option.object({
|
||||
url: option.string.layout({
|
||||
label: 'Audio URL',
|
||||
}),
|
||||
transcriptionVariableId: option.string.layout({
|
||||
label: 'Save result to',
|
||||
inputType: 'variableDropdown',
|
||||
}),
|
||||
}),
|
||||
getSetVariableIds: (options) =>
|
||||
options.transcriptionVariableId ? [options.transcriptionVariableId] : [],
|
||||
run: {
|
||||
server: async ({ credentials: { apiKey }, options, variables, logs }) => {
|
||||
if (!options.url) return logs.add('Audio URL is empty')
|
||||
if (!options.transcriptionVariableId)
|
||||
return logs.add('Missing transcription variable')
|
||||
|
||||
const config = {
|
||||
apiKey,
|
||||
baseURL: options.baseUrl ?? defaultOpenAIOptions.baseUrl,
|
||||
defaultHeaders: {
|
||||
'api-key': apiKey,
|
||||
},
|
||||
defaultQuery: isNotEmpty(options.apiVersion)
|
||||
? {
|
||||
'api-version': options.apiVersion,
|
||||
}
|
||||
: undefined,
|
||||
} satisfies ClientOptions
|
||||
|
||||
const openai = new OpenAI(config)
|
||||
|
||||
const result = await openai.audio.transcriptions.create({
|
||||
file: await fetch(options.url),
|
||||
model: 'whisper-1',
|
||||
})
|
||||
|
||||
variables.set(options.transcriptionVariableId, result.text)
|
||||
},
|
||||
},
|
||||
})
|
@ -6,6 +6,7 @@ import { auth } from './auth'
|
||||
import { baseOptions } from './baseOptions'
|
||||
import { askAssistant } from './actions/askAssistant'
|
||||
import { generateVariables } from './actions/generateVariables'
|
||||
import { createTranscription } from './actions/createTranscription'
|
||||
|
||||
export const openAIBlock = createBlock({
|
||||
id: 'openai' as const,
|
||||
@ -20,6 +21,7 @@ export const openAIBlock = createBlock({
|
||||
askAssistant,
|
||||
generateVariables,
|
||||
createSpeech,
|
||||
createTranscription,
|
||||
],
|
||||
docsUrl: 'https://docs.typebot.io/forge/blocks/openai',
|
||||
})
|
||||
|
Reference in New Issue
Block a user