diff --git a/packages/gen/gen_ai_hub/proxy/langchain/__init__.py b/packages/gen/gen_ai_hub/proxy/langchain/__init__.py index 16c5f7e0..4e2065d1 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,47 @@ ) -__all__ = [ - "init_llm", - "init_embedding_model", - "OpenAIClient", - "OpenAIEmbeddings", - "AsyncOpenAIClient", - "ChatOpenAI", - "BedrockEmbeddings", - "AICoreBedrockBaseModel", - "ChatBedrock", - "ChatBedrockConverse", - "ChatGoogleGenerativeAI", - "GoogleGenerativeAIEmbeddings", - "OpenAI", -] +__all__ = ["init_llm", "init_embedding_model"] try: - from .openai import ChatOpenAI, OpenAI, OpenAIEmbeddings - - __all__.extend(["ChatOpenAI", "OpenAI", "OpenAIEmbeddings"]) + 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/amazon.py b/packages/gen/gen_ai_hub/proxy/langchain/amazon.py index 84e5f06c..adf548e0 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,43 @@ 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: + 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 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 - 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 +233,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 +247,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 +291,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 +312,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 +332,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 bf2ec723..f9aba3f0 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 + return amazon.init_chat_converse_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: @@ -128,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, @@ -141,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, )