Skip to content
Kward Search API index

Class: Kward::Tools::TypeSafeEvaluate

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/typesafe_evaluate.rb

Overview

Evaluates bounded state with TypeSafe's typed decision model.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(client: TypeSafeClient.new) ⇒ TypeSafeEvaluate

Returns a new instance of TypeSafeEvaluate.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kward/tools/typesafe_evaluate.rb', line 9

def initialize(client: TypeSafeClient.new)
  @client = client
  super(
    "typesafe_evaluate",
    "Evaluate text with TypeSafe and return typed decisions, probabilities, and confidence. Use for classification, scoring, routing, or verification; this does not generate text.",
    properties: {
      state: {
        type: "string",
        description: "The text or structured state to evaluate; do not include secrets unless explicitly requested."
      },
      model: {
        type: "string",
        description: "TypeSafe model ID or alias; defaults to jev-latest."
      },
      questions: {
        type: "object",
        description: "Named typed questions. Each value must be a noul, choice, or score question.",
        additionalProperties: {
          type: "object",
          properties: {
            type: { type: "string", enum: %w[noul choice score] },
            instructions: { type: "string" },
            criteria: { type: ["object", "array"] }
          },
          required: %w[type instructions],
          additionalProperties: false
        }
      }
    },
    required: %w[state questions]
  )
end

Instance Method Details

#call(args, _conversation, cancellation: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kward/tools/typesafe_evaluate.rb', line 42

def call(args, _conversation, cancellation: nil)
  state = argument(args, "state").to_s
  questions = argument(args, "questions")
  error = validate(state, questions)
  return "Error: #{error}" if error

  response = @client.evaluate(
    state: state,
    questions: questions,
    model: argument(args, "model") || TypeSafeClient::DEFAULT_MODEL,
    cancellation: cancellation
  )
  JSON.pretty_generate(response)
rescue TypeSafeClient::RequestError => e
  "Error: #{e.message}"
rescue JSON::GeneratorError, TypeError => e
  "Error: invalid TypeSafe response: #{e.message}"
end