chalice-a4ab


Namechalice-a4ab JSON
Version 0.3.1 PyPI version JSON
download
home_pagehttps://github.com/ShotaOki/ChaliceA4AB
SummaryChalice x Agents for Amazon Bedrock plug-ins
upload_time2024-01-08 02:55:20
maintainer
docs_urlNone
authorShota Oki
requires_python>=3.8,<4.0
licenseMIT
keywords chalice aws agents for amazon bedrock bedrock
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Chalice-A4AB

![PyPI - Python Version](https://img.shields.io/pypi/pyversions/Chalice-A4AB)

[![tests](https://github.com/ShotaOki/ChaliceA4AB/actions/workflows/test.yml/badge.svg)](https://github.com/ShotaOki/ChaliceA4AB/actions/workflows/test.yml)
![PyPI - Version](https://img.shields.io/pypi/v/chalice-a4ab)
![PyPI - Downloads](https://img.shields.io/pypi/dm/chalice-a4ab)
![GitHub License](https://img.shields.io/github/license/ShotaOki/ChaliceA4AB)

## 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
   ```

1. Replace `from chalice import Chalice` to `from chalice_a4ab import Chalice`.

   Before:

   ```python
   from chalice import Chalice

   app = Chalice("app-name")

   @app.router("path-name")
   ...
   ```

   After:

   ```python
   from chalice_a4ab import Chalice

   app = Chalice("app-name")

   @app.router("path-name")
   ...
   ```

1. Update requirements.txt

   ```txt
   chalice-a4ab
   pydantic
   ```

1. Read parameters in function

   ```python
   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

   ```python
   @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 {}
   ```

1. **(Optional)** Add Parser Lambda Function

   Add these decorator, function becomes "parser lambda"

   ```python
   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
   ```

1. 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

   ```python
   pip install -U chalice chalice-spec==0.7.0 chalice-a4ab boto3 pydantic
   ```

1. Update requirements.txt

   ```txt
   chalice-spec==0.7.0
   chalice-a4ab
   pydantic
   ```

1. Add Setting

   ```python
   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.
      """
   ...
   ```

1. Read values with PyDantic

   ```python
   @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

1. Management CLI

   **Init command** :: Create AWS Resource and OpenAPI Schema.

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

   **Sync command** :: Update Already AWS Resource and OpenAPISchema.

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

   **Show command** :: Show OpenAPI Schema.

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

   **Info command** :: Show about Agents for Amazon Bedrock.

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

   **Invoke command** :: Invoke Agents for Amazon Bedrock.

   ```bash
   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**

   ```bash
   chalice-a4ab delete --profile ${PROFILE_NAME} --region ${REGION_NAME}
   ```

1. Application works ::

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

# Develop

1. Setup

   ```bash
   poetry install
   ```

1. Run test

   ```bash
   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.                     |

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ShotaOki/ChaliceA4AB",
    "name": "chalice-a4ab",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "Chalice,AWS,Agents for Amazon Bedrock,Bedrock",
    "author": "Shota Oki",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/f2/6d/72775c7bfd92259e1290327f4b29132d012b8bbc0f5d8e15641b7e1f7bf9/chalice_a4ab-0.3.1.tar.gz",
    "platform": null,
    "description": "# Chalice-A4AB\n\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/Chalice-A4AB)\n\n[![tests](https://github.com/ShotaOki/ChaliceA4AB/actions/workflows/test.yml/badge.svg)](https://github.com/ShotaOki/ChaliceA4AB/actions/workflows/test.yml)\n![PyPI - Version](https://img.shields.io/pypi/v/chalice-a4ab)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/chalice-a4ab)\n![GitHub License](https://img.shields.io/github/license/ShotaOki/ChaliceA4AB)\n\n## What is this?\n\nChalice plugin: Support `Agents for Amazon Bedrock`\n\n## URL Links\n\n**PYPI :: Chalice-a4ab**  \nhttps://pypi.org/project/chalice-a4ab/\n\n**Github :: Chalice-a4ab**  \nhttps://github.com/ShotaOki/ChaliceA4AB\n\n## Usage\n\n1. Install\n\n   ```\n   pip install -U chalice chalice-a4ab pydantic\n   ```\n\n1. Replace `from chalice import Chalice` to `from chalice_a4ab import Chalice`.\n\n   Before:\n\n   ```python\n   from chalice import Chalice\n\n   app = Chalice(\"app-name\")\n\n   @app.router(\"path-name\")\n   ...\n   ```\n\n   After:\n\n   ```python\n   from chalice_a4ab import Chalice\n\n   app = Chalice(\"app-name\")\n\n   @app.router(\"path-name\")\n   ...\n   ```\n\n1. Update requirements.txt\n\n   ```txt\n   chalice-a4ab\n   pydantic\n   ```\n\n1. Read parameters in function\n\n   ```python\n   from chalice_a4ab import read_session_attributes, read_prompt_session_attributes, read_body\n\n   @app.router(\"path-name\")\n   def method_name():\n\n      # Read body\n      body: PyDanticParserClass = read_body(app, PyDanticParserClass)\n\n      # Read session attributes :: key -> session attribute key\n      session_attributes: dict = read_session_attributes(app, \"KEY\")\n\n      # Read prompt session attributes :: key -> prompt session attribute key\n      prompt_session_attributes: dict = read_prompt_session_attributes(app, \"KEY\")\n\n      # Return response (dict type)\n      return {\n         \"response\": \"object\"\n      }\n   ```\n\n   or read directory\n\n   ```python\n   @app.router(\"path-name\")\n   def method_name():\n      # Read body\n      body : dict = app.current_request.json_body\n\n      # Read Session attributes or Prompt attributes from header\n      for k in app.current_request.headers:\n         if k.startsWith(\"sessionAttributes\")\n            # Session attributes\n            print(app.current_request.headers[k])\n         if k.startsWith(\"promptSessionAttributes\")\n            # Prompt session attributes\n            print(app.current_request.headers[k])\n\n      return {}\n   ```\n\n1. **(Optional)** Add Parser Lambda Function\n\n   Add these decorator, function becomes \"parser lambda\"\n\n   ```python\n   from chalice_a4ab import (\n       Chalice,\n       AgentsForAmazonBedrockConfig,\n       ParserLambdaAbortException,\n       ParserLambdaResponseModel,\n   )\n\n   # Set Config for Agents for Amazon bedrock\n   AgentsForAmazonBedrockConfig().apply()\n\n   ...\n\n   # PRE_PROCESSING\n   @app.parser_lambda_pre_processing()\n   def pre_processing(event, default_result: ParserLambdaResponseModel) -> ParserLambdaResponseModel:\n       # [MEMO] is_valid_input :: Allow/Deny API invocation\n       # default_result.pre_processing_parsed_response.is_valid_input = True\n       return default_result\n\n\n   # ORCHESTRATION\n   @app.parser_lambda_orchestration()\n   def orchestration(event: dict, default_result: ParserLambdaResponseModel) -> ParserLambdaResponseModel:\n       # [MEMO] Throw this Exception, Overwrite LLM response\n       # raise ParserLambdaAbortException(message=\"Overwrited\")\n       return default_result\n   ```\n\n1. Application works ::\n\n   | What you can do                        | Status |\n   | -------------------------------------- | ------ |\n   | Execute from Agents for Amazon Bedrock | \u3007     |\n   | Auto generate OpenAPI schema           | \u00d7      |\n   | Management on cli                      | \u00d7      |\n\n## Advanced Usage\n\nCreate OpenAPI Schema automatically.\n\n1. Install Chalice-a4ab and Chalice-spec\n\n   ```python\n   pip install -U chalice chalice-spec==0.7.0 chalice-a4ab boto3 pydantic\n   ```\n\n1. Update requirements.txt\n\n   ```txt\n   chalice-spec==0.7.0\n   chalice-a4ab\n   pydantic\n   ```\n\n1. Add Setting\n\n   ```python\n   from chalice_a4ab import Chalice, AgentsForAmazonBedrockConfig, ModelTypesAntropicClaudeV2\n   from chalice_spec.docs import Docs, Operation\n\n   # Set Config for Agents for Amazon bedrock\n   AgentsForAmazonBedrockConfig(\n      # Agent Situation\n      instructions=\"Situation Settings for talking with Human and agent.(more than 40 words)\",\n      # Agent Description\n      description=\"Description of application\",\n      # Use Model :: Claude V2\n      foundation_model=ModelTypesAntropicClaudeV2,\n   ).apply()\n\n   app = Chalice(app_name=\"app-name\")\n\n   @app.router(\"path-name\",\n       methods=[\"POST\"],\n       docs=Docs(\n           post=Operation(\n               request=PyDanticRequestModelClass,\n               response=PyDanticOutputModelClass,\n           )\n       ))\n   def post_method():\n      \"\"\"\n      Function summary\n\n      Description of this method, brabrarba, this comment becomes LLM Prompt.\n      \"\"\"\n   ...\n   ```\n\n1. Read values with PyDantic\n\n   ```python\n   @app.router(\"path-name\",\n       methods=[\"POST\"],\n       docs=Docs(\n           post=Operation(\n               request=PyDanticRequestModelClass,\n               response=PyDanticOutputModelClass,\n           )\n       ))\n   def method_name():\n      \"\"\"\n      Function summary\n\n      Description of this method, brabrarba, this comment becomes LLM Prompt.\n      \"\"\"\n\n      # Read body with utility\n      body: PyDanticRequestModelClass = read_body(app, PyDanticRequestModelClass)\n\n      # or direct\n      #   Pydantic V1 -> parse_obj()\n      #   Pydantic V2 -> model_validate()\n      body = PyDanticRequestModelClass.model_validate(app.current_request.json_body)\n\n      # return response as dict class\n      #   Pydantic V1 -> json()\n      #   Pydantic V2 -> model_dump_json()\n      return PyDanticOutputModelClass(\n         response=...\n      ).model_dump_json()\n   ```\n\n   documentation for `@app.router` sample: https://github.com/TestBoxLab/chalice-spec\n\n1. Management CLI\n\n   **Init command** :: Create AWS Resource and OpenAPI Schema.\n\n   ```bash\n   chalice-a4ab init --profile ${PROFILE_NAME} --region ${REGION_NAME}\n   ```\n\n   **Sync command** :: Update Already AWS Resource and OpenAPISchema.\n\n   ```bash\n   chalice-a4ab sync --profile ${PROFILE_NAME} --region ${REGION_NAME}\n   ```\n\n   **Show command** :: Show OpenAPI Schema.\n\n   ```bash\n   chalice-a4ab show --profile ${PROFILE_NAME} --region ${REGION_NAME}\n   ```\n\n   **Info command** :: Show about Agents for Amazon Bedrock.\n\n   ```bash\n   chalice-a4ab info --profile ${PROFILE_NAME} --region ${REGION_NAME}\n   ```\n\n   **Invoke command** :: Invoke Agents for Amazon Bedrock.\n\n   ```bash\n   chalice-a4ab invoke \"Natural Language Query\" \\\n   --agent-id ${AGENT_ID} \\\n   --agent-alias-id ${AGENT_ALIAS_ID} \\\n   --end-session \\\n   --profile ${PROFILE_NAME} --region ${REGION_NAME}\n   ```\n\n   **Delete AWS Resource**\n\n   ```bash\n   chalice-a4ab delete --profile ${PROFILE_NAME} --region ${REGION_NAME}\n   ```\n\n1. Application works ::\n\n   | What you can do                        | Status |\n   | -------------------------------------- | ------ |\n   | Execute from Agents for Amazon Bedrock | \u3007     |\n   | Auto generate OpenAPI schema           | \u3007     |\n   | Management on cli                      | \u3007     |\n\n# Develop\n\n1. Setup\n\n   ```bash\n   poetry install\n   ```\n\n1. Run test\n\n   ```bash\n   poetry run pytest\n   ```\n\n# Lisence\n\nMIT\n\n# API\n\n## Command Line TOOL\n\n| Command             | Descritpion                                       |\n| :------------------ | :------------------------------------------------ |\n| chalice-a4ab init   | Create AWS resource for Agents for amazon bedrock |\n| chalice-a4ab sync   | Sync OpenAPI schema to AWS                        |\n| chalice-a4ab delete | Delete AWS resource for Agents for amazon bedrock |\n\n| Options   | Description                                 |\n| :-------- | :------------------------------------------ |\n| --bucket  | Set S3 bucket name (for put OpenAPI schema) |\n| --profile | Set AWS Profile Name                        |\n| --region  | Set AWS Region Name                         |\n| --help    | Show Help                                   |\n\n## API\n\n**AgentsForAmazonBedrockConfig**\n\n| Method                         | Type   | Description                              |\n| :----------------------------- | :----- | :--------------------------------------- |\n| apply                          | -      | Current instace becomes global variable. |\n| agents_for_bedrock_schema_json | -      | Get OpenAPI Schema                       |\n| save_schema_to_local           | -      | Save OpenAPI Schema to local folder      |\n| save_schema_to_s3              | -      | Upload OpenAPI Schema to S3 bucket       |\n| save_config_to_local           | -      | Save Config setting to local folder      |\n| get_global_config              | static | Get global variable.                     |\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Chalice x Agents for Amazon Bedrock plug-ins",
    "version": "0.3.1",
    "project_urls": {
        "Homepage": "https://github.com/ShotaOki/ChaliceA4AB",
        "Repository": "https://github.com/ShotaOki/ChaliceA4AB"
    },
    "split_keywords": [
        "chalice",
        "aws",
        "agents for amazon bedrock",
        "bedrock"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e5fc3333a5b4807206287cb45221ad13d45622853fb73a50649bc63923c1cf6d",
                "md5": "d815b331290af7ba52e8262706d75d3e",
                "sha256": "c3772f0f65acc3e05326d5f2040567cb37b2f40469729fe290e4db25f8a4aef4"
            },
            "downloads": -1,
            "filename": "chalice_a4ab-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d815b331290af7ba52e8262706d75d3e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 39086,
            "upload_time": "2024-01-08T02:55:19",
            "upload_time_iso_8601": "2024-01-08T02:55:19.074072Z",
            "url": "https://files.pythonhosted.org/packages/e5/fc/3333a5b4807206287cb45221ad13d45622853fb73a50649bc63923c1cf6d/chalice_a4ab-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f26d72775c7bfd92259e1290327f4b29132d012b8bbc0f5d8e15641b7e1f7bf9",
                "md5": "1dcf8c119f40dedffedd325d594f3714",
                "sha256": "6cd10e99ab2672d4f887654e9bc660133ff6a7b319fdfd403d7770de6b8c4446"
            },
            "downloads": -1,
            "filename": "chalice_a4ab-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1dcf8c119f40dedffedd325d594f3714",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 26545,
            "upload_time": "2024-01-08T02:55:20",
            "upload_time_iso_8601": "2024-01-08T02:55:20.861718Z",
            "url": "https://files.pythonhosted.org/packages/f2/6d/72775c7bfd92259e1290327f4b29132d012b8bbc0f5d8e15641b7e1f7bf9/chalice_a4ab-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-08 02:55:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ShotaOki",
    "github_project": "ChaliceA4AB",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "chalice-a4ab"
}
        
Elapsed time: 0.21995s