thinkthen 0.2.0-x64-mingw-ucrt
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/API.md +40 -0
- data/LICENSE +21 -0
- data/README.md +562 -0
- data/THIRD-PARTY-NOTICES/anstream-1.0.0/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstream-1.0.0/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-1.0.14/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-1.0.14/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-parse-1.0.0/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-parse-1.0.0/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-query-1.1.5/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-query-1.1.5/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/anstyle-wincon-3.0.11/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/anstyle-wincon-3.0.11/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/base64-0.23.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/base64-0.23.1/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/block-buffer-0.10.4/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/block-buffer-0.10.4/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/bytes-1.12.1/LICENSE +25 -0
- data/THIRD-PARTY-NOTICES/cfg-if-1.0.5/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/cfg-if-1.0.5/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/clap-4.6.7/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap-4.6.7/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_builder-4.6.7/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_builder-4.6.7/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_complete-4.6.11/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_complete-4.6.11/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_lex-1.1.1/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_lex-1.1.1/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/clap_mangen-0.2.33/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/clap_mangen-0.2.33/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/colorchoice-1.0.5/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/colorchoice-1.0.5/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/command-group-5.0.1/COPYRIGHT +29 -0
- data/THIRD-PARTY-NOTICES/command-group-5.0.1/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/command-group-5.0.1/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/cpufeatures-0.2.17/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/cpufeatures-0.2.17/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/crc32fast-1.5.3/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/crc32fast-1.5.3/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/crypto-common-0.1.7/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/crypto-common-0.1.7/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/ctrlc-3.5.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/ctrlc-3.5.2/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/digest-0.10.7/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/digest-0.10.7/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/dyn-clone-1.0.20/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/dyn-clone-1.0.20/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/equivalent-1.0.3/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/equivalent-1.0.3/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/fastrand-2.5.0/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/fastrand-2.5.0/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/flate2-1.1.10/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/flate2-1.1.10/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/generic-array-0.14.7/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.2.17/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.2.17/LICENSE-MIT +26 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.3.4/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.3.4/LICENSE-MIT +26 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.4.3/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/getrandom-0.4.3/LICENSE-MIT +26 -0
- data/THIRD-PARTY-NOTICES/hashbrown-0.17.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/hashbrown-0.17.1/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/http-1.5.0/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/http-1.5.0/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/httparse-1.10.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/httparse-1.10.1/LICENSE-MIT +20 -0
- data/THIRD-PARTY-NOTICES/httpdate-1.0.3/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/httpdate-1.0.3/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/index.json +605 -0
- data/THIRD-PARTY-NOTICES/indexmap-2.14.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/indexmap-2.14.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/is_terminal_polyfill-1.70.2/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/is_terminal_polyfill-1.70.2/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/itoa-1.0.18/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/itoa-1.0.18/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/log-0.4.34/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/log-0.4.34/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/memchr-2.8.3/COPYING +3 -0
- data/THIRD-PARTY-NOTICES/memchr-2.8.3/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/once_cell-1.21.4/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/once_cell-1.21.4/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/once_cell_polyfill-1.70.2/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/once_cell_polyfill-1.70.2/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/percent-encoding-2.3.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/percent-encoding-2.3.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/quick-xml-0.41.0/LICENSE-MIT.md +23 -0
- data/THIRD-PARTY-NOTICES/ref-cast-1.0.27/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/ref-cast-1.0.27/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/LICENSE +9 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/LICENSE-BoringSSL +272 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/LICENSE-other-bits +13 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/src/polyfill/once_cell/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/src/polyfill/once_cell/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/ring-0.17.14/third_party/fiat/LICENSE +15 -0
- data/THIRD-PARTY-NOTICES/roff-1.1.1/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/roff-1.1.1/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/rustls-0.23.46/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-0.23.46/LICENSE-ISC +15 -0
- data/THIRD-PARTY-NOTICES/rustls-0.23.46/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/rustls-pki-types-1.15.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-pki-types-1.15.1/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/rustls-platform-verifier-0.7.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/rustls-platform-verifier-0.7.1/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/rustls-webpki-0.103.16/LICENSE +19 -0
- data/THIRD-PARTY-NOTICES/schemars-1.2.3/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/serde-1.0.229/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/serde-1.0.229/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/serde_core-1.0.229/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/serde_core-1.0.229/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/serde_json-1.0.151/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/serde_json-1.0.151/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/sha2-0.10.9/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/sha2-0.10.9/LICENSE-MIT +27 -0
- data/THIRD-PARTY-NOTICES/strsim-0.11.1/LICENSE +23 -0
- data/THIRD-PARTY-NOTICES/subtle-2.6.1/LICENSE +29 -0
- data/THIRD-PARTY-NOTICES/tempfile-3.23.0/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/tempfile-3.23.0/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/thiserror-2.0.21/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/thiserror-2.0.21/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/tinyvec-1.13.3/LICENSE-APACHE.md +202 -0
- data/THIRD-PARTY-NOTICES/tinyvec-1.13.3/LICENSE-MIT.md +5 -0
- data/THIRD-PARTY-NOTICES/tinyvec-1.13.3/LICENSE-ZLIB.md +11 -0
- data/THIRD-PARTY-NOTICES/typed-path-0.13.0/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/typed-path-0.13.0/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/typenum-1.20.1/LICENSE +1 -0
- data/THIRD-PARTY-NOTICES/typenum-1.20.1/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/typenum-1.20.1/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/unicode-general-category-1.1.0/LICENSE +201 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/COPYRIGHT +7 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/unicode-normalization-0.1.26/LICENSE-UNICODE +39 -0
- data/THIRD-PARTY-NOTICES/untrusted-0.9.0/LICENSE.txt +13 -0
- data/THIRD-PARTY-NOTICES/ureq-3.4.2/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/ureq-3.4.2/LICENSE-MIT +21 -0
- data/THIRD-PARTY-NOTICES/ureq-proto-0.6.4/LICENSE-APACHE.txt +202 -0
- data/THIRD-PARTY-NOTICES/ureq-proto-0.6.4/LICENSE-MIT.txt +7 -0
- data/THIRD-PARTY-NOTICES/utf8-zero-0.8.1/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/utf8-zero-0.8.1/LICENSE-MIT +23 -0
- data/THIRD-PARTY-NOTICES/utf8parse-0.2.2/LICENSE-APACHE +176 -0
- data/THIRD-PARTY-NOTICES/utf8parse-0.2.2/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/winapi-0.3.9/LICENSE-APACHE +201 -0
- data/THIRD-PARTY-NOTICES/winapi-0.3.9/LICENSE-MIT +19 -0
- data/THIRD-PARTY-NOTICES/windows-link-0.2.1/license-apache-2.0 +201 -0
- data/THIRD-PARTY-NOTICES/windows-link-0.2.1/license-mit +21 -0
- data/THIRD-PARTY-NOTICES/windows-sys-0.61.2/license-apache-2.0 +201 -0
- data/THIRD-PARTY-NOTICES/windows-sys-0.61.2/license-mit +21 -0
- data/THIRD-PARTY-NOTICES/zeroize-1.9.0/LICENSE-APACHE +202 -0
- data/THIRD-PARTY-NOTICES/zeroize-1.9.0/LICENSE-MIT +25 -0
- data/THIRD-PARTY-NOTICES/zip-9.1.2/LICENSE +21 -0
- data/THIRD-PARTY-NOTICES/zlib-rs-0.6.8/LICENSE +19 -0
- data/THIRD-PARTY-NOTICES/zmij-1.0.23/LICENSE-MIT +23 -0
- data/lib/thinkthen/call_owner.rb +68 -0
- data/lib/thinkthen/client.rb +293 -0
- data/lib/thinkthen/configuration.rb +76 -0
- data/lib/thinkthen/contract.rb +89 -0
- data/lib/thinkthen/native.rb +350 -0
- data/lib/thinkthen/native_allocator.rb +35 -0
- data/lib/thinkthen/native_reader.rb +87 -0
- data/lib/thinkthen/questions.rb +184 -0
- data/lib/thinkthen/row.rb +173 -0
- data/lib/thinkthen/rows.rb +199 -0
- data/lib/thinkthen/thinkthen_ffi.dll +0 -0
- data/lib/thinkthen/values.rb +111 -0
- data/lib/thinkthen/version.rb +5 -0
- data/lib/thinkthen.rb +105 -0
- data/sig/error_codes.rbs +3 -0
- data/sig/internal.rbs +277 -0
- data/sig/thinkthen.rbs +440 -0
- metadata +231 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
class Client
|
|
5
|
+
# Open a client. A block closes it reliably and returns the block result.
|
|
6
|
+
def self.open(**settings)
|
|
7
|
+
client = new(**settings)
|
|
8
|
+
return client unless block_given?
|
|
9
|
+
begin
|
|
10
|
+
yield client
|
|
11
|
+
ensure
|
|
12
|
+
client.close
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
# Open the native engine with optional backend settings and a RetryPolicy policy.
|
|
16
|
+
def initialize(backend: nil, model: nil, url: nil, api_key: nil, batch: nil, max_requests: nil, retry: nil)
|
|
17
|
+
@mutex, @streams = Mutex.new, []
|
|
18
|
+
@pid = Process.pid
|
|
19
|
+
ThinkThen.send(:native)
|
|
20
|
+
key = api_key.nil? || api_key.is_a?(RedactedKey) ? api_key : RedactedKey.new(api_key)
|
|
21
|
+
@api_key = key
|
|
22
|
+
policy = binding.local_variable_get(:retry)
|
|
23
|
+
raise UsageError.new("retry must be a RetryPolicy", code: :"usage.settings") unless policy.nil? || policy.is_a?(RetryPolicy)
|
|
24
|
+
raise UsageError.new("backend must be a Symbol", code: :"usage.backend") unless backend.nil? || backend.is_a?(Symbol)
|
|
25
|
+
raw_settings = {backend: backend, model: model, url: url, batch: batch, max_requests: max_requests, retry: policy&.to_h}.reject { |_, value| value.nil? }
|
|
26
|
+
output = FFI::MemoryPointer.new(:pointer)
|
|
27
|
+
Native.bytes(Native.context_dump(raw_settings, :settings)) do |bytes, length|
|
|
28
|
+
Native.checked { |error| Native.thinkthen_settings_from_json(bytes, length, output, error) }
|
|
29
|
+
end
|
|
30
|
+
settings = Native::SettingsPointer.new(output.read_pointer)
|
|
31
|
+
key&.send(:copy_to, settings)
|
|
32
|
+
Thread.handle_interrupt(Object => :never) do
|
|
33
|
+
output = FFI::MemoryPointer.new(:pointer)
|
|
34
|
+
Native.checked { |error| Native.thinkthen_client_open(settings, output, error) }
|
|
35
|
+
@pointer = Native::ClientPointer.new(output.read_pointer)
|
|
36
|
+
end
|
|
37
|
+
ensure
|
|
38
|
+
settings&.free
|
|
39
|
+
end
|
|
40
|
+
# The redacted key wrapper; its display never reveals the key.
|
|
41
|
+
attr_reader :api_key
|
|
42
|
+
def inspect = "#<ThinkThen::Client closed=#{closed?}>"
|
|
43
|
+
alias to_s inspect
|
|
44
|
+
# Close active calls and release this client. Repeated close calls are safe.
|
|
45
|
+
def close
|
|
46
|
+
check_process
|
|
47
|
+
streams = @mutex.synchronize do
|
|
48
|
+
@closed = true
|
|
49
|
+
@streams.filter_map { |reference| reference.__getobj__ if reference.weakref_alive? }
|
|
50
|
+
end
|
|
51
|
+
streams.each(&:close)
|
|
52
|
+
Thread.handle_interrupt(Object => :never) do
|
|
53
|
+
@mutex.synchronize do
|
|
54
|
+
if @pointer
|
|
55
|
+
@usage = Native.usage(@pointer, settled: true)
|
|
56
|
+
@pointer.free
|
|
57
|
+
@pointer = nil
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
# Cumulative usage; after close returns the copied settled snapshot.
|
|
64
|
+
def usage
|
|
65
|
+
check_process
|
|
66
|
+
@mutex.synchronize { @pointer ? Native.usage(@pointer) : @usage }
|
|
67
|
+
end
|
|
68
|
+
# Whether this client has released its native engine.
|
|
69
|
+
def closed? = @mutex.synchronize { @closed || !@pointer }
|
|
70
|
+
# Return a boolean answer row with its probability. A collection belongs in decide_each.
|
|
71
|
+
def decide(question, record, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET)
|
|
72
|
+
question = build_question(:decide, question, {yes_means: yes_means, no_means: no_means, threshold: threshold, field: field})
|
|
73
|
+
stream = open_rows(question, single_record(record, :decide), name, cancel)
|
|
74
|
+
single_result(stream)
|
|
75
|
+
end
|
|
76
|
+
# Yield one decide row per input. Without a block, return a closable Rows enumerator.
|
|
77
|
+
def decide_each(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET, &block)
|
|
78
|
+
question = build_question(:decide, question, {yes_means: yes_means, no_means: no_means, threshold: threshold, field: field})
|
|
79
|
+
stream = open_rows(question, records, name, cancel)
|
|
80
|
+
return stream unless block
|
|
81
|
+
stream.each(&block)
|
|
82
|
+
nil
|
|
83
|
+
end
|
|
84
|
+
# Return the selected label and its distribution. A collection belongs in choose_each.
|
|
85
|
+
def choose(question, record, choices: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET)
|
|
86
|
+
question = build_question(:choose, question, {descriptions: descriptions, choices: choices, threshold: threshold, field: field})
|
|
87
|
+
stream = open_rows(question, single_record(record, :choose), name, cancel)
|
|
88
|
+
single_result(stream)
|
|
89
|
+
end
|
|
90
|
+
# Yield one choose row per input. Without a block, return a closable Rows enumerator.
|
|
91
|
+
def choose_each(question, records, choices: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block)
|
|
92
|
+
question = build_question(:choose, question, {descriptions: descriptions, choices: choices, threshold: threshold, field: field})
|
|
93
|
+
stream = open_rows(question, records, name, cancel)
|
|
94
|
+
return stream unless block
|
|
95
|
+
stream.each(&block)
|
|
96
|
+
nil
|
|
97
|
+
end
|
|
98
|
+
# Return the matching labels and their probabilities. A collection belongs in tag_each.
|
|
99
|
+
def tag(question, record, labels: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET)
|
|
100
|
+
question = build_question(:tag, question, {descriptions: descriptions, labels: labels, threshold: threshold, field: field})
|
|
101
|
+
stream = open_rows(question, single_record(record, :tag), name, cancel)
|
|
102
|
+
single_result(stream)
|
|
103
|
+
end
|
|
104
|
+
# Yield one tag row per input. Without a block, return a closable Rows enumerator.
|
|
105
|
+
def tag_each(question, records, labels: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block)
|
|
106
|
+
question = build_question(:tag, question, {descriptions: descriptions, labels: labels, threshold: threshold, field: field})
|
|
107
|
+
stream = open_rows(question, records, name, cancel)
|
|
108
|
+
return stream unless block
|
|
109
|
+
stream.each(&block)
|
|
110
|
+
nil
|
|
111
|
+
end
|
|
112
|
+
# Return a normalized score and its level probabilities. A collection belongs in score_each.
|
|
113
|
+
def score(question, record, levels: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET)
|
|
114
|
+
question = build_question(:score, question, {descriptions: descriptions, levels: levels, field: field})
|
|
115
|
+
stream = open_rows(question, single_record(record, :score), name, cancel)
|
|
116
|
+
single_result(stream)
|
|
117
|
+
end
|
|
118
|
+
# Yield one score row per input. Without a block, return a closable Rows enumerator.
|
|
119
|
+
def score_each(question, records, levels: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block)
|
|
120
|
+
question = build_question(:score, question, {descriptions: descriptions, levels: levels, field: field})
|
|
121
|
+
stream = open_rows(question, records, name, cancel)
|
|
122
|
+
return stream unless block
|
|
123
|
+
stream.each(&block)
|
|
124
|
+
nil
|
|
125
|
+
end
|
|
126
|
+
# Yield only the original kept records, then raise once with failed results.
|
|
127
|
+
def filter(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET, &block)
|
|
128
|
+
question = build_question(:filter, question, {yes_means: yes_means, no_means: no_means, threshold: threshold, field: field})
|
|
129
|
+
stream = open_rows(question, records, name, cancel)
|
|
130
|
+
delivered = Enumerator.new do |output|
|
|
131
|
+
# @type var failed: Array[Result]
|
|
132
|
+
failed = []
|
|
133
|
+
begin
|
|
134
|
+
stream.each do |row|
|
|
135
|
+
if row.failed?
|
|
136
|
+
failed << row
|
|
137
|
+
elsif row.answer
|
|
138
|
+
output << row.item
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
unless failed.empty?
|
|
142
|
+
# Collected native failed outcomes always carry their error.
|
|
143
|
+
# @type var error: untyped
|
|
144
|
+
error = failed.first.error
|
|
145
|
+
raise FailedError.new(error.message, code: error.code, http_status: error.http_status, rows: failed)
|
|
146
|
+
end
|
|
147
|
+
ensure
|
|
148
|
+
stream.close
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
return delivered unless block
|
|
152
|
+
delivered.each(&block)
|
|
153
|
+
nil
|
|
154
|
+
end
|
|
155
|
+
# Yield ranked results, failed records last, retaining each caller-owned item.
|
|
156
|
+
def rank(question, records, levels: UNSET, top: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block)
|
|
157
|
+
question = build_question(:rank, question, {descriptions: descriptions, levels: levels, top: top, field: field})
|
|
158
|
+
stream = open_rows(question, records, name, cancel)
|
|
159
|
+
return stream unless block
|
|
160
|
+
stream.each(&block)
|
|
161
|
+
nil
|
|
162
|
+
end
|
|
163
|
+
# Return the matched span, or an answered nil when no span matches. A collection belongs in find_each.
|
|
164
|
+
def find(question, record, threshold: UNSET, field: UNSET, name: nil, cancel: nil)
|
|
165
|
+
question = build_question(:find, question, {threshold: threshold, field: field})
|
|
166
|
+
stream = open_rows(question, single_record(record, :find), name, cancel)
|
|
167
|
+
single_result(stream)
|
|
168
|
+
end
|
|
169
|
+
# Yield one find row per input. Without a block, return a closable Rows enumerator.
|
|
170
|
+
def find_each(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, &block)
|
|
171
|
+
question = build_question(:find, question, {threshold: threshold, field: field})
|
|
172
|
+
stream = open_rows(question, records, name, cancel)
|
|
173
|
+
return stream unless block
|
|
174
|
+
stream.each(&block)
|
|
175
|
+
nil
|
|
176
|
+
end
|
|
177
|
+
# Return recognized names with character offsets and probabilities. A collection belongs in recognize_each.
|
|
178
|
+
def recognize(record, kinds: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil)
|
|
179
|
+
question = build_question(:recognize, nil, {kinds: kinds, threshold: threshold, field: field})
|
|
180
|
+
stream = open_rows(question, single_record(record, :recognize), name, cancel)
|
|
181
|
+
single_result(stream)
|
|
182
|
+
end
|
|
183
|
+
def recognize_each(records, kinds: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, &block)
|
|
184
|
+
question = build_question(:recognize, nil, {kinds: kinds, threshold: threshold, field: field})
|
|
185
|
+
stream = open_rows(question, records, name, cancel)
|
|
186
|
+
return stream unless block
|
|
187
|
+
stream.each(&block)
|
|
188
|
+
nil
|
|
189
|
+
end
|
|
190
|
+
def relate(record, rules: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil)
|
|
191
|
+
question = build_question(:relate, nil, {rules: rules, threshold: threshold, field: field})
|
|
192
|
+
stream = open_rows(question, single_record(record, :relate), name, cancel)
|
|
193
|
+
single_result(stream)
|
|
194
|
+
end
|
|
195
|
+
def relate_each(records, rules: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, &block)
|
|
196
|
+
question = build_question(:relate, nil, {rules: rules, threshold: threshold, field: field})
|
|
197
|
+
stream = open_rows(question, records, name, cancel)
|
|
198
|
+
return stream unless block
|
|
199
|
+
stream.each(&block)
|
|
200
|
+
nil
|
|
201
|
+
end
|
|
202
|
+
# Return named child answer rows for the supplied questions. A collection belongs in annotate_each.
|
|
203
|
+
def annotate(question, record, field: UNSET, name: nil, cancel: nil)
|
|
204
|
+
question = build_question(:annotate, question, {field: field})
|
|
205
|
+
stream = open_rows(question, single_record(record, :annotate), name, cancel)
|
|
206
|
+
single_result(stream)
|
|
207
|
+
end
|
|
208
|
+
# Yield one annotate row per input. Without a block, return a closable Rows enumerator.
|
|
209
|
+
def annotate_each(question, records, field: UNSET, name: nil, cancel: nil, &block)
|
|
210
|
+
question = build_question(:annotate, question, {field: field})
|
|
211
|
+
stream = open_rows(question, records, name, cancel)
|
|
212
|
+
return stream unless block
|
|
213
|
+
stream.each(&block)
|
|
214
|
+
nil
|
|
215
|
+
end
|
|
216
|
+
private
|
|
217
|
+
def single_result(stream)
|
|
218
|
+
row = stream.next
|
|
219
|
+
if row.failed?
|
|
220
|
+
error = row.error
|
|
221
|
+
raise FailedError.new(error.message, code: error.code, http_status: error.http_status, rows: [row])
|
|
222
|
+
end
|
|
223
|
+
row
|
|
224
|
+
ensure
|
|
225
|
+
stream.close
|
|
226
|
+
end
|
|
227
|
+
def single_record(record, function)
|
|
228
|
+
if record.is_a?(Array) || record.respond_to?(:each) && !record.is_a?(Hash) && !record.is_a?(String) && !record.respond_to?(:to_h)
|
|
229
|
+
raise UsageError.new("a single call does not accept a collection; use #{function}_each", code: :"usage.argument")
|
|
230
|
+
end
|
|
231
|
+
[record]
|
|
232
|
+
end
|
|
233
|
+
def check_process
|
|
234
|
+
raise IOError, "client belongs to another process; create a client after fork" unless @pid == Process.pid
|
|
235
|
+
end
|
|
236
|
+
def build_question(function, question, options)
|
|
237
|
+
check_process
|
|
238
|
+
# @type var klass: untyped
|
|
239
|
+
klass = ThinkThen.const_get("#{function.to_s.capitalize}Question")
|
|
240
|
+
# @type var options: Hash[Symbol, untyped]
|
|
241
|
+
options = options.reject { |_, value| value.equal?(UNSET) }
|
|
242
|
+
if question.is_a?(Question)
|
|
243
|
+
raise UsageError.new("question does not match function", code: :"usage.argument") unless question.is_a?(klass)
|
|
244
|
+
raise UsageError.new("saved question owns its options", code: :"usage.argument") unless options.empty?
|
|
245
|
+
question
|
|
246
|
+
elsif function == :annotate
|
|
247
|
+
klass.new(question, **options)
|
|
248
|
+
elsif [:recognize, :relate].include?(function)
|
|
249
|
+
klass.new(**options)
|
|
250
|
+
else
|
|
251
|
+
klass.new(question, **options)
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
def open_rows(question, records, name, cancel)
|
|
255
|
+
raise UsageError.new("records must be an Enumerable; use the single-record method for text", code: :"usage.argument") unless records.respond_to?(:each) && !records.is_a?(Hash)
|
|
256
|
+
raise UsageError.new("name must be a String", code: :"usage.name") unless name.nil? || name.is_a?(String)
|
|
257
|
+
raise UsageError.new("cancel must be a Cancel", code: :"usage.argument") unless cancel.nil? || cancel.is_a?(Cancel)
|
|
258
|
+
request = Native.context_dump({question: question.value, **(name.nil? ? {} : {name: name})}, :question)
|
|
259
|
+
stream = Rows.new(self, request, records, cancel, question)
|
|
260
|
+
@mutex.synchronize do
|
|
261
|
+
raise UsageError.new("client is closed", code: :"usage.closed") if @closed || !@pointer
|
|
262
|
+
@streams.reject! { |reference| !reference.weakref_alive? }
|
|
263
|
+
@streams << WeakRef.new(stream)
|
|
264
|
+
end
|
|
265
|
+
stream
|
|
266
|
+
rescue Exception
|
|
267
|
+
stream&.close
|
|
268
|
+
raise
|
|
269
|
+
end
|
|
270
|
+
def admit(request, records = nil)
|
|
271
|
+
@mutex.synchronize do
|
|
272
|
+
raise UsageError.new("client is closed", code: :"usage.closed") if @closed || !@pointer
|
|
273
|
+
Native.bytes(request) do |bytes, length|
|
|
274
|
+
output = FFI::MemoryPointer.new(:pointer)
|
|
275
|
+
if records
|
|
276
|
+
Native.records(records) do |input, count|
|
|
277
|
+
Native.checked do |error|
|
|
278
|
+
Native.thinkthen_call_open_records_json(@pointer, bytes, length, input, count, Native::RecordInput.size,
|
|
279
|
+
NativeContract::OFFSET_NAMES.key("code_points"), output, error)
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
else
|
|
283
|
+
Native.checked do |error|
|
|
284
|
+
Native.thinkthen_call_open_json(@pointer, bytes, length, NativeContract::OFFSET_NAMES.key("code_points"), output, error)
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
output.read_pointer
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
def forget(stream) = @mutex.synchronize { @streams.reject! { |reference| !reference.weakref_alive? || reference.__getobj__.equal?(stream) } }
|
|
292
|
+
end
|
|
293
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ThinkThen
|
|
4
|
+
class RedactedKey
|
|
5
|
+
def initialize(value)
|
|
6
|
+
raise UsageError.new("api_key must be a String", code: :"usage.settings") unless value.is_a?(String)
|
|
7
|
+
ThinkThen.send(:native)
|
|
8
|
+
@settings = Native.settings
|
|
9
|
+
Native.bytes(value) do |bytes, length|
|
|
10
|
+
Native.checked { |error| Native.thinkthen_settings_set_api_key(@settings, bytes, length, error) }
|
|
11
|
+
end
|
|
12
|
+
freeze
|
|
13
|
+
end
|
|
14
|
+
def inspect = "#<ThinkThen::RedactedKey [REDACTED]>"
|
|
15
|
+
alias to_s inspect
|
|
16
|
+
def copy_to(settings)
|
|
17
|
+
Native.checked { |error| Native.thinkthen_settings_copy_api_key(settings, @settings, error) }
|
|
18
|
+
end
|
|
19
|
+
private :copy_to
|
|
20
|
+
end
|
|
21
|
+
class Configuration
|
|
22
|
+
attr_reader :api_key
|
|
23
|
+
attr_accessor :backend, :model, :url, :batch, :max_requests, :retry
|
|
24
|
+
def initialize
|
|
25
|
+
@backend = @model = @url = @batch = @max_requests = @retry = nil
|
|
26
|
+
end
|
|
27
|
+
def api_key=(value)
|
|
28
|
+
@api_key = value.nil? || value.is_a?(RedactedKey) ? value : RedactedKey.new(value)
|
|
29
|
+
end
|
|
30
|
+
def inspect = "#<ThinkThen::Configuration>"
|
|
31
|
+
alias to_s inspect
|
|
32
|
+
def to_h
|
|
33
|
+
{backend: backend, model: model, url: url, api_key: api_key, batch: batch, max_requests: max_requests, retry: self.retry}
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
@configuration_mutex = Mutex.new
|
|
37
|
+
@configuration = Configuration.new
|
|
38
|
+
class << self
|
|
39
|
+
def configure
|
|
40
|
+
@configuration_mutex.synchronize do
|
|
41
|
+
raise IOError, "shared client is already configured" if @shared_client
|
|
42
|
+
candidate = @configuration.dup
|
|
43
|
+
yield candidate
|
|
44
|
+
@configuration = candidate.dup
|
|
45
|
+
@configuration.model = Values.freeze_copy(candidate.model)
|
|
46
|
+
@configuration.url = Values.freeze_copy(candidate.url)
|
|
47
|
+
end
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
def client
|
|
51
|
+
@configuration_mutex.synchronize { @shared_client ||= Client.new(**@configuration.to_h) }
|
|
52
|
+
end
|
|
53
|
+
def close
|
|
54
|
+
@configuration_mutex.synchronize { @shared_client&.close; @shared_client = nil }
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
def decide(question, record, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET) = client.public_send(:decide, question, record, threshold: threshold, field: field, name: name, cancel: cancel, yes_means: yes_means, no_means: no_means)
|
|
58
|
+
def decide_each(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, yes_means: UNSET, no_means: UNSET, &block) = client.public_send(:decide_each, question, records, threshold: threshold, field: field, name: name, cancel: cancel, yes_means: yes_means, no_means: no_means, &block)
|
|
59
|
+
def choose(question, record, choices: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET) = client.public_send(:choose, question, record, choices: choices, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions)
|
|
60
|
+
def choose_each(question, records, choices: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block) = client.public_send(:choose_each, question, records, choices: choices, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions, &block)
|
|
61
|
+
def tag(question, record, labels: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET) = client.public_send(:tag, question, record, labels: labels, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions)
|
|
62
|
+
def tag_each(question, records, labels: UNSET, threshold: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block) = client.public_send(:tag_each, question, records, labels: labels, threshold: threshold, field: field, name: name, cancel: cancel, descriptions: descriptions, &block)
|
|
63
|
+
def score(question, record, levels: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET) = client.public_send(:score, question, record, levels: levels, field: field, name: name, cancel: cancel, descriptions: descriptions)
|
|
64
|
+
def score_each(question, records, levels: UNSET, field: UNSET, name: nil, cancel: nil, descriptions: UNSET, &block) = client.public_send(:score_each, question, records, levels: levels, field: field, name: name, cancel: cancel, descriptions: descriptions, &block)
|
|
65
|
+
def filter(question, records, **options, &block) = client.public_send(:filter, question, records, **options, &block)
|
|
66
|
+
def rank(question, records, **options, &block) = client.public_send(:rank, question, records, **options, &block)
|
|
67
|
+
def find(question, record, threshold: UNSET, field: UNSET, name: nil, cancel: nil) = client.public_send(:find, question, record, threshold: threshold, field: field, name: name, cancel: cancel)
|
|
68
|
+
def find_each(question, records, threshold: UNSET, field: UNSET, name: nil, cancel: nil, &block) = client.public_send(:find_each, question, records, threshold: threshold, field: field, name: name, cancel: cancel, &block)
|
|
69
|
+
def recognize(record, **options) = client.public_send(:recognize, record, **options)
|
|
70
|
+
def recognize_each(records, **options, &block) = client.public_send(:recognize_each, records, **options, &block)
|
|
71
|
+
def relate(record, **options) = client.public_send(:relate, record, **options)
|
|
72
|
+
def relate_each(records, **options, &block) = client.public_send(:relate_each, records, **options, &block)
|
|
73
|
+
def annotate(question, record, field: UNSET, name: nil, cancel: nil) = client.public_send(:annotate, question, record, field: field, name: name, cancel: cancel)
|
|
74
|
+
def annotate_each(question, records, field: UNSET, name: nil, cancel: nil, &block) = client.public_send(:annotate_each, question, records, field: field, name: name, cancel: cancel, &block)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# Generated native requirement. Do not edit.
|
|
3
|
+
module ThinkThen
|
|
4
|
+
module NativeContract
|
|
5
|
+
ABI_VERSION = 327681
|
|
6
|
+
ABI_CODE = "usage.abi"
|
|
7
|
+
ABI_MESSAGE = "the native ABI is incompatible"
|
|
8
|
+
LIBRARY_VARIABLE = "THINKTHEN_LIBRARY"
|
|
9
|
+
LIBRARY_PATH_MESSAGE = "THINKTHEN_LIBRARY must be an absolute path"
|
|
10
|
+
FUNCTION_NAMES = {
|
|
11
|
+
1 => "decide",
|
|
12
|
+
2 => "choose",
|
|
13
|
+
3 => "tag",
|
|
14
|
+
4 => "score",
|
|
15
|
+
7 => "find",
|
|
16
|
+
8 => "recognize",
|
|
17
|
+
9 => "relate",
|
|
18
|
+
5 => "filter",
|
|
19
|
+
6 => "rank",
|
|
20
|
+
10 => "annotate",
|
|
21
|
+
}.freeze
|
|
22
|
+
STATUS_NAMES = {
|
|
23
|
+
1 => "answered",
|
|
24
|
+
2 => "unsure",
|
|
25
|
+
3 => "failed",
|
|
26
|
+
}.freeze
|
|
27
|
+
BACKEND_NAMES = {
|
|
28
|
+
1 => "systemone",
|
|
29
|
+
2 => "openai",
|
|
30
|
+
3 => "stub",
|
|
31
|
+
}.freeze
|
|
32
|
+
ID_NAMES = {
|
|
33
|
+
0 => "absent",
|
|
34
|
+
1 => "string",
|
|
35
|
+
2 => "signed",
|
|
36
|
+
3 => "unsigned",
|
|
37
|
+
}.freeze
|
|
38
|
+
RECORD_NAMES = {
|
|
39
|
+
0 => "text",
|
|
40
|
+
1 => "json",
|
|
41
|
+
2 => "missing",
|
|
42
|
+
3 => "invalid",
|
|
43
|
+
}.freeze
|
|
44
|
+
OFFSET_NAMES = {
|
|
45
|
+
0 => "utf8",
|
|
46
|
+
1 => "utf16",
|
|
47
|
+
2 => "code_points",
|
|
48
|
+
3 => "code_points_inclusive",
|
|
49
|
+
}.freeze
|
|
50
|
+
CODE_NAMES = {
|
|
51
|
+
1001 => "usage.threshold",
|
|
52
|
+
1002 => "usage.question",
|
|
53
|
+
1003 => "usage.name",
|
|
54
|
+
1004 => "usage.settings",
|
|
55
|
+
1005 => "usage.backend",
|
|
56
|
+
1006 => "usage.url",
|
|
57
|
+
1007 => "usage.key-missing",
|
|
58
|
+
1008 => "usage.batch",
|
|
59
|
+
1009 => "usage.pointer",
|
|
60
|
+
1010 => "usage.sql-policy",
|
|
61
|
+
1011 => "usage.sql-selection",
|
|
62
|
+
1012 => "usage.abi",
|
|
63
|
+
1013 => "usage.closed",
|
|
64
|
+
1014 => "usage.function",
|
|
65
|
+
1015 => "usage.record-kind",
|
|
66
|
+
1016 => "usage.offsets",
|
|
67
|
+
1017 => "usage.argument",
|
|
68
|
+
1018 => "usage.option",
|
|
69
|
+
1019 => "usage.column",
|
|
70
|
+
1020 => "usage.sheet",
|
|
71
|
+
1021 => "usage.output",
|
|
72
|
+
1022 => "usage.table",
|
|
73
|
+
2001 => "record.json",
|
|
74
|
+
2002 => "record.missing",
|
|
75
|
+
2003 => "record.too-large",
|
|
76
|
+
2004 => "record.invalid",
|
|
77
|
+
2005 => "record.empty",
|
|
78
|
+
3001 => "backend.transport",
|
|
79
|
+
3002 => "backend.status",
|
|
80
|
+
3003 => "backend.reply",
|
|
81
|
+
4001 => "local.cancelled",
|
|
82
|
+
4002 => "local.io",
|
|
83
|
+
4003 => "local.toolbox",
|
|
84
|
+
4004 => "local.accounting",
|
|
85
|
+
4005 => "local.request-limit",
|
|
86
|
+
5001 => "internal.bug",
|
|
87
|
+
}.freeze
|
|
88
|
+
end
|
|
89
|
+
end
|