Class: Langchain::LLM::AwsBedrock
- Defined in:
- lib/langchain/llm/aws_bedrock.rb
Overview
LLM interface for Aws Bedrock APIs: docs.aws.amazon.com/bedrock/
Gem requirements:
gem 'aws-sdk-bedrockruntime', '~> 1.1'
Usage:
llm = Langchain::LLM::AwsBedrock.new(llm_options: {})
Constant Summary collapse
- DEFAULTS =
{ chat_model: "anthropic.claude-v2", completion_model: "anthropic.claude-v2", embedding_model: "amazon.titan-embed-text-v1", max_tokens_to_sample: 300, temperature: 1, top_k: 250, top_p: 0.999, stop_sequences: ["\n\nHuman:"], anthropic_version: "bedrock-2023-05-31", return_likelihoods: "NONE", count_penalty: { scale: 0, apply_to_whitespaces: false, apply_to_punctuations: false, apply_to_numbers: false, apply_to_stopwords: false, apply_to_emojis: false }, presence_penalty: { scale: 0, apply_to_whitespaces: false, apply_to_punctuations: false, apply_to_numbers: false, apply_to_stopwords: false, apply_to_emojis: false }, frequency_penalty: { scale: 0, apply_to_whitespaces: false, apply_to_punctuations: false, apply_to_numbers: false, apply_to_stopwords: false, apply_to_emojis: false } }.freeze
- SUPPORTED_COMPLETION_PROVIDERS =
%i[anthropic ai21 cohere meta].freeze
- SUPPORTED_CHAT_COMPLETION_PROVIDERS =
%i[anthropic].freeze
- SUPPORTED_EMBEDDING_PROVIDERS =
%i[amazon cohere].freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
Instance Method Summary collapse
-
#chat(params = {}) {|Hash| ... } ⇒ Langchain::LLM::AnthropicResponse
Generate a chat completion for a given prompt Currently only configured to work with the Anthropic provider and the claude-3 model family.
-
#complete(prompt:, **params) ⇒ Langchain::LLM::AnthropicResponse
Generate a completion for a given prompt.
-
#embed(text:, **params) ⇒ Langchain::LLM::AwsTitanResponse
Generate an embedding for a given text.
-
#initialize(aws_client_options: {}, default_options: {}) ⇒ AwsBedrock
constructor
A new instance of AwsBedrock.
Methods inherited from Base
#chat_parameters, #default_dimension, #default_dimensions, #summarize
Methods included from DependencyHelper
Constructor Details
#initialize(aws_client_options: {}, default_options: {}) ⇒ AwsBedrock
Returns a new instance of AwsBedrock.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 56 def initialize(aws_client_options: {}, default_options: {}) depends_on "aws-sdk-bedrockruntime", req: "aws-sdk-bedrockruntime" @client = ::Aws::BedrockRuntime::Client.new(**) @defaults = DEFAULTS.merge() chat_parameters.update( model: {default: @defaults[:chat_model]}, temperature: {}, max_tokens: {default: @defaults[:max_tokens_to_sample]}, metadata: {}, system: {}, anthropic_version: {default: "bedrock-2023-05-31"} ) chat_parameters.ignore(:n, :user) chat_parameters.remap(stop: :stop_sequences) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
50 51 52 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 50 def client @client end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
50 51 52 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 50 def defaults @defaults end |
Instance Method Details
#chat(params = {}) {|Hash| ... } ⇒ Langchain::LLM::AnthropicResponse
Generate a chat completion for a given prompt Currently only configured to work with the Anthropic provider and the claude-3 model family
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 138 def chat(params = {}, &block) parameters = chat_parameters.to_params(params) raise ArgumentError.new("messages argument is required") if Array(parameters[:messages]).empty? raise "Model #{parameters[:model]} does not support chat completions." unless Langchain::LLM::AwsBedrock::SUPPORTED_CHAT_COMPLETION_PROVIDERS.include?(completion_provider) if block response_chunks = [] client.invoke_model_with_response_stream( model_id: parameters[:model], body: parameters.except(:model).to_json, content_type: "application/json", accept: "application/json" ) do |stream| stream.on_event do |event| chunk = JSON.parse(event.bytes) response_chunks << chunk yield chunk end end response_from_chunks(response_chunks) else response = client.invoke_model({ model_id: parameters[:model], body: parameters.except(:model).to_json, content_type: "application/json", accept: "application/json" }) parse_response response end end |
#complete(prompt:, **params) ⇒ Langchain::LLM::AnthropicResponse
Generate a completion for a given prompt
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 103 def complete(prompt:, **params) raise "Completion provider #{completion_provider} is not supported." unless SUPPORTED_COMPLETION_PROVIDERS.include?(completion_provider) raise "Model #{@defaults[:completion_model]} only supports #chat." if @defaults[:completion_model].include?("claude-3") parameters = compose_parameters params parameters[:prompt] = wrap_prompt prompt response = client.invoke_model({ model_id: @defaults[:completion_model], body: parameters.to_json, content_type: "application/json", accept: "application/json" }) parse_response response end |
#embed(text:, **params) ⇒ Langchain::LLM::AwsTitanResponse
Generate an embedding for a given text
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 81 def (text:, **params) raise "Completion provider #{} is not supported." unless SUPPORTED_EMBEDDING_PROVIDERS.include?() parameters = params.merge(text:) response = client.invoke_model({ model_id: @defaults[:embedding_model], body: parameters.to_json, content_type: "application/json", accept: "application/json" }) response end |