yuno-proofreader 0.1.2 → 0.1.3

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.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/lib/yuno/proofreader/analysis/adjustments.rb +30 -32
  4. data/lib/yuno/proofreader/analysis/crawler.rb +71 -73
  5. data/lib/yuno/proofreader/analysis/entries.rb +52 -54
  6. data/lib/yuno/proofreader/analysis/extractor.rb +162 -164
  7. data/lib/yuno/proofreader/analysis/graph.rb +86 -88
  8. data/lib/yuno/proofreader/analysis/judge.rb +80 -82
  9. data/lib/yuno/proofreader/analysis/judge_prompt.rb +50 -52
  10. data/lib/yuno/proofreader/analysis/judgments.rb +50 -52
  11. data/lib/yuno/proofreader/analysis/links.rb +44 -46
  12. data/lib/yuno/proofreader/analysis/list_order.rb +20 -22
  13. data/lib/yuno/proofreader/analysis/reasons.rb +44 -43
  14. data/lib/yuno/proofreader/analysis/report.rb +79 -81
  15. data/lib/yuno/proofreader/analysis/runner.rb +133 -135
  16. data/lib/yuno/proofreader/analysis/scorer.rb +97 -99
  17. data/lib/yuno/proofreader/analysis/settings.rb +65 -67
  18. data/lib/yuno/proofreader/analysis/signals.rb +77 -79
  19. data/lib/yuno/proofreader/analysis/structured_data.rb +40 -42
  20. data/lib/yuno/proofreader/analysis/url_tree.rb +34 -36
  21. data/lib/yuno/proofreader/analysis/weights.rb +67 -69
  22. data/lib/yuno/proofreader/audit/agenda.rb +63 -65
  23. data/lib/yuno/proofreader/audit/agenda_report.rb +44 -46
  24. data/lib/yuno/proofreader/audit/answers.rb +53 -55
  25. data/lib/yuno/proofreader/audit/attempt.rb +50 -52
  26. data/lib/yuno/proofreader/audit/batch.rb +49 -51
  27. data/lib/yuno/proofreader/audit/batch_report.rb +62 -64
  28. data/lib/yuno/proofreader/audit/cadence.rb +47 -49
  29. data/lib/yuno/proofreader/audit/instructions.rb +15 -17
  30. data/lib/yuno/proofreader/audit/log_fields.rb +16 -18
  31. data/lib/yuno/proofreader/audit/prompt.rb +118 -120
  32. data/lib/yuno/proofreader/audit/report.rb +116 -118
  33. data/lib/yuno/proofreader/audit/runner.rb +150 -152
  34. data/lib/yuno/proofreader/audit/schemas.rb +38 -40
  35. data/lib/yuno/proofreader/audit/steps.rb +46 -48
  36. data/lib/yuno/proofreader/cli/analysis_commands.rb +36 -38
  37. data/lib/yuno/proofreader/cli/audit_commands.rb +89 -91
  38. data/lib/yuno/proofreader/cli/command_log.rb +34 -36
  39. data/lib/yuno/proofreader/cli/flags.rb +33 -35
  40. data/lib/yuno/proofreader/cli/generate_commands.rb +99 -101
  41. data/lib/yuno/proofreader/cli/init_command.rb +123 -125
  42. data/lib/yuno/proofreader/cli/model_commands.rb +35 -37
  43. data/lib/yuno/proofreader/cli/queue_commands.rb +79 -81
  44. data/lib/yuno/proofreader/cli/sheet_commands.rb +42 -44
  45. data/lib/yuno/proofreader/cli.rb +197 -199
  46. data/lib/yuno/proofreader/config.rb +13 -17
  47. data/lib/yuno/proofreader/context.rb +45 -50
  48. data/lib/yuno/proofreader/env_file.rb +52 -56
  49. data/lib/yuno/proofreader/errors.rb +7 -9
  50. data/lib/yuno/proofreader/findings/finding.rb +73 -75
  51. data/lib/yuno/proofreader/findings/matching.rb +21 -23
  52. data/lib/yuno/proofreader/findings/merger.rb +35 -37
  53. data/lib/yuno/proofreader/findings/outline.rb +50 -52
  54. data/lib/yuno/proofreader/findings/plan.rb +14 -16
  55. data/lib/yuno/proofreader/findings/reconciler.rb +170 -172
  56. data/lib/yuno/proofreader/findings/source.rb +54 -56
  57. data/lib/yuno/proofreader/findings/text.rb +24 -26
  58. data/lib/yuno/proofreader/generate/cache.rb +54 -56
  59. data/lib/yuno/proofreader/generate/collector.rb +98 -100
  60. data/lib/yuno/proofreader/generate/describe.rb +18 -20
  61. data/lib/yuno/proofreader/generate/llm/prompt.rb +50 -52
  62. data/lib/yuno/proofreader/generate/llm/reader.rb +84 -86
  63. data/lib/yuno/proofreader/generate/llm/wording.rb +12 -14
  64. data/lib/yuno/proofreader/generate/llms_builder.rb +36 -38
  65. data/lib/yuno/proofreader/generate/llms_report.rb +82 -84
  66. data/lib/yuno/proofreader/generate/llms_txt.rb +99 -101
  67. data/lib/yuno/proofreader/generate/llms_writer.rb +43 -45
  68. data/lib/yuno/proofreader/generate/outline.rb +67 -69
  69. data/lib/yuno/proofreader/generate/passage.rb +41 -43
  70. data/lib/yuno/proofreader/generate/saved_answer.rb +42 -44
  71. data/lib/yuno/proofreader/http/client.rb +94 -96
  72. data/lib/yuno/proofreader/http/net_http_transport.rb +35 -37
  73. data/lib/yuno/proofreader/http/robots.rb +51 -53
  74. data/lib/yuno/proofreader/init/ask.rb +45 -47
  75. data/lib/yuno/proofreader/init/detector.rb +75 -77
  76. data/lib/yuno/proofreader/init/interview.rb +155 -157
  77. data/lib/yuno/proofreader/init/parsing.rb +37 -39
  78. data/lib/yuno/proofreader/init/web.rb +22 -24
  79. data/lib/yuno/proofreader/llm/client.rb +116 -118
  80. data/lib/yuno/proofreader/llm/defaults.rb +10 -12
  81. data/lib/yuno/proofreader/llm/errors.rb +17 -19
  82. data/lib/yuno/proofreader/llm/factory.rb +34 -36
  83. data/lib/yuno/proofreader/llm/handoff.rb +86 -88
  84. data/lib/yuno/proofreader/llm/http_transport.rb +25 -27
  85. data/lib/yuno/proofreader/llm/json_text.rb +12 -14
  86. data/lib/yuno/proofreader/llm/provider.rb +49 -51
  87. data/lib/yuno/proofreader/llm/providers/gemini.rb +111 -113
  88. data/lib/yuno/proofreader/llm/providers/manual.rb +34 -36
  89. data/lib/yuno/proofreader/llm/providers.rb +17 -19
  90. data/lib/yuno/proofreader/llm/types.rb +6 -8
  91. data/lib/yuno/proofreader/llm/usage.rb +8 -10
  92. data/lib/yuno/proofreader/log.rb +88 -96
  93. data/lib/yuno/proofreader/pages/fetcher.rb +53 -55
  94. data/lib/yuno/proofreader/pages/report.rb +37 -39
  95. data/lib/yuno/proofreader/pages/snapshot.rb +32 -34
  96. data/lib/yuno/proofreader/pages/trimmer.rb +23 -25
  97. data/lib/yuno/proofreader/settings_file.rb +35 -38
  98. data/lib/yuno/proofreader/site.rb +52 -54
  99. data/lib/yuno/proofreader/site_reader.rb +122 -124
  100. data/lib/yuno/proofreader/sitemap/aligner.rb +58 -60
  101. data/lib/yuno/proofreader/sitemap/importer.rb +19 -21
  102. data/lib/yuno/proofreader/sitemap/loader.rb +95 -97
  103. data/lib/yuno/proofreader/sitemap/mismatch.rb +53 -55
  104. data/lib/yuno/proofreader/sitemap/pairer.rb +171 -173
  105. data/lib/yuno/proofreader/sitemap/parser.rb +50 -52
  106. data/lib/yuno/proofreader/sitemap/report.rb +72 -74
  107. data/lib/yuno/proofreader/store/cells.rb +53 -57
  108. data/lib/yuno/proofreader/store/gateways/csv.rb +53 -56
  109. data/lib/yuno/proofreader/store/gateways/google.rb +152 -158
  110. data/lib/yuno/proofreader/store/memory.rb +60 -64
  111. data/lib/yuno/proofreader/store/records.rb +18 -20
  112. data/lib/yuno/proofreader/store/schema.rb +85 -89
  113. data/lib/yuno/proofreader/store/sheets.rb +132 -145
  114. data/lib/yuno/proofreader/store/tab_setup.rb +51 -54
  115. data/lib/yuno/proofreader/version.rb +1 -1
  116. data/lib/yuno/proofreader.rb +2 -4
  117. metadata +2 -2
@@ -2,112 +2,110 @@
2
2
 
3
3
  require "fileutils"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Llm
8
- # A manual round trip with a model that runs somewhere else, such as a chat in the browser
9
- class Handoff
10
- DEFAULT_DIR = "tmp/handoff"
11
- PREAMBLE = "Reply with only what the instructions ask for: no introduction and no closing remarks. " \
12
- "Put the whole reply in one code block, so it can be copied exactly."
13
-
14
- # Where the files go unless told otherwise. PROOFREADER_HANDOFF_DIR moves them, which the tests use.
15
- def self.default_dir = ENV.fetch("PROOFREADER_HANDOFF_DIR", DEFAULT_DIR)
16
-
17
- def self.interactive? = true
18
-
19
- attr_reader :prompt_path, :answer_path, :previous_path
20
-
21
- def initialize(dir: self.class.default_dir, input: $stdin, output: $stdout)
22
- @prompt_path = File.join(dir, "prompt.md")
23
- @answer_path = File.join(dir, "answer.md")
24
- @previous_path = File.join(dir, "answer.previous.md")
25
- @input = input
26
- @output = output
27
- end
28
-
29
- # label: what to tell the person to pick in the chat, for example the model name.
30
- def ask(system:, user:, label: nil, check: nil)
31
- FileUtils.mkdir_p(File.dirname(@prompt_path))
32
- keep_previous_answer
33
- File.write(@answer_path, "")
34
- File.write(@prompt_path, document(system, user))
35
- @output.puts "Prompt written to #{@prompt_path}#{" (use #{label})" if label}."
36
- wait(check)
37
- File.read(@answer_path)
38
- end
39
-
40
- # Whether the prompt file is exactly the one these texts would produce, so an answer saved next to it
41
- # belongs to them.
42
- def prompt_written?(system:, user:)
43
- File.file?(@prompt_path) && File.read(@prompt_path) == document(system, user)
44
- end
5
+ module Yuno::Proofreader
6
+ module Llm
7
+ # A manual round trip with a model that runs somewhere else, such as a chat in the browser
8
+ class Handoff
9
+ DEFAULT_DIR = "tmp/handoff"
10
+ PREAMBLE = "Reply with only what the instructions ask for: no introduction and no closing remarks. " \
11
+ "Put the whole reply in one code block, so it can be copied exactly."
12
+
13
+ # Where the files go unless told otherwise. PROOFREADER_HANDOFF_DIR moves them, which the tests use.
14
+ def self.default_dir = ENV.fetch("PROOFREADER_HANDOFF_DIR", DEFAULT_DIR)
15
+
16
+ def self.interactive? = true
17
+
18
+ attr_reader :prompt_path, :answer_path, :previous_path
19
+
20
+ def initialize(dir: self.class.default_dir, input: $stdin, output: $stdout)
21
+ @prompt_path = File.join(dir, "prompt.md")
22
+ @answer_path = File.join(dir, "answer.md")
23
+ @previous_path = File.join(dir, "answer.previous.md")
24
+ @input = input
25
+ @output = output
26
+ end
45
27
 
46
- # Waits for the answer to a prompt that is already written. It writes no prompt and does not empty the answer
47
- # file. An answer that is already there and passes the check is used at once.
48
- def await(check: nil)
49
- FileUtils.mkdir_p(File.dirname(@answer_path))
50
- File.write(@answer_path, "") unless File.file?(@answer_path)
51
- saved = saved_answer(check)
52
- return saved if saved
28
+ # label: what to tell the person to pick in the chat, for example the model name.
29
+ def ask(system:, user:, label: nil, check: nil)
30
+ FileUtils.mkdir_p(File.dirname(@prompt_path))
31
+ keep_previous_answer
32
+ File.write(@answer_path, "")
33
+ File.write(@prompt_path, document(system, user))
34
+ @output.puts "Prompt written to #{@prompt_path}#{" (use #{label})" if label}."
35
+ wait(check)
36
+ File.read(@answer_path)
37
+ end
53
38
 
54
- wait(check)
55
- File.read(@answer_path)
56
- end
39
+ # Whether the prompt file is exactly the one these texts would produce, so an answer saved next to it
40
+ # belongs to them.
41
+ def prompt_written?(system:, user:)
42
+ File.file?(@prompt_path) && File.read(@prompt_path) == document(system, user)
43
+ end
57
44
 
58
- # The text of the prompt file, or nil when there is none.
59
- def prompt_text = (File.read(@prompt_path) if File.file?(@prompt_path))
45
+ # Waits for the answer to a prompt that is already written. It writes no prompt and does not empty the answer
46
+ # file. An answer that is already there and passes the check is used at once.
47
+ def await(check: nil)
48
+ FileUtils.mkdir_p(File.dirname(@answer_path))
49
+ File.write(@answer_path, "") unless File.file?(@answer_path)
50
+ saved = saved_answer(check)
51
+ return saved if saved
60
52
 
61
- private
53
+ wait(check)
54
+ File.read(@answer_path)
55
+ end
62
56
 
63
- def document(system, user)
64
- [PREAMBLE, "## Instructions\n\n#{system}", "## Material\n\n#{user}"].join("\n\n") << "\n"
65
- end
57
+ # The text of the prompt file, or nil when there is none.
58
+ def prompt_text = (File.read(@prompt_path) if File.file?(@prompt_path))
66
59
 
67
- def wait(check)
68
- loop do
69
- @output.puts "Save the reply as #{@answer_path}, then press Enter."
70
- if @input.gets.nil?
71
- raise ProviderError.new("No input to wait on. Run it in an interactive terminal.",
72
- kind: :other)
73
- end
60
+ private
74
61
 
75
- problem = problem_with_answer(check)
76
- return unless problem
62
+ def document(system, user)
63
+ [PREAMBLE, "## Instructions\n\n#{system}", "## Material\n\n#{user}"].join("\n\n") << "\n"
64
+ end
77
65
 
78
- @output.puts problem
66
+ def wait(check)
67
+ loop do
68
+ @output.puts "Save the reply as #{@answer_path}, then press Enter."
69
+ if @input.gets.nil?
70
+ raise ProviderError.new("No input to wait on. Run it in an interactive terminal.",
71
+ kind: :other)
79
72
  end
80
- end
81
73
 
82
- def problem_with_answer(check)
83
- return "#{@answer_path} is missing or empty." unless answered?
74
+ problem = problem_with_answer(check)
75
+ return unless problem
84
76
 
85
- check&.call(File.read(@answer_path))
77
+ @output.puts problem
86
78
  end
79
+ end
87
80
 
88
- def answered?
89
- File.file?(@answer_path) && !File.read(@answer_path).strip.empty?
90
- end
81
+ def problem_with_answer(check)
82
+ return "#{@answer_path} is missing or empty." unless answered?
91
83
 
92
- # The answer that is already in the file, when there is one and it passes the check. A bad one is explained.
93
- def saved_answer(check)
94
- return unless answered?
84
+ check&.call(File.read(@answer_path))
85
+ end
95
86
 
96
- text = File.read(@answer_path)
97
- problem = check&.call(text)
98
- unless problem
99
- @output.puts "Using the answer saved in #{@answer_path}."
100
- return text
101
- end
87
+ def answered?
88
+ File.file?(@answer_path) && !File.read(@answer_path).strip.empty?
89
+ end
102
90
 
103
- @output.puts problem
104
- nil
105
- end
91
+ # The answer that is already in the file, when there is one and it passes the check. A bad one is explained.
92
+ def saved_answer(check)
93
+ return unless answered?
106
94
 
107
- # An answer that was never used is not lost when the next question starts. Only the last one is kept.
108
- def keep_previous_answer
109
- FileUtils.cp(@answer_path, @previous_path) if answered?
95
+ text = File.read(@answer_path)
96
+ problem = check&.call(text)
97
+ unless problem
98
+ @output.puts "Using the answer saved in #{@answer_path}."
99
+ return text
110
100
  end
101
+
102
+ @output.puts problem
103
+ nil
104
+ end
105
+
106
+ # An answer that was never used is not lost when the next question starts. Only the last one is kept.
107
+ def keep_previous_answer
108
+ FileUtils.cp(@answer_path, @previous_path) if answered?
111
109
  end
112
110
  end
113
111
  end
@@ -3,38 +3,36 @@
3
3
  require "net/http"
4
4
  require "uri"
5
5
 
6
- module Yuno
7
- module Proofreader
8
- module Llm
9
- # Sends one request to a model API and returns the answer whatever its status. Network errors are raised
10
- # as they are. The provider turns them into ProviderError.
11
- class HttpTransport
12
- CONNECT_TIMEOUT = 15
6
+ module Yuno::Proofreader
7
+ module Llm
8
+ # Sends one request to a model API and returns the answer whatever its status. Network errors are raised
9
+ # as they are. The provider turns them into ProviderError.
10
+ class HttpTransport
11
+ CONNECT_TIMEOUT = 15
13
12
 
14
- def call(method, url, headers:, timeout:, body: nil)
15
- uri = URI.parse(url)
16
- response = connection(uri, timeout).request(build(method, uri, headers, body))
17
- Http::RawResponse.new(status: response.code.to_i, headers: response.to_hash.transform_values do |v|
18
- v.join(", ")
19
- end,
20
- body: response.body.to_s.dup.force_encoding(Encoding::UTF_8).scrub)
21
- end
13
+ def call(method, url, headers:, timeout:, body: nil)
14
+ uri = URI.parse(url)
15
+ response = connection(uri, timeout).request(build(method, uri, headers, body))
16
+ Http::RawResponse.new(status: response.code.to_i, headers: response.to_hash.transform_values do |v|
17
+ v.join(", ")
18
+ end,
19
+ body: response.body.to_s.dup.force_encoding(Encoding::UTF_8).scrub)
20
+ end
22
21
 
23
- private
22
+ private
24
23
 
25
- def connection(uri, timeout)
26
- Net::HTTP.new(uri.host, uri.port).tap do |http|
27
- http.use_ssl = uri.scheme == "https"
28
- http.open_timeout = CONNECT_TIMEOUT
29
- http.read_timeout = timeout
30
- end
24
+ def connection(uri, timeout)
25
+ Net::HTTP.new(uri.host, uri.port).tap do |http|
26
+ http.use_ssl = uri.scheme == "https"
27
+ http.open_timeout = CONNECT_TIMEOUT
28
+ http.read_timeout = timeout
31
29
  end
30
+ end
32
31
 
33
- def build(method, uri, headers, body)
34
- request = (method == :post ? Net::HTTP::Post : Net::HTTP::Get).new(uri, headers)
35
- request.body = body if body
36
- request
37
- end
32
+ def build(method, uri, headers, body)
33
+ request = (method == :post ? Net::HTTP::Post : Net::HTTP::Get).new(uri, headers)
34
+ request.body = body if body
35
+ request
38
36
  end
39
37
  end
40
38
  end
@@ -2,21 +2,19 @@
2
2
 
3
3
  require "json"
4
4
 
5
- module Yuno
6
- module Proofreader
7
- module Llm
8
- # Reads the JSON a model wrote. Models sometimes wrap it in a markdown code fence.
9
- module JsonText
10
- module_function
5
+ module Yuno::Proofreader
6
+ module Llm
7
+ # Reads the JSON a model wrote. Models sometimes wrap it in a markdown code fence.
8
+ module JsonText
9
+ module_function
11
10
 
12
- def parse(text, finish: nil)
13
- clean = text.to_s.strip.sub(/\A```(?:json)?\s*/i, "").sub(/\s*```\z/, "").strip
14
- JSON.parse(clean)
15
- rescue JSON::ParserError
16
- cut = finish.to_s == "MAX_TOKENS" ? " The answer was cut off at the output limit." : ""
17
- raise AnswerError, "The answer is not valid JSON (finish: #{finish.inspect}).#{cut} " \
18
- "It starts: #{clean[0, 120].inspect}"
19
- end
11
+ def parse(text, finish: nil)
12
+ clean = text.to_s.strip.sub(/\A```(?:json)?\s*/i, "").sub(/\s*```\z/, "").strip
13
+ JSON.parse(clean)
14
+ rescue JSON::ParserError
15
+ cut = finish.to_s == "MAX_TOKENS" ? " The answer was cut off at the output limit." : ""
16
+ raise AnswerError, "The answer is not valid JSON (finish: #{finish.inspect}).#{cut} " \
17
+ "It starts: #{clean[0, 120].inspect}"
20
18
  end
21
19
  end
22
20
  end
@@ -4,57 +4,55 @@ require "net/http"
4
4
  require "openssl"
5
5
  require "timeout"
6
6
 
7
- module Yuno
8
- module Proofreader
9
- module Llm
10
- # What every model provider offers. A provider is a light wrapper around one family of models: it knows
11
- # the address, how the request is written and how errors are reported. The rest (model list, retries,
12
- # JSON handling) is shared. To add a provider, subclass this, define the two class methods and the two
13
- # public methods, and register the class (see providers/gemini.rb).
14
- class Provider
15
- NETWORK_ERRORS = [SocketError, SystemCallError, IOError, Timeout::Error, OpenSSL::SSL::SSLError,
16
- Net::ProtocolError, Net::HTTPBadResponse].freeze
17
- TIMEOUT = 180
18
-
19
- # The name used for it in the settings file, in lowercase.
20
- def self.id
21
- raise NotImplementedError, "#{name} must define .id"
22
- end
23
-
24
- # The environment variable that holds its API key, may be nil
25
- def self.api_key_env
26
- raise NotImplementedError, "#{name} must define .api_key_env"
27
- end
28
-
29
- # Whether a person has to answer each question. A batch of several pages cannot work then.
30
- def self.interactive? = false
31
-
32
- def initialize(api_key:, transport: HttpTransport.new, timeout: TIMEOUT)
33
- @api_key = api_key
34
- @transport = transport
35
- @timeout = timeout
36
- end
37
-
38
- # Sends one request. Returns a Completion, or raises ProviderError saying what kind of failure it was.
39
- def complete(model:, system:, user:, max_output_tokens:, schema: nil)
40
- raise NotImplementedError, "#{self.class.name} must define #complete"
41
- end
42
-
43
- # The names of the models this key can use for text generation.
44
- def models
45
- raise NotImplementedError, "#{self.class.name} must define #models"
46
- end
47
-
48
- # A model name as the provider's API wants it. Providers that write names differently override this.
49
- def normalize(name) = name.to_s
50
-
51
- private
52
-
53
- def request(method, url, headers: {}, body: nil)
54
- @transport.call(method, url, headers:, timeout: @timeout, body:)
55
- rescue *NETWORK_ERRORS => e
56
- raise ProviderError.new("network problem (#{e.class}: #{e.message})", kind: :network)
57
- end
7
+ module Yuno::Proofreader
8
+ module Llm
9
+ # What every model provider offers. A provider is a light wrapper around one family of models: it knows
10
+ # the address, how the request is written and how errors are reported. The rest (model list, retries,
11
+ # JSON handling) is shared. To add a provider, subclass this, define the two class methods and the two
12
+ # public methods, and register the class (see providers/gemini.rb).
13
+ class Provider
14
+ NETWORK_ERRORS = [SocketError, SystemCallError, IOError, Timeout::Error, OpenSSL::SSL::SSLError,
15
+ Net::ProtocolError, Net::HTTPBadResponse].freeze
16
+ TIMEOUT = 180
17
+
18
+ # The name used for it in the settings file, in lowercase.
19
+ def self.id
20
+ raise NotImplementedError, "#{name} must define .id"
21
+ end
22
+
23
+ # The environment variable that holds its API key, may be nil
24
+ def self.api_key_env
25
+ raise NotImplementedError, "#{name} must define .api_key_env"
26
+ end
27
+
28
+ # Whether a person has to answer each question. A batch of several pages cannot work then.
29
+ def self.interactive? = false
30
+
31
+ def initialize(api_key:, transport: HttpTransport.new, timeout: TIMEOUT)
32
+ @api_key = api_key
33
+ @transport = transport
34
+ @timeout = timeout
35
+ end
36
+
37
+ # Sends one request. Returns a Completion, or raises ProviderError saying what kind of failure it was.
38
+ def complete(model:, system:, user:, max_output_tokens:, schema: nil)
39
+ raise NotImplementedError, "#{self.class.name} must define #complete"
40
+ end
41
+
42
+ # The names of the models this key can use for text generation.
43
+ def models
44
+ raise NotImplementedError, "#{self.class.name} must define #models"
45
+ end
46
+
47
+ # A model name as the provider's API wants it. Providers that write names differently override this.
48
+ def normalize(name) = name.to_s
49
+
50
+ private
51
+
52
+ def request(method, url, headers: {}, body: nil)
53
+ @transport.call(method, url, headers:, timeout: @timeout, body:)
54
+ rescue *NETWORK_ERRORS => e
55
+ raise ProviderError.new("network problem (#{e.class}: #{e.message})", kind: :network)
58
56
  end
59
57
  end
60
58
  end