Skip to main content

Bedrock

Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. Using Amazon Bedrock, you can easily experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG), and build agents that execute tasks using your enterprise systems and data sources. Since Amazon Bedrock is serverless, you don’t have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with.

%pip install boto3
from langchain_community.llms import Bedrock

llm = Bedrock(
credentials_profile_name="bedrock-admin", model_id="amazon.titan-text-express-v1"
)

Using in a conversation chain​

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

conversation = ConversationChain(
llm=llm, verbose=True, memory=ConversationBufferMemory()
)

conversation.predict(input="Hi there!")

Conversation Chain With Streaming​

from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.llms import Bedrock

llm = Bedrock(
credentials_profile_name="bedrock-admin",
model_id="amazon.titan-text-express-v1",
streaming=True,
callbacks=[StreamingStdOutCallbackHandler()],
)
conversation = ConversationChain(
llm=llm, verbose=True, memory=ConversationBufferMemory()
)

conversation.predict(input="Hi there!")