Code Monkey home page Code Monkey logo

generative-ai-amazon-bedrock-langchain-agent-example's Introduction

Build generative AI agents with Amazon Bedrock, Amazon DynamoDB, Amazon Kendra, Amazon Lex, and LangChain


Content

Overview

Generative AI agents are capable of producing human-like responses and engaging in natural language conversations by orchestrating a chain of calls to foundation models (FMs) and other augmenting tools based on user input. Instead of only fulfilling pre-defined intents through a static decision tree, agents are autonomous within the context of their suite of available tools. Amazon Bedrock is a fully managed service that makes leading foundation models from AI companies available through an API along with developer tooling to help build and scale generative AI applications.

This sample solution creates a generative AI financial services agent powered by Amazon Bedrock. The agent can assist users with finding their account information, completing a loan application, or answering natural language questions while also citing sources for the provided answers. This solution is intended to act as a launchpad for developers to create their own personalized conversational agents for various applications, such as chatbots, virtual assistants, and customer support systems.

Amazon Lex supplies the natural language understanding (NLU) and natural language processing (NLP) interface for the open source LangChain conversational agent within an AWS Amplify website. The agent is equipped with tools that include an Anthropic Claude 3 Sonnet FM hosted on Amazon Bedrock and synthetic customer data stored on Amazon DynamoDB and Amazon Kendra.

Demo Recording

  • Provide Personalized Responses - Query DynamoDB for customer account information, such as mortgage summary details, due balance, and next payment date.
  • Access General Knowledge - Harness the agent’s reasoning logic in tandem with the vast amounts of data used to pretrain the different FMs provided through Bedrock to produce replies for any customer prompt.
  • Curate Opinionated Answers - Inform agent responses using a Kendra Index configured with authoritative data sources: customer documents stored in Amazon Simple Storage Service (S3) and Web Crawler configured for the customer's website.

Solution Architecture

Diagram 1: Solution Architecture Overview

  1. Users perform natural dialog with the Agent through their choice of Web, SMS, or Voice channels. The Web channel includes an AWS Amplify hosted website with an Amazon Lex embedded chatbot for an example customer, Octank Financial. Each user request is processed by Lex which invokes an AWS Lambda handler for intent fulfillment. SMS and Voice channels can be optionally configured using Amazon Connect and messaging integrations for Amazon Lex.

    Each user request is processed by Lex to determine user intent through a process called intent recognition, which involves analyzing and interpreting the user's input (text or speech) to understand the user's intended action or purpose.

  2. Lex then invokes an AWS Lambda handler for user intent fulfillment. The Lambda function associated with the Lex chatbot contains the logic and business rules required to process the user's intent. Lambda performs specific actions or retrieves information based on the user's input, making decisions and generating appropriate responses.

  3. Lambda instruments the Financial Services agent logic as a LangChain Conversational Agent that can access customer-specific data stored on DynamoDB, curate opinionated responses using your documents and webpages indexed by Kendra, and provide general knowledge answers through the FM on Bedrock.

    Responses generated by Kendra will include source attribution, demonstrating how you can provide additional contextual information to the agent through Retrieval-Augmented Generation (RAG). RAG allows you to enhance your agent’s ability to generate more accurate and contextually relevant responses using your own data.

Agent Architecture

Diagram 2: LangChain Conversational Agent Architecture

  1. The LangChain Conversational Agent incorporates conversation memory so it can respond to multiple queries with contextual generation. This memory allows the agent to provide responses that take into account the context of the ongoing conversation. This is achieved through contextual generation, where the agent generates responses that are relevant and contextually appropriate based on the information it has remembered from the conversation.

    In simpler terms, the agent remembers what was said earlier and uses that information to respond to multiple questions in a way that makes sense in the ongoing discussion. Our agent leverages LangChain's DynamoDB Chat Message History class as a conversation memory buffer so it can recall past interactions and enhance the user experience with more meaningful, context-aware responses.

  2. The agent uses Anthropic Claude 3 Sonnet on Amazon Bedrock to complete the desired task through a series of carefully self-generated text inputs known as prompts. The primary objective of prompt engineering is to elicit specific and accurate responses from the FM. Different prompt engineering techniques include:

    • Zero-Shot - A single question is presented to the model without any additional clues. The model is expected to generate a response based solely on the given question.
    • Few-Shot - A set of sample questions and their corresponding answers are included before the actual question. By exposing the model to these examples, it learns to respond in a similar manner.
    • Chain-of-Thought - A specific style of few-shot prompting where the prompt is designed to contain a series of intermediate reasoning steps, guiding the model through a logical thought process, ultimately leading to the desired answer.

    Our Agent utilizes chain-of-thought reasoning by executing a set of Actions upon receiving a request. Following each Action, the Agent enters the Observation step, where it expresses a Thought. If a Final Answer is not yet achieved, the Agent iterates, selecting different Actions to progress towards reaching the Final Answer.

Thought: Do I need to use a tool? Yes
Action: The action to take
Action Input: The input to the action
Observation: The result of the action

Thought: Do I need to use a tool? No
FSI Agent: [answer and source documents]
  1. As part of the agent's different reasoning paths and self-evaluating choices to decide the next course of action, it has the ability to access customer authoritative data sources using an Amazon Kendra index. Using Kendra, the agent performs a semantic similarity search across a wide range of content types, including documents, FAQs, knowledge bases, manuals, and websites - Please refer to the list of Kendra supported Data Sources.

    The agent has the power to use this tool to provide opinionated responses to user prompts that should be answered using an authoritative, customer-provided knowledge library, instead of the more general knowledge corpus used to pretrain the Bedrock FM.

Sample Prompts:

  • Why should I use Octank Financial?
  • How competitive are their rates?
  • Which type of mortgage should I use?
  • What are current mortgage trends?
  • How much do I need saved for a down payment?
  • What other costs will I pay at closing?

Deployment Guide

see Deployment Guide

Testing and Validation

see Testing and Validation

Clean Up

see Clean Up


Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0

generative-ai-amazon-bedrock-langchain-agent-example's People

Contributors

kyleblocksom 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

generative-ai-amazon-bedrock-langchain-agent-example's Issues

Why Use Kommunicate?

It's not clear why we're using a third party service - Kommunicate when Lex (or the AWS SDK) performs the same function. It would be great if this were clarified in the documentation.

KendraIndex is not used for RAG

I am facing following 2 issues when the chatbot is setup from the latest fork repo.

Issue 1: The chat question doesn't use the kendra index as trusted data source at all despite temperature configured to '0.1'
Issue 2: The chat history is not saving into the table configured under the environment variable "CONVERSATION_TABLE"

It seems the agent execution doesn't invoke tools for running the chain and similarly, doesn't invoke chat for storing the chat.

Appreciate guidance for the fixes.

Remembering the chat history

Hey Kyle!

Loving this project! Hey question for you:

In the chat.py we have this:

def __init__(self, event):
        print("Initializing Chat with FSI Agent")
        self.set_user_id(event)
        self.set_chat_index()
        self.set_memory(event)
        self.create_new_chat()

If I'm understanding this correctly:

  1. We get the User ID
  2. We get the index # from the index ddb table (ie. '0')
  3. We use this index # and User ID to create a session id which we then use within Langchain
  4. We finally update the index # in the index table.

I'd like subsequent invocations of the lambda to recall the existing history (ie. stay on the same index). So that if I ask follow up question to the LLM it remembers the history.

Before I start making changes to the file, I wanted to confirm my understanding is correct or if I'm going about this incorrectly. I'm pretty new with LLMs and LangChain but what I'm doing seems to me it would be the entire intent of having "memory"; which makes me feel I'm missing something.

Thanks!

ValueError: Error raised by bedrock service: An error occurred (ValidationException) when calling the InvokeModel operation: The requested operation is not recognized by the service.

After deployment, i type in Demo User and 1234, and can see 3 buttons in the Lex bot. Then typing "Good morning", and see this error

It is raised in this code snippet

    llm = Bedrock(
        model_id="anthropic.claude-instant-v1"
    )  
    llm.model_kwargs = {'max_tokens_to_sample': 200}
    lex_agent = FSIAgent(llm, chat.memory)

    try:
        message = lex_agent.run(input=prompt)
Screenshot 2023-10-18 at 21 46 58

Please can you provide a requirements.txt

  • I have run into an issue with the Claude bedrock model due to an outdated langchain lib version.
  • Of course this requires me to update the langchain version (to overcome this issue - langchain-ai/langchain#11130) which is fixed in this commit (langchain-ai/langchain@23065f5)
  • When i updated the langchain version, I then run into a numpy version conflict.
  • In conclusion, it might be easier for a user to have a requirements.txt file that would allow them to directly modify and update their lambda layer.

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.