lara-sdk 1.11.1 → 1.12.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1ee8cb6868618c828617b0de78b32a3741effb9f45f1a195f47585c29728e51c
4
- data.tar.gz: 2cd9efe5a99ad41df2a3c6cdfddeeb82469ae7f67779a0c4146cb97e0644ff85
3
+ metadata.gz: ace6028817d3734e535a624de3996392ea52d8713d77e67b2f29d004dac3eca0
4
+ data.tar.gz: '063339bb60f85a74d76ea75b92a1fbc4a1b0a19bf1d18b9ef059220872ae7375'
5
5
  SHA512:
6
- metadata.gz: 4714a55dbee8c29c9133400037c7026c311a052a42d8861e133a952415fcfda557299b74cfed837740a2651b3779e8ce884cea8050b9c6ccaa0f52d44a384538
7
- data.tar.gz: 4f11b83d1ac9c02dec61fbb154c9aff00c342b37ece3db8ff1ad1ccffb503bef09c7db8645149b09cbeb148d02f5813ca83595ce00fcb8ed7c206dd0574521fc
6
+ metadata.gz: 369038c43f35cfab08f4c1fd55cbc70274c20fd3c39c7c36267a7d90e929dab226c559280be68c8b4e0d8728346aca1f23369497e059d354f6f1fa2898a76c1e
7
+ data.tar.gz: 763aad5804951fbfd638881c443c571bbf818884a93197c530f47f0ce7c801bc0873adbbc8f1794a5fa4438bc5e32a8f6b1e8a10eed93cf4403fa8e8a58ac957
data/lib/lara/client.rb CHANGED
@@ -14,6 +14,7 @@ module Lara
14
14
 
15
15
  class Client
16
16
  DEFAULT_BASE_URL = "https://api.laratranslate.com"
17
+ DEFAULT_EVALUATE_READ_TIMEOUT = 45 * 60
17
18
 
18
19
  def initialize(auth_method, base_url: DEFAULT_BASE_URL, connection_timeout: nil,
19
20
  read_timeout: nil, session_id: nil)
@@ -73,6 +74,7 @@ module Lara
73
74
  end
74
75
 
75
76
  # Sends a POST request to the Lara API.
77
+ # @param params [Hash,nil] URL query parameters.
76
78
  # @param path [String] The path to send the request to.
77
79
  # @param body [Hash,nil] The parameters to send with the request.
78
80
  # @param files [Hash,nil] The files to send with the request. If present, request will be sent as multipart/form-data.
@@ -80,8 +82,8 @@ module Lara
80
82
  # @param raw_response [Boolean] If true, returns the raw response body (useful for binary data like images).
81
83
  # @yield [Hash] Each partial JSON result from the stream (if streaming)
82
84
  # @return [Hash, Array, String, nil] The JSON 'content' from the API, CSV body, or raw bytes.
83
- def post(path, body: nil, files: nil, headers: nil, raw_response: false, &callback)
84
- request(:post, path, body: body, files: files, headers: headers, raw_response: raw_response,
85
+ def post(path, body: nil, files: nil, headers: nil, raw_response: false, params: nil, &callback)
86
+ request(:post, path, body: body, files: files, headers: headers, params: params, raw_response: raw_response,
85
87
  &callback)
86
88
  end
87
89
 
@@ -191,11 +193,7 @@ module Lara
191
193
 
192
194
  headers["X-Lara-Auth-Session-Id"] = @session_id if @session_id && !@session_id.empty?
193
195
 
194
- conn = Faraday.new(url: @base_url) do |c|
195
- c.adapter Faraday.default_adapter
196
- end
197
-
198
- response = conn.post(path) do |req|
196
+ response = @connection.post(path) do |req|
199
197
  req.headers = headers
200
198
  req.body = body_json
201
199
  end
@@ -211,11 +209,7 @@ module Lara
211
209
  def do_refresh
212
210
  raise LaraError, "No refresh token available" unless @auth_token&.refresh_token
213
211
 
214
- conn = Faraday.new(url: @base_url) do |c|
215
- c.adapter Faraday.default_adapter
216
- end
217
-
218
- response = conn.post("/v2/auth/refresh") do |req|
212
+ response = @connection.post("/v2/auth/refresh") do |req|
219
213
  req.headers = {
220
214
  "Date" => Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT"),
221
215
  "X-Lara-SDK-Name" => "lara-ruby",
@@ -246,10 +240,16 @@ module Lara
246
240
  files.each { |key, value| req.body[key] = value }
247
241
  end
248
242
  else
249
- @connection.send(method, path) do |req|
243
+ evaluate = method == :post && path == "/v2/evaluate"
244
+ connection = @connection
245
+ connection = batch_evaluate_connection if evaluate && body.is_a?(Array) && @read_timeout.nil?
246
+ connection.send(method, path) do |req|
247
+ if evaluate && body.is_a?(Hash)
248
+ req.options.read_timeout ||= DEFAULT_EVALUATE_READ_TIMEOUT
249
+ end
250
250
  req.headers.merge!(request_headers)
251
251
  req.params = params if params
252
- req.body = body.to_json if body.is_a?(Hash) && !body.empty?
252
+ req.body = body.to_json if json_body?(body)
253
253
  end
254
254
  end
255
255
 
@@ -262,7 +262,7 @@ module Lara
262
262
  content_type = response.headers["content-type"] || response.headers["Content-Type"]
263
263
  return response.body if content_type&.include?("text/csv") || content_type&.include?("application/xml")
264
264
 
265
- if callback || (body && body[:reasoning])
265
+ if callback || (body.is_a?(Hash) && body[:reasoning])
266
266
  parse_stream_response(response.body, &callback)
267
267
  else
268
268
  parse_json(response.body)
@@ -324,20 +324,31 @@ module Lara
324
324
 
325
325
  headers.merge!(extra_headers) if extra_headers
326
326
 
327
- if !files&.any? && body.is_a?(Hash) && !body.empty?
327
+ if !files&.any? && json_body?(body)
328
328
  headers["Content-Type"] = "application/json"
329
329
  end
330
330
 
331
331
  headers
332
332
  end
333
333
 
334
- def build_connection
334
+ def json_body?(body)
335
+ body.is_a?(Array) || (body.is_a?(Hash) && !body.empty?)
336
+ end
337
+
338
+ def batch_evaluate_connection
339
+ # Faraday skips nil request timeouts, so clear Net::HTTP's default on the transport itself.
340
+ @batch_evaluate_connection ||= build_connection do |http|
341
+ http.read_timeout = nil if http.respond_to?(:read_timeout=)
342
+ end
343
+ end
344
+
345
+ def build_connection(&configure_adapter)
335
346
  Faraday.new(url: @base_url) do |conn|
336
347
  conn.request :multipart
337
348
  conn.request :url_encoded
338
- conn.adapter Faraday.default_adapter
339
- conn.options.timeout = @connection_timeout if @connection_timeout
340
- conn.options.open_timeout = @read_timeout if @read_timeout
349
+ conn.adapter Faraday.default_adapter, &configure_adapter
350
+ conn.options.open_timeout = @connection_timeout if @connection_timeout
351
+ conn.options.read_timeout = @read_timeout if @read_timeout
341
352
  end
342
353
  end
343
354
 
@@ -25,7 +25,8 @@ module Lara
25
25
  # Uploads a file to S3
26
26
  # @return [Lara::Models::Document]
27
27
  def upload(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil,
28
- no_trace: false, style: nil, password: nil, extraction_params: nil)
28
+ no_trace: false, style: nil, password: nil, extraction_params: nil,
29
+ styleguide_id: nil, styleguide_explanation_language: nil)
29
30
  response_data = @client.get("/v2/documents/upload-url", params: { filename: filename })
30
31
  url = response_data["url"]
31
32
  fields = response_data["fields"]
@@ -39,6 +40,8 @@ module Lara
39
40
  adapt_to: adapt_to,
40
41
  glossaries: glossaries,
41
42
  style: style,
43
+ styleguide_id: styleguide_id,
44
+ styleguide_explanation_language: styleguide_explanation_language,
42
45
  password: password,
43
46
  extraction_params: extraction_params&.to_h
44
47
  }.compact
@@ -59,6 +62,12 @@ module Lara
59
62
  Lara::Models::Document.new(**filter_document_params(response_params))
60
63
  end
61
64
 
65
+ def get_styleguide_report(id, verbose: false)
66
+ params = verbose ? { verbose: true } : {}
67
+ report = @client.get("/v2/documents/#{id}/styleguide-report", params: params)
68
+ report.to_h { |index, segment| [Integer(index), Models::DocumentStyleguideSegment.from_hash(segment)] }
69
+ end
70
+
62
71
  # Download translated document bytes
63
72
  # @return [String] bytes
64
73
  def download(id, output_format: nil)
@@ -71,10 +80,12 @@ module Lara
71
80
  # Translates a document end-to-end
72
81
  # @return [String] translated file bytes
73
82
  def translate(file_path:, filename:, target:, source: nil, adapt_to: nil, glossaries: nil, output_format: nil,
74
- no_trace: false, style: nil, password: nil, extraction_params: nil)
83
+ no_trace: false, style: nil, password: nil, extraction_params: nil,
84
+ styleguide_id: nil, styleguide_explanation_language: nil)
75
85
  document = upload(file_path: file_path, filename: filename, target: target, source: source,
76
86
  adapt_to: adapt_to, glossaries: glossaries, no_trace: no_trace, style: style, password: password,
77
- extraction_params: extraction_params)
87
+ extraction_params: extraction_params, styleguide_id: styleguide_id,
88
+ styleguide_explanation_language: styleguide_explanation_language)
78
89
 
79
90
  max_wait_time = 60 * 15 # 15 minutes
80
91
  start = Time.now
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "base"
4
+ require_relative "text"
4
5
 
5
6
  module Lara
6
7
  module Models
@@ -39,6 +40,18 @@ module Lara
39
40
  end
40
41
  end
41
42
 
43
+ class DocumentStyleguideSegment < TextResult
44
+ def self.convert_styleguide_results(value)
45
+ return unless value
46
+
47
+ blocks = value.fetch("original_translation").map do |block|
48
+ TextBlock.new(text: block.fetch("text"), translatable: block.fetch("translatable", true))
49
+ end
50
+ super(value.merge("original_translation" => blocks))
51
+ end
52
+ private_class_method :convert_styleguide_results
53
+ end
54
+
42
55
  class Document < Base
43
56
  attr_reader :id, :status, :source, :target, :filename, :created_at, :updated_at,
44
57
  :options, :translated_chars, :total_chars, :error_reason
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Lara
6
+ module Models
7
+ class EvaluateResponse < Base
8
+ attr_reader :results, :summary
9
+
10
+ def initialize(response)
11
+ super()
12
+ @results = response.fetch("results").map { |result| EvaluateResult.new(result) }
13
+ @summary = EvaluateSummary.new(response["summary"]) if response["summary"]
14
+ end
15
+ end
16
+
17
+ class EvaluateResult < Base
18
+ attr_reader :id, :source, :source_language, :target_language, :status, :models, :explanation
19
+
20
+ def initialize(result)
21
+ super()
22
+ @id = result.fetch("id")
23
+ @source = result.fetch("source")
24
+ @source_language = result.fetch("source_language")
25
+ @target_language = result.fetch("target_language")
26
+ @status = result.fetch("status")
27
+ @models = result["models"]
28
+ @explanation = result["explanation"]
29
+ end
30
+ end
31
+
32
+ # Per-model verdict rates range from 0 to 1 and are nil when no sources complete.
33
+ class EvaluateSummary < Base
34
+ attr_reader :total_sources, :completed_sources, :models
35
+
36
+ def initialize(summary)
37
+ super()
38
+ @total_sources = summary.fetch("total_sources")
39
+ @completed_sources = summary.fetch("completed_sources")
40
+ @models = summary.fetch("models").transform_values do |rates|
41
+ rates.slice("pass_rate", "fail_rate", "unknown_rate")
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -156,6 +156,7 @@ module Lara
156
156
  when "NGMemoryMatch"
157
157
  NGMemoryMatch.new(
158
158
  memory: hash["memory"],
159
+ score: hash["score"],
159
160
  tuid: hash["tuid"],
160
161
  language: hash["language"],
161
162
  sentence: hash["sentence"],
@@ -15,11 +15,12 @@ module Lara
15
15
  end
16
16
 
17
17
  class NGMemoryMatch < Base
18
- attr_reader :memory, :tuid, :language, :sentence, :translation
18
+ attr_reader :memory, :tuid, :language, :sentence, :translation, :score
19
19
 
20
- def initialize(memory:, language:, sentence:, translation:, tuid: nil)
20
+ def initialize(memory:, language:, sentence:, translation:, tuid: nil, score: nil)
21
21
  super()
22
22
  @memory = memory
23
+ @score = score
23
24
  @tuid = tuid
24
25
  @language = language
25
26
  @sentence = sentence
@@ -221,6 +222,12 @@ module Lara
221
222
  class TextWarnings < Base
222
223
  attr_reader :reasoning, :references, :prosa
223
224
 
225
+ def self.from_hash(hash)
226
+ return nil if hash.nil?
227
+
228
+ new(**hash.transform_keys(&:to_sym).slice(:reasoning, :references, :prosa))
229
+ end
230
+
224
231
  def initialize(reasoning: nil, references: nil, prosa: nil)
225
232
  super()
226
233
  @reasoning = ReasoningFeedback.new(**reasoning.transform_keys(&:to_sym)) if reasoning
@@ -239,10 +246,10 @@ module Lara
239
246
  return nil unless hash.is_a?(Hash)
240
247
 
241
248
  translation = hash["translation"]
242
- if translation.is_a?(Array) && translation.is_a?(Array) && !translation.all?(String)
249
+ if translation.is_a?(Array) && !translation.all?(String)
243
250
  translation = translation.map do |e|
244
251
  TextBlock.new(text: e["text"],
245
- translatable: e["translatable"])
252
+ translatable: e.fetch("translatable", true))
246
253
  end
247
254
  end
248
255
 
@@ -261,7 +268,7 @@ module Lara
261
268
  glossaries_matches: glossaries_matches,
262
269
  profanities: profanities,
263
270
  styleguide_results: styleguide_results,
264
- warnings: hash["warnings"] && TextWarnings.new(**hash["warnings"].transform_keys(&:to_sym))
271
+ warnings: TextWarnings.from_hash(hash["warnings"])
265
272
  )
266
273
  end
267
274
 
@@ -356,6 +363,7 @@ module Lara
356
363
  when "NGMemoryMatch"
357
364
  NGMemoryMatch.new(
358
365
  memory: h["memory"],
366
+ score: h["score"],
359
367
  tuid: h["tuid"],
360
368
  language: h["language"],
361
369
  sentence: h["sentence"],
@@ -9,8 +9,9 @@ module Lara
9
9
  # @param access_key_id [String,nil]
10
10
  # @param access_key_secret [String,nil]
11
11
  # @param base_url [String,nil]
12
- # @param connection_timeout [Integer,nil]
13
- # @param read_timeout [Integer,nil]
12
+ # @param connection_timeout [Integer,nil] Seconds to wait for the HTTP connection to open.
13
+ # @param read_timeout [Integer,nil] Seconds to wait for response data. Single-source evaluations
14
+ # default to 45 minutes. Batch evaluations have no read timeout by default.
14
15
  # @param session_id [String,nil]
15
16
  def initialize(credentials: nil, auth_token: nil, access_key_id: nil, access_key_secret: nil,
16
17
  base_url: nil, connection_timeout: nil, read_timeout: nil, session_id: nil)
@@ -42,12 +43,14 @@ module Lara
42
43
  # @param text [String, Array<String>, Array<Lara::Models::TextBlock>]
43
44
  # @param source [String,nil]
44
45
  # @param source_hint [String,nil]
46
+ # @param strict_languages [Boolean,nil] If true, require exact source and target variants
47
+ # (e.g. en-AU); otherwise, base language support (e.g. en) is sufficient.
45
48
  # @param target [String]
46
49
  # @param adapt_to [Array<String>,nil]
47
50
  # @param glossaries [Array<String>,nil]
48
51
  # @param instructions [Array<String>,nil]
49
52
  # @param content_type [String,nil]
50
- # @param multiline [Boolean]
53
+ # @param multiline [Boolean,nil] Deprecated.
51
54
  # @param timeout_ms [Integer,nil]
52
55
  # @param priority [String,nil]
53
56
  # @param use_cache [String,Boolean,nil]
@@ -56,7 +59,7 @@ module Lara
56
59
  # @param verbose [Boolean]
57
60
  # @param style [String,nil]
58
61
  # @param reasoning [Boolean] When true with a block, yields partial results during reasoning
59
- # @param effort [String,nil] "low", "medium", or "high"; requires reasoning: true. Omit for the server default.
62
+ # @param reasoning_effort [String,nil] "low", "medium", or "high"; requires reasoning: true. Omit for the server default.
60
63
  # @param image_context [Hash,Array<Hash>,nil] One HTTPS or base64 PNG/JPEG/WebP image; requires reasoning when non-empty.
61
64
  # Image validation is performed by the API. An empty array preserves text-only translation.
62
65
  # @param headers [Hash,nil]
@@ -66,12 +69,12 @@ module Lara
66
69
  # @yield [Lara::Models::TextResult] Partial translation result (only when reasoning is true)
67
70
  # @return [Lara::Models::TextResult] Final translation result
68
71
  def translate(text, target:, source: nil, source_hint: nil, adapt_to: nil, glossaries: nil,
69
- instructions: nil, content_type: nil, multiline: true, timeout_ms: nil,
72
+ instructions: nil, content_type: nil, multiline: nil, timeout_ms: nil,
70
73
  priority: nil, use_cache: nil, cache_ttl_s: nil, no_trace: false, verbose: false,
71
- style: nil, reasoning: false, effort: nil, image_context: nil, headers: nil, metadata: nil,
74
+ style: nil, reasoning: false, reasoning_effort: nil, image_context: nil, headers: nil, metadata: nil,
72
75
  profanities_detect: nil, profanities_handling: nil,
73
76
  styleguide_id: nil, styleguide_reasoning: nil,
74
- styleguide_explanation_language: nil, &callback)
77
+ styleguide_explanation_language: nil, strict_languages: nil, &callback)
75
78
  q = normalize_text_input(text)
76
79
 
77
80
  use_cache_value = case use_cache
@@ -85,6 +88,7 @@ module Lara
85
88
  source: source,
86
89
  target: target,
87
90
  source_hint: source_hint,
91
+ strict_languages: strict_languages,
88
92
  content_type: content_type,
89
93
  multiline: multiline,
90
94
  adapt_to: adapt_to,
@@ -97,7 +101,7 @@ module Lara
97
101
  verbose: verbose,
98
102
  style: style,
99
103
  reasoning: reasoning,
100
- effort: effort,
104
+ effort: reasoning_effort,
101
105
  image_context: image_context,
102
106
  metadata: metadata,
103
107
  profanities_detect: profanities_detect,
@@ -119,6 +123,21 @@ module Lara
119
123
  Lara::Models::TextResult.from_hash(result) if result
120
124
  end
121
125
 
126
+ # Evaluates one source or a batch. Each source has source, source_language, target_language,
127
+ # and models (one to four names mapped to hashes with a translation). Optional fields are
128
+ # adapt_to, glossaries, styleguide_id and context_description. An empty adapt_to disables memory matches.
129
+ # @param source [Hash, Array<Hash>]
130
+ # @param reasoning_effort [String,nil] "low", "medium", or "high" for the entire request. The API defaults to "high".
131
+ # @return [Lara::Models::EvaluateResponse] A batch also includes a benchmark summary.
132
+ def evaluate(source = nil, reasoning_effort: nil, **source_fields)
133
+ raise ArgumentError, "supply a source or keyword fields" if source && !source_fields.empty?
134
+
135
+ source ||= source_fields
136
+ body = source.is_a?(Array) ? source.map { |item| evaluate_source_params(item) } : evaluate_source_params(source)
137
+ query = { reasoning_effort: reasoning_effort }.compact
138
+ Models::EvaluateResponse.new(@client.post("/v2/evaluate", body: body, params: query))
139
+ end
140
+
122
141
  # Detects the language of the given text.
123
142
  # @param text [String, Array<String>] Text to detect language for
124
143
  # @param hint [String, nil] Language hint
@@ -189,6 +208,15 @@ module Lara
189
208
 
190
209
  private
191
210
 
211
+ def evaluate_source_params(source)
212
+ params = source.compact
213
+ key = params.key?(:models) ? :models : "models"
214
+ if params[key].is_a?(Hash)
215
+ params[key] = params[key].transform_values { |candidate| candidate.slice("translation", :translation) }
216
+ end
217
+ params
218
+ end
219
+
192
220
  def normalize_text_input(text)
193
221
  case text
194
222
  when String
data/lib/lara/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lara
4
- VERSION = "1.11.1"
4
+ VERSION = "1.12.0"
5
5
  end
data/lib/lara.rb CHANGED
@@ -19,6 +19,7 @@ require_relative "lara/models/base"
19
19
  require_relative "lara/models/collection_result"
20
20
  require_relative "lara/models/resource_query_options"
21
21
  require_relative "lara/models/text"
22
+ require_relative "lara/models/evaluate"
22
23
  require_relative "lara/models/memories"
23
24
  require_relative "lara/models/glossaries"
24
25
  require_relative "lara/models/shares"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lara-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.1
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Translated
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-28 00:00:00.000000000 Z
11
+ date: 2026-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -163,6 +163,7 @@ files:
163
163
  - lib/lara/models/base.rb
164
164
  - lib/lara/models/collection_result.rb
165
165
  - lib/lara/models/documents.rb
166
+ - lib/lara/models/evaluate.rb
166
167
  - lib/lara/models/glossaries.rb
167
168
  - lib/lara/models/images.rb
168
169
  - lib/lara/models/memories.rb