Giter VIP home page Giter VIP logo

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

Building Generative AI Agents with LangChain, Amazon Bedrock, Amazon DynamoDB, Amazon Kendra, and Amazon Lex.


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 Large Language Models (LLMs) 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.

This sample solution creates a Generative AI-powered Financial Services Agent that can assist users with finding their account information, completing a loan application, or answering any natural language question while also sourcing links for the provided answers. 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 LLM hosted on Amazon Bedrock and synthetic customer data stored on Amazon DynamoDB and Amazon Kendra.

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.

Demo Recording

Solution Architecture

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.

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

Responses generated by Kendra will include source links with octankfinancial.com as the root domain, demonstrating how the customer can configure the Kendra Web Crawler with their own domain to serve opinionated, proprietary responses. Otherwise, source links are served from the Bedrock LLM's training dataset.

Diagram 1: Solution Architecture Overview

  • Provide Personalized Responses: Query DynamoDB for customer account information, such as mortgage summary details, next payment date, and balance due.
  • 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 Crawlers configured for the customer's website.
  • Access General Knowledge: Harness the vast amounts of data used to pre-train the different LLMs hosted on Bedrock to produce replies for any customer prompt.

Agent Architecture

The LangChain Conversational Agent incorporates conversation memory so it can respond to multiple queries with contextual generation. 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.

The Agent's brain is an Anthropic Claude LLM hosted on Bedrock, which allows the Agent to complete the desired task through a series of carefully crafted text inputs known as prompts. The primary objective of prompt engineering is to elicit specific and accurate responses from the LLM. 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 before presenting 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]

As part of the Agent's different reasoning paths and self-evaluating choices to decide the next course of action, it can access synthetic customer data sources through an Amazon Kendra Index Retriever tool. Using Kendra, the Agent can perform contextual 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 can use this tool to provide opinionated responses to user prompts that should be answered using an authoritative, customer-provided knowledge base, instead of the more general knowledge corpus used to pretrain the Bedrock LLM.

Diagram 2: LangChain Conversational Agent Architecture

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

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.