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

fix guardrail attributes when invoking bedrock #26

Merged
merged 2 commits into from
May 3, 2024

Conversation

brnaba-aws
Copy link
Contributor

Fixed bedrock attributes when using guardrails. See example of usage:

from langchain_core.messages import HumanMessage
from langchain_aws.chat_models import ChatBedrock



llm_model_id='anthropic.claude-3-haiku-20240307-v1:0'

chat = ChatBedrock(
    model_id=llm_model_id,
    streaming=True,
    model_kwargs={"temperature": 0.1},
    guardrails={"guardrailIdentifier": "identifier", "guardrailVersion": "1", "trace": True},
)

messages = [
    HumanMessage(
        content="What are the financial advice you can give me?"
    )
]
for chunk in chat.stream(messages):
    print(chunk.content,  end="", flush=True)

@brnaba-aws
Copy link
Contributor Author

@3coins This is the PR for guardrails with the new structure. Can you please have a look?
Thanks.

Copy link

@rsgrewal-aws rsgrewal-aws left a comment

Choose a reason for hiding this comment

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

approving from bedrock testing

@3coins
Copy link
Collaborator

3coins commented Apr 30, 2024

@brnaba-aws
Thanks for making this update. This looks like a breaking change, does Bedrock have a version that works with the previous format or all clients need to move to this new API?
Also, can you add some unit tests for this, or paste your test results here.

@nagelpat
Copy link

nagelpat commented May 2, 2024

Looking forward to get this fix merged for the GA version of Guardrails for Amazon Bedrock.

This looks like a breaking change, does Bedrock have a version that works with the previous format or all clients need to move to this new API?

Is backward compatibility needed for the "Preview" version of Guardrails?

@MaverickScientist
Copy link

langchain-aws version used: 0.1.4

Have tried all the below scenarios:

  1.  guardrails={ "trace": False,
                 "accept": "application/json",
                 "contentType": "application/json",
                 "modelId": model_id,
     "guardrailIdentifier": "identifier", --> "id" of the Guardrail I created using Bedrock Console.
     "guardrailVersion": "5"}
    
  2. guardrail_kwargs={"guardrailIdentifier": "identifier", "guardrailVersion": "5"}

  3. guardrails={ "trace": False,
    "guardrailIdentifier": "xxxx" --> the ARN of the guardrail I created using Bedrock Console.
    "guardrailVersion": "5"}

  4. guardrails={ "trace": False,
    "guardrailIdentifier": "identifier", --> "id" of the Guardrail I created using Bedrock Console.
    "guardrailVersion": "5"}

Adding the exception below:

  1. 2024-05-19 15:27:29,346 ERROR root ScriptRunner.scriptThread : Error Occured: Error raised by bedrock service: Parameter validation failed:
    Unknown parameter in input: "guardrailIdentifier", must be one of: body, contentType, accept, modelId
    Unknown parameter in input: "guardrailVersion", must be one of: body, contentType, accept, modelId

Tried the alternative as below:
guardrails={ "trace": False,
"id": "identifier", --> "id" of the Guardrail I created using Bedrock Console.
"version": "5"}

Got the exception as below:

Error Occured: Guardrails must be a dictionary with 'guardrailIdentifier' and 'guardrailVersion' keys.

The below option doesn't give any error but eventually has no relevance as the identifier value is None:

guardrails={ "trace": False,
"guardrailIdentifier": None
"guardrailVersion": "5"}

Please suggest the correct way of passing the guardrail parameter for it to work.

PS: I have tested the Guardrail from Bedrock Console and it works like a charm.

@MaverickScientist
Copy link

Followed your example, getting the same error

image

@brnaba-aws
Copy link
Contributor Author

@MaverickScientist Which version of boto3 do you use? It seems like the version you have is not the latest. If you use a lambda, verify the boto3 version and if it is not a version that supports guardrails attributes, package your lambda with the latest version of boto3.

@MaverickScientist
Copy link

Moving to boto 1.34.108 fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants