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 converse #1061

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9d1fb5e
copy from bedrock chat generator folder
FloRul Aug 13, 2024
946d9ac
modify supported model patterns for supported tools model patterns
FloRul Aug 14, 2024
c54de18
add unsupported models for chat patterns
FloRul Aug 15, 2024
abe617d
Merge branch 'deepset-ai:main' into feat-bedrock-converse
FloRul Aug 15, 2024
dfa27bf
add converseMessage class
FloRul Aug 15, 2024
bd91600
add utils and call converse with only text
FloRul Aug 15, 2024
bcf91cd
adding tool config class and clean up from chat copy
FloRul Aug 15, 2024
f0f5cda
bring tools config to kwargs
FloRul Aug 18, 2024
0eddd9d
first draft for tool calling - add test file
FloRul Aug 20, 2024
1d70fa5
tool use streaming
FloRul Aug 22, 2024
cc373c2
attempt for tool use 1#
FloRul Aug 24, 2024
231bbd6
tool use working
FloRul Aug 24, 2024
cef6ff6
refactor get_stream_response
FloRul Aug 24, 2024
f01af07
fix streaming accumulative response
FloRul Aug 24, 2024
c2c8fea
validate input params against model capabilities
FloRul Aug 24, 2024
0a4e8c0
format check in document block
FloRul Aug 24, 2024
8dac6f5
move files to proper folder
FloRul Aug 26, 2024
98efefc
fix imports
FloRul Aug 27, 2024
21081a1
set up tests and fix module import paths
FloRul Sep 1, 2024
75139dd
more tests
FloRul Sep 2, 2024
8b9d0c1
test consolidation - fix streaming chunk callback
FloRul Sep 5, 2024
4beb3be
update test, examples and model capabilities
FloRul Sep 7, 2024
abf79dd
clean up
FloRul Sep 7, 2024
51c6f65
Merge remote-tracking branch 'upstream/main' into feat-bedrock-converse
FloRul Sep 7, 2024
9a459af
prepare for PR
FloRul Sep 7, 2024
f5c59f8
Update test_converse_generator.py
FloRul Sep 7, 2024
cd63bcf
fixing encoding
FloRul Sep 8, 2024
0af2b89
fixing lint and tests
FloRul Sep 8, 2024
933c192
fixing tests and serialization
FloRul Sep 8, 2024
b3904fc
linter
FloRul Sep 8, 2024
5a456b8
fix lint and tests
FloRul Sep 8, 2024
ea0ff1e
is it AWS_REGION over AWS_DEFAULT_REGION as specified in the pipeline ?
FloRul Sep 8, 2024
6b92ff3
Revert "is it AWS_REGION over AWS_DEFAULT_REGION as specified in the …
FloRul Sep 8, 2024
43d4424
Update test_converse_generator.py
FloRul Sep 9, 2024
b5b2004
remove unused os import
FloRul Sep 9, 2024
1f56a4a
fix test fixtures
FloRul Sep 9, 2024
5f686c8
Update test_converse_generator.py
FloRul Sep 13, 2024
f11fd3b
Merge branch 'main' into feat-bedrock-converse
FloRul Sep 13, 2024
e446ad3
Merge branch 'main' into feat-bedrock-converse
FloRul Sep 13, 2024
f1b68e6
Merge branch 'main' into feat-bedrock-converse
FloRul Sep 17, 2024
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
6 changes: 6 additions & 0 deletions integrations/amazon_bedrock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [unreleased]

### 🚀 Features

- New generator for the bedrock converse API (#977)

## [unreleased]

### 🐛 Bug Fixes

- *(Bedrock)* Allow tools kwargs for AWS Bedrock Claude model (#976)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from haystack import Pipeline

from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockConverseGenerator
from haystack_integrations.components.generators.amazon_bedrock.converse.utils import ConverseMessage, ToolConfig


def get_current_weather(location: str, unit: str = "celsius") -> str:
"""Get the current weather in a given location"""
return f"The weather in {location} is 22 degrees {unit}."


def get_current_time(timezone: str) -> str:
"""Get the current time in a given timezone"""
return f"The current time in {timezone} is 14:30."


generator = AmazonBedrockConverseGenerator(
model="anthropic.claude-3-5-sonnet-20240620-v1:0",
streaming_callback=print,
)

# Create ToolConfig from functions
tool_config = ToolConfig.from_functions([get_current_weather, get_current_time])

tool_config_dict = tool_config.to_dict()


pipeline = Pipeline()
pipeline.add_component("generator", generator)

result = pipeline.run(
data={
"generator": {
"inference_config": {
"temperature": 0.1,
"maxTokens": 256,
"topP": 0.1,
"stopSequences": ["\\n"],
},
"messages": [
ConverseMessage.from_user(["What's the weather like in Paris and what time is it in New York?"]),
],
"tool_config": tool_config_dict,
},
},
)
1 change: 1 addition & 0 deletions integrations/amazon_bedrock/pydoc/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ loaders:
"haystack_integrations.common.amazon_bedrock.errors",
"haystack_integrations.components.generators.amazon_bedrock.handlers",
"haystack_integrations.components.generators.amazon_bedrock.chat.chat_generator",
"haystack_integrations.components.generators.amazon_bedrock.converse.converse_generator",
"haystack_integrations.components.embedders.amazon_bedrock.text_embedder",
"haystack_integrations.components.embedders.amazon_bedrock.document_embedder",
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
#
# SPDX-License-Identifier: Apache-2.0
from .chat.chat_generator import AmazonBedrockChatGenerator
from .converse.capabilities import MODEL_CAPABILITIES
from .converse.converse_generator import AmazonBedrockConverseGenerator
from .converse.utils import ConverseMessage, ToolConfig
from .generator import AmazonBedrockGenerator

__all__ = ["AmazonBedrockGenerator", "AmazonBedrockChatGenerator"]
__all__ = [
"AmazonBedrockGenerator",
"AmazonBedrockChatGenerator",
"AmazonBedrockConverseGenerator",
"ConverseMessage",
"ToolConfig",
"MODEL_CAPABILITIES",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from enum import Enum, auto


class ModelCapability(Enum):
CONVERSE = auto()
CONVERSE_STREAM = auto()
SYSTEM_PROMPTS = auto()
DOCUMENT_CHAT = auto()
VISION = auto()
TOOL_USE = auto()
STREAMING_TOOL_USE = auto()
GUARDRAILS = auto()


# https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html

MODEL_CAPABILITIES = {
"ai21.jamba-instruct-.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
},
"ai21.j2-.*": {
ModelCapability.CONVERSE,
ModelCapability.GUARDRAILS,
},
"amazon.titan-text-.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.GUARDRAILS,
},
"anthropic.claude-v2.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.GUARDRAILS,
},
"anthropic.claude-3.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.VISION,
ModelCapability.TOOL_USE,
ModelCapability.STREAMING_TOOL_USE,
ModelCapability.GUARDRAILS,
},
"cohere.command-text.*": {
ModelCapability.CONVERSE,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.GUARDRAILS,
},
"cohere.command-light.*": {
ModelCapability.CONVERSE,
ModelCapability.GUARDRAILS,
},
"cohere.command-r.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.TOOL_USE,
},
"meta.llama2.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.GUARDRAILS,
},
"meta.llama3.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.GUARDRAILS,
},
"meta.llama3-1.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.TOOL_USE,
ModelCapability.GUARDRAILS,
},
"mistral.mistral-.*-instruct": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.GUARDRAILS,
},
"mistral.mistral-large.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.DOCUMENT_CHAT,
ModelCapability.TOOL_USE,
ModelCapability.GUARDRAILS,
},
"mistral.mistral-small.*": {
ModelCapability.CONVERSE,
ModelCapability.CONVERSE_STREAM,
ModelCapability.SYSTEM_PROMPTS,
ModelCapability.TOOL_USE,
ModelCapability.GUARDRAILS,
},
}
Loading