Class: Typesafe::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/typesafe/client.rb

Overview

HTTP client for the TypeSafe System One evaluation endpoint.

client = Typesafe::Client.new(api_key: "sk-...", model: "jev-latest")
client.evaluate(
state: { ticket: { text: "My payouts failed" } },
questions: { refund_requested: Typesafe::Noul.new("Does the customer request a refund?") }
)

The API key comes from the TYPESAFE_API_KEY environment variable unless passed at initialization. The model defaults to "jev-latest" and may be overridden per call.

Direct Known Subclasses

Jev

Constant Summary collapse

DEFAULT_MODEL =
"jev-latest"
API_ENDPOINT =
"https://api.typesafe.ai/v1/systemone"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    the TypeSafe API key; falls back to the TYPESAFE_API_KEY environment variable.

  • model (String, nil) (defaults to: nil)

    the default model; defaults to "jev-latest".

Raises:

  • (ArgumentError)

    if no usable API key is found or model is present but not a non-empty String.



35
36
37
38
39
40
41
# File 'lib/typesafe/client.rb', line 35

def initialize(api_key: nil, model: nil)
  @api_key = freeze_string(api_key || ENV.fetch("TYPESAFE_API_KEY") { nil },
                           "api_key must be a non-empty String " \
                           "(passed at initialization or set in the TYPESAFE_API_KEY environment variable)")
  @model = freeze_string(model || DEFAULT_MODEL, "model must be a non-empty String")
  freeze
end

Instance Attribute Details

#api_keyString (readonly)

Returns the API key sent as Authorization: Bearer <key>.

Returns:

  • (String)

    the API key sent as Authorization: Bearer <key>.



25
26
27
# File 'lib/typesafe/client.rb', line 25

def api_key
  @api_key
end

#modelString (readonly)

Returns the default model used when evaluate gets none.

Returns:

  • (String)

    the default model used when evaluate gets none.



28
29
30
# File 'lib/typesafe/client.rb', line 28

def model
  @model
end

Instance Method Details

#evaluate(state:, questions:, model: nil) ⇒ Response

Evaluates a state against a map of questions and returns a Response with one typed Answer per question, e.g. response[:refund_requested]. response.to_h gives the raw parsed body as a Hash.

Parameters:

  • state (String, Object, Array)

    the content to evaluate; passed through as-is.

  • questions (Hash{String, Symbol => Question})

    question ids mapped to Question objects; answers come back under the same keys.

  • model (String, nil) (defaults to: nil)

    model for this call; falls back to the model given at initialization.

Returns:

Raises:

  • (ArgumentError)

    if questions is not a non-empty Hash of String/Symbol keys to Question values, model is invalid, or the response body is not a valid response shape.

  • (JSON::ParserError)

    if the response body is not valid JSON.

  • (Net::HTTPClientException, Net::HTTPFatalError)

    on any non-2xx HTTP response.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/typesafe/client.rb', line 60

def evaluate(state:, questions:, model: nil)
  model = model.nil? ? self.model : freeze_string(model, "model must be a non-empty String")
  questions = validate_questions!(questions)

  body = JSON.generate(
    state: state,
    model: model,
    questions: questions.transform_values(&:to_h)
  )

  response = post(body)
  response.value
  Response.from_json(response.body)
end