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:
bedrock = Langchain::LLM::AwsBedrock.new(llm_options: {})
Constant Summary collapse
- DEFAULTS =
{ completion_model_name: "anthropic.claude-v2", embedding_model_name: "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 cohere ai21].freeze
- SUPPORTED_CHAT_COMPLETION_PROVIDERS =
%i[anthropic].freeze
- SUPPORTED_EMBEDDING_PROVIDERS =
%i[amazon].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(messages: [], system: nil, model: , max_tokens: , stop_sequences: nil, temperature: nil, top_p: nil, top_k: nil) ⇒ Langchain::LLM::AnthropicMessagesResponse
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(completion_model: , embedding_model: , aws_client_options: {}, default_options: {}) ⇒ AwsBedrock
constructor
A new instance of AwsBedrock.
Methods inherited from Base
#default_dimensions, #summarize
Methods included from DependencyHelper
Constructor Details
#initialize(completion_model: , embedding_model: , aws_client_options: {}, default_options: {}) ⇒ AwsBedrock
Returns a new instance of AwsBedrock.
55 56 57 58 59 60 61 62 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 55 def initialize(completion_model: DEFAULTS[:completion_model_name], embedding_model: DEFAULTS[:embedding_model_name], aws_client_options: {}, default_options: {}) depends_on "aws-sdk-bedrockruntime", req: "aws-sdk-bedrockruntime" @client = ::Aws::BedrockRuntime::Client.new(**) @defaults = DEFAULTS.merge() .merge(completion_model_name: completion_model) .merge(embedding_model_name: ) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
49 50 51 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 49 def client @client end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
49 50 51 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 49 def defaults @defaults end |
Instance Method Details
#chat(messages: [], system: nil, model: , max_tokens: , stop_sequences: nil, temperature: nil, top_p: nil, top_k: nil) ⇒ Langchain::LLM::AnthropicMessagesResponse
Generate a chat completion for a given prompt Currently only configured to work with the Anthropic provider and the claude-3 model family
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 125 def chat( messages: [], system: nil, model: defaults[:completion_model_name], max_tokens: defaults[:max_tokens_to_sample], stop_sequences: nil, temperature: nil, top_p: nil, top_k: nil ) raise ArgumentError.new("messages argument is required") if .empty? raise "Model #{model} does not support chat completions." unless Langchain::LLM::AwsBedrock::SUPPORTED_CHAT_COMPLETION_PROVIDERS.include?(completion_provider) inference_parameters = { messages: , max_tokens: max_tokens, anthropic_version: @defaults[:anthropic_version] } inference_parameters[:system] = system if system inference_parameters[:stop_sequences] = stop_sequences if stop_sequences inference_parameters[:temperature] = temperature if temperature inference_parameters[:top_p] = top_p if top_p inference_parameters[:top_k] = top_k if top_k response = client.invoke_model({ model_id: model, body: inference_parameters.to_json, content_type: "application/json", accept: "application/json" }) parse_response response end |
#complete(prompt:, **params) ⇒ Langchain::LLM::AnthropicResponse
Generate a completion for a given prompt
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 94 def complete(prompt:, **params) raise "Completion provider #{completion_provider} is not supported." unless SUPPORTED_COMPLETION_PROVIDERS.include?(completion_provider) raise "Model #{@defaults[:completion_model_name]} only supports #chat." if @defaults[:completion_model_name].include?("claude-3") parameters = compose_parameters params parameters[:prompt] = wrap_prompt prompt response = client.invoke_model({ model_id: @defaults[:completion_model_name], 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/langchain/llm/aws_bedrock.rb', line 71 def (text:, **params) raise "Completion provider #{} is not supported." unless SUPPORTED_EMBEDDING_PROVIDERS.include?() parameters = {inputText: text} parameters = parameters.merge(params) response = client.invoke_model({ model_id: @defaults[:embedding_model_name], body: parameters.to_json, content_type: "application/json", accept: "application/json" }) Langchain::LLM::AwsTitanResponse.new(JSON.parse(response.body.string)) end |