Class: ZnyxSdk::Client

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

Overview

Official Ruby SDK for the ZNYX Runtime guardrails API.

Examples:

client = ZnyxSdk::Client.new("http://localhost:8080")

result = client.evaluate_input(
  tenant_id: "my-org",
  app_id:    "my-app",
  text:      user_message
)
raise result.user_message if result.blocked?

Instance Method Summary collapse

Constructor Details

#initialize(base_url, api_key: nil, timeout: 10) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String)

    URL of the running ZNYX Runtime (e.g. "http://localhost:8080")

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

    Optional API key sent as Authorization: Bearer

  • timeout (Integer) (defaults to: 10)

    HTTP read/open timeout in seconds (default: 10)



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/znyx_sdk/client.rb', line 23

def initialize(base_url, api_key: nil, timeout: 10)
  @uri     = URI.parse(base_url.chomp("/"))
  @api_key = api_key
  @timeout = timeout

  # Anonymous, opt-out install telemetry (ZNYX_TELEMETRY=false to disable).
  begin
    Telemetry.maybe_send_install_ping
  rescue StandardError
    # telemetry must never break client construction
  end
end

Instance Method Details

#evaluate_input(text:, tenant_id: "default", app_id: "default", **kwargs) ⇒ EvaluationResponse

Evaluates a user prompt before sending it to the LLM.

Parameters:

  • text (String)

    The text to evaluate

  • tenant_id (String) (defaults to: "default")
  • app_id (String) (defaults to: "default")
  • kwargs (Hash)

    Optional: agent_id, env, trace_id, session_id, metadata

Returns:



43
44
45
# File 'lib/znyx_sdk/client.rb', line 43

def evaluate_input(text:, tenant_id: "default", app_id: "default", **kwargs)
  post("/v1/evaluate/input", build_payload(text: text, tenant_id: tenant_id, app_id: app_id, **kwargs))
end

#evaluate_output(text:, tenant_id: "default", app_id: "default", **kwargs) ⇒ Object

Evaluates an LLM response before returning it to the user.



48
49
50
# File 'lib/znyx_sdk/client.rb', line 48

def evaluate_output(text:, tenant_id: "default", app_id: "default", **kwargs)
  post("/v1/evaluate/output", build_payload(text: text, tenant_id: tenant_id, app_id: app_id, **kwargs))
end

#evaluate_tool(tool_name:, tool_args:, tenant_id: "default", app_id: "default", **kwargs) ⇒ Object

Evaluates a tool call before executing it.

Parameters:

  • tool_name (String)
  • tool_args (Hash)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/znyx_sdk/client.rb', line 56

def evaluate_tool(tool_name:, tool_args:, tenant_id: "default", app_id: "default", **kwargs)
  payload = {
    request_id: new_request_id,
    tenant_id:  tenant_id,
    app_id:     app_id,
    agent_id:   kwargs.delete(:agent_id) || "default",
    env:        kwargs.delete(:env)      || "prod",
    tool_name:  tool_name,
    tool_args:  tool_args,
  }.merge(kwargs.compact)
  post("/v1/evaluate/tool", payload)
end