-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(openai) Add "Create transcription" action
Closes #1134
- Loading branch information
1 parent
2275c69
commit 984c2bf
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/forge/blocks/openai/actions/createTranscription.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters