Class: Typesafe::Client
- Inherits:
-
Object
- Object
- Typesafe::Client
- 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
Constant Summary collapse
- DEFAULT_MODEL =
"jev-latest"- API_ENDPOINT =
"https://api.typesafe.ai/v1/systemone"
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
The API key sent as
Authorization: Bearer <key>. -
#model ⇒ String
readonly
The default model used when
evaluategets none.
Instance Method Summary collapse
- #evaluate(state:, questions:, model: nil) ⇒ Response
-
#initialize(api_key: nil, model: nil) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key: nil, model: nil) ⇒ Client
Returns a new instance of Client.
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_key ⇒ String (readonly)
Returns the API key sent as Authorization: Bearer <key>.
25 26 27 |
# File 'lib/typesafe/client.rb', line 25 def api_key @api_key end |
#model ⇒ String (readonly)
Returns 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
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 |