cherry-pick ce28f30c
This commit is contained in:
parent
cacea8ab56
commit
0166a38d01
1 changed files with 28 additions and 18 deletions
|
|
@ -511,7 +511,9 @@ def create_app(args):
|
|||
|
||||
return optimized_azure_openai_model_complete
|
||||
|
||||
def create_optimized_gemini_llm_func(config_cache: LLMConfigCache, args):
|
||||
def create_optimized_gemini_llm_func(
|
||||
config_cache: LLMConfigCache, args, llm_timeout: int
|
||||
):
|
||||
"""Create optimized Gemini LLM function with cached configuration"""
|
||||
|
||||
async def optimized_gemini_model_complete(
|
||||
|
|
@ -526,6 +528,8 @@ def create_app(args):
|
|||
if history_messages is None:
|
||||
history_messages = []
|
||||
|
||||
# Use pre-processed configuration to avoid repeated parsing
|
||||
kwargs["timeout"] = llm_timeout
|
||||
if (
|
||||
config_cache.gemini_llm_options is not None
|
||||
and "generation_config" not in kwargs
|
||||
|
|
@ -567,7 +571,7 @@ def create_app(args):
|
|||
config_cache, args, llm_timeout
|
||||
)
|
||||
elif binding == "gemini":
|
||||
return create_optimized_gemini_llm_func(config_cache, args)
|
||||
return create_optimized_gemini_llm_func(config_cache, args, llm_timeout)
|
||||
else: # openai and compatible
|
||||
# Use optimized function with pre-processed configuration
|
||||
return create_optimized_openai_llm_func(config_cache, args, llm_timeout)
|
||||
|
|
@ -601,7 +605,7 @@ def create_app(args):
|
|||
Uses lazy imports for all bindings and avoids repeated configuration parsing.
|
||||
"""
|
||||
|
||||
async def optimized_embedding_function(texts):
|
||||
async def optimized_embedding_function(texts, embedding_dim=None):
|
||||
try:
|
||||
if binding == "lollms":
|
||||
from lightrag.llm.lollms import lollms_embed
|
||||
|
|
@ -640,13 +644,13 @@ def create_app(args):
|
|||
from lightrag.llm.jina import jina_embed
|
||||
|
||||
return await jina_embed(
|
||||
texts, base_url=host, api_key=api_key
|
||||
texts, embedding_dim=embedding_dim, base_url=host, api_key=api_key
|
||||
)
|
||||
else: # openai and compatible
|
||||
from lightrag.llm.openai import openai_embed
|
||||
|
||||
return await openai_embed(
|
||||
texts, model=model, base_url=host, api_key=api_key
|
||||
texts, model=model, base_url=host, api_key=api_key, embedding_dim=embedding_dim
|
||||
)
|
||||
except ImportError as e:
|
||||
raise Exception(f"Failed to import {binding} embedding: {e}")
|
||||
|
|
@ -687,7 +691,7 @@ def create_app(args):
|
|||
|
||||
# Create embedding function with optimized configuration
|
||||
import inspect
|
||||
|
||||
|
||||
# Create the optimized embedding function
|
||||
optimized_embedding_func = create_optimized_embedding_function(
|
||||
config_cache=config_cache,
|
||||
|
|
@ -697,27 +701,33 @@ def create_app(args):
|
|||
api_key=args.embedding_binding_api_key,
|
||||
args=args, # Pass args object for fallback option generation
|
||||
)
|
||||
|
||||
|
||||
# Check environment variable for sending dimensions
|
||||
embedding_send_dim = os.getenv("EMBEDDING_SEND_DIM", "false").lower() == "true"
|
||||
|
||||
|
||||
# Check if the function signature has embedding_dim parameter
|
||||
# Note: Since optimized_embedding_func is an async function, inspect its signature
|
||||
sig = inspect.signature(optimized_embedding_func)
|
||||
has_embedding_dim_param = 'embedding_dim' in sig.parameters
|
||||
|
||||
# Determine send_dimensions value
|
||||
# Only send dimensions if both conditions are met:
|
||||
# 1. EMBEDDING_SEND_DIM environment variable is true
|
||||
# 2. The function has embedding_dim parameter
|
||||
send_dimensions = embedding_send_dim and has_embedding_dim_param
|
||||
|
||||
has_embedding_dim_param = "embedding_dim" in sig.parameters
|
||||
|
||||
# Determine send_dimensions value based on binding type
|
||||
# Jina REQUIRES dimension parameter (forced to True)
|
||||
# OpenAI and others: controlled by EMBEDDING_SEND_DIM environment variable
|
||||
if args.embedding_binding == "jina":
|
||||
# Jina API requires dimension parameter - always send it
|
||||
send_dimensions = has_embedding_dim_param
|
||||
dimension_control = "forced (Jina API requirement)"
|
||||
else:
|
||||
# For OpenAI and other bindings, respect EMBEDDING_SEND_DIM setting
|
||||
send_dimensions = embedding_send_dim and has_embedding_dim_param
|
||||
dimension_control = f"env_var={embedding_send_dim}"
|
||||
|
||||
logger.info(
|
||||
f"Embedding configuration: send_dimensions={send_dimensions} "
|
||||
f"(env_var={embedding_send_dim}, has_param={has_embedding_dim_param}, "
|
||||
f"({dimension_control}, has_param={has_embedding_dim_param}, "
|
||||
f"binding={args.embedding_binding})"
|
||||
)
|
||||
|
||||
|
||||
# Create EmbeddingFunc with send_dimensions attribute
|
||||
embedding_func = EmbeddingFunc(
|
||||
embedding_dim=args.embedding_dim,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue