Langchain with Amazon Bedrock

· Thomas Taylor

How to use Langchain with Amazon Bedrock

In this article, we’ll cover how to easily leverage Amazon Bedrock in Langchain.

What is Amazon Bedrock?

Amazon Bedrock is a fully managed service that provides an API to invoke LLMs. At the time of writing, Amazon Bedrock supports:

with many more slated to come out!

How to use Amazon Bedrock with Langchain

Langchain easily integrates with Amazon Bedrock via the langchain.llms module.

Setup credentials

In the execution environment, ensure that the appropriate credentials are configured. This can be inferred from environment variables, the ~/.aws/credentials configuration file, credentials in the Amazon Bedrock client, etc.

For the sake of this tutorial, I have exported my AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY pair.

1export AWS_ACCESS_KEY_ID=xxxx

Create a bedrock client

Using boto3, a bedrock-runtime client can be created and passed as a parameter to the Langchain Bedrock class.

1from langchain.llms import Bedrock
3import boto3
5client = boto3.client("bedrock-runtime")
7model = Bedrock(client=client, model_id="anthropic.claude-instant-v1")
9print(model("who are you?"))


1I'm Claude, an AI assistant created by Anthropic.

Creating a chain with Bedrock

Now that the model is instantiated, we may use it with the Langchain Expression Language (LCEL) to create a chain.

 1from langchain.llms import Bedrock
 2from langchain.schema.output_parser import StrOutputParser
 3from langchain.prompts import ChatPromptTemplate
 5import boto3
 7client = boto3.client("bedrock-runtime")
 8model = Bedrock(client=client, model_id="anthropic.claude-instant-v1")
 9prompt = ChatPromptTemplate.from_template("write me a haiku about {topic}")
11chain = prompt | model | StrOutputParser()
12print(chain.invoke({"topic": "money"}))


1Here is a haiku about money:
3Green paper bills flow
4Endless pursuit of riches
5Money rules all things


Integrating Langchain with Amazon Bedrock unlocks many capabilities for utilizing large language models in diverse applications. This guide has demonstrated the ease of setting up this integration, enabling you to enhance your projects with AI. As Amazon Bedrock expands its model support, the potential applications and efficiencies you can achieve will only grow.

#generative-ai   #python  

Reply to this post by email ↪