yuno-proofreader 0.1.0 → 0.1.2
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 +16 -0
- data/README.md +169 -58
- data/lib/yuno/proofreader/audit/attempt.rb +11 -4
- data/lib/yuno/proofreader/audit/batch.rb +5 -3
- data/lib/yuno/proofreader/audit/report.rb +6 -1
- data/lib/yuno/proofreader/audit/runner.rb +28 -14
- data/lib/yuno/proofreader/audit/schemas.rb +51 -0
- data/lib/yuno/proofreader/cli/audit_commands.rb +9 -5
- data/lib/yuno/proofreader/cli/command_log.rb +4 -0
- data/lib/yuno/proofreader/cli/generate_commands.rb +133 -0
- data/lib/yuno/proofreader/cli/init_command.rb +24 -10
- data/lib/yuno/proofreader/cli/model_commands.rb +10 -2
- data/lib/yuno/proofreader/cli/queue_commands.rb +11 -1
- data/lib/yuno/proofreader/cli/sheet_commands.rb +4 -1
- data/lib/yuno/proofreader/cli.rb +28 -11
- data/lib/yuno/proofreader/config.rb +3 -3
- data/lib/yuno/proofreader/generate/cache.rb +77 -0
- data/lib/yuno/proofreader/generate/collector.rb +115 -0
- data/lib/yuno/proofreader/generate/describe.rb +29 -0
- data/lib/yuno/proofreader/generate/llm/prompt.rb +62 -0
- data/lib/yuno/proofreader/generate/llm/reader.rb +114 -0
- data/lib/yuno/proofreader/generate/llm/rules.yml +14 -0
- data/lib/yuno/proofreader/generate/llm/wording.rb +20 -0
- data/lib/yuno/proofreader/generate/llms_builder.rb +50 -0
- data/lib/yuno/proofreader/generate/llms_report.rb +112 -0
- data/lib/yuno/proofreader/generate/llms_txt.rb +107 -0
- data/lib/yuno/proofreader/generate/llms_writer.rb +54 -0
- data/lib/yuno/proofreader/generate/outline.rb +91 -0
- data/lib/yuno/proofreader/generate/passage.rb +54 -0
- data/lib/yuno/proofreader/generate/saved_answer.rb +50 -0
- data/lib/yuno/proofreader/llm/client.rb +18 -11
- data/lib/yuno/proofreader/llm/defaults.yml +3 -0
- data/lib/yuno/proofreader/llm/errors.rb +2 -0
- data/lib/yuno/proofreader/llm/factory.rb +9 -6
- data/lib/yuno/proofreader/llm/handoff.rb +114 -0
- data/lib/yuno/proofreader/llm/provider.rb +5 -2
- data/lib/yuno/proofreader/llm/providers/gemini.rb +7 -5
- data/lib/yuno/proofreader/llm/providers/manual.rb +44 -0
- data/lib/yuno/proofreader/llm/providers.rb +4 -10
- data/lib/yuno/proofreader/site.rb +2 -1
- data/lib/yuno/proofreader/site_reader.rb +29 -3
- data/lib/yuno/proofreader/store/cells.rb +13 -3
- data/lib/yuno/proofreader/store/gateways/csv.rb +79 -0
- data/lib/yuno/proofreader/store/sheets.rb +8 -4
- data/lib/yuno/proofreader/version.rb +1 -1
- data/lib/yuno/proofreader.rb +18 -0
- data/proofreader.example.yml +39 -10
- metadata +42 -7
- data/lib/yuno/proofreader/llm/model_commands.rb +0 -34
- /data/lib/yuno/proofreader/store/{google_gateway.rb → gateways/google.rb} +0 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Yuno
|
|
7
|
+
module Proofreader
|
|
8
|
+
module Generate
|
|
9
|
+
# One llms.txt written to the data folder, with the address it is meant to be uploaded to.
|
|
10
|
+
LlmsFile = Data.define(:language, :path, :address, :built)
|
|
11
|
+
|
|
12
|
+
# Writes the llms.txt of each language to the data folder, with links to the files of the other languages.
|
|
13
|
+
class LlmsWriter
|
|
14
|
+
def initialize(site:, builder:)
|
|
15
|
+
@site = site
|
|
16
|
+
@builder = builder
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns the files that were written. A language none of whose pages could be read has no file.
|
|
20
|
+
def call(collected, languages)
|
|
21
|
+
languages.filter_map { |language| write(collected, language) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def write(collected, language)
|
|
27
|
+
return unless collected.readable?(language)
|
|
28
|
+
|
|
29
|
+
built = @builder.call(collected, language:, fallback_title: URI.parse(@site.website).host,
|
|
30
|
+
others: others(collected, language))
|
|
31
|
+
path = File.join(File.expand_path(@site.data_dir), "llms-#{language}.txt")
|
|
32
|
+
write_text(path, built.text)
|
|
33
|
+
LlmsFile.new(language:, path:, address: address(language), built:)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# The llms.txt of the other languages, as [code, address].
|
|
37
|
+
# Only languages whose pages could be read have one.
|
|
38
|
+
def others(collected, language)
|
|
39
|
+
(@site.languages - [language]).select { |code| collected.readable?(code) }
|
|
40
|
+
.map { |code| [code, address(code)] }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def address(language) = "#{@site.website}#{@site.prefixes.fetch(language)}/llms.txt"
|
|
44
|
+
|
|
45
|
+
def write_text(path, text)
|
|
46
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
47
|
+
File.write(path, text, encoding: "UTF-8")
|
|
48
|
+
rescue SystemCallError => e
|
|
49
|
+
raise ConfigError, "Could not write #{path}: #{e.message}"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Yuno
|
|
4
|
+
module Proofreader
|
|
5
|
+
module Generate
|
|
6
|
+
# One page in the outline, with the id the model refers to it by.
|
|
7
|
+
Item = Data.define(:id, :facts)
|
|
8
|
+
|
|
9
|
+
# A group of pages that share the start of their address. hub is the page at the address of the group itself
|
|
10
|
+
# (its index page), when there is one. items are the other pages directly in the group, children the groups
|
|
11
|
+
# below it. Ids are given in the order the file lists things.
|
|
12
|
+
Section = Data.define(:id, :slug, :hub, :items, :children) do
|
|
13
|
+
def all_items = [hub, *items, *children.flat_map(&:all_items)].compact
|
|
14
|
+
|
|
15
|
+
def listed_items = [*items, *children.flat_map(&:listed_items)]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The sections in order of their most important page, and the pages that belong to no section.
|
|
19
|
+
Outline = Data.define(:sections, :loose) do
|
|
20
|
+
def all_sections = sections.flat_map { |section| [section, *section.children] }
|
|
21
|
+
|
|
22
|
+
def items = [*sections.flat_map(&:all_items), *loose]
|
|
23
|
+
|
|
24
|
+
def listed = [*sections.flat_map(&:listed_items), *loose]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Groups pages by the start of their address. The first part of the path makes a section and the second a
|
|
28
|
+
# subsection, and a group needs at least two pages to get a heading of its own. Nothing here asks a model:
|
|
29
|
+
# the addresses of a site already say what belongs together.
|
|
30
|
+
class Grouper
|
|
31
|
+
MIN_GROUP = 2
|
|
32
|
+
Row = Data.define(:facts, :rank, :segments)
|
|
33
|
+
|
|
34
|
+
def self.call(entries) = new(entries).call
|
|
35
|
+
|
|
36
|
+
# entries: Facts, most important first.
|
|
37
|
+
def initialize(entries)
|
|
38
|
+
rows = entries.each_with_index.map do |facts, index|
|
|
39
|
+
Row.new(facts:, rank: index, segments: segments_of(facts.path))
|
|
40
|
+
end
|
|
41
|
+
@rows = rows.reject { |row| row.segments.empty? }.uniq(&:segments)
|
|
42
|
+
@counters = Hash.new(0)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def call
|
|
46
|
+
sections = []
|
|
47
|
+
loose = []
|
|
48
|
+
@rows.group_by { |row| row.segments.first }.each do |slug, group|
|
|
49
|
+
group.size >= MIN_GROUP ? sections << top(slug, group) : loose.concat(group)
|
|
50
|
+
end
|
|
51
|
+
Outline.new(sections:, loose: loose.map { |row| item(row) })
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def segments_of(path) = path.split("/").reject(&:empty?)
|
|
57
|
+
|
|
58
|
+
def next_id(prefix) = "#{prefix}#{@counters[prefix] += 1}"
|
|
59
|
+
|
|
60
|
+
def item(row) = Item.new(id: next_id("p"), facts: row.facts)
|
|
61
|
+
|
|
62
|
+
def split_hub(rows, depth)
|
|
63
|
+
hub = rows.find { |row| row.segments.size == depth }
|
|
64
|
+
[hub, rows - [hub]]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def top(slug, group)
|
|
68
|
+
hub, rest = split_hub(group, 1)
|
|
69
|
+
nested, flat = subgroups(rest)
|
|
70
|
+
id = next_id("s")
|
|
71
|
+
Section.new(id:, slug:, hub: (item(hub) if hub), items: items_of(flat.flat_map(&:last)),
|
|
72
|
+
children: nested.map { |name, rows| leaf(name, rows) })
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The pages below a section: the groups that get a heading of their own, and the single pages that do not.
|
|
76
|
+
def subgroups(rows)
|
|
77
|
+
rows.group_by { |row| row.segments[1] }.partition { |_name, group| group.size >= MIN_GROUP }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def items_of(rows) = rows.sort_by(&:rank).map { |row| item(row) }
|
|
81
|
+
|
|
82
|
+
def leaf(slug, rows)
|
|
83
|
+
hub, rest = split_hub(rows, 2)
|
|
84
|
+
id = next_id("s")
|
|
85
|
+
hub_item = hub && item(hub)
|
|
86
|
+
Section.new(id:, slug:, hub: hub_item, items: rest.sort_by(&:rank).map { |row| item(row) }, children: [])
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "nokogiri"
|
|
4
|
+
|
|
5
|
+
module Yuno
|
|
6
|
+
module Proofreader
|
|
7
|
+
module Generate
|
|
8
|
+
# The text of a page as the model gets it: its paragraphs, whole and in order, outside the header, the
|
|
9
|
+
# navigation and the footer. Nothing is cut inside a paragraph. With a budget, paragraphs are taken from the
|
|
10
|
+
# start while they fit, and the first one is always taken, however long it is. cut says whether some were left.
|
|
11
|
+
module Passage
|
|
12
|
+
Body = Data.define(:paragraphs, :cut)
|
|
13
|
+
|
|
14
|
+
MIN_PARAGRAPH = 40
|
|
15
|
+
# Characters of text handed over for a page of each tier. It keeps the request small and says nothing about
|
|
16
|
+
# how much the model may write. A page of a lower tier, or one the analysis has not ranked, gets the smallest.
|
|
17
|
+
BUDGETS = { 1 => 4_000, 2 => 2_500, 3 => 1_500 }.freeze
|
|
18
|
+
DEFAULT_BUDGET = 800
|
|
19
|
+
MAIN = "main, [role=main]"
|
|
20
|
+
OUTSIDE_CHROME = "not(ancestor::header or ancestor::nav or ancestor::footer or ancestor::aside or " \
|
|
21
|
+
"ancestor::*[@role='banner' or @role='navigation' or @role='contentinfo'])"
|
|
22
|
+
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
def budget(tier) = BUDGETS.fetch(tier, DEFAULT_BUDGET)
|
|
26
|
+
|
|
27
|
+
# budget: characters to hand over, or nil for all of the text.
|
|
28
|
+
def call(html, budget: nil)
|
|
29
|
+
paragraphs = paragraphs_of(html)
|
|
30
|
+
return Body.new(paragraphs:, cut: false) unless budget
|
|
31
|
+
|
|
32
|
+
taken = []
|
|
33
|
+
used = 0
|
|
34
|
+
paragraphs.each do |paragraph|
|
|
35
|
+
break if !taken.empty? && used + paragraph.length > budget
|
|
36
|
+
|
|
37
|
+
taken << paragraph
|
|
38
|
+
used += paragraph.length
|
|
39
|
+
end
|
|
40
|
+
Body.new(paragraphs: taken, cut: taken.size < paragraphs.size)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def paragraphs_of(html)
|
|
44
|
+
doc = Nokogiri::HTML(html.to_s)
|
|
45
|
+
scope = doc.at_css(MAIN) || doc.at_css("body")
|
|
46
|
+
return [] unless scope
|
|
47
|
+
|
|
48
|
+
texts = scope.xpath(".//p[#{OUTSIDE_CHROME}]").map { |node| Findings::Text.collapse(node.text) }
|
|
49
|
+
texts.select { |text| text.length >= MIN_PARAGRAPH }.uniq
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Yuno
|
|
4
|
+
module Proofreader
|
|
5
|
+
module Generate
|
|
6
|
+
# Stands in for the model client when the answer may already be on disk, as with the manual provider after an
|
|
7
|
+
# earlier run. If the prompt file holds exactly the prompt it is asked for, it uses the answer file (or waits for
|
|
8
|
+
# it) instead of asking again. Otherwise it asks as usual: a new prompt is written, and an answer that was in the
|
|
9
|
+
# answer file is kept as answer.previous.md.
|
|
10
|
+
class SavedAnswer
|
|
11
|
+
LANGUAGE_IN_PROMPT = /## Material\s*\{"language":"([^"]+)"/
|
|
12
|
+
|
|
13
|
+
def initialize(handoff:, model:, say: ->(_message) {})
|
|
14
|
+
@handoff = handoff
|
|
15
|
+
@model = model
|
|
16
|
+
@say = say
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# The languages, with the one whose prompt is in the prompt file first, so that its answer is used before
|
|
20
|
+
# another question replaces it.
|
|
21
|
+
def order(languages)
|
|
22
|
+
text = @handoff.prompt_text
|
|
23
|
+
saved = text && text[LANGUAGE_IN_PROMPT, 1]
|
|
24
|
+
languages.include?(saved) ? [saved, *(languages - [saved])] : languages
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def generate(system:, user:, **)
|
|
28
|
+
Llm::Answer.new(data: Llm::JsonText.parse(text_for(system, user)), model: @model, finish: "STOP",
|
|
29
|
+
input_tokens: 0, output_tokens: 0, thinking_tokens: 0, seconds: 0.0)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def text_for(system, user)
|
|
35
|
+
return @handoff.await(check: method(:problem_with)) if @handoff.prompt_written?(system:, user:)
|
|
36
|
+
|
|
37
|
+
@say.call("#{@handoff.prompt_path} is not the prompt for this language, so a new prompt is written.")
|
|
38
|
+
@handoff.ask(system:, user:, label: @model, check: method(:problem_with))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def problem_with(text)
|
|
42
|
+
Llm::JsonText.parse(text)
|
|
43
|
+
nil
|
|
44
|
+
rescue Llm::AnswerError => e
|
|
45
|
+
"#{e.message} Fix the file and press Enter, or press Ctrl-C to stop."
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -4,9 +4,7 @@ module Yuno
|
|
|
4
4
|
module Proofreader
|
|
5
5
|
module Llm
|
|
6
6
|
# Asks a list of models in turn until one answers with JSON. A model that is over its limit or does not
|
|
7
|
-
# exist is skipped for the rest of the run
|
|
8
|
-
# repeated after a pause. It knows nothing about a particular provider or model. What goes wrong is written
|
|
9
|
-
# to the log as warnings, and each answer, with its tokens, at debug level.
|
|
7
|
+
# exist is skipped for the rest of the run
|
|
10
8
|
class Client
|
|
11
9
|
RETRY_ROUNDS = 3
|
|
12
10
|
RETRY_WAIT = 20
|
|
@@ -36,17 +34,16 @@ module Yuno
|
|
|
36
34
|
# Whether there is a model other than this one, for a second opinion that is independent of the first.
|
|
37
35
|
def independent_of?(model) = @models.any? { |name| name != model }
|
|
38
36
|
|
|
39
|
-
|
|
40
|
-
def generate(system:, user:, avoid: [])
|
|
37
|
+
def generate(system:, user:, avoid: [], schema: nil)
|
|
41
38
|
failures = []
|
|
42
39
|
RETRY_ROUNDS.times do |round|
|
|
43
|
-
outcome = round_of(order(avoid), system, user, failures)
|
|
40
|
+
outcome = round_of(order(avoid), system, user, schema, failures)
|
|
44
41
|
return outcome if outcome.is_a?(Answer)
|
|
45
42
|
break unless outcome == :transient && round < RETRY_ROUNDS - 1
|
|
46
43
|
|
|
47
44
|
pause(round)
|
|
48
45
|
end
|
|
49
|
-
raise
|
|
46
|
+
raise exhausted(failures)
|
|
50
47
|
end
|
|
51
48
|
|
|
52
49
|
private
|
|
@@ -63,12 +60,12 @@ module Yuno
|
|
|
63
60
|
end
|
|
64
61
|
|
|
65
62
|
# Tries every model once. Returns the Answer, or :transient when waiting might help, or nil.
|
|
66
|
-
def round_of(models, system, user, failures)
|
|
63
|
+
def round_of(models, system, user, schema, failures)
|
|
67
64
|
transient = false
|
|
68
65
|
models.each do |model|
|
|
69
66
|
next if skipped?(model)
|
|
70
67
|
|
|
71
|
-
result = attempt(model, system, user, failures)
|
|
68
|
+
result = attempt(model, system, user, schema, failures)
|
|
72
69
|
return result if result.is_a?(Answer)
|
|
73
70
|
|
|
74
71
|
transient ||= result == :transient
|
|
@@ -76,9 +73,10 @@ module Yuno
|
|
|
76
73
|
transient ? :transient : nil
|
|
77
74
|
end
|
|
78
75
|
|
|
79
|
-
def attempt(model, system, user, failures)
|
|
76
|
+
def attempt(model, system, user, schema, failures)
|
|
80
77
|
started = @clock.call
|
|
81
|
-
|
|
78
|
+
extra = schema ? { schema: } : {}
|
|
79
|
+
completion = @provider.complete(model:, system:, user:, max_output_tokens: @max_output_tokens, **extra)
|
|
82
80
|
log_answer(answer_from(model, completion, @clock.call - started))
|
|
83
81
|
rescue AnswerError => e
|
|
84
82
|
failures << Failure.new(model:, message: e.message, kind: :answer)
|
|
@@ -134,6 +132,15 @@ module Yuno
|
|
|
134
132
|
|
|
135
133
|
" Run yuno-proofreader models to see which models your key can use, then set models in proofreader.yml."
|
|
136
134
|
end
|
|
135
|
+
|
|
136
|
+
# Every model answered, but none of the answers could be used: that is presumably a problem of the one page,
|
|
137
|
+
# not of the models or the key
|
|
138
|
+
def exhausted(failures)
|
|
139
|
+
message = unavailable_message(failures)
|
|
140
|
+
return UnusableAnswersError.new(message) if failures.any? { |failure| failure.kind == :answer }
|
|
141
|
+
|
|
142
|
+
UnavailableError.new(message)
|
|
143
|
+
end
|
|
137
144
|
end
|
|
138
145
|
end
|
|
139
146
|
end
|
|
@@ -8,16 +8,19 @@ module Yuno
|
|
|
8
8
|
Selection = Data.define(:names, :source)
|
|
9
9
|
|
|
10
10
|
class << self
|
|
11
|
-
# The provider of the site, with its API key from the environment.
|
|
12
11
|
def provider_for(site, env: ENV, transport: nil)
|
|
13
12
|
klass = Providers.resolve(site.provider)
|
|
13
|
+
key = api_key_from(klass, env)
|
|
14
|
+
klass.new(api_key: key, **(transport ? { transport: } : {}))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def api_key_from(klass, env)
|
|
18
|
+
return nil unless klass.api_key_env
|
|
19
|
+
|
|
14
20
|
key = env[klass.api_key_env].to_s.strip
|
|
15
|
-
|
|
16
|
-
raise ConfigError,
|
|
17
|
-
"Missing environment variable #{klass.api_key_env}: your #{klass.id} API key."
|
|
18
|
-
end
|
|
21
|
+
return key unless key.empty?
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
raise ConfigError, "Missing environment variable #{klass.api_key_env}: your #{klass.id} API key."
|
|
21
24
|
end
|
|
22
25
|
|
|
23
26
|
# The settings win. For the second review, review_models win over models. Without any, the provider's
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
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
|
|
45
|
+
|
|
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
|
|
53
|
+
|
|
54
|
+
wait(check)
|
|
55
|
+
File.read(@answer_path)
|
|
56
|
+
end
|
|
57
|
+
|
|
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))
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def document(system, user)
|
|
64
|
+
[PREAMBLE, "## Instructions\n\n#{system}", "## Material\n\n#{user}"].join("\n\n") << "\n"
|
|
65
|
+
end
|
|
66
|
+
|
|
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
|
|
74
|
+
|
|
75
|
+
problem = problem_with_answer(check)
|
|
76
|
+
return unless problem
|
|
77
|
+
|
|
78
|
+
@output.puts problem
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def problem_with_answer(check)
|
|
83
|
+
return "#{@answer_path} is missing or empty." unless answered?
|
|
84
|
+
|
|
85
|
+
check&.call(File.read(@answer_path))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def answered?
|
|
89
|
+
File.file?(@answer_path) && !File.read(@answer_path).strip.empty?
|
|
90
|
+
end
|
|
91
|
+
|
|
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?
|
|
95
|
+
|
|
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
|
|
102
|
+
|
|
103
|
+
@output.puts problem
|
|
104
|
+
nil
|
|
105
|
+
end
|
|
106
|
+
|
|
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?
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -21,11 +21,14 @@ module Yuno
|
|
|
21
21
|
raise NotImplementedError, "#{name} must define .id"
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
# The environment variable that holds its API key
|
|
24
|
+
# The environment variable that holds its API key, may be nil
|
|
25
25
|
def self.api_key_env
|
|
26
26
|
raise NotImplementedError, "#{name} must define .api_key_env"
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# Whether a person has to answer each question. A batch of several pages cannot work then.
|
|
30
|
+
def self.interactive? = false
|
|
31
|
+
|
|
29
32
|
def initialize(api_key:, transport: HttpTransport.new, timeout: TIMEOUT)
|
|
30
33
|
@api_key = api_key
|
|
31
34
|
@transport = transport
|
|
@@ -33,7 +36,7 @@ module Yuno
|
|
|
33
36
|
end
|
|
34
37
|
|
|
35
38
|
# Sends one request. Returns a Completion, or raises ProviderError saying what kind of failure it was.
|
|
36
|
-
def complete(model:, system:, user:, max_output_tokens:)
|
|
39
|
+
def complete(model:, system:, user:, max_output_tokens:, schema: nil)
|
|
37
40
|
raise NotImplementedError, "#{self.class.name} must define #complete"
|
|
38
41
|
end
|
|
39
42
|
|
|
@@ -19,8 +19,8 @@ module Yuno
|
|
|
19
19
|
|
|
20
20
|
def self.api_key_env = "GEMINI_API_KEY"
|
|
21
21
|
|
|
22
|
-
def complete(model:, system:, user:, max_output_tokens:)
|
|
23
|
-
body = JSON.generate(payload(system, user, max_output_tokens))
|
|
22
|
+
def complete(model:, system:, user:, max_output_tokens:, schema: nil)
|
|
23
|
+
body = JSON.generate(payload(model, system, user, max_output_tokens, schema))
|
|
24
24
|
url = "#{BASE}/models/#{normalize(model)}:generateContent"
|
|
25
25
|
response = request(:post, url, headers: auth_headers.merge("Content-Type" => "application/json"), body:)
|
|
26
26
|
raise failure(response) unless response.status == 200
|
|
@@ -46,12 +46,14 @@ module Yuno
|
|
|
46
46
|
|
|
47
47
|
def auth_headers = { "x-goog-api-key" => @api_key }
|
|
48
48
|
|
|
49
|
-
def payload(system, user, max_output_tokens)
|
|
49
|
+
def payload(model, system, user, max_output_tokens, schema)
|
|
50
|
+
config = { responseMimeType: "application/json", maxOutputTokens: max_output_tokens }
|
|
51
|
+
config[:temperature] = 0 if normalize(model).start_with?("gemini-2")
|
|
52
|
+
config[:responseJsonSchema] = schema if schema
|
|
50
53
|
{
|
|
51
54
|
systemInstruction: { parts: [{ text: system }] },
|
|
52
55
|
contents: [{ role: "user", parts: [{ text: user }] }],
|
|
53
|
-
generationConfig:
|
|
54
|
-
maxOutputTokens: max_output_tokens }
|
|
56
|
+
generationConfig: config
|
|
55
57
|
}
|
|
56
58
|
end
|
|
57
59
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Yuno
|
|
4
|
+
module Proofreader
|
|
5
|
+
module Llm
|
|
6
|
+
# The model providers that are installed. Each wrapper registers itself when it is loaded.
|
|
7
|
+
module Providers
|
|
8
|
+
# Prompt goes to a file instead of the API, a person asks a model by hand and saves the answer to another file.
|
|
9
|
+
class Manual < Provider
|
|
10
|
+
def self.id = "manual"
|
|
11
|
+
|
|
12
|
+
def self.api_key_env = nil
|
|
13
|
+
|
|
14
|
+
def self.interactive? = true
|
|
15
|
+
|
|
16
|
+
def initialize(handoff: Handoff.new, api_key: nil, **)
|
|
17
|
+
super(api_key:, **)
|
|
18
|
+
@handoff = handoff
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# No output limit
|
|
22
|
+
def complete(model:, system:, user:, **)
|
|
23
|
+
text = @handoff.ask(system:, user:, label: model, check: method(:problem_with))
|
|
24
|
+
Completion.new(text:, finish: "STOP", input_tokens: 0, output_tokens: 0, thinking_tokens: 0)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def models = []
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
# The client reads every answer as JSON, so a bad paste is caught here and awaited upon
|
|
32
|
+
def problem_with(text)
|
|
33
|
+
JsonText.parse(text)
|
|
34
|
+
nil
|
|
35
|
+
rescue AnswerError => e
|
|
36
|
+
"#{e.message} Fix the file and press Enter, or press Ctrl-C to stop."
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
register(Manual)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -5,6 +5,8 @@ module Yuno
|
|
|
5
5
|
module Llm
|
|
6
6
|
# The providers that are installed. Each wrapper registers itself when it is loaded.
|
|
7
7
|
module Providers
|
|
8
|
+
DEFAULT_ID = "manual"
|
|
9
|
+
|
|
8
10
|
@registry = {}
|
|
9
11
|
|
|
10
12
|
class << self
|
|
@@ -14,23 +16,15 @@ module Yuno
|
|
|
14
16
|
|
|
15
17
|
def ids = @registry.keys
|
|
16
18
|
|
|
17
|
-
# The provider named in the settings. Without a name, the
|
|
19
|
+
# The provider named in the settings. Without a name, the manual one: it needs no account or key.
|
|
18
20
|
def resolve(id)
|
|
19
21
|
name = id.to_s.strip.downcase
|
|
20
|
-
|
|
22
|
+
name = DEFAULT_ID if name.empty?
|
|
21
23
|
|
|
22
24
|
@registry.fetch(name) do
|
|
23
25
|
raise ConfigError, "Site: unknown provider #{name.inspect}. Available: #{ids.join(', ')}"
|
|
24
26
|
end
|
|
25
27
|
end
|
|
26
|
-
|
|
27
|
-
private
|
|
28
|
-
|
|
29
|
-
def only_provider
|
|
30
|
-
return @registry.values.first if @registry.size == 1
|
|
31
|
-
|
|
32
|
-
raise ConfigError, "Site: set provider to one of: #{ids.join(', ')}"
|
|
33
|
-
end
|
|
34
28
|
end
|
|
35
29
|
end
|
|
36
30
|
end
|
|
@@ -7,7 +7,8 @@ module Yuno
|
|
|
7
7
|
module Proofreader
|
|
8
8
|
# What you tell the tool about your website. Read from a YAML file.
|
|
9
9
|
Site = Data.define(:website, :prefixes, :language_notes, :sitemap_url, :default_language_only_paths,
|
|
10
|
-
:skip_paths, :provider, :models, :review_models, :
|
|
10
|
+
:skip_paths, :provider, :models, :review_models, :review, :store, :data_dir, :context,
|
|
11
|
+
:analysis, :cadence) do
|
|
11
12
|
def self.load(path)
|
|
12
13
|
raise ConfigError, "Site file not found: #{path}" unless File.file?(path)
|
|
13
14
|
|