Skip to content

Commit

Permalink
Initial migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vblagoje committed Jan 30, 2024
1 parent 762045d commit faf2ab9
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 33 deletions.
40 changes: 40 additions & 0 deletions integrations/amazon_bedrock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
**Table of Contents**

- [Installation](#installation)
- [Contributing](#contributing)
- [License](#license)

## Installation
Expand All @@ -16,6 +17,45 @@
pip install amazon-bedrock-haystack
```

## Contributing

`hatch` is the best way to interact with this project, to install it:
```sh
pip install hatch
```

With `hatch` installed, to run all the tests:
```
hatch run test
```
> Note: integration tests will be skipped unless the env var COHERE_API_KEY is set. The api key needs to be valid
> in order to pass the tests.
To only run unit tests:
```
hatch run test -m"not integration"
```

To only run embedders tests:
```
hatch run test -m"embedders"
```

To only run generators tests:
```
hatch run test -m"generators"
```

Markers can be combined, for example you can run only integration tests for embedders with:
```
hatch run test -m"integrations and embedders"
```

To run the linters `ruff` and `mypy`:
```
hatch run lint:all
```

## License

`amazon-bedrock-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
21 changes: 12 additions & 9 deletions integrations/amazon_bedrock/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/m
Issues = "https://github.com/deepset-ai/haystack-core-integrations/issues"
Source = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_bedrock"

[tool.hatch.build.targets.wheel]
packages = ["src/haystack_integrations"]

[tool.hatch.version]
source = "vcs"
tag-pattern = 'integrations\/amazon_bedrock-v(?P<version>.*)'
Expand Down Expand Up @@ -71,7 +74,8 @@ dependencies = [
"ruff>=0.0.243",
]
[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:src/amazon_bedrock_haystack tests}"
typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}"

style = [
"ruff {args:.}",
"black --check --diff {args:.}",
Expand Down Expand Up @@ -136,26 +140,24 @@ unfixable = [
]

[tool.ruff.isort]
known-first-party = ["amazon_bedrock_haystack"]
known-first-party = ["src"]

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "all"
ban-relative-imports = "parents"

[tool.ruff.per-file-ignores]
# Tests can use magic values, assertions, and relative imports
"tests/**/*" = ["PLR2004", "S101", "TID252"]

[tool.coverage.run]
source_pkgs = ["amazon_bedrock_haystack", "tests"]
source_pkgs = ["src", "tests"]
branch = true
parallel = true
omit = [
"src/amazon_bedrock_haystack/__about__.py",
]


[tool.coverage.paths]
amazon_bedrock_haystack = ["src/amazon_bedrock_haystack", "*/amazon_bedrock/src/amazon_bedrock_haystack"]
tests = ["tests", "*/amazon_bedrock_haystack/tests"]
amazon_bedrock_haystack = ["src/haystack_integrations", "*/amazon_bedrock/src/haystack_integrations"]
tests = ["tests", "*/amazon_bedrock/tests"]

[tool.coverage.report]
exclude_lines = [
Expand All @@ -170,6 +172,7 @@ module = [
"transformers.*",
"boto3.*",
"haystack.*",
"haystack_integrations.*",
"pytest.*",
"numpy.*",
]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
from amazon_bedrock_haystack.generators.amazon_bedrock import AmazonBedrockGenerator
from .generator import AmazonBedrockGenerator

__all__ = ["AmazonBedrockGenerator"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional

from amazon_bedrock_haystack.generators.amazon_bedrock_handlers import TokenStreamingHandler
from .amazon_bedrock_handlers import TokenStreamingHandler


class BedrockModelAdapter(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
from botocore.exceptions import BotoCoreError, ClientError
from haystack import component, default_from_dict, default_to_dict

from amazon_bedrock_haystack.errors import (
AmazonBedrockConfigurationError,
AmazonBedrockInferenceError,
AWSConfigurationError,
)
from amazon_bedrock_haystack.generators.amazon_bedrock_adapters import (
from .amazon_bedrock_adapters import (
AI21LabsJurassic2Adapter,
AmazonTitanAdapter,
AnthropicClaudeAdapter,
BedrockModelAdapter,
CohereCommandAdapter,
MetaLlama2ChatAdapter,
)
from amazon_bedrock_haystack.generators.amazon_bedrock_handlers import (
from .amazon_bedrock_handlers import (
DefaultPromptHandler,
DefaultTokenStreamingHandler,
TokenStreamingHandler,
)
from .errors import (
AmazonBedrockConfigurationError,
AmazonBedrockInferenceError,
AWSConfigurationError,
)

logger = logging.getLogger(__name__)

Expand Down
23 changes: 11 additions & 12 deletions integrations/amazon_bedrock/tests/test_amazon_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

import pytest
from botocore.exceptions import BotoCoreError

from amazon_bedrock_haystack.errors import AmazonBedrockConfigurationError
from amazon_bedrock_haystack.generators.amazon_bedrock import AmazonBedrockGenerator
from amazon_bedrock_haystack.generators.amazon_bedrock_adapters import (
from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockGenerator
from haystack_integrations.components.generators.amazon_bedrock.amazon_bedrock_adapters import (
AI21LabsJurassic2Adapter,
AmazonTitanAdapter,
AnthropicClaudeAdapter,
BedrockModelAdapter,
CohereCommandAdapter,
MetaLlama2ChatAdapter,
)
from haystack_integrations.components.generators.amazon_bedrock.errors import AmazonBedrockConfigurationError


@pytest.fixture
Expand All @@ -34,7 +33,7 @@ def mock_boto3_session():
@pytest.fixture
def mock_prompt_handler():
with patch(
"amazon_bedrock_haystack.generators.amazon_bedrock_handlers.DefaultPromptHandler"
"haystack_integrations.components.generators.amazon_bedrock.amazon_bedrock_handlers.DefaultPromptHandler"
) as mock_prompt_handler:
yield mock_prompt_handler

Expand All @@ -55,7 +54,7 @@ def test_to_dict(mock_auto_tokenizer, mock_boto3_session):
)

expected_dict = {
"type": "amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator",
"type": "haystack_integrations.components.generators.amazon_bedrock.generator.AmazonBedrockGenerator",
"init_parameters": {
"model": "anthropic.claude-v2",
"max_length": 99,
Expand All @@ -72,7 +71,7 @@ def test_from_dict(mock_auto_tokenizer, mock_boto3_session):
"""
generator = AmazonBedrockGenerator.from_dict(
{
"type": "amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator",
"type": "haystack_integrations.components.generators.amazon_bedrock.generator.AmazonBedrockGenerator",
"init_parameters": {
"model": "anthropic.claude-v2",
"max_length": 99,
Expand Down Expand Up @@ -235,7 +234,7 @@ def test_supports_for_valid_aws_configuration():

# Patch the class method to return the mock session
with patch(
"amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
"haystack_integrations.components.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
return_value=mock_session,
):
supported = AmazonBedrockGenerator.supports(
Expand Down Expand Up @@ -266,7 +265,7 @@ def test_supports_for_invalid_bedrock_config():

# Patch the class method to return the mock session
with patch(
"amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
"haystack_integrations.components.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
return_value=mock_session,
), pytest.raises(AmazonBedrockConfigurationError, match="Could not connect to Amazon Bedrock."):
AmazonBedrockGenerator.supports(
Expand All @@ -282,7 +281,7 @@ def test_supports_for_invalid_bedrock_config_error_on_list_models():

# Patch the class method to return the mock session
with patch(
"amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
"haystack_integrations.components.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
return_value=mock_session,
), pytest.raises(AmazonBedrockConfigurationError, match="Could not connect to Amazon Bedrock."):
AmazonBedrockGenerator.supports(
Expand Down Expand Up @@ -314,7 +313,7 @@ def test_supports_with_stream_true_for_model_that_supports_streaming():

# Patch the class method to return the mock session
with patch(
"amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
"haystack_integrations.components.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
return_value=mock_session,
):
supported = AmazonBedrockGenerator.supports(
Expand All @@ -335,7 +334,7 @@ def test_supports_with_stream_true_for_model_that_does_not_support_streaming():

# Patch the class method to return the mock session
with patch(
"amazon_bedrock_haystack.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
"haystack_integrations.components.generators.amazon_bedrock.AmazonBedrockGenerator.get_aws_session",
return_value=mock_session,
), pytest.raises(
AmazonBedrockConfigurationError,
Expand Down

0 comments on commit faf2ab9

Please sign in to comment.