Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bedrock access with aws access key #3032

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from langflow.base.models.model import LCModelComponent
from langflow.field_typing import Embeddings
from langflow.inputs import SecretStrInput
from langflow.io import DropdownInput, MessageTextInput, Output


Expand All @@ -19,32 +20,50 @@ class AmazonBedrockEmbeddingsComponent(LCModelComponent):
options=["amazon.titan-embed-text-v1"],
value="amazon.titan-embed-text-v1",
),
SecretStrInput(name="aws_access_key", display_name="Access Key"),
SecretStrInput(name="aws_secret_key", display_name="Secret Key"),
MessageTextInput(
name="credentials_profile_name",
display_name="Credentials Profile Name",
advanced=True,
),
MessageTextInput(
name="endpoint_url",
display_name="Bedrock Endpoint URL",
),
MessageTextInput(
name="region_name",
display_name="AWS Region",
),
MessageTextInput(name="region_name", display_name="Region Name", value="us-east-1"),
MessageTextInput(name="endpoint_url", display_name=" Endpoint URL", advanced=True),
]

outputs = [
Output(display_name="Embeddings", name="embeddings", method="build_embeddings"),
]

def build_embeddings(self) -> Embeddings:
try:
output = BedrockEmbeddings(
credentials_profile_name=self.credentials_profile_name,
model_id=self.model_id,
endpoint_url=self.endpoint_url,
region_name=self.region_name,
) # type: ignore
except Exception as e:
raise ValueError("Could not connect to Amazon Bedrock API.") from e
if self.aws_access_key:
import boto3

session = boto3.Session(
aws_access_key_id=self.aws_access_key,
aws_secret_access_key=self.aws_secret_key,
)
elif self.credentials_profile_name:
import boto3

session = boto3.Session(profile_name=self.credentials_profile_name)
else:
import boto3

session = boto3.Session()

client_params = {}
if self.endpoint_url:
client_params["endpoint_url"] = self.endpoint_url
if self.region_name:
client_params["region_name"] = self.region_name

boto3_client = session.client("bedrock-runtime", **client_params)
output = BedrockEmbeddings(
credentials_profile_name=self.credentials_profile_name,
client=boto3_client,
model_id=self.model_id,
endpoint_url=self.endpoint_url,
region_name=self.region_name,
) # type: ignore
return output
47 changes: 33 additions & 14 deletions src/backend/base/langflow/components/models/AmazonBedrockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel
from langflow.inputs import MessageTextInput
from langflow.inputs import MessageTextInput, SecretStrInput
from langflow.io import DictInput, DropdownInput


Expand Down Expand Up @@ -51,27 +51,46 @@ class AmazonBedrockComponent(LCModelComponent):
],
value="anthropic.claude-3-haiku-20240307-v1:0",
),
MessageTextInput(name="credentials_profile_name", display_name="Credentials Profile Name"),
SecretStrInput(name="aws_access_key", display_name="Access Key"),
SecretStrInput(name="aws_secret_key", display_name="Secret Key"),
MessageTextInput(name="credentials_profile_name", display_name="Credentials Profile Name", advanced=True),
MessageTextInput(name="region_name", display_name="Region Name", value="us-east-1"),
DictInput(name="model_kwargs", display_name="Model Kwargs", advanced=True, is_list=True),
MessageTextInput(name="endpoint_url", display_name="Endpoint URL", advanced=True),
]

def build_model(self) -> LanguageModel: # type: ignore[type-var]
model_id = self.model_id
credentials_profile_name = self.credentials_profile_name
region_name = self.region_name
model_kwargs = self.model_kwargs
endpoint_url = self.endpoint_url
stream = self.stream
if self.aws_access_key:
import boto3

session = boto3.Session(
aws_access_key_id=self.aws_access_key,
aws_secret_access_key=self.aws_secret_key,
)
elif self.credentials_profile_name:
import boto3

session = boto3.Session(profile_name=self.credentials_profile_name)
else:
import boto3

session = boto3.Session()

client_params = {}
if self.endpoint_url:
client_params["endpoint_url"] = self.endpoint_url
if self.region_name:
client_params["region_name"] = self.region_name

boto3_client = session.client("bedrock-runtime", **client_params)
try:
output = ChatBedrock( # type: ignore
credentials_profile_name=credentials_profile_name,
model_id=model_id,
region_name=region_name,
model_kwargs=model_kwargs,
endpoint_url=endpoint_url,
streaming=stream,
client=boto3_client,
model_id=self.model_id,
region_name=self.region_name,
model_kwargs=self.model_kwargs,
endpoint_url=self.endpoint_url,
streaming=self.stream,
)
except Exception as e:
raise ValueError("Could not connect to AmazonBedrock API.") from e
Expand Down
2 changes: 2 additions & 0 deletions src/backend/base/langflow/services/settings/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
"VECTARA_CUSTOMER_ID",
"VECTARA_CORPUS_ID",
"VECTARA_API_KEY",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
]
Loading