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

Fixed no session issue while using bedrock #2337

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies = [
"tomli>=2.0.2",
"blinker>=1.9.0",
"json5>=0.10.0",
"boto3>=1.37.10",
]

[project.urls]
Expand Down
26 changes: 24 additions & 2 deletions src/crewai/utilities/embedding_configurator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from typing import Any, Dict, Optional, cast

import boto3
from chromadb import Documents, EmbeddingFunction, Embeddings
from chromadb.api.types import validate_embedding_function

Expand Down Expand Up @@ -154,11 +155,32 @@ def _configure_bedrock(config, model_name):
from chromadb.utils.embedding_functions.amazon_bedrock_embedding_function import (
AmazonBedrockEmbeddingFunction,
)

# Check if session is provided in config
session = config.get("session")

# If no session, try to create one from environment variables
if session is None:
required_env_vars = ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION_NAME"]
missing_vars = [var for var in required_env_vars if not os.getenv(var)]

if missing_vars:
raise ValueError(
f"Missing required environment variables: {', '.join(missing_vars)}. "
f"Either provide a boto3 session in embedding_config or set these environment variables."
)

# Create session from environment variables
session = boto3.Session(
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
region_name=os.getenv("AWS_REGION_NAME")
)

# Allow custom model_name override with backwards compatibility
kwargs = {"session": config.get("session")}
kwargs = {"session": session}
if model_name is not None:
kwargs["model_name"] = model_name

return AmazonBedrockEmbeddingFunction(**kwargs)

@staticmethod
Expand Down
Loading