refac
This commit is contained in:
1 parent
58dcc1b33f
commit
8b6fa1f4ab
1 file changed
+17
-38
@@ -550,8 +550,7 @@ def generate_openai_batch_embeddings(
|
||||
key: str = '',
|
||||
prefix: str = None,
|
||||
user: UserModel = None,
|
||||
) -> Optional[list[list[float]]]:
|
||||
try:
|
||||
) -> list[list[float]]:
|
||||
log.debug(f'generate_openai_batch_embeddings:model {model} batch size: {len(texts)}')
|
||||
json_data = {'input': texts, 'model': model}
|
||||
if isinstance(RAG_EMBEDDING_PREFIX_FIELD_NAME, str) and isinstance(prefix, str):
|
||||
@@ -575,9 +574,6 @@ def generate_openai_batch_embeddings(
|
||||
return [elem['embedding'] for elem in data['data']]
|
||||
else:
|
||||
raise ValueError("Unexpected OpenAI embeddings response: missing 'data' key")
|
||||
except Exception as e:
|
||||
log.exception(f'Error generating openai batch embeddings: {e}')
|
||||
return None
|
||||
|
||||
|
||||
async def agenerate_openai_batch_embeddings(
|
||||
@@ -587,8 +583,7 @@ async def agenerate_openai_batch_embeddings(
|
||||
key: str = '',
|
||||
prefix: str = None,
|
||||
user: UserModel = None,
|
||||
) -> Optional[list[list[float]]]:
|
||||
try:
|
||||
) -> list[list[float]]:
|
||||
log.debug(f'agenerate_openai_batch_embeddings:model {model} batch size: {len(texts)}')
|
||||
form_data = {'input': texts, 'model': model}
|
||||
if isinstance(RAG_EMBEDDING_PREFIX_FIELD_NAME, str) and isinstance(prefix, str):
|
||||
@@ -615,10 +610,7 @@ async def agenerate_openai_batch_embeddings(
|
||||
if 'data' in data:
|
||||
return [item['embedding'] for item in data['data']]
|
||||
else:
|
||||
raise Exception('Something went wrong :/')
|
||||
except Exception as e:
|
||||
log.exception(f'Error generating openai batch embeddings: {e}')
|
||||
return None
|
||||
raise ValueError("Unexpected OpenAI embeddings response: missing 'data' key")
|
||||
|
||||
|
||||
def generate_azure_openai_batch_embeddings(
|
||||
@@ -629,8 +621,7 @@ def generate_azure_openai_batch_embeddings(
|
||||
version: str = '',
|
||||
prefix: str = None,
|
||||
user: UserModel = None,
|
||||
) -> Optional[list[list[float]]]:
|
||||
try:
|
||||
) -> list[list[float]]:
|
||||
log.debug(f'generate_azure_openai_batch_embeddings:deployment {model} batch size: {len(texts)}')
|
||||
json_data = {'input': texts}
|
||||
if isinstance(RAG_EMBEDDING_PREFIX_FIELD_NAME, str) and isinstance(prefix, str):
|
||||
@@ -660,11 +651,8 @@ def generate_azure_openai_batch_embeddings(
|
||||
if 'data' in data:
|
||||
return [elem['embedding'] for elem in data['data']]
|
||||
else:
|
||||
raise Exception('Something went wrong :/')
|
||||
return None
|
||||
except Exception as e:
|
||||
log.exception(f'Error generating azure openai batch embeddings: {e}')
|
||||
return None
|
||||
raise ValueError("Unexpected Azure OpenAI embeddings response: missing 'data' key")
|
||||
raise Exception('Azure OpenAI embedding request failed: max retries (429) exceeded')
|
||||
|
||||
|
||||
async def agenerate_azure_openai_batch_embeddings(
|
||||
@@ -675,8 +663,7 @@ async def agenerate_azure_openai_batch_embeddings(
|
||||
version: str = '',
|
||||
prefix: str = None,
|
||||
user: UserModel = None,
|
||||
) -> Optional[list[list[float]]]:
|
||||
try:
|
||||
) -> list[list[float]]:
|
||||
log.debug(f'agenerate_azure_openai_batch_embeddings:deployment {model} batch size: {len(texts)}')
|
||||
form_data = {'input': texts}
|
||||
if isinstance(RAG_EMBEDDING_PREFIX_FIELD_NAME, str) and isinstance(prefix, str):
|
||||
@@ -705,10 +692,7 @@ async def agenerate_azure_openai_batch_embeddings(
|
||||
if 'data' in data:
|
||||
return [item['embedding'] for item in data['data']]
|
||||
else:
|
||||
raise Exception('Something went wrong :/')
|
||||
except Exception as e:
|
||||
log.exception(f'Error generating azure openai batch embeddings: {e}')
|
||||
return None
|
||||
raise ValueError("Unexpected Azure OpenAI embeddings response: missing 'data' key")
|
||||
|
||||
|
||||
def generate_ollama_batch_embeddings(
|
||||
@@ -718,8 +702,7 @@ def generate_ollama_batch_embeddings(
|
||||
key: str = '',
|
||||
prefix: str = None,
|
||||
user: UserModel = None,
|
||||
) -> Optional[list[list[float]]]:
|
||||
try:
|
||||
) -> list[list[float]]:
|
||||
log.debug(f'generate_ollama_batch_embeddings:model {model} batch size: {len(texts)}')
|
||||
json_data = {'input': texts, 'model': model, 'truncate': True}
|
||||
if isinstance(RAG_EMBEDDING_PREFIX_FIELD_NAME, str) and isinstance(prefix, str):
|
||||
@@ -746,9 +729,6 @@ def generate_ollama_batch_embeddings(
|
||||
return data['embeddings']
|
||||
else:
|
||||
raise ValueError("Unexpected Ollama embeddings response: missing 'embeddings' key")
|
||||
except Exception as e:
|
||||
log.exception(f'Error generating ollama batch embeddings: {e}')
|
||||
return None
|
||||
|
||||
|
||||
async def agenerate_ollama_batch_embeddings(
|
||||
@@ -758,8 +738,7 @@ async def agenerate_ollama_batch_embeddings(
|
||||
key: str = '',
|
||||
prefix: str = None,
|
||||
user: UserModel = None,
|
||||
) -> Optional[list[list[float]]]:
|
||||
try:
|
||||
) -> list[list[float]]:
|
||||
log.debug(f'agenerate_ollama_batch_embeddings:model {model} batch size: {len(texts)}')
|
||||
form_data = {'input': texts, 'model': model, 'truncate': True}
|
||||
if isinstance(RAG_EMBEDDING_PREFIX_FIELD_NAME, str) and isinstance(prefix, str):
|
||||
@@ -789,10 +768,7 @@ async def agenerate_ollama_batch_embeddings(
|
||||
if 'embeddings' in data:
|
||||
return data['embeddings']
|
||||
else:
|
||||
raise Exception('Something went wrong :/')
|
||||
except Exception as e:
|
||||
log.exception(f'Error generating ollama batch embeddings: {e}')
|
||||
return None
|
||||
raise ValueError("Unexpected Ollama embeddings response: missing 'embeddings' key")
|
||||
|
||||
|
||||
def get_embedding_function(
|
||||
@@ -860,10 +836,13 @@ def get_embedding_function(
|
||||
for batch in batches:
|
||||
batch_results.append(await embedding_function(batch, prefix=prefix, user=user))
|
||||
|
||||
# Flatten results
|
||||
# Flatten results — raise if any batch failed
|
||||
embeddings = []
|
||||
for batch_embeddings in batch_results:
|
||||
if isinstance(batch_embeddings, list):
|
||||
for i, batch_embeddings in enumerate(batch_results):
|
||||
if batch_embeddings is None:
|
||||
raise Exception(
|
||||
f'Embedding generation failed for batch {i + 1}/{len(batches)}'
|
||||
)
|
||||
embeddings.extend(batch_embeddings)
|
||||
|
||||
log.debug(
|
||||
|
||||
Reference in new issue
Block a user