community[patch]: support OpenAI whisper base url (#17695)

**Description:** The base URL for OpenAI is retrieved from the
environment variable "OPENAI_BASE_URL", whereas for langchain it is
obtained from "OPENAI_API_BASE". By adding `base_url =
os.environ.get("OPENAI_API_BASE")`, the OpenAI proxy can execute
correctly.

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
pull/17594/head^2
Zijian Han 3 months ago committed by GitHub
parent 44a3484503
commit 8e976545f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,4 +1,5 @@
import logging import logging
import os
import time import time
from typing import Dict, Iterator, Optional, Tuple from typing import Dict, Iterator, Optional, Tuple
@ -25,10 +26,17 @@ class OpenAIWhisperParser(BaseBlobParser):
""" """
def __init__( def __init__(
self, api_key: Optional[str] = None, *, chunk_duration_threshold: float = 0.1 self,
api_key: Optional[str] = None,
*,
chunk_duration_threshold: float = 0.1,
base_url: Optional[str] = None,
): ):
self.api_key = api_key self.api_key = api_key
self.chunk_duration_threshold = chunk_duration_threshold self.chunk_duration_threshold = chunk_duration_threshold
self.base_url = (
base_url if base_url is not None else os.environ.get("OPENAI_API_BASE")
)
def lazy_parse(self, blob: Blob) -> Iterator[Document]: def lazy_parse(self, blob: Blob) -> Iterator[Document]:
"""Lazily parse the blob.""" """Lazily parse the blob."""
@ -51,11 +59,13 @@ class OpenAIWhisperParser(BaseBlobParser):
if is_openai_v1(): if is_openai_v1():
# api_key optional, defaults to `os.environ['OPENAI_API_KEY']` # api_key optional, defaults to `os.environ['OPENAI_API_KEY']`
client = openai.OpenAI(api_key=self.api_key) client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url)
else: else:
# Set the API key if provided # Set the API key if provided
if self.api_key: if self.api_key:
openai.api_key = self.api_key openai.api_key = self.api_key
if self.base_url:
openai.base_url = self.base_url
# Audio file from disk # Audio file from disk
audio = AudioSegment.from_file(blob.path) audio = AudioSegment.from_file(blob.path)

Loading…
Cancel
Save