mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-12-17 18:17:10 +00:00
Migrate to OpenAI Python library >= 1.0
This commit is contained in:
parent
a57d529f39
commit
7009793170
4 changed files with 16 additions and 15 deletions
|
@ -42,7 +42,7 @@ dependencies = [
|
||||||
"fastapi >= 0.104.1",
|
"fastapi >= 0.104.1",
|
||||||
"python-multipart >= 0.0.5",
|
"python-multipart >= 0.0.5",
|
||||||
"jinja2 == 3.1.2",
|
"jinja2 == 3.1.2",
|
||||||
"openai >= 0.27.0, < 1.0.0",
|
"openai >= 1.0.0",
|
||||||
"tiktoken >= 0.3.2",
|
"tiktoken >= 0.3.2",
|
||||||
"tenacity >= 8.2.2",
|
"tenacity >= 8.2.2",
|
||||||
"pillow ~= 9.5.0",
|
"pillow ~= 9.5.0",
|
||||||
|
|
|
@ -36,11 +36,11 @@ class StreamingChatCallbackHandler(StreamingStdOutCallbackHandler):
|
||||||
|
|
||||||
@retry(
|
@retry(
|
||||||
retry=(
|
retry=(
|
||||||
retry_if_exception_type(openai.error.Timeout)
|
retry_if_exception_type(openai._exceptions.APITimeoutError)
|
||||||
| retry_if_exception_type(openai.error.APIError)
|
| retry_if_exception_type(openai._exceptions.APIError)
|
||||||
| retry_if_exception_type(openai.error.APIConnectionError)
|
| retry_if_exception_type(openai._exceptions.APIConnectionError)
|
||||||
| retry_if_exception_type(openai.error.RateLimitError)
|
| retry_if_exception_type(openai._exceptions.RateLimitError)
|
||||||
| retry_if_exception_type(openai.error.ServiceUnavailableError)
|
| retry_if_exception_type(openai._exceptions.APIStatusError)
|
||||||
),
|
),
|
||||||
wait=wait_random_exponential(min=1, max=10),
|
wait=wait_random_exponential(min=1, max=10),
|
||||||
stop=stop_after_attempt(3),
|
stop=stop_after_attempt(3),
|
||||||
|
@ -57,11 +57,11 @@ def completion_with_backoff(**kwargs):
|
||||||
|
|
||||||
@retry(
|
@retry(
|
||||||
retry=(
|
retry=(
|
||||||
retry_if_exception_type(openai.error.Timeout)
|
retry_if_exception_type(openai._exceptions.APITimeoutError)
|
||||||
| retry_if_exception_type(openai.error.APIError)
|
| retry_if_exception_type(openai._exceptions.APIError)
|
||||||
| retry_if_exception_type(openai.error.APIConnectionError)
|
| retry_if_exception_type(openai._exceptions.APIConnectionError)
|
||||||
| retry_if_exception_type(openai.error.RateLimitError)
|
| retry_if_exception_type(openai._exceptions.RateLimitError)
|
||||||
| retry_if_exception_type(openai.error.ServiceUnavailableError)
|
| retry_if_exception_type(openai._exceptions.APIStatusError)
|
||||||
),
|
),
|
||||||
wait=wait_exponential(multiplier=1, min=4, max=10),
|
wait=wait_exponential(multiplier=1, min=4, max=10),
|
||||||
stop=stop_after_attempt(3),
|
stop=stop_after_attempt(3),
|
||||||
|
|
|
@ -3,7 +3,7 @@ from io import BufferedReader
|
||||||
|
|
||||||
# External Packages
|
# External Packages
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
import openai
|
from openai import OpenAI
|
||||||
|
|
||||||
|
|
||||||
async def transcribe_audio(audio_file: BufferedReader, model, api_key) -> str:
|
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
|
Transcribe audio file using Whisper model via OpenAI's API
|
||||||
"""
|
"""
|
||||||
# Send the audio data to the Whisper 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"]
|
return response["text"]
|
||||||
|
|
|
@ -32,7 +32,7 @@ class OpenAI(BaseEncoder):
|
||||||
raise Exception(
|
raise Exception(
|
||||||
f"Set OpenAI API key under processor-config > conversation > openai-api-key in config file: {state.config_file}"
|
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
|
self.embedding_dimensions = None
|
||||||
|
|
||||||
def encode(self, entries, device=None, **kwargs):
|
def encode(self, entries, device=None, **kwargs):
|
||||||
|
@ -43,7 +43,7 @@ class OpenAI(BaseEncoder):
|
||||||
processed_entry = entries[index].replace("\n", " ")
|
processed_entry = entries[index].replace("\n", " ")
|
||||||
|
|
||||||
try:
|
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)]
|
embedding_tensors += [torch.tensor(response.data[0].embedding, device=device)]
|
||||||
# Use current models embedding dimension, once available
|
# Use current models embedding dimension, once available
|
||||||
# Else default to embedding dimensions of the text-embedding-ada-002 model
|
# Else default to embedding dimensions of the text-embedding-ada-002 model
|
||||||
|
|
Loading…
Reference in a new issue