Class: Langchain::LLM::AwsBedrock

Inherits:
Base
  • Object
show all
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(default_options: {})

Constant Summary collapse

DEFAULTS =
{
  chat_model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
  completion_model: "anthropic.claude-v2:1",
  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:"],
  return_likelihoods: "NONE"
}.freeze
SUPPORTED_COMPLETION_PROVIDERS =
%i[
  anthropic
  ai21
  cohere
  meta
].freeze
SUPPORTED_CHAT_COMPLETION_PROVIDERS =
%i[
  anthropic
  ai21
  mistral
].freeze
SUPPORTED_EMBEDDING_PROVIDERS =
%i[
  amazon
  cohere
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#chat_parameters, #default_dimension, #default_dimensions, #summarize

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(aws_client_options: {}, default_options: {}) ⇒ AwsBedrock

Returns a new instance of AwsBedrock.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/langchain/llm/aws_bedrock.rb', line 45

def initialize(aws_client_options: {}, default_options: {})
  depends_on "aws-sdk-bedrockruntime", req: "aws-sdk-bedrockruntime"

  @client = ::Aws::BedrockRuntime::Client.new(**aws_client_options)
  @defaults = DEFAULTS.merge(default_options)

  chat_parameters.update(
    model: {default: @defaults[:chat_model]},
    temperature: {},
    max_tokens: {default: @defaults[:max_tokens_to_sample]},
    metadata: {},
    system: {}
  )
  chat_parameters.ignore(:n, :user)
  chat_parameters.remap(stop: :stop_sequences)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/langchain/llm/aws_bedrock.rb', line 25

def client
  @client
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



25
26
27
# File 'lib/langchain/llm/aws_bedrock.rb', line 25

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

Parameters:

  • params (Hash) (defaults to: {})

    unified chat parmeters from [Langchain::LLM::Parameters::Chat::SCHEMA]

Options Hash (params):

  • :messages (Array<String>)

    The messages to generate a completion for

  • :system (String)

    The system prompt to provide instructions

  • :model (String)

    The model to use for completion defaults to @defaults

  • :max_tokens (Integer)

    The maximum number of tokens to generate defaults to @defaults

  • :stop (Array<String>)

    The stop sequences to use for completion

  • :stop_sequences (Array<String>)

    The stop sequences to use for completion

  • :temperature (Float)

    The temperature to use for completion

  • :top_p (Float)

    Use nucleus sampling.

  • :top_k (Integer)

    Only sample from the top K options for each subsequent token

Yields:

  • (Hash)

    Provides chunks of the response as they are received

Returns:



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
159
160
161
162
163
164
# File 'lib/langchain/llm/aws_bedrock.rb', line 128

def chat(params = {}, &block)
  parameters = chat_parameters.to_params(params)
  parameters = compose_parameters(parameters, parameters[:model])

  unless SUPPORTED_CHAT_COMPLETION_PROVIDERS.include?(provider_name(parameters[:model]))
    raise "Chat provider #{parameters[:model]} is not supported."
  end

  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, parameters[:model])
  end
end

#complete(prompt:, model: , **params) ⇒ Langchain::LLM::AnthropicResponse

Generate a completion for a given prompt

Parameters:

  • prompt (String)

    The prompt to generate a completion for

  • params

    extra parameters passed to Aws::BedrockRuntime::Client#invoke_model

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/langchain/llm/aws_bedrock.rb', line 91

def complete(
  prompt:,
  model: @defaults[:completion_model],
  **params
)
  raise "Completion provider #{model} is not supported." unless SUPPORTED_COMPLETION_PROVIDERS.include?(provider_name(model))

  parameters = compose_parameters(params, model)

  parameters[:prompt] = wrap_prompt prompt

  response = client.invoke_model({
    model_id: model,
    body: parameters.to_json,
    content_type: "application/json",
    accept: "application/json"
  })

  parse_response(response, model)
end

#embed(text:, **params) ⇒ Langchain::LLM::AwsTitanResponse

Generate an embedding for a given text

Parameters:

  • text (String)

    The text to generate an embedding for

  • params

    extra parameters passed to Aws::BedrockRuntime::Client#invoke_model

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/langchain/llm/aws_bedrock.rb', line 69

def embed(text:, **params)
  raise "Completion provider #{embedding_provider} is not supported." unless SUPPORTED_EMBEDDING_PROVIDERS.include?(embedding_provider)

  parameters = compose_embedding_parameters params.merge(text:)

  response = client.invoke_model({
    model_id: @defaults[:embedding_model],
    body: parameters.to_json,
    content_type: "application/json",
    accept: "application/json"
  })

  parse_embedding_response response
end