Class: Typesafe::Jev

Inherits:
Client show all
Defined in:
lib/typesafe/jev.rb

Overview

Convenience facade over Client for TypeSafe's flagship model, Jev.

The model is pinned to "jev-latest"; for any other model, use Client directly.

Typesafe::Jev.evaluate(state: "My payouts failed", questions: { urgent: Typesafe::Noul.new("Is this urgent?") })

or, reusing one client:

jev = Typesafe::Jev.new
jev.evaluate(state: state, questions: questions)

Constant Summary collapse

PINNED_MODEL =
"jev-latest"

Constants inherited from Client

Client::API_ENDPOINT, Client::DEFAULT_MODEL

Instance Attribute Summary

Attributes inherited from Client

#api_key, #model

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ Jev

Returns a new instance of Jev.

Parameters:

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

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

Raises:

  • (ArgumentError)

    if no usable API key is found.



23
24
25
# File 'lib/typesafe/jev.rb', line 23

def initialize(api_key: nil)
  super(api_key: api_key, model: PINNED_MODEL)
end

Class Method Details

.evaluate(state:, questions:, api_key: nil) ⇒ Response

One-shot evaluation using a client built from the environment (or the given API key). Equivalent to Jev.new(api_key:).evaluate(...).

Parameters:

  • state (String, Object, Array)

    the content to evaluate.

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

    question ids mapped to Question objects.

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

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

Returns:



58
59
60
# File 'lib/typesafe/jev.rb', line 58

def self.evaluate(state:, questions:, api_key: nil)
  new(api_key: api_key).evaluate(state: state, questions: questions)
end

Instance Method Details

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

Evaluates a state against a map of questions with the pinned model.

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)

    must be nil or "jev-latest".

Returns:

Raises:

  • (ArgumentError)

    if questions is invalid, model is not the pinned one, or the response body is not a valid response shape.

  • (JSON::ParserError)

    if the response body is not valid JSON.

  • (Typesafe::APIError)

    (or a subclass) on any non-2xx HTTP response; see Client#evaluate.



40
41
42
43
44
45
46
47
# File 'lib/typesafe/jev.rb', line 40

def evaluate(state:, questions:, model: nil)
  if model && model != PINNED_MODEL
    raise ArgumentError,
          "Typesafe::Jev pins the model to \"jev-latest\"; use Typesafe::Client for other models"
  end

  super(state: state, questions: questions, model: model)
end