Skip to content

Commit

Permalink
✨(openai) Add "Create transcription" action
Browse files Browse the repository at this point in the history
Closes #1134
  • Loading branch information
baptisteArno committed Aug 20, 2024
1 parent 2275c69 commit 984c2bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/forge/blocks/openai/actions/createTranscription.tsx
Original file line number Diff line number Diff line change
@@ -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)
},
},
})
2 changes: 2 additions & 0 deletions packages/forge/blocks/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,6 +21,7 @@ export const openAIBlock = createBlock({
askAssistant,
generateVariables,
createSpeech,
createTranscription,
],
docsUrl: 'https://docs.typebot.io/forge/blocks/openai',
})

0 comments on commit 984c2bf

Please sign in to comment.