Class: ActiveHarness::Providers::Custom

Inherits:
Base
  • Object
show all
Defined in:
lib/active_harness/providers/custom.rb

Overview

Custom — generic OpenAI-compatible provider for any named endpoint.

Configured via ActiveHarness.configure:

ActiveHarness.configure do |config|
  config.custom["MyLocal"]["url"]     = "http://localhost:8080/v1/chat/completions"
  config.custom["MyLocal"]["api_key"] = ENV["MYLOCAL_API_KEY"]   # omit if no auth needed

  config.custom["SecondProvider"]["url"]     = "https://second.example.com/v1/chat/completions"
  config.custom["SecondProvider"]["api_key"] = ENV["SECOND_API_KEY"]
end

Use in an agent:

model do
  use      provider: :custom, name: "MyLocal",        model: "llama3.2"
  fallback provider: :custom, name: "SecondProvider", model: "mixtral"
end

Constant Summary

Constants inherited from Base

Base::HTTP, Base::STREAMING_HTTP

Instance Method Summary collapse

Instance Method Details

#call(model:, messages:, temperature: 0.7, name: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_harness/providers/custom.rb', line 25

def call(model:, messages:, temperature: 0.7, name: nil)
  raise Errors::InvalidRequestError,
    "provider: :custom requires a `name:` key — e.g. `use provider: :custom, name: \"MyLocal\", model: \"...\"`" \
    if name.nil? || name.to_s.empty?

  settings = config.custom[name.to_s]

  url = settings["url"].to_s
  raise Errors::InvalidRequestError,
    "Custom provider \"#{name}\" has no url configured. " \
    "Set it with: config.custom[\"#{name}\"][\"url\"] = \"https://...\"" \
    if url.empty?

  headers = { "Content-Type" => "application/json" }
  key = settings["api_key"].to_s
  headers["Authorization"] = "Bearer #{key}" unless key.empty?

  raw  = post_json(URI(url),
    headers: headers,
    body:    { model: model, messages: messages, temperature: temperature }
  )
  data = parse!(raw)
  handle_error!(data, name: name)

  {
    content:  data.dig("choices", 0, "message", "content").to_s.strip,
    provider: :custom,
    model:    data["model"] || model,
    usage:    extract_usage_openai(data)
  }
end