Skip to main content

Chalice x Agents for Amazon Bedrock plug-ins

Project description

Chalice-A4AB

PyPI - Python Version

tests PyPI - Version PyPI - Downloads GitHub License

What is this?

Chalice plugin: Support Agents for Amazon Bedrock

URL Links

PYPI :: Chalice-a4ab
https://pypi.org/project/chalice-a4ab/

Github :: Chalice-a4ab
https://github.com/ShotaOki/ChaliceA4AB

Usage

  1. Install

    pip install -U chalice chalice-a4ab pydantic
    
  2. Replace from chalice import Chalice to from chalice_a4ab import Chalice.

    Before:

    from chalice import Chalice
    
    app = Chalice("app-name")
    
    @app.router("path-name")
    ...
    

    After:

    from chalice_a4ab import Chalice
    
    app = Chalice("app-name")
    
    @app.router("path-name")
    ...
    
  3. Update requirements.txt

    chalice-a4ab
    pydantic
    
  4. Read parameters in function

    from chalice_a4ab import read_session_attributes, read_prompt_session_attributes, read_body
    
    @app.router("path-name")
    def method_name():
    
       # Read body
       body: PyDanticParserClass = read_body(app, PyDanticParserClass)
    
       # Read session attributes :: key -> session attribute key
       session_attributes: dict = read_session_attributes(app, "KEY")
    
       # Read prompt session attributes :: key -> prompt session attribute key
       prompt_session_attributes: dict = read_prompt_session_attributes(app, "KEY")
    
       # Return response (dict type)
       return {
          "response": "object"
       }
    

    or read directory

    @app.router("path-name")
    def method_name():
       # Read body
       body : dict = app.current_request.json_body
    
       # Read Session attributes or Prompt attributes from header
       for k in app.current_request.headers:
          if k.startsWith("sessionAttributes")
             # Session attributes
             print(app.current_request.headers[k])
          if k.startsWith("promptSessionAttributes")
             # Prompt session attributes
             print(app.current_request.headers[k])
    
       return {}
    
  5. (Optional) Add Parser Lambda Function

    Add these decorator, function becomes "parser lambda"

    from chalice_a4ab import (
        Chalice,
        AgentsForAmazonBedrockConfig,
        ParserLambdaAbortException,
        ParserLambdaResponseModel,
    )
    
    # Set Config for Agents for Amazon bedrock
    AgentsForAmazonBedrockConfig().apply()
    
    ...
    
    # PRE_PROCESSING
    @app.parser_lambda_pre_processing()
    def pre_processing(event, default_result: ParserLambdaResponseModel) -> ParserLambdaResponseModel:
        # [MEMO] is_valid_input :: Allow/Deny API invocation
        # default_result.pre_processing_parsed_response.is_valid_input = True
        return default_result
    
    
    # ORCHESTRATION
    @app.parser_lambda_orchestration()
    def orchestration(event: dict, default_result: ParserLambdaResponseModel) -> ParserLambdaResponseModel:
        # [MEMO] Throw this Exception, Overwrite LLM response
        # raise ParserLambdaAbortException(message="Overwrited")
        return default_result
    
  6. Application works ::

    What you can do Status
    Execute from Agents for Amazon Bedrock
    Auto generate OpenAPI schema ×
    Management on cli ×

Advanced Usage

Create OpenAPI Schema automatically.

  1. Install Chalice-a4ab and Chalice-spec

    pip install -U chalice chalice-spec==0.7.0 chalice-a4ab boto3 pydantic
    
  2. Update requirements.txt

    chalice-spec==0.7.0
    chalice-a4ab
    pydantic
    
  3. Add Setting

    from chalice_a4ab import Chalice, AgentsForAmazonBedrockConfig, ModelTypesAntropicClaudeV2
    from chalice_spec.docs import Docs, Operation
    
    # Set Config for Agents for Amazon bedrock
    AgentsForAmazonBedrockConfig(
       # Agent Situation
       instructions="Situation Settings for talking with Human and agent.(more than 40 words)",
       # Agent Description
       description="Description of application",
       # Use Model :: Claude V2
       foundation_model=ModelTypesAntropicClaudeV2,
    ).apply()
    
    app = Chalice(app_name="app-name")
    
    @app.router("path-name",
        methods=["POST"],
        docs=Docs(
            post=Operation(
                request=PyDanticRequestModelClass,
                response=PyDanticOutputModelClass,
            )
        ))
    def post_method():
       """
       Function summary
    
       Description of this method, brabrarba, this comment becomes LLM Prompt.
       """
    ...
    
  4. Read values with PyDantic

    @app.router("path-name",
        methods=["POST"],
        docs=Docs(
            post=Operation(
                request=PyDanticRequestModelClass,
                response=PyDanticOutputModelClass,
            )
        ))
    def method_name():
       """
       Function summary
    
       Description of this method, brabrarba, this comment becomes LLM Prompt.
       """
    
       # Read body with utility
       body: PyDanticRequestModelClass = read_body(app, PyDanticRequestModelClass)
    
       # or direct
       #   Pydantic V1 -> parse_obj()
       #   Pydantic V2 -> model_validate()
       body = PyDanticRequestModelClass.model_validate(app.current_request.json_body)
    
       # return response as dict class
       #   Pydantic V1 -> json()
       #   Pydantic V2 -> model_dump_json()
       return PyDanticOutputModelClass(
          response=...
       ).model_dump_json()
    

    documentation for @app.router sample: https://github.com/TestBoxLab/chalice-spec

  5. Management CLI

    Init command :: Create AWS Resource and OpenAPI Schema.

    chalice-a4ab init --profile ${PROFILE_NAME} --region ${REGION_NAME}
    

    Sync command :: Update Already AWS Resource and OpenAPISchema.

    chalice-a4ab sync --profile ${PROFILE_NAME} --region ${REGION_NAME}
    

    Show command :: Show OpenAPI Schema.

    chalice-a4ab show --profile ${PROFILE_NAME} --region ${REGION_NAME}
    

    Info command :: Show about Agents for Amazon Bedrock.

    chalice-a4ab info --profile ${PROFILE_NAME} --region ${REGION_NAME}
    

    Invoke command :: Invoke Agents for Amazon Bedrock.

    chalice-a4ab invoke "Natural Language Query" \
    --agent-id ${AGENT_ID} \
    --agent-alias-id ${AGENT_ALIAS_ID} \
    --end-session \
    --profile ${PROFILE_NAME} --region ${REGION_NAME}
    

    Delete AWS Resource

    chalice-a4ab delete --profile ${PROFILE_NAME} --region ${REGION_NAME}
    
  6. Application works ::

    What you can do Status
    Execute from Agents for Amazon Bedrock
    Auto generate OpenAPI schema
    Management on cli

Develop

  1. Setup

    poetry install
    
  2. Run test

    poetry run pytest
    

Lisence

MIT

API

Command Line TOOL

Command Descritpion
chalice-a4ab init Create AWS resource for Agents for amazon bedrock
chalice-a4ab sync Sync OpenAPI schema to AWS
chalice-a4ab delete Delete AWS resource for Agents for amazon bedrock
Options Description
--bucket Set S3 bucket name (for put OpenAPI schema)
--profile Set AWS Profile Name
--region Set AWS Region Name
--help Show Help

API

AgentsForAmazonBedrockConfig

Method Type Description
apply - Current instace becomes global variable.
agents_for_bedrock_schema_json - Get OpenAPI Schema
save_schema_to_local - Save OpenAPI Schema to local folder
save_schema_to_s3 - Upload OpenAPI Schema to S3 bucket
save_config_to_local - Save Config setting to local folder
get_global_config static Get global variable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chalice_a4ab-0.3.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

chalice_a4ab-0.3.1-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file chalice_a4ab-0.3.1.tar.gz.

File metadata

  • Download URL: chalice_a4ab-0.3.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.11 Windows/10

File hashes

Hashes for chalice_a4ab-0.3.1.tar.gz
Algorithm Hash digest
SHA256 6cd10e99ab2672d4f887654e9bc660133ff6a7b319fdfd403d7770de6b8c4446
MD5 1dcf8c119f40dedffedd325d594f3714
BLAKE2b-256 f26d72775c7bfd92259e1290327f4b29132d012b8bbc0f5d8e15641b7e1f7bf9

See more details on using hashes here.

File details

Details for the file chalice_a4ab-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: chalice_a4ab-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 39.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.11 Windows/10

File hashes

Hashes for chalice_a4ab-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3772f0f65acc3e05326d5f2040567cb37b2f40469729fe290e4db25f8a4aef4
MD5 d815b331290af7ba52e8262706d75d3e
BLAKE2b-256 e5fc3333a5b4807206287cb45221ad13d45622853fb73a50649bc63923c1cf6d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page