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

Input formatting fix for Llama3 with Bedrock #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion libs/aws/langchain_aws/llms/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def prepare_input(
input_body["prompt"] = _human_assistant_format(prompt)
if "max_tokens_to_sample" not in input_body:
input_body["max_tokens_to_sample"] = 1024
elif provider in ("ai21", "cohere", "meta", "mistral"):
elif provider in ("ai21", "cohere", "mistral"):
input_body["prompt"] = prompt
elif provider == "meta":
input_body = dict()
Copy link
Collaborator

@3coins 3coins May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This excludes all model arguments from input body, is that an expected change? Should those be passed in a separate key?

Copy link
Author

@MasciocchiReply MasciocchiReply May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In AWS Bedrock, It says that that the body for a API request to Llama 3 should be in the format:
" "body": "{"prompt":"this is where you place your input text","max_gen_len":512,"temperature":0.5,"top_p":0.9}" "
(is mandatory to pass the field "prompt")

Without "input_body = dict()" I had the error:
"required key [prompt] not found#: extraneous key [stop_sequences] is not permitted"
Because in the input body remains the field "stop_sequence", which is a not-expected key for Llama 3.

So yes, all model arguments (only "stop_sequence" in this case) are to be excluded from the input body, and not be passed.

input_body["prompt"] = prompt
elif provider == "amazon":
input_body = dict()
Expand Down Expand Up @@ -212,6 +215,12 @@ def prepare_output_stream(
chunk_obj["is_finished"] or chunk_obj[output_key] == "<EOS_TOKEN>"
):
return

elif (
provider == "meta"
and chunk_obj.get("stop_reason") == "stop"
):
return

elif (
provider == "mistral"
Expand Down Expand Up @@ -340,6 +349,7 @@ class BedrockBase(BaseLanguageModel, ABC):
"ai21": "stop_sequences",
"cohere": "stop_sequences",
"mistral": "stop_sequences",
"meta": "stop_sequences",
}

guardrails: Optional[Mapping[str, Any]] = {
Expand Down
Loading