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

aws[patch]: add guardrails support #82

Merged
merged 8 commits into from
Jun 27, 2024
Merged

Conversation

baskaryan
Copy link
Collaborator

No description provided.

@baskaryan baskaryan changed the title Bagatur/guardrails support aws[patch]: add guardrails support Jun 24, 2024
@baskaryan baskaryan requested a review from 3coins June 24, 2024 18:57
@3coins
Copy link
Collaborator

3coins commented Jun 24, 2024

@supreetkt
Can you review this PR, and make sure to test the changes. Thanks.

Copy link
Collaborator

@3coins 3coins left a comment

Choose a reason for hiding this comment

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

@baskaryan
Thanks for making these changes. Some minor comments about the changes.

Copy link
Collaborator

@3coins 3coins left a comment

Choose a reason for hiding this comment

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

@baskaryan
Looks good. We should go ahead and merge.

One thing to note is that the usage from ChatBedrock is different which uses guardrails to set this config. We might want to align with the changes here and make guardrail_config as primary attribute, with guardrails as an alias there. But we can handle that in a separate PR. Thanks again for working on this.

@rsgrewal-aws
Copy link

hello Team, if the entry point will be ChatBedrock with converse api -- what will it mean for Guardrails config ? will it work as is ?

@3coins
Copy link
Collaborator

3coins commented Jun 27, 2024

@rsgrewal-aws
I believe it will not work at the moment, as ChatBedrock is using a different field, and flattening the guardrail properties to pass to the invoke function. We will need few more changes on ChatBedrock to make it work. For example, this is currently failing in my test.

def test_guardrails() -> None:
    params = {
        "region_name": "us-west-2",
        "model_id": "anthropic.claude-3-sonnet-20240229-v1:0",
        "guardrails": {
            "guardrailIdentifier": "e7esbceow153",
            "guardrailVersion": "1",
            "trace": "enabled",
        },
        "beta_use_converse_api": True
    }
    chat_model = ChatBedrock(**params)  # type: ignore[arg-type]
    messages = [
        HumanMessage(
            content=[
                "Create a playlist of 2 heavy metal songs.",
                {
                    "guardContent": {
                        "text": {"text": "Only answer with a list of songs."}
                    }
                },
            ]
        )
    ]
    response = chat_model.invoke(messages)

    assert (
        response.content == "Sorry, I can't answer questions about heavy metal music."
    )
    assert response.response_metadata["stopReason"] == "guardrail_intervened"
    assert response.response_metadata["trace"] is not None

    stream = chat_model.stream(messages)
    response = next(stream)
    for chunk in stream:
        response += chunk

    assert (
        response.content[0]["text"]  # type: ignore[index]
        == "Sorry, I can't answer questions about heavy metal music."
    )
    assert response.response_metadata["stopReason"] == "guardrail_intervened"
    assert response.response_metadata["trace"] is not None

Error

E           botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: The model returned the following errors: The guardrail can't assess the content in the guardContent field. The guardrail configuration is missing. Specify the guardrailConfig field and try again.

@3coins
Copy link
Collaborator

3coins commented Jun 27, 2024

@baskaryan
Can you take a look at the recent commit, this should fix calling the ChatBedrock with guardrails config. I have added an integration test to verify this.

@supreetkt
Copy link

Maybe this can be a part of a separate PR, but would advise adding docstrings to all util methods. Otherwise LGTM.

@3coins 3coins merged commit 29e38ac into main Jun 27, 2024
12 checks passed
@3coins 3coins deleted the bagatur/guardrails_support branch June 27, 2024 17:20
"max_tokens": 100,
"stop": [],
"guardrail_config": {
"guardrailIdentifier": "e7esbceow153",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

is this specific to a user or will it work with our creds as well?

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.

4 participants