Skip to content

Commit

Permalink
Fix for Whisper Error: 'File is not defined' when using Speech to Text (
Browse files Browse the repository at this point in the history
FlowiseAI#2526)

* tested ok

* update localai stt file

* update toFile method for OpenAI Assistant uploads

---------

Co-authored-by: Henry Heng <henryheng@flowiseai.com>
Co-authored-by: Henry <hzj94@hotmail.com>
  • Loading branch information
3 people authored May 30, 2024
1 parent 059eae4 commit eb738a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/components/src/speechToText.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICommonObject, IFileUpload } from './Interface'
import { getCredentialData } from './utils'
import { type ClientOptions, OpenAIClient } from '@langchain/openai'
import { type ClientOptions, OpenAIClient, toFile } from '@langchain/openai'
import { AssemblyAI } from 'assemblyai'
import { getFileFromStorage } from './storageUtils'

Expand All @@ -22,8 +22,9 @@ export const convertSpeechToText = async (upload: IFileUpload, speechToTextConfi
apiKey: credentialData.openAIApiKey
}
const openAIClient = new OpenAIClient(openAIClientOptions)
const file = await toFile(audio_file, upload.name)
const openAITranscription = await openAIClient.audio.transcriptions.create({
file: new File([new Blob([audio_file])], upload.name),
file: file,
model: 'whisper-1',
language: speechToTextConfig?.language,
temperature: speechToTextConfig?.temperature ? parseFloat(speechToTextConfig.temperature) : undefined,
Expand Down Expand Up @@ -56,8 +57,9 @@ export const convertSpeechToText = async (upload: IFileUpload, speechToTextConfi
baseURL: speechToTextConfig?.baseUrl
}
const localAIClient = new OpenAIClient(LocalAIClientOptions)
const file = await toFile(audio_file, upload.name)
const localAITranscription = await localAIClient.audio.transcriptions.create({
file: new File([new Blob([audio_file])], upload.name),
file: file,
model: speechToTextConfig?.model || 'whisper-1',
language: speechToTextConfig?.language,
temperature: speechToTextConfig?.temperature ? parseFloat(speechToTextConfig.temperature) : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ const uploadFilesToAssistantVectorStore = async (
const openai = new OpenAI({ apiKey: openAIApiKey })
const uploadedFiles = []
for (const file of files) {
const toFile = await OpenAI.toFile(fs.readFileSync(file.filePath), file.fileName)
const createdFile = await openai.files.create({
file: new File([new Blob([fs.readFileSync(file.filePath)])], file.fileName),
file: toFile,
purpose: 'assistants'
})
uploadedFiles.push(createdFile)
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/services/openai-assistants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ const uploadFilesToAssistant = async (credentialId: string, files: { filePath: s
const uploadedFiles = []

for (const file of files) {
const toFile = await OpenAI.toFile(fs.readFileSync(file.filePath), file.fileName)
const createdFile = await openai.files.create({
file: new File([new Blob([fs.readFileSync(file.filePath)])], file.fileName),
file: toFile,
purpose: 'assistants'
})
uploadedFiles.push(createdFile)
Expand Down

0 comments on commit eb738a1

Please sign in to comment.