diff --git a/pyproject.toml b/pyproject.toml index 519d2d60..5a206cce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ dependencies = [ "fastapi >= 0.104.1", "python-multipart >= 0.0.5", "jinja2 == 3.1.2", - "openai >= 0.27.0, < 1.0.0", + "openai >= 1.0.0", "tiktoken >= 0.3.2", "tenacity >= 8.2.2", "pillow ~= 9.5.0", diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index 69fab7e5..a5868cb8 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -36,11 +36,11 @@ class StreamingChatCallbackHandler(StreamingStdOutCallbackHandler): @retry( retry=( - retry_if_exception_type(openai.error.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + retry_if_exception_type(openai._exceptions.APITimeoutError) + | retry_if_exception_type(openai._exceptions.APIError) + | retry_if_exception_type(openai._exceptions.APIConnectionError) + | retry_if_exception_type(openai._exceptions.RateLimitError) + | retry_if_exception_type(openai._exceptions.APIStatusError) ), wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(3), @@ -57,11 +57,11 @@ def completion_with_backoff(**kwargs): @retry( retry=( - retry_if_exception_type(openai.error.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + retry_if_exception_type(openai._exceptions.APITimeoutError) + | retry_if_exception_type(openai._exceptions.APIError) + | retry_if_exception_type(openai._exceptions.APIConnectionError) + | retry_if_exception_type(openai._exceptions.RateLimitError) + | retry_if_exception_type(openai._exceptions.APIStatusError) ), wait=wait_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(3), diff --git a/src/khoj/processor/conversation/openai/whisper.py b/src/khoj/processor/conversation/openai/whisper.py index 72834d92..351319b7 100644 --- a/src/khoj/processor/conversation/openai/whisper.py +++ b/src/khoj/processor/conversation/openai/whisper.py @@ -3,7 +3,7 @@ from io import BufferedReader # External Packages from asgiref.sync import sync_to_async -import openai +from openai import OpenAI async def transcribe_audio(audio_file: BufferedReader, model, api_key) -> str: @@ -11,5 +11,6 @@ async def transcribe_audio(audio_file: BufferedReader, model, api_key) -> str: Transcribe audio file using Whisper model via OpenAI's API """ # Send the audio data to the Whisper API - response = await sync_to_async(openai.Audio.translate)(model=model, file=audio_file, api_key=api_key) + client = OpenAI(api_key=api_key) + response = await sync_to_async(client.audio.translations.create)(model=model, file=audio_file) return response["text"] diff --git a/src/khoj/utils/models.py b/src/khoj/utils/models.py index b5bbe292..37d09418 100644 --- a/src/khoj/utils/models.py +++ b/src/khoj/utils/models.py @@ -32,7 +32,7 @@ class OpenAI(BaseEncoder): raise Exception( f"Set OpenAI API key under processor-config > conversation > openai-api-key in config file: {state.config_file}" ) - openai.api_key = state.processor_config.conversation.openai_model.api_key + self.openai_client = openai.OpenAI(api_key=state.processor_config.conversation.openai_model.api_key) self.embedding_dimensions = None def encode(self, entries, device=None, **kwargs): @@ -43,7 +43,7 @@ class OpenAI(BaseEncoder): processed_entry = entries[index].replace("\n", " ") try: - response = openai.Embedding.create(input=processed_entry, model=self.model_name) + response = self.openai_client.embeddings.create(input=processed_entry, model=self.model_name) embedding_tensors += [torch.tensor(response.data[0].embedding, device=device)] # Use current models embedding dimension, once available # Else default to embedding dimensions of the text-embedding-ada-002 model