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
|
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
|
6
6
|
require "ruby-spacy"
|
|
7
7
|
require "fileutils"
|
|
8
8
|
|
|
9
|
-
# Requires the rsyntaxtree gem (>= 2.
|
|
9
|
+
# Requires the rsyntaxtree gem (>= 2.10.0): gem install rsyntaxtree
|
|
10
10
|
|
|
11
11
|
nlp = Spacy::Language.new("en_core_web_sm")
|
|
12
12
|
doc = nlp.read("The quick brown fox jumped over the lazy dog near the river.")
|
|
@@ -19,6 +19,10 @@ File.binwrite(File.join(output_dir, "tree_en_projection.png"),
|
|
|
19
19
|
doc.syntax_tree(format: :png))
|
|
20
20
|
File.binwrite(File.join(output_dir, "tree_en_chunks.png"),
|
|
21
21
|
doc.syntax_tree(style: :chunks, format: :png))
|
|
22
|
+
# The route through the tree from "fox" to "river": in a projection tree, the
|
|
23
|
+
# dependency path between the two words
|
|
24
|
+
File.binwrite(File.join(output_dir, "tree_en_path.png"),
|
|
25
|
+
doc.syntax_tree(format: :png, path: [doc[3], doc[11]]))
|
|
22
26
|
# A shorter sentence for the morphology tables: every leaf becomes a box, so a
|
|
23
27
|
# long sentence draws a figure too wide to read
|
|
24
28
|
compact = nlp.read("He was sitting under the old tree.")
|
|
@@ -6,10 +6,10 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
|
6
6
|
require "ruby-spacy"
|
|
7
7
|
require "fileutils"
|
|
8
8
|
|
|
9
|
-
# Requires the rsyntaxtree gem (>= 2.
|
|
9
|
+
# Requires the rsyntaxtree gem (>= 2.10.0): gem install rsyntaxtree
|
|
10
10
|
|
|
11
11
|
nlp = Spacy::Language.new("ja_core_news_sm")
|
|
12
|
-
doc = nlp.read("
|
|
12
|
+
doc = nlp.read("太郎は花子が書いた手紙を京都で読んだ。")
|
|
13
13
|
|
|
14
14
|
puts doc.syntax_tree
|
|
15
15
|
|
|
@@ -6,10 +6,10 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
|
6
6
|
require "ruby-spacy"
|
|
7
7
|
require "fileutils"
|
|
8
8
|
|
|
9
|
-
# Requires the rsyntaxtree gem (>= 2.
|
|
9
|
+
# Requires the rsyntaxtree gem (>= 2.10.0): gem install rsyntaxtree
|
|
10
10
|
|
|
11
11
|
nlp = Spacy::Language.new("ru_core_news_sm")
|
|
12
|
-
doc = nlp.read("Старый профессор читал
|
|
12
|
+
doc = nlp.read("Старый профессор читал в библиотеке интересную книгу.")
|
|
13
13
|
|
|
14
14
|
puts doc.syntax_tree
|
|
15
15
|
|
|
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(File.expand_path("../../lib", __dir__))
|
|
|
6
6
|
require "ruby-spacy"
|
|
7
7
|
require "fileutils"
|
|
8
8
|
|
|
9
|
-
# Requires the rsyntaxtree gem (>= 2.
|
|
9
|
+
# Requires the rsyntaxtree gem (>= 2.10.0): gem install rsyntaxtree
|
|
10
10
|
|
|
11
11
|
nlp = Spacy::Language.new("zh_core_web_sm")
|
|
12
12
|
doc = nlp.read("老教授昨天在图书馆读了一本有趣的书。")
|
|
@@ -10,7 +10,7 @@ module Spacy
|
|
|
10
10
|
# Messages API version header value
|
|
11
11
|
ANTHROPIC_VERSION = "2023-06-01"
|
|
12
12
|
# Default model for chat requests
|
|
13
|
-
DEFAULT_MODEL = "claude-sonnet-5"
|
|
13
|
+
DEFAULT_MODEL = "claude-sonnet-5-5"
|
|
14
14
|
# (see LLMClientBase::APIError)
|
|
15
15
|
APIError = LLMClientBase::APIError
|
|
16
16
|
|
|
@@ -18,8 +18,7 @@ module Spacy
|
|
|
18
18
|
# @param timeout [Integer] request timeout in seconds
|
|
19
19
|
# @param base_url [String] API endpoint override
|
|
20
20
|
def initialize(access_token:, timeout: DEFAULT_TIMEOUT, base_url: API_ENDPOINT)
|
|
21
|
-
super(base_url: base_url, timeout: timeout)
|
|
22
|
-
@access_token = access_token
|
|
21
|
+
super(base_url: base_url, access_token: access_token, timeout: timeout)
|
|
23
22
|
end
|
|
24
23
|
|
|
25
24
|
# Sends a Messages API request.
|
|
@@ -28,15 +27,17 @@ module Spacy
|
|
|
28
27
|
# Claude models (Opus 4.7+) reject it; the request is then retried once
|
|
29
28
|
# without it.
|
|
30
29
|
#
|
|
31
|
-
# @param model [String] The model to use (e.g., "claude-sonnet-5")
|
|
30
|
+
# @param model [String] The model to use (e.g., "claude-sonnet-5-5")
|
|
32
31
|
# @param messages [Array<Hash>] The conversation messages
|
|
33
32
|
# @param system [String, nil] System prompt (top-level parameter)
|
|
34
|
-
# @param max_tokens [Integer] Maximum tokens in the response
|
|
33
|
+
# @param max_tokens [Integer] Maximum tokens in the response, thinking
|
|
34
|
+
# included (required by the API)
|
|
35
35
|
# @param temperature [Float, nil] Sampling temperature (omitted when nil)
|
|
36
36
|
# @param output_config [Hash, nil] Output configuration, e.g.
|
|
37
|
-
# { format: { type: "json_schema", schema: {...} } } for structured
|
|
37
|
+
# { format: { type: "json_schema", schema: {...} } } for structured
|
|
38
|
+
# outputs, or { effort: "low" } for how much the model thinks
|
|
38
39
|
# @return [Hash] The API response
|
|
39
|
-
def messages(model:, messages:, system: nil, max_tokens:
|
|
40
|
+
def messages(model:, messages:, system: nil, max_tokens: DEFAULT_MAX_TOKENS, temperature: nil, output_config: nil)
|
|
40
41
|
body = {
|
|
41
42
|
model: model,
|
|
42
43
|
max_tokens: max_tokens,
|
|
@@ -24,16 +24,20 @@ module Spacy
|
|
|
24
24
|
# @param max_tokens [Integer] default maximum tokens in responses
|
|
25
25
|
# @param temperature [Float, nil] default sampling temperature (omitted from
|
|
26
26
|
# requests when nil; models that reject it are retried without it)
|
|
27
|
+
# @param effort [String, nil] default effort, how much the model thinks
|
|
28
|
+
# ("low", "medium", "high", "xhigh", "max"; the model's default when nil)
|
|
27
29
|
# @param base_url [String, nil] API endpoint override
|
|
28
30
|
def initialize(access_token: nil, model: AnthropicClient::DEFAULT_MODEL,
|
|
29
|
-
max_tokens:
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
max_tokens: LLMClientBase::DEFAULT_MAX_TOKENS, temperature: nil,
|
|
32
|
+
effort: nil, base_url: nil)
|
|
33
|
+
access_token ||= ENV["ANTHROPIC_API_KEY"]
|
|
34
|
+
raise "Error: ANTHROPIC_API_KEY is not set" unless access_token
|
|
32
35
|
|
|
33
36
|
@model = model
|
|
34
37
|
@default_max_tokens = max_tokens
|
|
35
38
|
@default_temperature = temperature
|
|
36
|
-
@
|
|
39
|
+
@default_effort = effort
|
|
40
|
+
@client = AnthropicClient.new(access_token: access_token,
|
|
37
41
|
base_url: base_url || AnthropicClient::API_ENDPOINT)
|
|
38
42
|
end
|
|
39
43
|
|
|
@@ -50,6 +54,7 @@ module Spacy
|
|
|
50
54
|
# @param model [String, nil] model override (defaults to instance model)
|
|
51
55
|
# @param max_tokens [Integer, nil] token limit override
|
|
52
56
|
# @param temperature [Float, nil] temperature override
|
|
57
|
+
# @param effort [String, nil] effort override
|
|
53
58
|
# @param schema [Hash, nil] JSON Schema for structured outputs; when given,
|
|
54
59
|
# the model output is constrained to the schema and the parsed Hash is
|
|
55
60
|
# returned. Objects in the schema must set `additionalProperties: false`.
|
|
@@ -59,12 +64,15 @@ module Spacy
|
|
|
59
64
|
# the schema output cannot be parsed as JSON (e.g., truncated by the
|
|
60
65
|
# token limit)
|
|
61
66
|
def chat(system: nil, user: nil, messages: nil,
|
|
62
|
-
model: nil, max_tokens: nil, temperature: nil,
|
|
67
|
+
model: nil, max_tokens: nil, temperature: nil, effort: nil,
|
|
63
68
|
schema: nil, raw: false)
|
|
64
69
|
msgs = messages || (user ? [{ role: "user", content: user }] : [])
|
|
65
70
|
raise ArgumentError, "No messages provided. Use user:/messages:" if msgs.empty?
|
|
66
71
|
|
|
67
|
-
output_config =
|
|
72
|
+
output_config = {}
|
|
73
|
+
output_config[:format] = { type: "json_schema", schema: schema } if schema
|
|
74
|
+
effort ||= @default_effort
|
|
75
|
+
output_config[:effort] = effort if effort
|
|
68
76
|
|
|
69
77
|
response = @client.messages(
|
|
70
78
|
model: model || @model,
|
|
@@ -72,7 +80,7 @@ module Spacy
|
|
|
72
80
|
system: system,
|
|
73
81
|
max_tokens: max_tokens || @default_max_tokens,
|
|
74
82
|
temperature: temperature || @default_temperature,
|
|
75
|
-
output_config: output_config
|
|
83
|
+
output_config: output_config.empty? ? nil : output_config
|
|
76
84
|
)
|
|
77
85
|
|
|
78
86
|
return response if raw
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "net/http"
|
|
4
4
|
require "openssl"
|
|
5
|
+
require "time"
|
|
5
6
|
require "uri"
|
|
6
7
|
require "json"
|
|
7
8
|
|
|
@@ -16,6 +17,18 @@ module Spacy
|
|
|
16
17
|
MAX_RETRIES = 3
|
|
17
18
|
# Base delay in seconds for exponential backoff between retries
|
|
18
19
|
BASE_RETRY_DELAY = 1
|
|
20
|
+
# Default cap on the tokens a response may generate. Reasoning models count
|
|
21
|
+
# their reasoning toward it, so it leaves room beyond the visible answer;
|
|
22
|
+
# only the tokens actually generated are billed
|
|
23
|
+
DEFAULT_MAX_TOKENS = 4000
|
|
24
|
+
# Longest wait in seconds before a retry, whatever the server asks for
|
|
25
|
+
MAX_RETRY_DELAY = 60
|
|
26
|
+
# Shortest access token masked in error messages. Shorter values are
|
|
27
|
+
# placeholders for local servers (e.g. "ollama"), and masking them would
|
|
28
|
+
# garble messages that mention the same word
|
|
29
|
+
MIN_REDACTED_LENGTH = 8
|
|
30
|
+
# What a masked access token reads as
|
|
31
|
+
REDACTED = "[REDACTED]"
|
|
19
32
|
|
|
20
33
|
# Raised when an LLM API request fails after retries; carries the HTTP
|
|
21
34
|
# status code and the parsed response body when available.
|
|
@@ -29,11 +42,23 @@ module Spacy
|
|
|
29
42
|
end
|
|
30
43
|
end
|
|
31
44
|
|
|
32
|
-
def initialize(base_url:, timeout: DEFAULT_TIMEOUT)
|
|
45
|
+
def initialize(base_url:, access_token: nil, timeout: DEFAULT_TIMEOUT)
|
|
33
46
|
@base_url = base_url.sub(%r{/\z}, "")
|
|
47
|
+
@access_token = access_token
|
|
34
48
|
@timeout = timeout
|
|
35
49
|
end
|
|
36
50
|
|
|
51
|
+
# Shows only the scheme, host, and port requests go to. The API key, and
|
|
52
|
+
# any credentials or tokens in the userinfo or path of +base_url+, are
|
|
53
|
+
# left out so that they cannot end up in logs or error reports.
|
|
54
|
+
# @return [String]
|
|
55
|
+
def inspect
|
|
56
|
+
uri = URI.parse(@base_url)
|
|
57
|
+
"#<#{self.class} endpoint=#{"#{uri.scheme}://#{uri.host}:#{uri.port}".inspect}>"
|
|
58
|
+
rescue URI::InvalidURIError
|
|
59
|
+
"#<#{self.class}>"
|
|
60
|
+
end
|
|
61
|
+
|
|
37
62
|
private
|
|
38
63
|
|
|
39
64
|
# Subclasses provide authentication and API-specific headers.
|
|
@@ -76,21 +101,17 @@ module Spacy
|
|
|
76
101
|
if [429, 529].include?(response.code.to_i)
|
|
77
102
|
retries += 1
|
|
78
103
|
if retries <= MAX_RETRIES
|
|
79
|
-
|
|
80
|
-
delay = retry_after || (BASE_RETRY_DELAY * (2**(retries - 1)) + rand * 0.5)
|
|
81
|
-
sleep delay
|
|
104
|
+
sleep retry_delay(retries, response["Retry-After"])
|
|
82
105
|
next
|
|
83
106
|
end
|
|
84
|
-
raise
|
|
85
|
-
status_code: response.code.to_i, response_body: response.body)
|
|
107
|
+
raise api_error("Rate limited after #{MAX_RETRIES} retries", response.code.to_i, response.body)
|
|
86
108
|
end
|
|
87
109
|
|
|
88
110
|
return handle_response(response)
|
|
89
111
|
rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNREFUSED, Errno::ECONNRESET, SocketError => e
|
|
90
112
|
retries += 1
|
|
91
113
|
if retries <= MAX_RETRIES
|
|
92
|
-
|
|
93
|
-
sleep delay
|
|
114
|
+
sleep retry_delay(retries)
|
|
94
115
|
next
|
|
95
116
|
end
|
|
96
117
|
raise APIError.new("Network error after #{MAX_RETRIES} retries: #{e.message}")
|
|
@@ -98,6 +119,42 @@ module Spacy
|
|
|
98
119
|
end
|
|
99
120
|
end
|
|
100
121
|
|
|
122
|
+
# Seconds to wait before the given retry: what the server asked for in
|
|
123
|
+
# Retry-After (seconds or an HTTP date) when it did, exponential backoff
|
|
124
|
+
# otherwise, and never longer than MAX_RETRY_DELAY.
|
|
125
|
+
def retry_delay(retries, retry_after = nil)
|
|
126
|
+
delay = retry_after_seconds(retry_after) || (BASE_RETRY_DELAY * (2**(retries - 1)) + rand * 0.5)
|
|
127
|
+
delay.clamp(0, MAX_RETRY_DELAY)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def retry_after_seconds(value)
|
|
131
|
+
return nil if value.nil?
|
|
132
|
+
|
|
133
|
+
# delay-seconds is a non-negative decimal integer (RFC 9110)
|
|
134
|
+
return value.to_i if value.match?(/\A\d+\z/)
|
|
135
|
+
|
|
136
|
+
Time.httpdate(value) - Time.now
|
|
137
|
+
rescue ArgumentError
|
|
138
|
+
nil
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# An APIError whose message and response body carry no access token, since
|
|
142
|
+
# an endpoint may echo the key it was sent (e.g. in an authentication error)
|
|
143
|
+
def api_error(message, status_code, response_body)
|
|
144
|
+
APIError.new(redact(message), status_code: status_code, response_body: redact(response_body))
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def redact(value)
|
|
148
|
+
return value if @access_token.nil? || @access_token.length < MIN_REDACTED_LENGTH
|
|
149
|
+
|
|
150
|
+
case value
|
|
151
|
+
when String then value.gsub(@access_token, REDACTED)
|
|
152
|
+
when Hash then value.transform_values { |v| redact(v) }
|
|
153
|
+
when Array then value.map { |v| redact(v) }
|
|
154
|
+
else value
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
101
158
|
def handle_response(response)
|
|
102
159
|
body = JSON.parse(response.body)
|
|
103
160
|
|
|
@@ -106,15 +163,15 @@ module Spacy
|
|
|
106
163
|
body
|
|
107
164
|
when 400..499
|
|
108
165
|
error_message = body.dig("error", "message") || "Client error"
|
|
109
|
-
raise
|
|
166
|
+
raise api_error(error_message, response.code.to_i, body)
|
|
110
167
|
when 500..599
|
|
111
168
|
error_message = body.dig("error", "message") || "Server error"
|
|
112
|
-
raise
|
|
169
|
+
raise api_error(error_message, response.code.to_i, body)
|
|
113
170
|
else
|
|
114
|
-
raise
|
|
171
|
+
raise api_error("Unexpected response: #{response.code}", response.code.to_i, body)
|
|
115
172
|
end
|
|
116
173
|
rescue JSON::ParserError
|
|
117
|
-
raise
|
|
174
|
+
raise api_error("Invalid JSON response", response.code.to_i, response.body)
|
|
118
175
|
end
|
|
119
176
|
end
|
|
120
177
|
end
|
|
@@ -13,7 +13,7 @@ module Spacy
|
|
|
13
13
|
# Default OpenAI API endpoint
|
|
14
14
|
API_ENDPOINT = "https://api.openai.com/v1"
|
|
15
15
|
# Default model for chat requests
|
|
16
|
-
DEFAULT_MODEL = "gpt-
|
|
16
|
+
DEFAULT_MODEL = "gpt-6-luna"
|
|
17
17
|
# Default model for embeddings requests
|
|
18
18
|
DEFAULT_EMBEDDINGS_MODEL = "text-embedding-3-small"
|
|
19
19
|
# (see LLMClientBase::APIError)
|
|
@@ -24,31 +24,36 @@ module Spacy
|
|
|
24
24
|
# @param base_url [String] API endpoint; include the version path
|
|
25
25
|
# (e.g., "http://localhost:11434/v1" for Ollama)
|
|
26
26
|
def initialize(access_token:, timeout: DEFAULT_TIMEOUT, base_url: API_ENDPOINT)
|
|
27
|
-
super(base_url: base_url, timeout: timeout)
|
|
28
|
-
@access_token = access_token
|
|
27
|
+
super(base_url: base_url, access_token: access_token, timeout: timeout)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
30
|
# Sends a chat completion request with optional tools support.
|
|
32
31
|
#
|
|
33
|
-
#
|
|
34
|
-
# model rejects
|
|
35
|
-
# is retried
|
|
32
|
+
# +temperature+ and +reasoning_effort+ are sent only when given. If the
|
|
33
|
+
# model rejects one of them (e.g., temperature on reasoning models,
|
|
34
|
+
# reasoning_effort on older models), the request is retried without it.
|
|
36
35
|
#
|
|
37
|
-
# @param model [String] The model to use (e.g., "gpt-
|
|
36
|
+
# @param model [String] The model to use (e.g., "gpt-6-luna")
|
|
38
37
|
# @param messages [Array<Hash>] The conversation messages
|
|
39
|
-
# @param max_completion_tokens [Integer] Maximum tokens in the response
|
|
38
|
+
# @param max_completion_tokens [Integer] Maximum tokens in the response,
|
|
39
|
+
# reasoning tokens included
|
|
40
40
|
# @param temperature [Float, nil] Sampling temperature (omitted when nil)
|
|
41
|
+
# @param reasoning_effort [String, nil] How much a reasoning model reasons
|
|
42
|
+
# ("none", "low", "medium", "high", ...; omitted when nil). Some models
|
|
43
|
+
# call tools on Chat Completions only with "none"
|
|
41
44
|
# @param tools [Array<Hash>, nil] Tool definitions for function calling
|
|
42
45
|
# @param tool_choice [String, Hash, nil] Tool selection strategy
|
|
43
46
|
# @param response_format [Hash, nil] Response format specification (e.g., { type: "json_object" })
|
|
44
47
|
# @return [Hash] The API response
|
|
45
|
-
def chat(model:, messages:, max_completion_tokens:
|
|
48
|
+
def chat(model:, messages:, max_completion_tokens: DEFAULT_MAX_TOKENS, temperature: nil,
|
|
49
|
+
reasoning_effort: nil, tools: nil, tool_choice: nil, response_format: nil)
|
|
46
50
|
body = {
|
|
47
51
|
model: model,
|
|
48
52
|
messages: messages,
|
|
49
53
|
max_completion_tokens: max_completion_tokens
|
|
50
54
|
}
|
|
51
55
|
body[:temperature] = temperature unless temperature.nil?
|
|
56
|
+
body[:reasoning_effort] = reasoning_effort if reasoning_effort
|
|
52
57
|
|
|
53
58
|
if tools && !tools.empty?
|
|
54
59
|
body[:tools] = tools
|
|
@@ -57,7 +62,7 @@ module Spacy
|
|
|
57
62
|
|
|
58
63
|
body[:response_format] = response_format if response_format
|
|
59
64
|
|
|
60
|
-
|
|
65
|
+
post_without_rejected_params("/chat/completions", body)
|
|
61
66
|
end
|
|
62
67
|
|
|
63
68
|
# Sends an embeddings request.
|
|
@@ -82,15 +87,21 @@ module Spacy
|
|
|
82
87
|
{ "Authorization" => "Bearer #{@access_token}" }
|
|
83
88
|
end
|
|
84
89
|
|
|
85
|
-
#
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
# Optional parameters that some models reject with a 400
|
|
91
|
+
DROPPABLE_PARAMS = %i[temperature reasoning_effort].freeze
|
|
92
|
+
|
|
93
|
+
# Retries without each optional parameter the model rejects, so callers
|
|
94
|
+
# need no per-model knowledge
|
|
95
|
+
def post_without_rejected_params(path, body)
|
|
88
96
|
post(path, body)
|
|
89
97
|
rescue APIError => e
|
|
90
|
-
raise unless
|
|
98
|
+
raise unless e.status_code == 400
|
|
99
|
+
|
|
100
|
+
param = DROPPABLE_PARAMS.find { |key| body.key?(key) && e.message.downcase.include?(key.to_s) }
|
|
101
|
+
raise unless param
|
|
91
102
|
|
|
92
|
-
warn "Warning: model does not support the
|
|
93
|
-
|
|
103
|
+
warn "Warning: model does not support the #{param} parameter; retrying without it"
|
|
104
|
+
post_without_rejected_params(path, body.except(param))
|
|
94
105
|
end
|
|
95
106
|
end
|
|
96
107
|
end
|
|
@@ -10,7 +10,7 @@ module Spacy
|
|
|
10
10
|
#
|
|
11
11
|
# @example Basic usage with linguistic_summary
|
|
12
12
|
# nlp = Spacy::Language.new("en_core_web_sm")
|
|
13
|
-
# nlp.with_openai(model: "gpt-
|
|
13
|
+
# nlp.with_openai(model: "gpt-6-luna") do |ai|
|
|
14
14
|
# doc = nlp.read("Apple Inc. was founded by Steve Jobs.")
|
|
15
15
|
# ai.chat(system: "Analyze the linguistic data.", user: doc.linguistic_summary)
|
|
16
16
|
# end
|
|
@@ -30,18 +30,21 @@ module Spacy
|
|
|
30
30
|
# @param max_tokens [Integer, nil] alias for max_completion_tokens (takes precedence)
|
|
31
31
|
# @param temperature [Float, nil] default sampling temperature (omitted from
|
|
32
32
|
# requests when nil; models that reject it are retried without it)
|
|
33
|
+
# @param reasoning_effort [String, nil] default reasoning effort for
|
|
34
|
+
# reasoning models ("none", "low", "medium", "high", ...; omitted when nil)
|
|
33
35
|
# @param base_url [String, nil] OpenAI-compatible API endpoint
|
|
34
36
|
# (e.g., "http://localhost:11434/v1" for Ollama)
|
|
35
37
|
def initialize(access_token: nil, model: OpenAIClient::DEFAULT_MODEL,
|
|
36
|
-
max_completion_tokens:
|
|
37
|
-
temperature: nil, base_url: nil)
|
|
38
|
-
|
|
39
|
-
raise "Error: OPENAI_API_KEY is not set" unless
|
|
38
|
+
max_completion_tokens: LLMClientBase::DEFAULT_MAX_TOKENS, max_tokens: nil,
|
|
39
|
+
temperature: nil, reasoning_effort: nil, base_url: nil)
|
|
40
|
+
access_token ||= ENV["OPENAI_API_KEY"]
|
|
41
|
+
raise "Error: OPENAI_API_KEY is not set" unless access_token
|
|
40
42
|
|
|
41
43
|
@model = model
|
|
42
44
|
@default_max_completion_tokens = max_tokens || max_completion_tokens
|
|
43
45
|
@default_temperature = temperature
|
|
44
|
-
@
|
|
46
|
+
@default_reasoning_effort = reasoning_effort
|
|
47
|
+
@client = OpenAIClient.new(access_token: access_token,
|
|
45
48
|
base_url: base_url || OpenAIClient::API_ENDPOINT)
|
|
46
49
|
end
|
|
47
50
|
|
|
@@ -58,6 +61,7 @@ module Spacy
|
|
|
58
61
|
# @param max_completion_tokens [Integer, nil] token limit override
|
|
59
62
|
# @param max_tokens [Integer, nil] alias for max_completion_tokens (takes precedence)
|
|
60
63
|
# @param temperature [Float, nil] temperature override
|
|
64
|
+
# @param reasoning_effort [String, nil] reasoning effort override
|
|
61
65
|
# @param response_format [Hash, nil] response format (e.g., { type: "json_object" })
|
|
62
66
|
# @param schema [Hash, nil] JSON Schema for structured outputs; when given,
|
|
63
67
|
# the model output is constrained to the schema and the parsed Hash is
|
|
@@ -69,7 +73,7 @@ module Spacy
|
|
|
69
73
|
# output cannot be parsed as JSON (e.g., truncated by the token limit)
|
|
70
74
|
def chat(system: nil, user: nil, messages: nil,
|
|
71
75
|
model: nil, max_completion_tokens: nil, max_tokens: nil,
|
|
72
|
-
temperature: nil, response_format: nil, schema: nil, raw: false)
|
|
76
|
+
temperature: nil, reasoning_effort: nil, response_format: nil, schema: nil, raw: false)
|
|
73
77
|
msgs = messages || build_messages(system: system, user: user)
|
|
74
78
|
raise ArgumentError, "No messages provided. Use system:/user: or messages:" if msgs.empty?
|
|
75
79
|
|
|
@@ -85,6 +89,7 @@ module Spacy
|
|
|
85
89
|
messages: msgs,
|
|
86
90
|
max_completion_tokens: max_tokens || max_completion_tokens || @default_max_completion_tokens,
|
|
87
91
|
temperature: temperature || @default_temperature,
|
|
92
|
+
reasoning_effort: reasoning_effort || @default_reasoning_effort,
|
|
88
93
|
response_format: response_format
|
|
89
94
|
)
|
|
90
95
|
|