main
 1# frozen_string_literal: true
 2
 3module Net
 4  module Llm
 5    class VertexAI
 6      attr_reader :project_id, :region, :model
 7
 8      def initialize(project_id: ENV.fetch("GOOGLE_CLOUD_PROJECT"), region: ENV.fetch("GOOGLE_CLOUD_REGION", "us-east5"), model: "claude-opus-4-5@20251101", http: Net::Llm.http)
 9        @project_id = project_id
10        @region = region
11        @model = model
12        @handler = build_handler(http)
13      end
14
15      def messages(...) = @handler.messages(...)
16      def fetch(...) = @handler.fetch(...)
17
18      private
19
20      def build_handler(http)
21        if model.start_with?("claude-")
22          Claude.new(
23            endpoint: "https://#{region}-aiplatform.googleapis.com/v1/projects/#{project_id}/locations/#{region}/publishers/anthropic/models/#{model}:rawPredict",
24            headers: -> { { "Authorization" => "Bearer #{access_token}" } },
25            http: http,
26            anthropic_version: "vertex-2023-10-16"
27          )
28        else
29          raise NotImplementedError, "Model '#{model}' is not yet supported. Only Claude models (claude-*) are currently implemented."
30        end
31      end
32
33      def access_token
34        @access_token ||= `gcloud auth application-default print-access-token`.strip
35      end
36    end
37  end
38end