From 6b674724bced5157e0b96415abaee4f2a8329f11 Mon Sep 17 00:00:00 2001 From: Sicheng Dong Date: Tue, 1 Dec 2026 16:08:39 +0100 Subject: [PATCH 1/4] fix the optional dependency bug --- .../gen_ai_hub/proxy/langchain/__init__.py | 69 +++++++++---------- .../gen_ai_hub/proxy/langchain/init_models.py | 7 +- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/packages/gen/gen_ai_hub/proxy/langchain/__init__.py b/packages/gen/gen_ai_hub/proxy/langchain/__init__.py index 16c5f7e0..dd8ee5d8 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/__init__.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/__init__.py @@ -1,18 +1,4 @@ from .init_models import init_embedding_model, init_llm -from .openai import ( - OpenAI, - OpenAIEmbeddings, - OpenAIClient, - AsyncOpenAIClient, - ChatOpenAI, -) -from .amazon import ( - BedrockEmbeddings, - AICoreBedrockBaseModel, - ChatBedrock, - ChatBedrockConverse, -) -from .google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings # Make sure that the user has a recent version of langchain installed _langchain_is_below_0_1_0 = None @@ -32,39 +18,48 @@ ) -__all__ = [ - "init_llm", - "init_embedding_model", - "OpenAIClient", - "OpenAIEmbeddings", - "AsyncOpenAIClient", - "ChatOpenAI", - "BedrockEmbeddings", - "AICoreBedrockBaseModel", - "ChatBedrock", - "ChatBedrockConverse", - "ChatGoogleGenerativeAI", - "GoogleGenerativeAIEmbeddings", - "OpenAI", -] -try: - from .openai import ChatOpenAI, OpenAI, OpenAIEmbeddings +__all__ = ["init_llm", "init_embedding_model"] - __all__.extend(["ChatOpenAI", "OpenAI", "OpenAIEmbeddings"]) +try: + from .openai import ( + AsyncOpenAIClient, + ChatOpenAI, + OpenAI, + OpenAIClient, + OpenAIEmbeddings, + ) except ImportError: pass +else: + __all__ += [ + "AsyncOpenAIClient", + "ChatOpenAI", + "OpenAI", + "OpenAIClient", + "OpenAIEmbeddings", + ] try: - from .amazon import ChatBedrock - - __all__.append("ChatBedrock") + from .amazon import ( + AICoreBedrockBaseModel, + BedrockEmbeddings, + ChatBedrock, + ChatBedrockConverse, + ) except ImportError: pass +else: + __all__ += [ + "AICoreBedrockBaseModel", + "BedrockEmbeddings", + "ChatBedrock", + "ChatBedrockConverse", + ] try: from .google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings - - __all__.extend(["ChatGoogleGenerativeAI", "GoogleGenerativeAIEmbeddings"]) except ImportError: pass +else: + __all__ += ["ChatGoogleGenerativeAI", "GoogleGenerativeAIEmbeddings"] diff --git a/packages/gen/gen_ai_hub/proxy/langchain/init_models.py b/packages/gen/gen_ai_hub/proxy/langchain/init_models.py index bf2ec723..96ec5559 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/init_models.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/init_models.py @@ -8,7 +8,6 @@ from gen_ai_hub.proxy.core import get_proxy_client from gen_ai_hub.proxy.core.base import BaseDeployment, BaseProxyClient -from gen_ai_hub.proxy.langchain import amazon, google_genai, openai def default_f_select_deployment( @@ -81,16 +80,22 @@ def _init_custom_model( def _get_init_func(model_name: str, model_type: ModelType): if any(model_name.startswith(prefix) for prefix in ["amazon", "anthropic"]): + from . import amazon + if model_type == ModelType.EMBEDDINGS: return amazon.init_embedding_model else: return amazon.init_chat_model elif any(model_name.startswith(prefix) for prefix in ["google", "gemini"]): + from . import google_genai + if model_type == ModelType.EMBEDDINGS: return google_genai.init_embedding_model else: return google_genai.init_chat_model else: + from . import openai + if model_type == ModelType.EMBEDDINGS: return openai.init_embedding_model else: From d3863a02b1aae89d01447765f4a1ec5b1026d8d9 Mon Sep 17 00:00:00 2001 From: Sicheng Dong Date: Wed, 2 Dec 2026 09:30:04 +0100 Subject: [PATCH 2/4] fix the code format --- packages/gen/gen_ai_hub/proxy/langchain/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gen/gen_ai_hub/proxy/langchain/__init__.py b/packages/gen/gen_ai_hub/proxy/langchain/__init__.py index dd8ee5d8..4e2065d1 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/__init__.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/__init__.py @@ -18,7 +18,6 @@ ) - __all__ = ["init_llm", "init_embedding_model"] try: From 8e3f413a68846f9d510b9a9879a5e6c055ea8520 Mon Sep 17 00:00:00 2001 From: Sicheng Dong Date: Fri, 4 Dec 2026 15:15:32 +0100 Subject: [PATCH 3/4] fix the init_llm function by removing default value and switch amazon to Converse API --- .../gen/gen_ai_hub/proxy/langchain/amazon.py | 122 +++++++++++------- .../proxy/langchain/google_genai.py | 32 +++-- .../gen_ai_hub/proxy/langchain/init_models.py | 28 ++-- .../gen/gen_ai_hub/proxy/langchain/openai.py | 30 +++-- 4 files changed, 129 insertions(+), 83 deletions(-) diff --git a/packages/gen/gen_ai_hub/proxy/langchain/amazon.py b/packages/gen/gen_ai_hub/proxy/langchain/amazon.py index 84e5f06c..cde23140 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/amazon.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/amazon.py @@ -63,7 +63,9 @@ def __init__( "config_name": config_name, "proxy_client": proxy_client, } - kwargs["client_params"] = client_params + model_kwargs = kwargs.pop("model_kwargs", {}) or {} + model_kwargs["client_params"] = client_params + kwargs["model_kwargs"] = model_kwargs super().__init__(*args, model_id=model_id, **kwargs) @staticmethod @@ -185,30 +187,41 @@ def __init__(self, *args, **kwargs): def _build_bedrock_model_kwargs( deployment: Deployment, - temperature: float, - max_tokens: int, + temperature: Optional[float], + max_tokens: Optional[int], top_k: Optional[int], - top_p: float, + top_p: Optional[float], stop_sequences: Optional[List[str]], config: Optional[Config], ) -> Dict[str, Any]: """Builds the model_kwargs dictionary for Bedrock models.""" - if top_k: - logging.warning( - "Top-k is disabled for Amazon Bedrock models. Ignoring top-k value." - ) + model_kwargs = {} + + is_anthropic = deployment.model_name.startswith("anthropic") - model_kwargs = {"temperature": temperature} + if temperature is not None and not is_anthropic: + model_kwargs["temperature"] = temperature if config: model_kwargs["config"] = config - if deployment.model_name.startswith("anthropic"): - model_kwargs["max_tokens"] = max_tokens - model_kwargs["top_p"] = top_p + if is_anthropic: + # The Bedrock Invoke API supports top_k for Anthropic Claude models. + if top_k is not None: + model_kwargs["top_k"] = top_k + if max_tokens is not None: + model_kwargs["max_tokens"] = max_tokens else: # Assuming Amazon bedrock models otherwise - model_kwargs["maxTokenCount"] = max_tokens - model_kwargs["topP"] = top_p + # Amazon Titan/Nova text-generation config has no top_k parameter. + if top_k is not None: + logging.warning( + "Top-k is not supported for Amazon Bedrock models. " + "Ignoring top-k value." + ) + if max_tokens is not None: + model_kwargs["maxTokenCount"] = max_tokens + if top_p is not None: + model_kwargs["topP"] = top_p if stop_sequences: model_kwargs["stopSequences"] = stop_sequences @@ -218,10 +231,10 @@ def _build_bedrock_model_kwargs( def init_chat_model( proxy_client: BaseProxyClient, deployment: Deployment, - temperature: float = 0.0, - max_tokens: int = 256, + temperature: Optional[float] = None, + max_tokens: Optional[int] = None, top_k: Optional[int] = None, - top_p: float = 1.0, + top_p: Optional[float] = None, stop_sequences: List[str] = None, model_id: Optional[str] = "", config: Optional[Config] = None, @@ -232,14 +245,18 @@ def init_chat_model( :type proxy_client: BaseProxyClient :param deployment: the deployment information :type deployment: Deployment - :param temperature: the temperature for the model, defaults to 0.0 - :type temperature: float, optional - :param max_tokens: the maximum number of tokens to generate, defaults to 256 - :type max_tokens: int, optional - :param top_k: the top-k sampling parameter, defaults to None + :param temperature: the temperature for the model. If not set, the model + default is used (optional) + :type temperature: Optional[float], optional + :param max_tokens: the maximum number of tokens to generate. If not set, the + model default is used (optional) + :type max_tokens: Optional[int], optional + :param top_k: the top-k sampling parameter. If not set, the model default is + used (optional) :type top_k: Optional[int], optional - :param top_p: the top-p sampling parameter, defaults to 1.0 - :type top_p: float, optional + :param top_p: the top-p sampling parameter. If not set, the model default is + used (optional) + :type top_p: Optional[float], optional :param stop_sequences: the stop sequences for the model, defaults to None :type stop_sequences: List[str], optional :param model_id: the model identifier, defaults to '' @@ -272,10 +289,10 @@ def init_chat_model( def init_chat_converse_model( proxy_client: BaseProxyClient, deployment: Deployment, - temperature: float = 0.0, - max_tokens: int = 256, + temperature: Optional[float] = None, + max_tokens: Optional[int] = None, top_k: Optional[int] = None, - top_p: float = 1.0, + top_p: Optional[float] = None, stop_sequences: List[str] = None, model_id: Optional[str] = "", config: Optional[Config] = None, @@ -293,14 +310,17 @@ def init_chat_converse_model( :type proxy_client: BaseProxyClient :param deployment: the deployment information :type deployment: Deployment - :param temperature: the temperature for the model, defaults to 0.0 - :type temperature: float, optional - :param max_tokens: the maximum number of tokens to generate, defaults to 256 - :type max_tokens: int, optional + :param temperature: the temperature for the model. If not set, the model + default is used (optional) + :type temperature: Optional[float], optional + :param max_tokens: the maximum number of tokens to generate. If not set, the + model default is used (optional) + :type max_tokens: Optional[int], optional :param top_k: the top-k sampling parameter, defaults to None :type top_k: Optional[int], optional - :param top_p: the top-p sampling parameter, defaults to 1.0 - :type top_p: float, optional + :param top_p: the top-p sampling parameter. If not set, the model default is + used (optional) + :type top_p: Optional[float], optional :param stop_sequences: the stop sequences for the model, defaults to None :type stop_sequences: List[str], optional :param model_id: the model identifier, defaults to '' @@ -310,17 +330,31 @@ def init_chat_converse_model( :return: the initialized chat model :rtype: ChatBedrockConverse """ - return ChatBedrockConverse( - model_name=deployment.model_name, - model_id=model_id, - deployment_id=deployment.deployment_id, - proxy_client=proxy_client, - temperature=temperature, - max_tokens=max_tokens, - top_p=top_p, - stop_sequences=stop_sequences, - config=config, - ) + + additional_model_request_fields = {} + if top_k is not None: + additional_model_request_fields["top_k"] = top_k + + converse_kwargs: Dict[str, Any] = { + "model_name": deployment.model_name, + "model_id": model_id, + "deployment_id": deployment.deployment_id, + "proxy_client": proxy_client, + "stop_sequences": stop_sequences, + "config": config, + } + if max_tokens is not None: + converse_kwargs["max_tokens"] = max_tokens + if temperature is not None: + converse_kwargs["temperature"] = temperature + if top_p is not None: + converse_kwargs["top_p"] = top_p + if additional_model_request_fields: + converse_kwargs["additional_model_request_fields"] = ( + additional_model_request_fields + ) + + return ChatBedrockConverse(**converse_kwargs) def init_embedding_model( diff --git a/packages/gen/gen_ai_hub/proxy/langchain/google_genai.py b/packages/gen/gen_ai_hub/proxy/langchain/google_genai.py index d13340dd..6cb8f59d 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/google_genai.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/google_genai.py @@ -144,10 +144,10 @@ def _init_parent(self, **kwargs): def init_chat_model( proxy_client: BaseProxyClient, deployment: Deployment, - temperature: float = 0.0, - max_tokens: int = 256, + temperature: Optional[float] = None, + max_tokens: Optional[int] = None, top_k: Optional[int] = None, - top_p: float = 1.0, + top_p: Optional[float] = None, ): """Initialize a ChatGoogleGenerativeAI model with the given parameters. @@ -155,25 +155,29 @@ def init_chat_model( :type proxy_client: BaseProxyClient :param deployment: deployment information for the model :type deployment: Deployment - :param temperature: sampling temperature, defaults to 0.0 - :type temperature: float, optional - :param max_tokens: maximum number of tokens to generate, defaults to 256 - :type max_tokens: int, optional - :param top_k: k for top-k sampling, defaults to None + :param temperature: sampling temperature; if None, the model default is used + :type temperature: Optional[float], optional + :param max_tokens: maximum number of tokens to generate; if None, the model default is used + :type max_tokens: Optional[int], optional + :param top_k: k for top-k sampling; if None, the model default is used :type top_k: Optional[int], optional - :param top_p: p for nucleus sampling, defaults to 1.0 - :type top_p: float, optional + :param top_p: p for nucleus sampling; if None, the model default is used + :type top_p: Optional[float], optional :return: initialized ChatGoogleGenerativeAI model :rtype: ChatGoogleGenerativeAI """ + model_kwargs = { + "temperature": temperature, + "max_output_tokens": max_tokens, + "top_k": top_k, + "top_p": top_p, + } + model_kwargs = {k: v for k, v in model_kwargs.items() if v is not None} return ChatGoogleGenerativeAI( model=deployment.model_name, deployment_id=deployment.deployment_id, proxy_client=proxy_client, - temperature=temperature, - max_output_tokens=max_tokens, - top_k=top_k, - top_p=top_p, + **model_kwargs, ) diff --git a/packages/gen/gen_ai_hub/proxy/langchain/init_models.py b/packages/gen/gen_ai_hub/proxy/langchain/init_models.py index 96ec5559..f9aba3f0 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/init_models.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/init_models.py @@ -85,7 +85,7 @@ def _get_init_func(model_name: str, model_type: ModelType): if model_type == ModelType.EMBEDDINGS: return amazon.init_embedding_model else: - return amazon.init_chat_model + return amazon.init_chat_converse_model elif any(model_name.startswith(prefix) for prefix in ["google", "gemini"]): from . import google_genai @@ -133,10 +133,10 @@ def _init_model( def init_llm( *args, proxy_client: Optional[BaseProxyClient] = None, - temperature: float = 0.0, - max_tokens: int = 256, + temperature: Optional[float] = None, + max_tokens: Optional[int] = None, top_k: Optional[int] = None, - top_p: float = 1.0, + top_p: Optional[float] = None, init_func: Optional[Callable] = None, model_id: Optional[str] = "", **kwargs, @@ -146,14 +146,18 @@ def init_llm( :param proxy_client: The proxy client to use for the model (optional) :type proxy_client: ProxyClient - :param temperature: The temperature parameter for model generation (default: 0.0) - :type temperature: float - :param max_tokens: The maximum number of tokens to generate (default: 256) - :type max_tokens: int - :param top_k: The top-k parameter for model generation (optional) - :type top_k: int - :param top_p: The top-p parameter for model generation (default: 1.0) - :type top_p: float + :param temperature: The temperature parameter for model generation. If None, the + model's own default is used (optional) + :type temperature: Optional[float] + :param max_tokens: The maximum number of tokens to generate. If None, the model's + own default is used (optional) + :type max_tokens: Optional[int] + :param top_k: The top-k parameter for model generation. If None, the model's own + default is used (optional) + :type top_k: Optional[int] + :param top_p: The top-p parameter for model generation. If None, the model's own + default is used (optional) + :type top_p: Optional[float] :param init_func: Function to call for initializing the model, optional :type init_func: Callable :param model_id: id of the Amazon Bedrock model, needed in case a custom Amazon Bedrock model is being diff --git a/packages/gen/gen_ai_hub/proxy/langchain/openai.py b/packages/gen/gen_ai_hub/proxy/langchain/openai.py index 932eb2ec..9fb97634 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/openai.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/openai.py @@ -354,10 +354,10 @@ def _invocation_params(self) -> Dict: def init_chat_model( proxy_client: BaseProxyClient, deployment: BaseDeployment, - temperature: float = 0.0, - max_tokens: int = 256, + temperature: Optional[float] = None, + max_tokens: Optional[int] = None, top_k: Optional[int] = None, - top_p: float = 1.0, + top_p: Optional[float] = None, ): """Initialize the ChatOpenAI model. @@ -365,23 +365,27 @@ def init_chat_model( :type proxy_client: BaseProxyClient :param deployment: the deployment :type deployment: BaseDeployment - :param temperature: the temperature, defaults to 0.0 - :type temperature: float, optional - :param max_tokens: the maximum tokens, defaults to 256 - :type max_tokens: int, optional - :param top_k: the top k, defaults to None + :param temperature: the temperature; if None, the model default is used + :type temperature: Optional[float], optional + :param max_tokens: the maximum tokens; if None, the model default is used + :type max_tokens: Optional[int], optional + :param top_k: the top k; if None, the model default is used :type top_k: Optional[int], optional - :param top_p: the top p, defaults to 1.0 - :type top_p: float, optional + :param top_p: the top p; if None, the model default is used + :type top_p: Optional[float], optional :return: the ChatOpenAI model :rtype: ChatOpenAI """ + model_kwargs = { + "temperature": temperature, + "max_tokens": max_tokens, + "top_p": top_p, + } + model_kwargs = {k: v for k, v in model_kwargs.items() if v is not None} return ChatOpenAI( deployment_id=deployment.deployment_id, proxy_client=proxy_client, - temperature=temperature, - max_tokens=max_tokens, - top_p=top_p, + **model_kwargs, ) From c165d7b3a617423607612c6d3877c03e757a5cec Mon Sep 17 00:00:00 2001 From: Sicheng Dong Date: Fri, 4 Dec 2026 15:40:16 +0100 Subject: [PATCH 4/4] allow temperature, top_p also in old invoke_model API --- packages/gen/gen_ai_hub/proxy/langchain/amazon.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/gen/gen_ai_hub/proxy/langchain/amazon.py b/packages/gen/gen_ai_hub/proxy/langchain/amazon.py index cde23140..adf548e0 100644 --- a/packages/gen/gen_ai_hub/proxy/langchain/amazon.py +++ b/packages/gen/gen_ai_hub/proxy/langchain/amazon.py @@ -199,7 +199,7 @@ def _build_bedrock_model_kwargs( is_anthropic = deployment.model_name.startswith("anthropic") - if temperature is not None and not is_anthropic: + if temperature is not None: model_kwargs["temperature"] = temperature if config: @@ -209,6 +209,8 @@ def _build_bedrock_model_kwargs( # The Bedrock Invoke API supports top_k for Anthropic Claude models. if top_k is not None: model_kwargs["top_k"] = top_k + if top_p is not None: + model_kwargs["top_p"] = top_p if max_tokens is not None: model_kwargs["max_tokens"] = max_tokens else: # Assuming Amazon bedrock models otherwise