how.wtf

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
2export AWS_SECRET_ACCESS_KEY=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
2
3import boto3
4
5client = boto3.client("bedrock-runtime")
6
7model = Bedrock(client=client, model_id="anthropic.claude-instant-v1")
8
9print(model("who are you?"))

Output:

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
 4
 5import boto3
 6
 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}")
10
11chain = prompt | model | StrOutputParser()
12print(chain.invoke({"topic": "money"}))

Output:

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

Conclusion

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 ↪