Class: LlmLogs::Batch::Adapters::Bedrock

Inherits:
Object
  • Object
show all
Defined in:
app/models/llm_logs/batch/adapters/bedrock.rb

Overview

AWS Bedrock Batch API (CreateModelInvocationJob). Writes a JSONL manifest to S3, starts an async job, polls it, and reads the output JSONL back from S3. Unlike the OpenAI adapter, submission/output is file-based (S3) rather than an upload endpoint.

Defined Under Namespace

Classes: Result

Constant Summary collapse

ANTHROPIC_VERSION =
"bedrock-2023-05-31"
DEFAULT_MAX_TOKENS =
1024

Instance Method Summary collapse

Constructor Details

#initialize(config: LlmLogs.bedrock_batch, s3: nil, bedrock: nil) ⇒ Bedrock

Returns a new instance of Bedrock.



16
17
18
19
20
# File 'app/models/llm_logs/batch/adapters/bedrock.rb', line 16

def initialize(config: LlmLogs.bedrock_batch, s3: nil, bedrock: nil)
  @config = config
  @s3 = s3
  @bedrock = bedrock
end

Instance Method Details

#error_ids(batch) ⇒ Object



74
75
76
# File 'app/models/llm_logs/batch/adapters/bedrock.rb', line 74

def error_ids(batch)
  parsed_output(batch).filter_map { |line| line["recordId"] if line["error"] }
end

#results(batch) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/llm_logs/batch/adapters/bedrock.rb', line 59

def results(batch)
  parsed_output(batch).each_with_object({}) do |line, acc|
    next if line["recordId"].nil? || line["error"]

    output = line["modelOutput"] || {}
    usage = output["usage"] || {}
    acc[line["recordId"]] = Result.new(
      content: extract_content(output),
      input_tokens: usage["input_tokens"],
      output_tokens: usage["output_tokens"],
      model_id: output["model"] || batch.model
    )
  end
end

#submit(batch, requests) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/llm_logs/batch/adapters/bedrock.rb', line 22

def submit(batch, requests)
  key = "#{@config.s3_prefix}/#{batch.id}/input.jsonl"
  manifest = requests.map { |r| JSON.generate(record_for(r)) }.join("\n") + "\n"
  s3.put_object(bucket: @config.s3_bucket, key: key, body: manifest)

  input_uri = "s3://#{@config.s3_bucket}/#{key}"
  output_uri = "s3://#{@config.s3_bucket}/#{@config.s3_prefix}/#{batch.id}/out/"
  job = bedrock.create_model_invocation_job(
    job_name: job_name_for(batch),
    role_arn: @config.role_arn,
    model_id: batch.model,
    input_data_config: {s3_input_data_config: {s3_uri: input_uri}},
    output_data_config: {s3_output_data_config: {s3_uri: output_uri}}
  )

  {
    provider_batch_id: job.job_arn,
    openai_batch_id: nil,
    provider_metadata: {
      "s3_input_uri" => input_uri,
      "s3_output_uri" => output_uri,
      "job_id" => job.job_arn.split("/").last,
      "input_basename" => "input.jsonl"
    }
  }
end

#terminal_status(batch) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/models/llm_logs/batch/adapters/bedrock.rb', line 49

def terminal_status(batch)
  job = bedrock.get_model_invocation_job(job_identifier: batch.provider_batch_id)
  case job.status
  when "Completed" then "completed"
  when "Failed", "Stopped", "PartiallyCompleted" then "failed"
  when "Expired" then "expired"
  else "in_progress"
  end
end