From 9cd8d505266e1671aa211600925c95161c9a95d6 Mon Sep 17 00:00:00 2001 From: Mark Ericksen Date: Sat, 23 Nov 2024 18:10:41 -0700 Subject: [PATCH] Documenting AWS Bedrock support with Anthropic Claude (#195) * working on documenting AWS Bedrock support with Anthropic * added documentation and helper functions * removing unused functions * cleaning up --- lib/chat_models/chat_anthropic.ex | 20 +++++++++ lib/utils/bedrock_config.ex | 72 +++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/lib/chat_models/chat_anthropic.ex b/lib/chat_models/chat_anthropic.ex index dd7d6a48..a479a0e5 100644 --- a/lib/chat_models/chat_anthropic.ex +++ b/lib/chat_models/chat_anthropic.ex @@ -53,6 +53,26 @@ defmodule LangChain.ChatModels.ChatAnthropic do tool_choice: %{"type" => "tool", "name" => "get_weather"} }) + ## AWS Bedrock Support + + Anthropic Claude is supported in [AWS Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html). + + To configure `ChatAnthropic` for use on AWS Bedrock: + + 1. Request [Model Access](https://console.aws.amazon.com/bedrock/home?#/modelaccess) to get access to the Anthropic models you intend to use. + 2. Using your AWS Console, create an Access Key for your application. + 3. Set the key values in your `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` ENVs. + 4. Get the Model ID for the model you intend to use. [Base Models](https://console.aws.amazon.com/bedrock/home?#/models) + 5. Refer to `LangChain.Utils.BedrockConfig` for setting up the Bedrock authentication credentials for your environment. + 6. Setup your ChatAnthropic similar to the following: + + alias LangChain.ChatModels.ChatAnthropic + + ChatAnthropic.new!(%{ + model: "anthropic.claude-3-5-sonnet-20241022-v2:0", + bedrock: BedrockConfig.from_application_env!() + }) + """ use Ecto.Schema require Logger diff --git a/lib/utils/bedrock_config.ex b/lib/utils/bedrock_config.ex index b97d4edd..1a6de5ea 100644 --- a/lib/utils/bedrock_config.ex +++ b/lib/utils/bedrock_config.ex @@ -1,9 +1,45 @@ defmodule LangChain.Utils.BedrockConfig do @moduledoc """ Configuration for AWS Bedrock. + + ## Examples + + For applications hosted in AWS, [ExAws](https://hex.pm/packages/ex_aws) caches + temporary credentials (when running on AWS), so in the credentials function + you can pull the current cached credentials from `ExAws`. + + ChatAnthropic.new!(%{ + model: "anthropic.claude-3-5-sonnet-20241022-v2:0", + bedrock: %{ + credentials: fn -> + ExAws.Config.new(:s3) + |> Map.take([:access_key_id, :secret_access_key]) + |> Map.to_list() + end, + region: "us-west-2" + } + }) + + For applications hosted anywhere, you can configure the Bedrock settings into + the LangChain config like this (recommended for `config/runtime.exs`): + + config :langchain, + aws_access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"), + aws_secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY"), + aws_region: System.get_env("AWS_REGION", "us-west-1") + + Then, when you want to later use a Bedrock Anthropic model, this is will load + it: + + ChatAnthropic.new!(%{ + model: "anthropic.claude-3-5-sonnet-20241022-v2:0", + bedrock: BedrockConfig.from_application_env!() + }) + """ use Ecto.Schema import Ecto.Changeset + alias __MODULE__ @primary_key false embedded_schema do @@ -14,23 +50,51 @@ defmodule LangChain.Utils.BedrockConfig do field :anthropic_version, :string, default: "bedrock-2023-05-31" end + @type t :: %BedrockConfig{} + + @create_fields [:credentials, :region, :anthropic_version] + @required_fields @create_fields + def changeset(bedrock, attrs) do bedrock - |> cast(attrs, [:credentials, :region, :anthropic_version]) - |> validate_required([:credentials, :region, :anthropic_version]) + |> cast(attrs, @create_fields) + |> validate_required(@required_fields) end - def aws_sigv4_opts(%__MODULE__{} = bedrock) do + def aws_sigv4_opts(%BedrockConfig{} = bedrock) do Keyword.merge(bedrock.credentials.(), region: bedrock.region, service: :bedrock ) end - def url(%__MODULE__{region: region}, model: model, stream: stream) do + def url(%BedrockConfig{region: region}, model: model, stream: stream) do "https://bedrock-runtime.#{region}.amazonaws.com/model/#{model}/#{action(stream: stream)}" end defp action(stream: true), do: "invoke-with-response-stream" defp action(stream: false), do: "invoke" + + @doc """ + Loads the Bedrock config settings from the previously configured Application settings. + + `config/runtime.exs`: + + config :langchain, + aws_access_key_id: System.fetch_env!("AWS_ACCESS_KEY_ID"), + aws_secret_access_key: System.fetch_env!("AWS_SECRET_ACCESS_KEY"), + aws_region: System.get_env("AWS_REGION", "us-west-1") + + """ + def from_application_env!() do + %{ + credentials: fn -> + [ + access_key_id: Application.fetch_env!(:langchain, :aws_access_key_id), + secret_access_key: Application.fetch_env!(:langchain, :aws_secret_access_key) + ] + end, + region: Application.fetch_env!(:langchain, :aws_region) + } + end end