Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 31 additions & 37 deletions packages/gen/gen_ai_hub/proxy/langchain/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
124 changes: 80 additions & 44 deletions packages/gen/gen_ai_hub/proxy/langchain/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
"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
Expand Down Expand Up @@ -185,30 +187,43 @@

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

Expand All @@ -218,10 +233,10 @@
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,
Expand All @@ -232,14 +247,18 @@
: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 ''
Expand Down Expand Up @@ -272,10 +291,10 @@
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,
Expand All @@ -293,14 +312,17 @@
: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 ''
Expand All @@ -310,17 +332,31 @@
: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,
)

Check failure on line 335 in packages/gen/gen_ai_hub/proxy/langchain/amazon.py

View workflow job for this annotation

GitHub Actions / checks

ruff (unformatted)

packages/gen/gen_ai_hub/proxy/langchain/amazon.py:335:1: unformatted: File would be reformatted
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(
Expand Down
32 changes: 18 additions & 14 deletions packages/gen/gen_ai_hub/proxy/langchain/google_genai.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,36 +144,40 @@ 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.

:param proxy_client: proxy client to use for the 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,
)


Expand Down
Loading
Loading