{ "cells": [ { "cell_type": "raw", "id": "fbc66410", "metadata": {}, "source": [ "---\n", "sidebar_label: Bedrock Chat\n", "---" ] }, { "cell_type": "markdown", "id": "bf733a38-db84-4363-89e2-de6735c37230", "metadata": {}, "source": [ "# BedrockChat\n", "\n", ">[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of \n", "> high-performing foundation models (FMs) from leading AI companies like `AI21 Labs`, `Anthropic`, `Cohere`, \n", "> `Meta`, `Stability AI`, and `Amazon` via a single API, along with a broad set of capabilities you need to \n", "> build generative AI applications with security, privacy, and responsible AI. Using `Amazon Bedrock`, \n", "> you can easily experiment with and evaluate top FMs for your use case, privately customize them with \n", "> your data using techniques such as fine-tuning and `Retrieval Augmented Generation` (`RAG`), and build \n", "> agents that execute tasks using your enterprise systems and data sources. Since `Amazon Bedrock` is \n", "> serverless, you don't have to manage any infrastructure, and you can securely integrate and deploy \n", "> generative AI capabilities into your applications using the AWS services you are already familiar with.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d51edc81", "metadata": {}, "outputs": [], "source": [ "%pip install --upgrade --quiet boto3" ] }, { "cell_type": "code", "execution_count": null, "id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9", "metadata": { "tags": [] }, "outputs": [], "source": [ "from langchain_community.chat_models import BedrockChat\n", "from langchain_core.messages import HumanMessage" ] }, { "cell_type": "code", "execution_count": 2, "id": "70cf04e8-423a-4ff6-8b09-f11fb711c817", "metadata": { "tags": [] }, "outputs": [], "source": [ "chat = BedrockChat(model_id=\"anthropic.claude-v2\", model_kwargs={\"temperature\": 0.1})" ] }, { "cell_type": "code", "execution_count": 3, "id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "AIMessage(content=\" Voici la traduction en français : J'adore programmer.\", additional_kwargs={}, example=False)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "messages = [\n", " HumanMessage(\n", " content=\"Translate this sentence from English to French. I love programming.\"\n", " )\n", "]\n", "chat(messages)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "a4a4f4d4", "metadata": {}, "source": [ "### For BedrockChat with Streaming" ] }, { "cell_type": "code", "execution_count": null, "id": "c253883f", "metadata": {}, "outputs": [], "source": [ "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", "\n", "chat = BedrockChat(\n", " model_id=\"anthropic.claude-v2\",\n", " streaming=True,\n", " callbacks=[StreamingStdOutCallbackHandler()],\n", " model_kwargs={\"temperature\": 0.1},\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "d9e52838", "metadata": {}, "outputs": [], "source": [ "messages = [\n", " HumanMessage(\n", " content=\"Translate this sentence from English to French. I love programming.\"\n", " )\n", "]\n", "chat(messages)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }