-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into mypy-poe-typecheck
- Loading branch information
Showing
114 changed files
with
3,577 additions
and
1,091 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,3 @@ | |
# limitations under the License. | ||
|
||
|
||
from beeai_framework.agents.bee.agent import BeeAgent | ||
|
||
__all__ = ["BeeAgent"] |
54 changes: 54 additions & 0 deletions
54
python/beeai_framework/adapters/amazon_bedrock/backend/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Amazon Bedrock | ||
|
||
## Configuration | ||
|
||
Set the following environment variables | ||
|
||
* AWS_ACCESS_KEY_ID | ||
* AWS_SECRET_ACCESS_KEY | ||
* AWS_REGION_NAME | ||
|
||
## Tested Models | ||
|
||
Only Meta, Mistral, & Amazon Titan serverless models have been tested. | ||
|
||
Other models should work, as beeai_framework uses LiteLLM. See [docs](https://docs.litellm.ai/docs/providers/bedrock) for more information | ||
|
||
## Known Issues with tool use and structured output | ||
|
||
The following models report Tool use not supported: | ||
|
||
```text | ||
litellm.llms.bedrock.common_utils.BedrockError: {"message":"This model doesn't support tool use."} | ||
``` | ||
|
||
* `meta.llama3-70b-instruct-v1:0` | ||
* `meta.llama3-8b-instruct-v1:0` | ||
|
||
The following fail to return structured output with beeai_framework. Initial investigation indicates that these models are not responding with structured JSON output when requested | ||
|
||
* `amazon.titan-text-express-v1` | ||
* `amazon.titan-text-lite-v1` | ||
* `mistral.mistral-7b-instruct-v0:2` | ||
* `mistral.mixtral-8x7b-instruct-v0:1` | ||
* `mistral.mistral-large-2402-v1:0` | ||
|
||
The following models fail with an exception: | ||
|
||
```text | ||
litellm.exceptions.BadRequestError: litellm.BadRequestError: BedrockException - {"message":"This model doesn't support the toolConfig.toolChoice.tool field. Remove toolConfig.toolChoice.tool and try again."} | ||
``` | ||
|
||
* `mistral.mistral-large-2402-v1:0` | ||
|
||
## Quota limits | ||
|
||
Default quota limits on Amazon Bedrock are low, and can cause even | ||
simple examples to fail with: | ||
|
||
```text | ||
litellm.exceptions.RateLimitError: litellm.RateLimitError: BedrockException - {"message":"Too many requests, please wait before trying again."} | ||
``` | ||
|
||
To increase quota limits, see [Amazon Bedrock #](https://aws.amazon.com/bedrock/#/) and | ||
[Amazon Bedrock quotas](https://docs.aws.amazon.com/bedrock/latest/userguide/quotas.html). |
15 changes: 15 additions & 0 deletions
15
python/beeai_framework/adapters/amazon_bedrock/backend/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2025 IBM Corp. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
63 changes: 63 additions & 0 deletions
63
python/beeai_framework/adapters/amazon_bedrock/backend/chat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2025 IBM Corp. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import os | ||
|
||
from beeai_framework.adapters.litellm.chat import LiteLLMChatModel | ||
from beeai_framework.backend.constants import ProviderName | ||
from beeai_framework.logger import Logger | ||
|
||
logger = Logger(__name__) | ||
|
||
|
||
class AmazonBedrockChatModel(LiteLLMChatModel): | ||
@property | ||
def provider_id(self) -> ProviderName: | ||
return "amazon_bedrock" | ||
|
||
def __init__(self, model_id: str | None = None, settings: dict | None = None) -> None: | ||
_settings = settings.copy() if settings is not None else {} | ||
|
||
aws_access_key_id = _settings.get("aws_access_key_id", os.getenv("AWS_ACCESS_KEY_ID")) | ||
if not aws_access_key_id: | ||
raise ValueError( | ||
"Access key is required for Amazon Bedrock model. Specify *aws_access_key_id* " | ||
+ "or set AWS_ACCESS_KEY_ID environment variable" | ||
) | ||
|
||
aws_secret_access_key = _settings.get("aws_secret_access_key", os.getenv("AWS_SECRET_ACCESS_KEY")) | ||
if not aws_secret_access_key: | ||
raise ValueError( | ||
"Secret key is required for Amazon Bedrock model. Specify *aws_secret_access_key* " | ||
+ "or set AWS_SECRET_ACCESS_KEY environment variable" | ||
) | ||
|
||
aws_region_name = _settings.get("aws_region_name", os.getenv("AWS_REGION_NAME")) | ||
if not aws_region_name: | ||
raise ValueError( | ||
"Region is required for Amazon Bedrock model. Specify *aws_region_name* " | ||
+ "or set AWS_REGION_NAME environment variable" | ||
) | ||
|
||
super().__init__( | ||
(model_id if model_id else os.getenv("AWS_CHAT_MODEL", "llama-3.1-8b-instant")), | ||
provider_id="bedrock", | ||
settings=_settings | ||
| { | ||
"aws_access_key_id": aws_access_key_id, | ||
"aws_secret_access_key": aws_secret_access_key, | ||
"aws_region_name": aws_region_name, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2025 IBM Corp. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2025 IBM Corp. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from typing import Any, TypeVar | ||
|
||
from langchain_core.callbacks import AsyncCallbackManagerForToolRun | ||
from langchain_core.runnables import RunnableConfig | ||
from langchain_core.tools import StructuredTool | ||
from langchain_core.tools import Tool as LangChainSimpleTool | ||
from pydantic import BaseModel, ConfigDict | ||
|
||
from beeai_framework.context import RunContext | ||
from beeai_framework.emitter.emitter import Emitter | ||
from beeai_framework.tools.tool import StringToolOutput, Tool, ToolRunOptions | ||
from beeai_framework.utils.strings import to_safe_word | ||
|
||
|
||
class LangChainToolRunOptions(ToolRunOptions): | ||
langchain_runnable_config: RunnableConfig | None = None | ||
model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True) | ||
|
||
|
||
T = TypeVar("T", bound=BaseModel) | ||
|
||
|
||
class LangChainTool(Tool[T, LangChainToolRunOptions, StringToolOutput]): | ||
@property | ||
def name(self) -> str: | ||
return self._tool.name | ||
|
||
@property | ||
def description(self) -> str: | ||
return self._tool.description | ||
|
||
@property | ||
def input_schema(self) -> type[T]: | ||
return self._tool.input_schema | ||
|
||
def _create_emitter(self) -> Emitter: | ||
return Emitter.root().child( | ||
namespace=["tool", "langchain", to_safe_word(self._tool.name)], | ||
creator=self, | ||
) | ||
|
||
def __init__(self, tool: StructuredTool | LangChainSimpleTool, options: dict[str, Any] | None = None) -> None: | ||
super().__init__(options) | ||
self._tool = tool | ||
|
||
async def _run(self, input: T, options: LangChainToolRunOptions | None, context: RunContext) -> StringToolOutput: | ||
langchain_runnable_config = options.langchain_runnable_config or {} if options else {} | ||
args = ( | ||
input if isinstance(input, dict) else input.model_dump(), | ||
{ | ||
**langchain_runnable_config, | ||
"signal": context.signal or None if context else None, | ||
}, | ||
) | ||
is_async = (isinstance(self._tool, StructuredTool) and self._tool.coroutine) or ( | ||
isinstance(args[0].get("run_manager"), AsyncCallbackManagerForToolRun) | ||
) | ||
if is_async: | ||
response = await self._tool.ainvoke(*args) | ||
else: | ||
response = self._tool.invoke(*args) | ||
|
||
return StringToolOutput(result=str(response)) |
Oops, something went wrong.