ruby-spacy 0.7.0 → 0.8.0
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 +4 -4
- data/CHANGELOG.md +71 -110
- data/Gemfile +1 -1
- data/README.md +21 -18
- data/Rakefile +6 -0
- data/docs/syntax_trees.md +15 -11
- data/examples/llm/anthropic_chat.rb +1 -1
- data/examples/openai_integration/openai_completion.rb +2 -3
- data/examples/openai_integration/openai_query_1.rb +2 -3
- data/examples/openai_integration/openai_query_2.rb +2 -3
- data/examples/openai_integration/openai_query_3.rb +2 -3
- data/examples/openai_integration/openai_query_4.rb +2 -3
- data/examples/rsyntaxtree/outputs/tree_ar_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_de_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_chunks.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_morphology.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_path.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_en_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ja_chunks.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ja_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ru_morphology.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_ru_projection.png +0 -0
- data/examples/rsyntaxtree/outputs/tree_zh_projection.png +0 -0
- data/examples/rsyntaxtree/syntax_tree_ar.rb +1 -1
- data/examples/rsyntaxtree/syntax_tree_de.rb +1 -1
- data/examples/rsyntaxtree/syntax_tree_en.rb +5 -1
- data/examples/rsyntaxtree/syntax_tree_ja.rb +2 -2
- data/examples/rsyntaxtree/syntax_tree_ru.rb +2 -2
- data/examples/rsyntaxtree/syntax_tree_zh.rb +1 -1
- data/lib/ruby-spacy/anthropic_client.rb +8 -7
- data/lib/ruby-spacy/anthropic_helper.rb +15 -7
- data/lib/ruby-spacy/llm_client_base.rb +69 -12
- data/lib/ruby-spacy/openai_client.rb +27 -16
- data/lib/ruby-spacy/openai_helper.rb +12 -7
- data/lib/ruby-spacy/syntax_tree.rb +76 -31
- data/lib/ruby-spacy/version.rb +1 -1
- data/lib/ruby-spacy.rb +66 -36
- data/ruby-spacy.gemspec +8 -1
- metadata +6 -4
- data/.github/FUNDING.yml +0 -6
- data/.github/workflows/ci.yml +0 -97
|
@@ -5,23 +5,32 @@ module Spacy
|
|
|
5
5
|
# them with the rsyntaxtree gem. This is an internal implementation module;
|
|
6
6
|
# the public API is {Doc#syntax_tree} and {Span#syntax_tree}.
|
|
7
7
|
#
|
|
8
|
-
# rsyntaxtree is a soft dependency: it is required (>=
|
|
9
|
-
#
|
|
8
|
+
# rsyntaxtree is a soft dependency: it is required (>= MIN_RSYNTAXTREE) on
|
|
9
|
+
# the first call, not at load time.
|
|
10
10
|
module SyntaxTree
|
|
11
11
|
FORMATS = %i[bracket svg png pdf tikz json].freeze
|
|
12
12
|
STYLES = %i[projection chunks].freeze
|
|
13
13
|
|
|
14
|
+
# Oldest rsyntaxtree with everything used here: RSyntaxTree.escape,
|
|
15
|
+
# OPTION_KEYS, leaf_align, leafstyle "dashed", and highlighted paths (+=)
|
|
16
|
+
MIN_RSYNTAXTREE = "2.10.0"
|
|
17
|
+
|
|
14
18
|
# rsyntaxtree drawing defaults chosen for the wide, shallow trees produced
|
|
15
|
-
# here
|
|
16
|
-
#
|
|
19
|
+
# here: the words line up on the bottom row so the sentence reads straight
|
|
20
|
+
# across, with a dashed line down to each from its tag. User-supplied
|
|
21
|
+
# options take precedence (except hyphen, which the escaping depends on).
|
|
17
22
|
RENDER_DEFAULTS = {
|
|
18
23
|
hyphen: "literal",
|
|
19
24
|
polyline: "on",
|
|
20
25
|
tidy: "medium",
|
|
21
|
-
leafstyle: "
|
|
26
|
+
leafstyle: "dashed",
|
|
27
|
+
leaf_align: "words",
|
|
22
28
|
color: "modern"
|
|
23
29
|
}.freeze
|
|
24
30
|
|
|
31
|
+
# Marks the two ends of a highlighted path (`path:`) on their leaf labels
|
|
32
|
+
PATH_MARKER = "+=1"
|
|
33
|
+
|
|
25
34
|
# Phrase label mapping from the head's POS tag
|
|
26
35
|
PHRASE_LABEL = {
|
|
27
36
|
"NOUN" => "NP", "PROPN" => "NP", "PRON" => "NP", "NUM" => "NP",
|
|
@@ -36,19 +45,24 @@ module Spacy
|
|
|
36
45
|
# @return [String] the bracket notation (format: :bracket), the rendered
|
|
37
46
|
# output (SVG/TikZ/JSON text, or PNG/PDF binary), depending on format
|
|
38
47
|
def generate(source, format: :bracket, style: :projection, morphology: false,
|
|
39
|
-
entities: true, punctuation: false, **render_opts)
|
|
48
|
+
entities: true, punctuation: false, path: nil, **render_opts)
|
|
40
49
|
ensure_rsyntaxtree!
|
|
41
50
|
format = format.to_sym
|
|
42
51
|
style = style.to_sym
|
|
52
|
+
render_opts = render_opts.transform_keys(&:to_sym)
|
|
43
53
|
validate_options!(format, style, render_opts)
|
|
44
54
|
|
|
45
55
|
bracket = bracket_for(source, style: style, morphology: morphology,
|
|
46
|
-
entities: entities, punctuation: punctuation)
|
|
56
|
+
entities: entities, punctuation: punctuation, path: path)
|
|
47
57
|
return bracket if format == :bracket
|
|
48
58
|
|
|
49
59
|
# Right-to-left scripts are drawn mirrored (leaves run right to left)
|
|
50
|
-
# unless the caller says otherwise.
|
|
51
|
-
|
|
60
|
+
# unless the caller says otherwise. A tree laid out left to right
|
|
61
|
+
# stacks the leaves down the page, where the script's direction plays
|
|
62
|
+
# no part. The notation itself is unaffected
|
|
63
|
+
if rtl?(source) && render_opts[:direction].to_s != "ltr"
|
|
64
|
+
render_opts = { mirror: "on" }.merge(render_opts)
|
|
65
|
+
end
|
|
52
66
|
render(bracket, format, render_opts)
|
|
53
67
|
end
|
|
54
68
|
|
|
@@ -74,10 +88,10 @@ module Spacy
|
|
|
74
88
|
begin
|
|
75
89
|
require "rsyntaxtree"
|
|
76
90
|
rescue LoadError
|
|
77
|
-
raise LoadError, "syntax_tree requires the rsyntaxtree gem (>=
|
|
91
|
+
raise LoadError, "syntax_tree requires the rsyntaxtree gem (>= #{MIN_RSYNTAXTREE}): gem install rsyntaxtree"
|
|
78
92
|
end
|
|
79
|
-
|
|
80
|
-
raise LoadError, "syntax_tree requires rsyntaxtree >=
|
|
93
|
+
if Gem::Version.new(RSyntaxTree::VERSION) < Gem::Version.new(MIN_RSYNTAXTREE)
|
|
94
|
+
raise LoadError, "syntax_tree requires rsyntaxtree >= #{MIN_RSYNTAXTREE} (found #{RSyntaxTree::VERSION})"
|
|
81
95
|
end
|
|
82
96
|
@loaded = true
|
|
83
97
|
end
|
|
@@ -92,10 +106,10 @@ module Spacy
|
|
|
92
106
|
if render_opts.key?(:hyphen)
|
|
93
107
|
raise ArgumentError, "hyphen: cannot be overridden (the notation is escaped for hyphen: :literal)"
|
|
94
108
|
end
|
|
95
|
-
unknown = render_opts.keys
|
|
109
|
+
unknown = render_opts.keys - ::OPTION_KEYS
|
|
96
110
|
unless unknown.empty?
|
|
97
111
|
raise ArgumentError,
|
|
98
|
-
"unknown rsyntaxtree option(s): #{unknown.join(', ')} (valid: #{::
|
|
112
|
+
"unknown rsyntaxtree option(s): #{unknown.join(', ')} (valid: #{::OPTION_KEYS.join(', ')})"
|
|
99
113
|
end
|
|
100
114
|
if format == :bracket && !render_opts.empty?
|
|
101
115
|
raise ArgumentError,
|
|
@@ -125,8 +139,9 @@ module Spacy
|
|
|
125
139
|
raise ArgumentError, "syntax_tree: cannot escape#{where}: #{e.message}"
|
|
126
140
|
end
|
|
127
141
|
|
|
128
|
-
def bracket_for(source, style:, morphology:, entities:, punctuation:)
|
|
142
|
+
def bracket_for(source, style:, morphology:, entities:, punctuation:, path:)
|
|
129
143
|
tokens, root, root_label = tree_scope(source)
|
|
144
|
+
marks = path_marks(source, tokens, path)
|
|
130
145
|
chunks = chunk_spans(source)
|
|
131
146
|
if chunks.nil? && style == :chunks
|
|
132
147
|
raise ArgumentError,
|
|
@@ -135,13 +150,42 @@ module Spacy
|
|
|
135
150
|
chunks ||= []
|
|
136
151
|
ents = entities ? ent_map(source) : {}
|
|
137
152
|
|
|
138
|
-
case style
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
153
|
+
bracket = case style
|
|
154
|
+
when :chunks
|
|
155
|
+
chunks_bracket(tokens, chunks, ents, morphology: morphology,
|
|
156
|
+
punctuation: punctuation, marks: marks)
|
|
157
|
+
else
|
|
158
|
+
projection_bracket(root, chunks, ents, root_label: root_label,
|
|
159
|
+
morphology: morphology, punctuation: punctuation, marks: marks)
|
|
160
|
+
end
|
|
161
|
+
# Escaping turns any "+=" in the text into "\+\=", so every marker
|
|
162
|
+
# found is one placed on a leaf
|
|
163
|
+
if bracket.scan(PATH_MARKER).size != marks.size
|
|
164
|
+
raise ArgumentError,
|
|
165
|
+
"path: a token is not drawn in the tree (punctuation is left out unless punctuation: true)"
|
|
144
166
|
end
|
|
167
|
+
bracket
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Doc-based indices of the two tokens that `path:` connects, or [] when
|
|
171
|
+
# no path is asked for
|
|
172
|
+
def path_marks(source, tokens, path)
|
|
173
|
+
return [] if path.nil?
|
|
174
|
+
|
|
175
|
+
unless path.is_a?(Array) && path.size == 2 && path.all?(Spacy::Token)
|
|
176
|
+
raise ArgumentError, "path: takes two tokens, e.g. path: [doc[1], doc[4]]"
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
py_doc = source.is_a?(Spacy::Span) ? source.py_span.doc : source.py_doc
|
|
180
|
+
range = tokens.first.i..tokens.last.i
|
|
181
|
+
path.each do |token|
|
|
182
|
+
next if Spacy::Builtins.id(token.py_token.doc) == Spacy::Builtins.id(py_doc) && range.cover?(token.i)
|
|
183
|
+
|
|
184
|
+
raise ArgumentError, "path: #{token.text.inspect} is not a token of this #{source.class.name.split('::').last.downcase}"
|
|
185
|
+
end
|
|
186
|
+
raise ArgumentError, "path: the two tokens must differ" if path[0].i == path[1].i
|
|
187
|
+
|
|
188
|
+
path.map(&:i)
|
|
145
189
|
end
|
|
146
190
|
|
|
147
191
|
# Returns [tokens, root_token, root_label]. All positions are doc-based
|
|
@@ -210,9 +254,10 @@ module Spacy
|
|
|
210
254
|
source.ents.to_h { |e| [[e.py_span.start, e.py_span.end], e.label] }
|
|
211
255
|
end
|
|
212
256
|
|
|
213
|
-
def leaf(token, morphology:)
|
|
257
|
+
def leaf(token, morphology:, marks:)
|
|
214
258
|
word = escape(token.text, as: :word, context: "token #{token.text.inspect}")
|
|
215
|
-
|
|
259
|
+
mark = marks.include?(token.i) ? PATH_MARKER : ""
|
|
260
|
+
return "[#{token.pos}#{mark} #{word}]" unless morphology
|
|
216
261
|
|
|
217
262
|
rows = ["pos\\t#{escape(token.pos, as: :cell)}"]
|
|
218
263
|
morph = token.morphology(hash: false)
|
|
@@ -222,7 +267,7 @@ module Spacy
|
|
|
222
267
|
rows << "#{escape(k, as: :cell)}\\t#{escape(v, as: :cell)}"
|
|
223
268
|
end
|
|
224
269
|
end
|
|
225
|
-
"[#(#{rows.join('\n')}#) #{word}]"
|
|
270
|
+
"[#(#{rows.join('\n')}#)#{mark} #{word}]"
|
|
226
271
|
end
|
|
227
272
|
|
|
228
273
|
# "%NP" for a bare chunk; "%@orange:NP\nLABEL" for one matching an entity
|
|
@@ -235,7 +280,7 @@ module Spacy
|
|
|
235
280
|
# A shallow tree: [S ...] with chunks as [%NP [POS w] ...] and other
|
|
236
281
|
# tokens as plain leaves. Chunks matching an entity get a colored
|
|
237
282
|
# background and a second label line.
|
|
238
|
-
def chunks_bracket(tokens, chunks, ents, morphology:, punctuation:)
|
|
283
|
+
def chunks_bracket(tokens, chunks, ents, morphology:, punctuation:, marks:)
|
|
239
284
|
i = 0
|
|
240
285
|
parts = []
|
|
241
286
|
while i < tokens.size
|
|
@@ -245,10 +290,10 @@ module Spacy
|
|
|
245
290
|
s, e = chunk
|
|
246
291
|
chunk_tokens = tokens.select { |t| t.i >= s && t.i < e }
|
|
247
292
|
label = chunk_label("NP", ents[[s, e]])
|
|
248
|
-
parts << "[#{label} #{chunk_tokens.map { |t| leaf(t, morphology: morphology) }.join(' ')}]"
|
|
293
|
+
parts << "[#{label} #{chunk_tokens.map { |t| leaf(t, morphology: morphology, marks: marks) }.join(' ')}]"
|
|
249
294
|
i += chunk_tokens.size
|
|
250
295
|
else
|
|
251
|
-
parts << leaf(token, morphology: morphology) unless token.pos == "PUNCT" && !punctuation
|
|
296
|
+
parts << leaf(token, morphology: morphology, marks: marks) unless token.pos == "PUNCT" && !punctuation
|
|
252
297
|
i += 1
|
|
253
298
|
end
|
|
254
299
|
end
|
|
@@ -262,11 +307,11 @@ module Spacy
|
|
|
262
307
|
# wrapped in an inner [%NP ...]. A single-token chunk (a frequent case
|
|
263
308
|
# for named entities) is wrapped as [%NP leaf] so that the background
|
|
264
309
|
# and entity label are not lost.
|
|
265
|
-
def projection_bracket(token, chunks, ents, root_label:, morphology:, punctuation:)
|
|
310
|
+
def projection_bracket(token, chunks, ents, root_label:, morphology:, punctuation:, marks:)
|
|
266
311
|
kids = token.children.to_a
|
|
267
312
|
kids = kids.reject { |k| k.pos == "PUNCT" } unless punctuation
|
|
268
313
|
if kids.empty?
|
|
269
|
-
node = leaf(token, morphology: morphology)
|
|
314
|
+
node = leaf(token, morphology: morphology, marks: marks)
|
|
270
315
|
single = [token.i, token.i + 1]
|
|
271
316
|
return node unless chunks.include?(single)
|
|
272
317
|
|
|
@@ -280,10 +325,10 @@ module Spacy
|
|
|
280
325
|
ordered = (kids + [token]).sort_by(&:i)
|
|
281
326
|
render_node = lambda do |t|
|
|
282
327
|
if t.i == token.i
|
|
283
|
-
leaf(t, morphology: morphology)
|
|
328
|
+
leaf(t, morphology: morphology, marks: marks)
|
|
284
329
|
else
|
|
285
330
|
projection_bracket(t, chunks, ents, root_label: nil,
|
|
286
|
-
morphology: morphology, punctuation: punctuation)
|
|
331
|
+
morphology: morphology, punctuation: punctuation, marks: marks)
|
|
287
332
|
end
|
|
288
333
|
end
|
|
289
334
|
|
data/lib/ruby-spacy/version.rb
CHANGED
data/lib/ruby-spacy.rb
CHANGED
|
@@ -348,7 +348,7 @@ module Spacy
|
|
|
348
348
|
# Generates a syntax tree in rsyntaxtree bracket notation, or renders it
|
|
349
349
|
# with the rsyntaxtree gem.
|
|
350
350
|
#
|
|
351
|
-
# Requires the rsyntaxtree gem (>= 2.
|
|
351
|
+
# Requires the rsyntaxtree gem (>= 2.10.0) at call time (it is a soft
|
|
352
352
|
# dependency; install it with `gem install rsyntaxtree`). The doc must
|
|
353
353
|
# contain a single sentence; for a multi-sentence doc, use
|
|
354
354
|
# `doc.sents.map { |s| s.syntax_tree }`.
|
|
@@ -366,6 +366,9 @@ module Spacy
|
|
|
366
366
|
# @param entities [Boolean] mark chunks that match a named entity with a
|
|
367
367
|
# colored background and the entity label
|
|
368
368
|
# @param punctuation [Boolean] keep punctuation tokens
|
|
369
|
+
# @param path [Array<Token>, nil] two tokens whose connecting path through
|
|
370
|
+
# the tree is drawn highlighted; in a projection tree, this is the
|
|
371
|
+
# dependency path between them
|
|
369
372
|
# @param render_opts [Hash] rsyntaxtree drawing options (e.g. `fontsize:`).
|
|
370
373
|
# `hyphen:` cannot be overridden
|
|
371
374
|
# @return [String] the bracket notation or the rendered output
|
|
@@ -373,10 +376,11 @@ module Spacy
|
|
|
373
376
|
# doc.syntax_tree # => "[S [%NP [DET The] ...] ...]"
|
|
374
377
|
# doc.syntax_tree(format: :svg) # => "<svg ..."
|
|
375
378
|
# doc.syntax_tree(format: :png, fontsize: 12)
|
|
379
|
+
# doc.syntax_tree(format: :svg, path: [doc[3], doc[11]])
|
|
376
380
|
def syntax_tree(format: :bracket, style: :projection, morphology: false,
|
|
377
|
-
entities: true, punctuation: false, **render_opts)
|
|
381
|
+
entities: true, punctuation: false, path: nil, **render_opts)
|
|
378
382
|
SyntaxTree.generate(self, format: format, style: style, morphology: morphology,
|
|
379
|
-
entities: entities, punctuation: punctuation, **render_opts)
|
|
383
|
+
entities: entities, punctuation: punctuation, path: path, **render_opts)
|
|
380
384
|
end
|
|
381
385
|
|
|
382
386
|
# Generates a JSON string summarizing the linguistic analysis of the document.
|
|
@@ -444,7 +448,10 @@ module Spacy
|
|
|
444
448
|
# @param max_tokens [Integer] Alias for max_completion_tokens (deprecated, for backward compatibility)
|
|
445
449
|
# @param temperature [Float, nil] Sampling temperature (omitted from requests
|
|
446
450
|
# when nil; models that reject it are retried without it)
|
|
447
|
-
# @param model [String] The model to use (default: gpt-
|
|
451
|
+
# @param model [String] The model to use (default: gpt-6-luna)
|
|
452
|
+
# @param reasoning_effort [String, nil] Reasoning effort; "none" by
|
|
453
|
+
# default, since models such as gpt-6-luna call tools on Chat
|
|
454
|
+
# Completions only without reasoning. nil leaves it out of the request
|
|
448
455
|
# @param messages [Array<Hash>] Conversation history (for recursive tool calls). Note: this array is modified in place when tool calls occur.
|
|
449
456
|
# @param prompt [String, nil] System prompt for the query
|
|
450
457
|
# @return [String, nil] The model's response content
|
|
@@ -453,13 +460,14 @@ module Spacy
|
|
|
453
460
|
max_tokens: nil,
|
|
454
461
|
temperature: nil,
|
|
455
462
|
model: OpenAIClient::DEFAULT_MODEL,
|
|
463
|
+
reasoning_effort: "none",
|
|
456
464
|
messages: [],
|
|
457
465
|
prompt: nil,
|
|
458
466
|
response_format: nil,
|
|
459
467
|
max_tool_call_depth: 5,
|
|
460
468
|
_tool_call_depth: 0)
|
|
461
469
|
# Support both max_completion_tokens and max_tokens for backward compatibility
|
|
462
|
-
max_completion_tokens ||= max_tokens ||
|
|
470
|
+
max_completion_tokens ||= max_tokens || LLMClientBase::DEFAULT_MAX_TOKENS
|
|
463
471
|
if messages.empty?
|
|
464
472
|
messages = [
|
|
465
473
|
{ role: "system", content: prompt },
|
|
@@ -500,41 +508,26 @@ module Spacy
|
|
|
500
508
|
messages: messages,
|
|
501
509
|
max_completion_tokens: max_completion_tokens,
|
|
502
510
|
temperature: temperature,
|
|
511
|
+
reasoning_effort: reasoning_effort,
|
|
503
512
|
tools: tools,
|
|
504
513
|
tool_choice: tool_choice,
|
|
505
514
|
response_format: response_format
|
|
506
515
|
)
|
|
507
516
|
|
|
508
517
|
message = response.dig("choices", 0, "message")
|
|
518
|
+
return nil unless message
|
|
509
519
|
|
|
510
|
-
# Handle tool calls (GPT-5 format)
|
|
511
|
-
if message["tool_calls"] && !message["tool_calls"].empty?
|
|
520
|
+
# Handle tool calls (GPT-5 format), only while tools are offered
|
|
521
|
+
if tools && message["tool_calls"] && !message["tool_calls"].empty?
|
|
512
522
|
messages << message
|
|
513
523
|
|
|
524
|
+
# Every tool call needs an answer, or the next request is rejected
|
|
514
525
|
message["tool_calls"].each do |tool_call|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
result = tokens.map do |t|
|
|
521
|
-
{
|
|
522
|
-
surface: t.text,
|
|
523
|
-
lemma: t.lemma,
|
|
524
|
-
pos: t.pos,
|
|
525
|
-
tag: t.tag,
|
|
526
|
-
dep: t.dep,
|
|
527
|
-
ent_type: t.ent_type,
|
|
528
|
-
morphology: t.morphology
|
|
529
|
-
}
|
|
530
|
-
end.to_json
|
|
531
|
-
|
|
532
|
-
messages << {
|
|
533
|
-
role: "tool",
|
|
534
|
-
tool_call_id: tool_call_id,
|
|
535
|
-
content: result
|
|
536
|
-
}
|
|
537
|
-
end
|
|
526
|
+
messages << {
|
|
527
|
+
role: "tool",
|
|
528
|
+
tool_call_id: tool_call["id"],
|
|
529
|
+
content: tool_result(tool_call["function"] || {})
|
|
530
|
+
}
|
|
538
531
|
end
|
|
539
532
|
|
|
540
533
|
# Recursive call to get final response after tool execution
|
|
@@ -543,6 +536,7 @@ module Spacy
|
|
|
543
536
|
max_completion_tokens: max_completion_tokens,
|
|
544
537
|
temperature: temperature,
|
|
545
538
|
model: model,
|
|
539
|
+
reasoning_effort: reasoning_effort,
|
|
546
540
|
messages: messages,
|
|
547
541
|
prompt: prompt,
|
|
548
542
|
response_format: response_format,
|
|
@@ -564,11 +558,11 @@ module Spacy
|
|
|
564
558
|
# @param max_tokens [Integer] Alias for max_completion_tokens (deprecated, for backward compatibility)
|
|
565
559
|
# @param temperature [Float, nil] Sampling temperature (omitted from requests
|
|
566
560
|
# when nil; models that reject it are retried without it)
|
|
567
|
-
# @param model [String] The model to use (default: gpt-
|
|
561
|
+
# @param model [String] The model to use (default: gpt-6-luna)
|
|
568
562
|
# @return [String, nil] The completed text
|
|
569
563
|
def openai_completion(access_token: nil, max_completion_tokens: nil, max_tokens: nil, temperature: nil, model: OpenAIClient::DEFAULT_MODEL)
|
|
570
564
|
# Support both max_completion_tokens and max_tokens for backward compatibility
|
|
571
|
-
max_completion_tokens ||= max_tokens ||
|
|
565
|
+
max_completion_tokens ||= max_tokens || LLMClientBase::DEFAULT_MAX_TOKENS
|
|
572
566
|
|
|
573
567
|
messages = [
|
|
574
568
|
{ role: "system", content: "Complete the text input by the user." },
|
|
@@ -609,8 +603,40 @@ module Spacy
|
|
|
609
603
|
access_token ||= ENV["OPENAI_API_KEY"]
|
|
610
604
|
raise "Error: OPENAI_API_KEY is not set" unless access_token
|
|
611
605
|
|
|
612
|
-
|
|
613
|
-
|
|
606
|
+
OpenAIClient.new(access_token: access_token)
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
# The content answering one tool call of openai_query (a JSON string)
|
|
610
|
+
def tool_result(function)
|
|
611
|
+
unless function["name"] == "get_tokens"
|
|
612
|
+
return { error: "unknown tool: #{function['name']}" }.to_json
|
|
613
|
+
end
|
|
614
|
+
|
|
615
|
+
args = begin
|
|
616
|
+
JSON.parse(function["arguments"])
|
|
617
|
+
rescue JSON::ParserError, TypeError
|
|
618
|
+
nil
|
|
619
|
+
end
|
|
620
|
+
# text may be left out (the whole document), but not given as a non-string
|
|
621
|
+
unless args.is_a?(Hash) && (!args.key?("text") || args["text"].is_a?(String))
|
|
622
|
+
return { error: "get_tokens takes a JSON object with a text string" }.to_json
|
|
623
|
+
end
|
|
624
|
+
|
|
625
|
+
# The model may ask about part of the document or a rewording of it
|
|
626
|
+
text = args["text"].to_s
|
|
627
|
+
doc = if text.strip.empty? || text == @text
|
|
628
|
+
self
|
|
629
|
+
else
|
|
630
|
+
begin
|
|
631
|
+
Doc.new(@py_nlp, text: text, max_retrial: 0)
|
|
632
|
+
rescue StandardError
|
|
633
|
+
return { error: "the text could not be analyzed" }.to_json
|
|
634
|
+
end
|
|
635
|
+
end
|
|
636
|
+
doc.tokens.map do |t|
|
|
637
|
+
{ surface: t.text, lemma: t.lemma, pos: t.pos, tag: t.tag, dep: t.dep,
|
|
638
|
+
ent_type: t.ent_type, morphology: t.morphology }
|
|
639
|
+
end.to_json
|
|
614
640
|
end
|
|
615
641
|
|
|
616
642
|
public
|
|
@@ -845,22 +871,26 @@ module Spacy
|
|
|
845
871
|
# @param max_completion_tokens [Integer] default maximum tokens in responses
|
|
846
872
|
# @param temperature [Float, nil] default sampling temperature (omitted from
|
|
847
873
|
# requests when nil)
|
|
874
|
+
# @param reasoning_effort [String, nil] default reasoning effort (omitted
|
|
875
|
+
# from requests when nil)
|
|
848
876
|
# @param base_url [String, nil] OpenAI-compatible API endpoint override
|
|
849
877
|
# @yield [OpenAIHelper] the helper instance for making API calls
|
|
850
878
|
# @return [Object] the block's return value
|
|
851
879
|
# @example Batch processing with pipe
|
|
852
|
-
# nlp.with_openai(model: "gpt-
|
|
880
|
+
# nlp.with_openai(model: "gpt-6-luna") do |ai|
|
|
853
881
|
# nlp.pipe(texts).map do |doc|
|
|
854
882
|
# ai.chat(system: "Analyze.", user: doc.linguistic_summary)
|
|
855
883
|
# end
|
|
856
884
|
# end
|
|
857
885
|
def with_openai(access_token: nil, model: OpenAIClient::DEFAULT_MODEL,
|
|
858
|
-
max_completion_tokens:
|
|
886
|
+
max_completion_tokens: LLMClientBase::DEFAULT_MAX_TOKENS, temperature: nil,
|
|
887
|
+
reasoning_effort: nil, base_url: nil)
|
|
859
888
|
helper = OpenAIHelper.new(
|
|
860
889
|
access_token: access_token,
|
|
861
890
|
model: model,
|
|
862
891
|
max_completion_tokens: max_completion_tokens,
|
|
863
892
|
temperature: temperature,
|
|
893
|
+
reasoning_effort: reasoning_effort,
|
|
864
894
|
base_url: base_url
|
|
865
895
|
)
|
|
866
896
|
yield helper
|
data/ruby-spacy.gemspec
CHANGED
|
@@ -17,10 +17,17 @@ Gem::Specification.new do |spec|
|
|
|
17
17
|
spec.license = "MIT"
|
|
18
18
|
spec.required_ruby_version = Gem::Requirement.new(">= 3.2")
|
|
19
19
|
|
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
21
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
22
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
23
|
+
|
|
20
24
|
# Specify which files should be added to the gem when it is released.
|
|
21
25
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
26
|
+
# `scripts/` holds release-time tooling that inspects the built gem; excluding it
|
|
27
|
+
# keeps that tooling out of the package it checks. `.github/` configures the
|
|
28
|
+
# repository on GitHub and is of no use once installed.
|
|
22
29
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
23
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features|scripts|\.github)/}) }
|
|
24
31
|
end
|
|
25
32
|
# spec.bindir = "exe"
|
|
26
33
|
# spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby-spacy
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yoichiro Hasebe
|
|
@@ -159,8 +159,6 @@ executables: []
|
|
|
159
159
|
extensions: []
|
|
160
160
|
extra_rdoc_files: []
|
|
161
161
|
files:
|
|
162
|
-
- ".github/FUNDING.yml"
|
|
163
|
-
- ".github/workflows/ci.yml"
|
|
164
162
|
- ".gitignore"
|
|
165
163
|
- CHANGELOG.md
|
|
166
164
|
- Gemfile
|
|
@@ -235,6 +233,7 @@ files:
|
|
|
235
233
|
- examples/rsyntaxtree/outputs/tree_de_projection.png
|
|
236
234
|
- examples/rsyntaxtree/outputs/tree_en_chunks.png
|
|
237
235
|
- examples/rsyntaxtree/outputs/tree_en_morphology.png
|
|
236
|
+
- examples/rsyntaxtree/outputs/tree_en_path.png
|
|
238
237
|
- examples/rsyntaxtree/outputs/tree_en_projection.png
|
|
239
238
|
- examples/rsyntaxtree/outputs/tree_ja_chunks.png
|
|
240
239
|
- examples/rsyntaxtree/outputs/tree_ja_projection.png
|
|
@@ -261,7 +260,10 @@ files:
|
|
|
261
260
|
homepage: https://github.com/yohasebe/ruby-spacy
|
|
262
261
|
licenses:
|
|
263
262
|
- MIT
|
|
264
|
-
metadata:
|
|
263
|
+
metadata:
|
|
264
|
+
source_code_uri: https://github.com/yohasebe/ruby-spacy
|
|
265
|
+
changelog_uri: https://github.com/yohasebe/ruby-spacy/blob/main/CHANGELOG.md
|
|
266
|
+
rubygems_mfa_required: 'true'
|
|
265
267
|
rdoc_options: []
|
|
266
268
|
require_paths:
|
|
267
269
|
- lib
|
data/.github/FUNDING.yml
DELETED
data/.github/workflows/ci.yml
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
name: CI
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
# Every branch, so that work in progress is tested before a pull request exists
|
|
5
|
-
push:
|
|
6
|
-
branches: ["**"]
|
|
7
|
-
pull_request:
|
|
8
|
-
# Weekly run to detect breakage from new spaCy / PyCall / Ruby releases
|
|
9
|
-
# even when the repository itself is untouched
|
|
10
|
-
schedule:
|
|
11
|
-
- cron: "23 3 * * 2"
|
|
12
|
-
|
|
13
|
-
jobs:
|
|
14
|
-
test:
|
|
15
|
-
name: ruby ${{ matrix.ruby }} / python ${{ matrix.python }}
|
|
16
|
-
runs-on: ubuntu-latest
|
|
17
|
-
# Pull requests from this repository are already covered by the push event;
|
|
18
|
-
# only run the pull_request event for forks, to avoid duplicate matrices
|
|
19
|
-
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository
|
|
20
|
-
# This project has known hang paths (pipeline calls from non-main threads,
|
|
21
|
-
# spacy.load hangs that Ruby's Timeout cannot interrupt); never let a job
|
|
22
|
-
# run until GitHub's 6-hour limit
|
|
23
|
-
timeout-minutes: 30
|
|
24
|
-
continue-on-error: ${{ matrix.experimental == true }}
|
|
25
|
-
strategy:
|
|
26
|
-
fail-fast: false
|
|
27
|
-
matrix:
|
|
28
|
-
ruby: ["3.2", "3.3", "3.4", "4.0"]
|
|
29
|
-
python: ["3.11", "3.12", "3.13", "3.14"]
|
|
30
|
-
include:
|
|
31
|
-
# Ruby 4.1 is not released yet; track the development branch instead
|
|
32
|
-
- { ruby: head, python: "3.13", experimental: true }
|
|
33
|
-
env:
|
|
34
|
-
# PyCall locates the Python interpreter via this variable
|
|
35
|
-
PYTHON: python
|
|
36
|
-
# rsyntaxtree (used by the syntax_tree tests) is in the :rendering
|
|
37
|
-
# bundler group and needs pango/rsvg system libraries; install it only
|
|
38
|
-
# on representative jobs
|
|
39
|
-
BUNDLE_WITHOUT: ${{ matrix.python != '3.13' && 'rendering' || '' }}
|
|
40
|
-
steps:
|
|
41
|
-
- uses: actions/checkout@v7
|
|
42
|
-
|
|
43
|
-
# Must precede ruby/setup-ruby: installing the pycall gem compiles its
|
|
44
|
-
# native extension against this Python
|
|
45
|
-
- uses: actions/setup-python@v7
|
|
46
|
-
with:
|
|
47
|
-
python-version: ${{ matrix.python }}
|
|
48
|
-
|
|
49
|
-
# Must precede ruby/setup-ruby: bundler-cache installs the pango/rsvg2
|
|
50
|
-
# gems, which compile against these system libraries
|
|
51
|
-
- name: Install rsyntaxtree system dependencies (representative jobs)
|
|
52
|
-
if: matrix.python == '3.13'
|
|
53
|
-
run: sudo apt-get update && sudo apt-get install -y libpango1.0-dev librsvg2-dev libgirepository1.0-dev
|
|
54
|
-
|
|
55
|
-
- uses: ruby/setup-ruby@v1
|
|
56
|
-
with:
|
|
57
|
-
ruby-version: ${{ matrix.ruby }}
|
|
58
|
-
bundler-cache: true
|
|
59
|
-
|
|
60
|
-
- id: site-packages
|
|
61
|
-
run: echo "path=$(python -c 'import site; print(site.getsitepackages()[0])')" >> "$GITHUB_OUTPUT"
|
|
62
|
-
|
|
63
|
-
# Cache the language models only (en_core_web_lg alone is several hundred
|
|
64
|
-
# MB). spaCy itself is intentionally installed fresh on every run so that
|
|
65
|
-
# new releases are exercised immediately. Bump the key suffix when
|
|
66
|
-
# upgrading the models. The ja/ru/de/zh models are installed only on the
|
|
67
|
-
# python 3.13 jobs (where the syntax_tree tests run); listing them here
|
|
68
|
-
# for other jobs is harmless
|
|
69
|
-
- uses: actions/cache@v6
|
|
70
|
-
with:
|
|
71
|
-
path: |
|
|
72
|
-
${{ steps.site-packages.outputs.path }}/en_core_web_sm*
|
|
73
|
-
${{ steps.site-packages.outputs.path }}/en_core_web_lg*
|
|
74
|
-
${{ steps.site-packages.outputs.path }}/ja_core_news_sm*
|
|
75
|
-
${{ steps.site-packages.outputs.path }}/ru_core_news_sm*
|
|
76
|
-
${{ steps.site-packages.outputs.path }}/de_core_news_sm*
|
|
77
|
-
${{ steps.site-packages.outputs.path }}/zh_core_web_sm*
|
|
78
|
-
key: spacy-models-${{ matrix.python }}-v2
|
|
79
|
-
|
|
80
|
-
# en_core_web_lg and the ja/ru/de/zh models are large; install them only
|
|
81
|
-
# on representative jobs (where the syntax_tree tests run). Tests that
|
|
82
|
-
# need them are skipped automatically where they are absent.
|
|
83
|
-
- name: Install spaCy and language models
|
|
84
|
-
run: |
|
|
85
|
-
python -m pip install --upgrade pip
|
|
86
|
-
python -m pip install --upgrade spacy
|
|
87
|
-
python -m spacy download en_core_web_sm
|
|
88
|
-
if [ "${{ matrix.python }}" = "3.13" ]; then
|
|
89
|
-
python -m spacy download en_core_web_lg
|
|
90
|
-
python -m spacy download ja_core_news_sm
|
|
91
|
-
python -m spacy download ru_core_news_sm
|
|
92
|
-
python -m spacy download de_core_news_sm
|
|
93
|
-
python -m spacy download zh_core_web_sm
|
|
94
|
-
fi
|
|
95
|
-
|
|
96
|
-
- name: Run tests
|
|
97
|
-
run: bundle exec rake test
|