Allow passing api key into OpenAIWhisperParser (#7281)

This just allows the user to pass in an api_key directly into
OpenAIWhisperParser. Very simple addition.
This commit is contained in:
Kenny 2023-07-07 15:07:45 -04:00 committed by GitHub
parent 4e7d0c115b
commit 34a2755a54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
from typing import Iterator
from typing import Iterator, Optional
from langchain.document_loaders.base import BaseBlobParser
from langchain.document_loaders.blob_loaders import Blob
@ -9,6 +9,9 @@ class OpenAIWhisperParser(BaseBlobParser):
"""Transcribe and parse audio files.
Audio transcription is with OpenAI Whisper model."""
def __init__(self, api_key: Optional[str] = None):
self.api_key = api_key
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
"""Lazily parse the blob."""
@ -28,6 +31,10 @@ class OpenAIWhisperParser(BaseBlobParser):
"pydub package not found, please install it with " "`pip install pydub`"
)
# Set the API key if provided
if self.api_key:
openai.api_key = self.api_key
# Audio file from disk
audio = AudioSegment.from_file(blob.path)