-
Notifications
You must be signed in to change notification settings - Fork 126
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: Add Amazon Bedrock chat model support #333
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0e925c4
Add AmazonBedrockChatGenerator, add Anthropic Claude support
vblagoje 94de537
Add Meta Llama 2 chat model support
vblagoje 40a1e1d
Add unit tests, small fixes
vblagoje 86bf417
Rename print_streaming_chunk back to default_streaming_callback until…
vblagoje c3e4bcd
Hatch lint
vblagoje 091d190
Test updates
vblagoje b41cc5d
Minor fix
vblagoje a38e5b0
Use gpt2 tokenizer and llama 2 chat template
vblagoje e603bc1
Pylint
vblagoje baac9fe
Revert back to llama2 tokenizer
vblagoje 13279db
Use gpt2 with special_token_map, use llama2 chat template
vblagoje 2a62b8c
Hook prompt length check
vblagoje 199bdc6
Merge branch 'main' into amzn_chat
vblagoje 11b9b2f
PR feedback David
vblagoje 1c071b0
Reverse PR unrelated files
vblagoje 1b2faaa
First check model, then open AWS connection - David
vblagoje 57d7d5f
Small fix
vblagoje c3ede50
Merge branch 'main' into amzn_chat
vblagoje c2dd097
Merge branch 'main' into amzn_chat
vblagoje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
...amazon_bedrock/src/haystack_integrations/components/generators/amazon_bedrock/__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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from .chat.chat_generator import AmazonBedrockChatGenerator | ||
from .generator import AmazonBedrockGenerator | ||
|
||
__all__ = ["AmazonBedrockGenerator"] | ||
__all__ = ["AmazonBedrockGenerator", "AmazonBedrockChatGenerator"] |
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
3 changes: 3 additions & 0 deletions
3
...n_bedrock/src/haystack_integrations/components/generators/amazon_bedrock/chat/__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,3 @@ | ||
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 |
258 changes: 258 additions & 0 deletions
258
...n_bedrock/src/haystack_integrations/components/generators/amazon_bedrock/chat/adapters.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,258 @@ | ||
import json | ||
import logging | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Callable, Dict, List | ||
|
||
from haystack.dataclasses import ChatMessage, ChatRole, StreamingChunk | ||
from transformers import AutoTokenizer, PreTrainedTokenizer | ||
|
||
from haystack_integrations.components.generators.amazon_bedrock.handlers import DefaultPromptHandler | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BedrockModelChatAdapter(ABC): | ||
""" | ||
Base class for Amazon Bedrock chat model adapters. | ||
""" | ||
|
||
def __init__(self, generation_kwargs: Dict[str, Any]) -> None: | ||
self.generation_kwargs = generation_kwargs | ||
|
||
@abstractmethod | ||
def prepare_body(self, messages: List[ChatMessage], **inference_kwargs) -> Dict[str, Any]: | ||
"""Prepares the body for the Amazon Bedrock request.""" | ||
|
||
def get_responses(self, response_body: Dict[str, Any]) -> List[ChatMessage]: | ||
"""Extracts the responses from the Amazon Bedrock response.""" | ||
return self._extract_messages_from_response(response_body) | ||
|
||
def get_stream_responses(self, stream, stream_handler: Callable[[StreamingChunk], None]) -> List[str]: | ||
tokens: List[str] = [] | ||
for event in stream: | ||
chunk = event.get("chunk") | ||
if chunk: | ||
decoded_chunk = json.loads(chunk["bytes"].decode("utf-8")) | ||
token = self._extract_token_from_stream(decoded_chunk) | ||
# take all the rest key/value pairs from the chunk, add them to the metadata | ||
stream_metadata = {k: v for (k, v) in decoded_chunk.items() if v != token} | ||
stream_chunk = StreamingChunk(content=token, meta=stream_metadata) | ||
# callback the stream handler with StreamingChunk | ||
stream_handler(stream_chunk) | ||
tokens.append(token) | ||
responses = ["".join(tokens).lstrip()] | ||
return responses | ||
|
||
def _update_params(self, target_dict: Dict[str, Any], updates_dict: Dict[str, Any]) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
""" | ||
Updates target_dict with values from updates_dict. Merges lists instead of overriding them. | ||
|
||
:param target_dict: The dictionary to update. | ||
:param updates_dict: The dictionary with updates. | ||
""" | ||
for key, value in updates_dict.items(): | ||
if key in target_dict and isinstance(target_dict[key], list) and isinstance(value, list): | ||
# Merge lists and remove duplicates | ||
target_dict[key] = sorted(set(target_dict[key] + value)) | ||
else: | ||
# Override the value in target_dict | ||
target_dict[key] = value | ||
|
||
def _get_params(self, inference_kwargs: Dict[str, Any], default_params: Dict[str, Any]) -> Dict[str, Any]: | ||
""" | ||
Merges params from inference_kwargs with the default params and self.generation_kwargs. | ||
Uses a helper function to merge lists or override values as necessary. | ||
""" | ||
davidsbatista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Start with a copy of default_params | ||
kwargs = default_params.copy() | ||
|
||
# Update the default params with self.generation_kwargs and finally inference_kwargs | ||
self._update_params(kwargs, self.generation_kwargs) | ||
self._update_params(kwargs, inference_kwargs) | ||
|
||
return kwargs | ||
|
||
def _ensure_token_limit(self, prompt: str) -> str: | ||
resize_info = self.check_prompt(prompt) | ||
if resize_info["prompt_length"] != resize_info["new_prompt_length"]: | ||
logger.warning( | ||
"The prompt was truncated from %s tokens to %s tokens so that the prompt length and " | ||
"the answer length (%s tokens) fit within the model's max token limit (%s tokens). " | ||
"Shorten the prompt or it will be cut off.", | ||
resize_info["prompt_length"], | ||
max(0, resize_info["model_max_length"] - resize_info["max_length"]), # type: ignore | ||
resize_info["max_length"], | ||
resize_info["model_max_length"], | ||
) | ||
return str(resize_info["resized_prompt"]) | ||
|
||
@abstractmethod | ||
def check_prompt(self, prompt: str) -> Dict[str, Any]: | ||
""" | ||
Checks the prompt length and resizes it if necessary. | ||
|
||
:param prompt: The prompt to check. | ||
:return: A dictionary containing the resized prompt and additional information. | ||
""" | ||
|
||
@abstractmethod | ||
def _extract_messages_from_response(self, response_body: Dict[str, Any]) -> List[ChatMessage]: | ||
"""Extracts the responses from the Amazon Bedrock response.""" | ||
|
||
@abstractmethod | ||
def _extract_token_from_stream(self, chunk: Dict[str, Any]) -> str: | ||
"""Extracts the token from a streaming chunk.""" | ||
|
||
|
||
class AnthropicClaudeChatAdapter(BedrockModelChatAdapter): | ||
""" | ||
Model adapter for the Anthropic Claude model. | ||
""" | ||
|
||
ANTHROPIC_USER_TOKEN = "\n\nHuman:" | ||
ANTHROPIC_ASSISTANT_TOKEN = "\n\nAssistant:" | ||
|
||
def __init__(self, generation_kwargs: Dict[str, Any]): | ||
super().__init__(generation_kwargs) | ||
|
||
# We pop the model_max_length as it is not sent to the model | ||
# but used to truncate the prompt if needed | ||
# Anthropic Claude has a limit of at least 100000 tokens | ||
# https://docs.anthropic.com/claude/reference/input-and-output-sizes | ||
model_max_length = self.generation_kwargs.pop("model_max_length", 100000) | ||
|
||
# Truncate prompt if prompt tokens > model_max_length-max_length | ||
# (max_length is the length of the generated text) | ||
# TODO use Anthropic tokenizer to get the precise prompt length | ||
# See https://github.com/anthropics/anthropic-sdk-python?tab=readme-ov-file#token-counting | ||
self.prompt_handler = DefaultPromptHandler( | ||
tokenizer="gpt2", | ||
model_max_length=model_max_length, | ||
max_length=self.generation_kwargs.get("max_tokens_to_sample") or 512, | ||
) | ||
|
||
def prepare_body(self, messages: List[ChatMessage], **inference_kwargs) -> Dict[str, Any]: | ||
default_params = { | ||
"max_tokens_to_sample": self.generation_kwargs.get("max_tokens_to_sample") or 512, | ||
"stop_sequences": ["\n\nHuman:"], | ||
} | ||
|
||
# combine stop words with default stop sequences, remove stop_words as Anthropic does not support it | ||
stop_sequences = inference_kwargs.get("stop_sequences", []) + inference_kwargs.pop("stop_words", []) | ||
if stop_sequences: | ||
inference_kwargs["stop_sequences"] = stop_sequences | ||
params = self._get_params(inference_kwargs, default_params) | ||
body = {"prompt": self.prepare_chat_messages(messages=messages), **params} | ||
return body | ||
|
||
def prepare_chat_messages(self, messages: List[ChatMessage]) -> str: | ||
conversation = [] | ||
for index, message in enumerate(messages): | ||
if message.is_from(ChatRole.USER): | ||
conversation.append(f"{AnthropicClaudeChatAdapter.ANTHROPIC_USER_TOKEN} {message.content.strip()}") | ||
elif message.is_from(ChatRole.ASSISTANT): | ||
conversation.append(f"{AnthropicClaudeChatAdapter.ANTHROPIC_ASSISTANT_TOKEN} {message.content.strip()}") | ||
elif message.is_from(ChatRole.FUNCTION): | ||
error_message = "Anthropic does not support function calls." | ||
raise ValueError(error_message) | ||
elif message.is_from(ChatRole.SYSTEM) and index == 0: | ||
# Until we transition to the new chat message format system messages will be ignored | ||
# see https://docs.anthropic.com/claude/reference/messages_post for more details | ||
logger.warning( | ||
"System messages are not fully supported by the current version of Claude and will be ignored." | ||
) | ||
else: | ||
invalid_role = f"Invalid role {message.role} for message {message.content}" | ||
raise ValueError(invalid_role) | ||
|
||
prepared_prompt = "".join(conversation) + AnthropicClaudeChatAdapter.ANTHROPIC_ASSISTANT_TOKEN + " " | ||
return self._ensure_token_limit(prepared_prompt) | ||
|
||
def check_prompt(self, prompt: str) -> Dict[str, Any]: | ||
return self.prompt_handler(prompt) | ||
|
||
def _extract_messages_from_response(self, response_body: Dict[str, Any]) -> List[ChatMessage]: | ||
davidsbatista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metadata = {k: v for (k, v) in response_body.items() if k != "completion"} | ||
return [ChatMessage.from_assistant(response_body["completion"], meta=metadata)] | ||
|
||
def _extract_token_from_stream(self, chunk: Dict[str, Any]) -> str: | ||
return chunk.get("completion", "") | ||
|
||
|
||
class MetaLlama2ChatAdapter(BedrockModelChatAdapter): | ||
""" | ||
Model adapter for the Meta Llama 2 models. | ||
""" | ||
|
||
chat_template = ( | ||
"{% if messages[0]['role'] == 'system' %}" | ||
"{% set loop_messages = messages[1:] %}" | ||
"{% set system_message = messages[0]['content'] %}" | ||
"{% else %}" | ||
"{% set loop_messages = messages %}" | ||
"{% set system_message = false %}" | ||
"{% endif %}" | ||
"{% for message in loop_messages %}" | ||
"{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}" | ||
"{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}" | ||
"{% endif %}" | ||
"{% if loop.index0 == 0 and system_message != false %}" | ||
"{% set content = '<<SYS>>\n' + system_message + '\n<</SYS>>\n\n' + message['content'] %}" | ||
"{% else %}" | ||
"{% set content = message['content'] %}" | ||
"{% endif %}" | ||
"{% if message['role'] == 'user' %}" | ||
"{{ bos_token + '[INST] ' + content.strip() + ' [/INST]' }}" | ||
"{% elif message['role'] == 'assistant' %}" | ||
"{{ ' ' + content.strip() + ' ' + eos_token }}" | ||
"{% endif %}" | ||
"{% endfor %}" | ||
) | ||
|
||
def __init__(self, generation_kwargs: Dict[str, Any]) -> None: | ||
super().__init__(generation_kwargs) | ||
# We pop the model_max_length as it is not sent to the model | ||
# but used to truncate the prompt if needed | ||
# Llama 2 has context window size of 4096 tokens | ||
# with some exceptions when the context window has been extended | ||
model_max_length = self.generation_kwargs.pop("model_max_length", 4096) | ||
|
||
# Use `google/flan-t5-base` as it's also BPE sentencepiece tokenizer just like llama 2 | ||
# a) we should get good estimates for the prompt length (empirically close to llama 2) | ||
# b) we can use apply_chat_template with the template above to delineate ChatMessages | ||
tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base") | ||
tokenizer.bos_token = "<s>" | ||
tokenizer.eos_token = "</s>" | ||
tokenizer.unk_token = "<unk>" | ||
self.prompt_handler = DefaultPromptHandler( | ||
tokenizer=tokenizer, | ||
model_max_length=model_max_length, | ||
max_length=self.generation_kwargs.get("max_gen_len") or 512, | ||
) | ||
|
||
def prepare_body(self, messages: List[ChatMessage], **inference_kwargs) -> Dict[str, Any]: | ||
default_params = {"max_gen_len": self.generation_kwargs.get("max_gen_len") or 512} | ||
|
||
# combine stop words with default stop sequences, remove stop_words as MetaLlama2 does not support it | ||
stop_sequences = inference_kwargs.get("stop_sequences", []) + inference_kwargs.pop("stop_words", []) | ||
if stop_sequences: | ||
inference_kwargs["stop_sequences"] = stop_sequences | ||
params = self._get_params(inference_kwargs, default_params) | ||
body = {"prompt": self.prepare_chat_messages(messages=messages), **params} | ||
return body | ||
|
||
def prepare_chat_messages(self, messages: List[ChatMessage]) -> str: | ||
prepared_prompt: str = self.prompt_handler.tokenizer.apply_chat_template( | ||
conversation=messages, tokenize=False, chat_template=self.chat_template | ||
) | ||
return self._ensure_token_limit(prepared_prompt) | ||
|
||
def check_prompt(self, prompt: str) -> Dict[str, Any]: | ||
return self.prompt_handler(prompt) | ||
|
||
def _extract_messages_from_response(self, response_body: Dict[str, Any]) -> List[ChatMessage]: | ||
metadata = {k: v for (k, v) in response_body.items() if k != "generation"} | ||
return [ChatMessage.from_assistant(response_body["generation"], meta=metadata)] | ||
|
||
def _extract_token_from_stream(self, chunk: Dict[str, Any]) -> str: | ||
return chunk.get("generation", "") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can add a type to
stream
?