Skip to content

griptape-ai/griptape-structure-chatbot

Repository files navigation

Griptape Structure Chatbot

Description

This project runs a script that generates resources through an AWS CDK and deploys it with a Griptape Structure to invoke the Structure for use as a chatbot with the Gradio Interface in the Griptape Cloud. Deploying the resources creates both a DynamoDB table with AWS and creates a Griptape Structure in the Griptape Cloud. Once deployed, the user can call the Griptape Structure with the Griptape Cloud to invoke their structure.

Prerequisites

AWS

You need:

  1. An AWS Account

  2. The aws-cli installed Follow the instructions for your OS.

  3. AWS credentials or aws sso configured

  4. To configure aws sso:

    aws configure sso

Values for the SSO:

`SSO session name (Recommended): my-sso`

`SSO start URL [None]: <YourStartURL>`

`SSO region [None]: <YourRegion>`

`SSO registration scopes [None]: sso:account:access`
    aws s3 ls --profile <YourProfileName>

AWS CDK

You need:

  1. Node.js installed

This is necessary in order to install aws-cdk.

    https://nodejs.org/en/download/package-manager
  1. The aws-cdk installed
    npm install -g aws-cdk

To verify installation:

    cdk --version

Docker

You need:

  1. Docker installed

Griptape Cloud

You need:

  1. A Griptape Cloud account
  2. A Griptape Cloud API Key

Open AI

You need:

  1. An Open AI account
  2. An Open AI API Key

Griptape Chat

To run the structure with Gradio, you need to clone and configure the Griptape Chat repository.

  1. Follow the instructions on the README

Environment Setup

If you plan on modifying the structure to be deployed to the cloud:

  1. Set the .env variables based on the repository of your structure.
        GITHUB_REPO_OWNER=<your-owner>
        GITHUB_REPO_NAME=<your-repo-name>
        GITHUB_REPO_BRANCH=<your-branch>
        STRUCTURE_FILE_PATH=<your-path-from-repo>
        REQUIREMENTS_FILE_PATH=<your-path-from-repo>
  1. In your repository, add this configuration to your agent and pass a session_id when creating your agent.
    conversation_memory=ConversationMemory(
        driver=AmazonDynamoDbConversationMemoryDriver(
            session=boto3.Session(
                aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
                aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
            ),
            table_name=conversation_memory_table_name,
            partition_key="id",
            partition_key_value=session_id, 
            value_attribute_key="value",
        )
    )
  1. Update your structure with the parsing information at the bottom of the app.py - this is necessary for the way that Gradio passes in the session_id and inputs. Replace init_structure with your own agent creation method.
    # TODO: Keep this logic for running your own structure
    if __name__ == "__main__":
        input_arg = sys.argv[1]
        input_arg_dict = json.loads(input_arg)
        agent = init_structure(input_arg_dict["session_id"])
        agent.run(input_arg_dict["input"])

If you do not plan on modifying the structure:

  1. Clone the Repository
    git clone git!@github.com:griptape-ai/griptape-structure-chatbot.git

Install dependencies

    npm install

Create a .env file, and populate it accordingly to .env.example file. Do not forget to add your Griptape Cloud API Key.

    GRIPTAPE_API_KEY=<YOUR_GRIPTAPE_CLOUD_API_KEY>

Deploy

  1. Follow the Token provider configuration documentation for AWS IAM Identity Center
  2. Configure Token provider
    aws configure sso-session 

If necessary to sign into an IAM Identity Center session:

    aws sso login --profile <your-profile-name>
  1. Export SSO credentials:
    eval $(aws configure export-credentials --profile <profile> --format env)
  1. npm run bootstrap
  2. npm run deploy

Run Structure in Skatepark

This can only be accomplished after deploying the CDK Prerequisites:

  1. Go to IAM Dashboard
  2. Under Access Management go to Users
  3. Go to griptape-chatbot-user (Should exist if CDK has been deployed)
  4. Create an access key and save the values in the .env:
    AWS_ACCESS_KEY_ID=<YourAccessKey>
    AWS_SECRET_ACCESS_KEY=<YourSecretAccessKey>

Additional environment variables needed:

    GT_CLOUD_BASE_URL=http://127.0.0.1:5000

http://127.0.0.1:5000 is the default for Skatepark, but you can change this when running gt skatepark start

Run Structure in Skatepark with Griptape Chat

Start in Skatepark:

    gt skatepark start

Navigate to the directory of your structure:

    gt skatepark register --main-file <StructureFilename>

Put your Structure ID in your Griptape Chat .env

GT_STRUCTURE_ID=<your-structure-id>

If you change any environment variables

    gt skatepark build

Run Structure in GriptapeCloud with Griptape Chat

After you deploy the structure

  1. Get the Structure ID from the Griptape Structure Chatbot: https://cloud.griptape.ai/structures
  2. Add additional environment variables in the Griptape Cloud
    1. Create any additional secrets necessary and add them to your structure in the Griptape Cloud.
      1. ex: Adding a Zenrows API Key
        1. Click Create Secret
          1. Name: Zenrows API Key
          2. Value: <your-Zenrows-API-key-value>
        2. Go to your structure and click Add Variable.
          1. Name: ZENROWS_API_KEY
          2. Source: Secret
          3. Value: select Zenrows API Key from the dropdown menu.
    2. Add any environment variables that your structure requires that haven't already been set by clicking Add Variable and setting the source to Manual.
      1. ex: Adding a Knowledge Base
        1. Click Add Variable
          1. Name: KNOWLEDGE_BASE_ID
          2. Source: Manual
          3. Value: <your-kb-id>
  3. Put the Structure ID in your .env in Griptape Chat
    GT_STRUCTURE_ID=<your-structure-id>

Run Griptape Chat

In your CLI in the Griptape Chat folder

    poetry run python app.py 

Invoke the Lambda Function

Invoke the endpoint as follows:

  1. Retrieve a session_id
curl --json '{"operation": "create_session"}' https://<YOUR_LAMBDA_URL_ID>.lambda-url.<REGION>.on.aws/

Useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • npx cdk deploy deploy this stack to your default AWS account/region
  • npx cdk diff compare deployed stack with current state
  • npx cdk synth emits the synthesized CloudFormation template