Giter Site home page Giter Site logo

aiml-genai-multimodal-agent's Introduction

Generative AI and Multi-Modal Agents in AWS: The Key to Unlocking New Value in Financial Markets

This file walks you through how to set up the infrastructure and applications, and run the code to create a multi-modal agents. The blog post provides a detailed discussion of this solution. This Solution uses Full-Access policies for certain services involved in the architecture and may not meet the security standards of certain business units, however, this solution should be used to show the art-of-possible and access policy should be scoped down for any other use.

Achitecture Diagram

Prerequisites

This solution uses five Lambda functions, which are serverless, event-driven compute services that runs applications. The Python code for the applications are packaged as zip files, stored in lambda_zip_files in this repo. You need to add them to an S3 bucket in your account in order to set up the Lambda functions.

First, make an S3 bucket. Go to S3 page in AWS, click "Create bucket". Then enter a bucket name, which should be universally unique. Take a note of the name, because we will need it in another section. Leave the rest as default, and click "Create bucket" at the bottom of the page.

Once the bucket is created, click the bucket name, and create a folder called code. To create a folder, click "Create folder", and then enter the folder name "code", then click "Submit".

Upload the four zip files in folder lambda_zip_files to the S3 bucket.

This Soluition requires Anthropic Claude2 model in Bedrock. Make sure you have access to the model in Bedrock. Navigate to the Bedrock console and click on Model Access to the left tab. Select the Manage model access button to the top-right.

Submit the Use case details and select the checkboxes beside the Anthropic models. Then select save changes button.

Create infrastructure using CloudFormation

AWS CloudFormation allows you to create infrasturecture as code.

First, download the CloudFormation template Consolidated-cfn.yaml.

Then upload it in CloudFormation to create a stack. This stack sets up the necessary infrastructure, such as IAM roles and Lambda functions. Go to CloudFormation console in AWS, click Stacks -> Create stack -> With new resources (standard).

Upload cloudformation.yaml, and click "Next".

On the "Specify stack details" page,

Give the Stack a name. Take a note of this name as we will need it when running the app. Change the "SourceCodeBucket" to the name of the S3 bucket you created above.

Leave the rest as default. Check the acknowledgement box on the last page and submit it.

It takes a few minutes to create. Once it is created, you can check the generated resources by clicking on Stacks -> Stack Name. Then click "Resources". The example below shows that the AudioTranscriptsSourceBucketResource is an S3 bucket with the bucket name "test-cf-stack-audiotranscriptssourcebucketresourc-1kg41ts9dy7hk".

Upload the Jupyter Notebook

The CloudFormation stack creates a SageMaker Notebook instance that we can use to run the .ipynb file.

Go to SageMaker page, and click Notebook -> Notebook instances. You will see a Notebook instance named "MultiModalSagemakerInstance". Click "Open jupyter" next to it.

Pull in the code

On Jupyter Notebook, click New -> Terminal. This opens a command-line interface.

Copy and paste the following command lines to pull the code from Github.

git init
git clone [github_link]

Set Up StreamLit Front-End

⚠️ NOTE: Before using the Streamlit app, completely run the multimodal-demo.ipynb notebook to setup the stock database and all other tools used by this agent.

The streamlit app for this prioject is located in app_complete.py. It uses dependencies located in the utility folder.

To run this Streamlit App on Sagemaker Studio follow the steps in the link below:

  • Follow the steps outlined above for the Prerequisite and Create infrastructure using CloudFormation section to deploy/create the relevant services.
  • Set Up SageMaker Studio.
    • Use the sagemaker execution role SageMakerRole deployed by the cloudformation template above for your SageMaker Studio Domain default execution role.
  • Launch SageMaker Studio
  • Clone this git repo into studio
  • ⚠️ Change the values for the variables REGION and STACK_NAME to the region you are working in and name of the deployed cloudformation stack respectively in app_complete.py
  • Open a system terminal by clicking on Amazon SageMaker Studio and then System Terminal as shown in the diagram below
  • Navigate into the cloned repository directory using the cd command and run the command pip install -r requirements.txt to install the needed python libraries
  • Run command python3 -m streamlit run app_complete.py to start the Streamlit server. Do not use the links generated by the command as they won't work in studio.
  • To enter the Streamlit app, open and run the StreamlitLink.ipynb notebook. This will generate the appropiate link to enter your Streamlit app from SageMaker studio. Click on the link to enter your Streamlit app. Happy querying :)
  • ⚠ Note: If you rerun the Streamlit server it may use a different port. Take not of the port used (port number is the last 4 digit number after the last :) and modify the port variable in the StreamlitLink.ipynb notebook to get the correct link.

To run this Streamlit App on AWS EC2 (I tested this on the Ubuntu Image)

  • Follow the steps outlined above for the Prerequisiteand Create infrastructure using CloudFormation section to deploy/create the relevant services.
  • Create a new ec2 instance
  • Expose TCP port range 8500-9000 on Inbound connections of the attached Security group to the ec2 instance. TCP port 8501 is needed for Streamlit to work. See image below
  • Connect to your ec2 instance
  • Run the appropiate commands to update the ec2 instance (sudo apt update and sudo apt upgrade -for Ubuntu)
  • Clone this git repo
  • Change the values for the variables REGION and STACK_NAME to the region you are working in and name of the deployed cloudformation stack respectively in app_complete.py. Make use of the nano command to make this changes. e.g. nano app_complete.py
  • Install python3 and pip if not already installed
  • Install the dependencies in the requirements.txt file by running the command sudo pip install -r requirements.txt
  • Run command python3 -m streamlit run app_complete.py
  • Copy the external link and paste in a new browser tab

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

aiml-genai-multimodal-agent's People

Contributors

dviviali avatar mohankm1011 avatar rizblie avatar sovikaws avatar ucegbe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

aiml-genai-multimodal-agent's Issues

bedrock multil agent

It seems that multi agent is failing for bedrock claude model because of the new prompt "\n\nHuman:...\n\nAssistant:"

CopyZips failed for the cfn template

The error message:

[ERROR] 2023-09-29T18:25:46.733Z 1baf5814-38e9-44fc-9b7f-bdc0c20a43dc Exception: An error occurred (AccessDenied) when calling the CopyObject operation: Access DeniedTraceback (most recent call last): File "/var/task/index.py", line 51, in handler copy_objects(source_bucket, dest_bucket, prefix, objects) File "/var/task/index.py", line 19, in copy_objects s3.copy_object(CopySource=copy_source, Bucket=dest_bucket, File "/var/runtime/botocore/client.py", line 530, in _api_call return self._make_api_call(operation_name, kwargs) File "/var/runtime/botocore/client.py", line 960, in _make_api_call raise error_class(parsed_response, operation_name)botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the CopyObject operation: Access Denied [ERROR] 2023-09-29T18:25:46.733Z 1baf5814-38e9-44fc-9b7f-bdc0c20a43dc Exception: An error occurred (AccessDenied) when calling the CopyObject operation: Access Denied Traceback (most recent call last): File "/var/task/index.py", line 51, in handler copy_objects(source_bucket, dest_bucket, prefix, objects) File "/var/task/index.py", line 19, in copy_objects s3.copy_object(CopySource=copy_source, Bucket=dest_bucket, File "/var/runtime/botocore/client.py", line 530, in _api_call return self._make_api_call(operation_name, kwargs) File "/var/runtime/botocore/client.py", line 960, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the CopyObject operation: Access Denied

where is the long term memory usage?

thanks for the sharing, very informatively!

1 question refers to the langchain chat memory usage , i didn't see the memory added into the chat chain, does it missing in the QA_retrival chain section? as I can't find the memory instance refers to any other part

am I missing anything?
Thanks a lot

Error with Tabular data

When I ask the sample question ""What are the closing prices of stocks AAAA, WWW, DDD in year 2018? Can you build an optimized portfolio using these three stocks? Please provide answers to both questions.""

I get the below error:
OperationalError: (pyathena.error.OperationalError) COLUMN_NOT_FOUND: line 3:7: Column 'symbol' cannot be resolved or requester is not authorized to access requested resources [SQL: SELECT symbol, closing_price FROM stock_prices WHERE symbol IN ('AAAA', 'WWW', 'DDD') AND YEAR(date) = 2018 ORDER BY symbol] (Background on this error at: https://sqlalche.me/e/14/e3q8)

File "/opt/conda/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 542, in _run_script
exec(code, module.dict)
File "/home/sagemaker-user/aiml-genai-multimodal-agent/app_complete.py", line 383, in
main(agent, chat_history_memory)
File "/home/sagemaker-user/aiml-genai-multimodal-agent/app_complete.py", line 379, in main
action_doc(agent, chat_history_memory)
File "/home/sagemaker-user/aiml-genai-multimodal-agent/app_complete.py", line 299, in action_doc
output_answer=query(prompt, agent, chat_history_memory)
File "/home/sagemaker-user/aiml-genai-multimodal-agent/app_complete.py", line 268, in query
output=agent(request)
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 306, in call
raise e
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 300, in call
self._call(inputs, run_manager=run_manager)
File "/opt/conda/lib/python3.9/site-packages/langchain_experimental/plan_and_execute/agent_executor.py", line 56, in _call
response = self.executor.step(
File "/opt/conda/lib/python3.9/site-packages/langchain_experimental/plan_and_execute/executors/base.py", line 37, in step
response = self.chain.run(**inputs, callbacks=callbacks)
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 506, in run
return self(kwargs, callbacks=callbacks, tags=tags, metadata=metadata)[
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 306, in call
raise e
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 300, in call
self._call(inputs, run_manager=run_manager)
File "/opt/conda/lib/python3.9/site-packages/langchain/agents/agent.py", line 1141, in _call
next_step_output = self._take_next_step(
File "/opt/conda/lib/python3.9/site-packages/langchain/agents/agent.py", line 991, in _take_next_step
observation = tool.run(
File "/opt/conda/lib/python3.9/site-packages/langchain/tools/base.py", line 364, in run
raise e
File "/opt/conda/lib/python3.9/site-packages/langchain/tools/base.py", line 336, in run
self._run(*tool_args, run_manager=run_manager, **tool_kwargs)
File "/opt/conda/lib/python3.9/site-packages/langchain/tools/base.py", line 515, in _run
else self.func(*args, **kwargs)
File "/home/sagemaker-user/aiml-genai-multimodal-agent/utility/stock_query_mm.py", line 78, in run_query
response=db_chain.run(query)
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 501, in run
return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 306, in call
raise e
File "/opt/conda/lib/python3.9/site-packages/langchain/chains/base.py", line 300, in call
self._call(inputs, run_manager=run_manager)
File "/opt/conda/lib/python3.9/site-packages/langchain_experimental/sql/base.py", line 198, in _call
raise exc
File "/opt/conda/lib/python3.9/site-packages/langchain_experimental/sql/base.py", line 143, in _call
result = self.database.run(sql_cmd)
File "/opt/conda/lib/python3.9/site-packages/langchain/utilities/sql_database.py", line 429, in run
result = self._execute(command, fetch)
File "/opt/conda/lib/python3.9/site-packages/langchain/utilities/sql_database.py", line 407, in _execute
cursor = connection.execute(text(command))
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1380, in execute
return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 334, in _execute_on_connection
return connection._execute_clauseelement(
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1572, in _execute_clauseelement
ret = self._execute_context(
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1943, in _execute_context
self.handle_dbapi_exception(
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2124, in handle_dbapi_exception
util.raise(
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 211, in raise
raise exception
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
self.dialect.do_execute(
File "/opt/conda/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
cursor.execute(statement, parameters)
File "/opt/conda/lib/python3.9/site-packages/pyathena/cursor.py", line 117, in execute
raise OperationalError(query_execution.state_change_reason)

OperationalError: (pyathena.error.OperationalError) COLUMN_NOT_FOUND: line 3:7: Column 'symbol' cannot be resolved or requester is not authorized to access requested resources [SQL: SELECT symbol, closing_price FROM stock_prices WHERE symbol IN ('AAAA', 'WWW', 'DDD') AND YEAR(date) = 2018 ORDER BY symbol] (Background on this error at:[ https://sqlalche.me/e/14/e3q8](https://sqlalche.me/e/14/e3q8))

keep getting this error whenever I use the llm to interact with the Tabular data.

This is the output error when I ask the first sample prompt provided in the UI.
OperationalError: (pyathena.error.OperationalError) COLUMN_NOT_FOUND: line 3:7: Column 'symbol' cannot be resolved or requester is not authorized to access requested resources [SQL: SELECT symbol, closing_price FROM stock_prices WHERE symbol IN ('AAAA', 'WWW', 'DDD') AND YEAR(date) = 2018 ORDER BY symbol] (Background on this error at: https://sqlalche.me/e/14/e3q8)

I have seen different customers facing the same exact issue and getting the same error when deploying this sample. Using other prompts (to the PDF docs) works normally - issue only happens with Tabular data.

Note that we are not doing any changes, we follow the steps, deploy cfn template and run the streamlit app. We ask the first question from the sample provided on the left pane, then we get this error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.