llmed 0.6.2 → 0.7.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/README.md +9 -3
- data/exe/llmed +14 -16
- data/exe/llmed.literate +11 -14
- data/lib/llm.rb +153 -65
- data/lib/llmed/application.rb +8 -14
- data/lib/llmed/configuration.rb +37 -33
- data/lib/llmed/goal.rb +2 -1
- data/lib/llmed/literate_programming/markdown.rb +3 -3
- data/lib/llmed/literate_programming.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3026654d540610fff4f26af6cded11dcfe9804fbd227449c27767602e5934f28
|
|
4
|
+
data.tar.gz: fb4e763db206fd9e155c2f575964ea743f2ce4a5c05b5e9a5cf5316e18aa0cc8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bf9268253d05f21f35b5218a15fe837aa6e9c9bf72c15c70424251960cdc603acdd1a163545fcf9afd018739536d81af546a59ac8998c50014bc2dccb997498a
|
|
7
|
+
data.tar.gz: 861a82ae735f4374905732f9eb396ffd5cf5348c0f862643b3fd38702e6cca52b0ce760f2f1a7e73861057d4299c6df0529de9731026111ca55d84d890a13741
|
data/README.md
CHANGED
|
@@ -43,12 +43,18 @@ end
|
|
|
43
43
|
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
Alternatively, you can compile by asking the local [OpenCode](https://github.com/sst/opencode) CLI (`opencode` on PATH; override with `options: {bin:}`). The `options:` hash also supports `{dir:}` (sandbox directory passed as `--dir`), `{timeout:}` (seconds) and `{flags:}` (extra CLI flags). The `api_key:` is unused, OpenCode uses its own auth.
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
set_llm provider: :opencode, api_key: nil, model: 'opencode-go/glm-5.3-flash'
|
|
50
|
+
```
|
|
51
|
+
|
|
46
52
|
Since version 0.4.0, literate programming was introduced, and it is now possible to write code using a markdown-like syntax.
|
|
47
53
|
|
|
48
54
|
```md
|
|
49
|
-
#!language ruby
|
|
55
|
+
#!environment language ruby
|
|
50
56
|
#% increase release once you agree with the change
|
|
51
|
-
#!environment release
|
|
57
|
+
#!environment release 1
|
|
52
58
|
#!environment output_file minicounter.rb
|
|
53
59
|
|
|
54
60
|
# Dependencies
|
|
@@ -120,7 +126,7 @@ So, programming with LLMed means being aware of the technology (programming lang
|
|
|
120
126
|
# Usage Development
|
|
121
127
|
|
|
122
128
|
* `bundle3.1 install --path vendor/`
|
|
123
|
-
* `OPENAI_API_KEY=xxx rake llmed[examples/tictactoe.
|
|
129
|
+
* `OPENAI_API_KEY=xxx rake llmed[examples-v0.2.0/tictactoe.llmed]`
|
|
124
130
|
|
|
125
131
|
# Interesting
|
|
126
132
|
|
data/exe/llmed
CHANGED
|
@@ -1,32 +1,31 @@
|
|
|
1
1
|
#!/bin/env ruby
|
|
2
2
|
# Copyright 2025 Jovany Leandro G.C <bit4bit@riseup.net>
|
|
3
3
|
# frozen_string_literal: true
|
|
4
|
-
|
|
5
4
|
require 'optparse'
|
|
6
5
|
require 'llmed'
|
|
7
6
|
|
|
8
7
|
logger = Logger.new(STDERR)
|
|
9
8
|
output_dir = './llmed-out'
|
|
10
9
|
release_dir = output_dir
|
|
11
|
-
template =
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
end
|
|
10
|
+
template = <<-TMP
|
|
11
|
+
set_llm provider: :openai, api_key: ENV['OPENAI_API_KEY'], model: 'gpt-4o'
|
|
12
|
+
|
|
13
|
+
# Increment the RELEASE number once you approve the output.
|
|
14
|
+
application "hi world", release: nil, language: '<HERE LANGUAGE>', output_file: "<HERE NAME>.ollmed" do
|
|
15
|
+
context "main" do
|
|
16
|
+
<<-LLM
|
|
17
|
+
Show to user 'hi world!'.
|
|
18
|
+
LLM
|
|
21
19
|
end
|
|
20
|
+
end
|
|
22
21
|
TMP
|
|
23
22
|
|
|
24
23
|
OptionParser.new do |parser|
|
|
25
24
|
parser.banner = "Usage: llmed [options] <application file .llmed or stdin>"
|
|
26
25
|
parser.on_tail("-h", "--help", "Show this message") do
|
|
27
26
|
puts parser
|
|
28
|
-
puts "\n# Website\nhttps://github.com/bit4bit/
|
|
29
|
-
puts "\n# Examples\nhttps://github.com/bit4bit/
|
|
27
|
+
puts "\n# Website\nhttps://github.com/bit4bit/llmed"
|
|
28
|
+
puts "\n# Examples\nhttps://github.com/bit4bit/llmed/tree/main/examples"
|
|
30
29
|
exit
|
|
31
30
|
end
|
|
32
31
|
|
|
@@ -48,10 +47,9 @@ OptionParser.new do |parser|
|
|
|
48
47
|
end
|
|
49
48
|
end.parse!
|
|
50
49
|
|
|
50
|
+
source_file = ARGV.first
|
|
51
51
|
source_code = ARGF.read
|
|
52
|
-
|
|
53
|
-
release_dir = File.dirname(ARGF.path)
|
|
54
|
-
end
|
|
52
|
+
release_dir = File.dirname(source_file) unless source_file.nil?
|
|
55
53
|
|
|
56
54
|
llmed = LLMed.new(logger: logger, output_dir: output_dir, release_dir: release_dir)
|
|
57
55
|
llmed.eval_source source_code
|
data/exe/llmed.literate
CHANGED
|
@@ -16,7 +16,7 @@ output_file = 'a.out.ollmed'
|
|
|
16
16
|
|
|
17
17
|
template = <<~TMP
|
|
18
18
|
#% Minimal example
|
|
19
|
-
#% llm_provider : like_openai | openai | anthropic
|
|
19
|
+
#% llm_provider : like_openai | openai | anthropic | opencode
|
|
20
20
|
#!environment llm_provider like_openai
|
|
21
21
|
#% llm_provider_model : depends provider
|
|
22
22
|
#!environment llm_provider_model Qwen/Qwen2.5-Coder-32B-Instruct
|
|
@@ -34,15 +34,15 @@ OptionParser.new do |parser|
|
|
|
34
34
|
parser.banner = "Usage: llmed.literate [options] <application file .llmed.literate or stdin>"
|
|
35
35
|
parser.on_tail("-h", "--help", "Show this message") do
|
|
36
36
|
puts parser
|
|
37
|
-
puts "\n# Website\nhttps://github.com/bit4bit/
|
|
38
|
-
puts "\n# Examples\nhttps://github.com/bit4bit/
|
|
37
|
+
puts "\n# Website\nhttps://github.com/bit4bit/llmed"
|
|
38
|
+
puts "\n# Examples\nhttps://github.com/bit4bit/llmed/tree/main/examples"
|
|
39
39
|
puts "\n# Current Environment\nLLMED_PROVIDER_API_KEY\nLLMED_PROVIDER_MODEL=#{provider_model}\nLLMED_PROVIDER_URI_BASE=#{provider_uri_base}"
|
|
40
40
|
puts <<~DOC
|
|
41
41
|
|
|
42
42
|
# A minimal example
|
|
43
43
|
<!--
|
|
44
44
|
#% Minimal example
|
|
45
|
-
#% llm_provider : like_openai | openai | anthropic
|
|
45
|
+
#% llm_provider : like_openai | openai | anthropic | opencode
|
|
46
46
|
#!environment llm_provider like_openai
|
|
47
47
|
#% llm_provider_model : depends provider
|
|
48
48
|
#!environment llm_provider_model Qwen/Qwen2.5-Coder-32B-Instruct
|
|
@@ -85,28 +85,25 @@ OptionParser.new do |parser|
|
|
|
85
85
|
end
|
|
86
86
|
end.parse!
|
|
87
87
|
|
|
88
|
+
source_file = ARGV.first
|
|
88
89
|
source_code = ARGF.read
|
|
89
|
-
|
|
90
|
-
release_dir = File.dirname(ARGF.path)
|
|
91
|
-
end
|
|
90
|
+
release_dir = File.dirname(source_file) unless source_file.nil?
|
|
92
91
|
|
|
93
92
|
llmed = LLMed.new(logger: logger, output_dir: output_dir, release_dir: release_dir)
|
|
94
93
|
llmed.set_language language
|
|
95
94
|
LLMed::LiterateProgramming.execute(source_code, output_file: output_file) do |contexts, application_args, environment|
|
|
96
95
|
environment.each do |k, v|
|
|
97
96
|
case k
|
|
98
|
-
when
|
|
99
|
-
provider = v
|
|
100
|
-
when
|
|
97
|
+
when :llm_provider
|
|
98
|
+
provider = v.to_sym
|
|
99
|
+
when :llm_provider_model
|
|
101
100
|
provider_model = v
|
|
102
|
-
when
|
|
101
|
+
when :llm_provider_uri_base
|
|
103
102
|
provider_uri_base = v
|
|
104
|
-
when 'language'
|
|
105
|
-
language = v
|
|
106
103
|
end
|
|
107
104
|
end
|
|
108
105
|
|
|
109
|
-
if provider_api_key.strip.empty?
|
|
106
|
+
if provider_api_key.strip.empty? && !%i[opencode test].include?(provider)
|
|
110
107
|
logger.error "LLMED_PROVIDER_API_KEY is not set. Please set the environment variable and try again."
|
|
111
108
|
exit 1
|
|
112
109
|
end
|
data/lib/llm.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require 'openai'
|
|
2
2
|
require 'langchain'
|
|
3
|
+
require 'open3'
|
|
3
4
|
|
|
4
5
|
Langchain.logger.level = Logger::ERROR
|
|
5
6
|
|
|
@@ -18,45 +19,19 @@ class LLMed
|
|
|
18
19
|
|
|
19
20
|
Response = Struct.new(:provider, :model, :source_code, :duration_seconds, :total_tokens, keyword_init: true)
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
def
|
|
23
|
-
|
|
24
|
-
@assistant = Langchain::Assistant.new(
|
|
25
|
-
llm: @llm_instance.llm
|
|
26
|
-
)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def chat(messages: [])
|
|
30
|
-
llm_messages = messages.map do |m|
|
|
31
|
-
case m
|
|
32
|
-
when Message::System
|
|
33
|
-
{ role: 'system', content: m.content }
|
|
34
|
-
when Message::User
|
|
35
|
-
{ role: 'user', content: m.content }
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
start = Time.now
|
|
40
|
-
|
|
41
|
-
@assistant.add_messages(messages: llm_messages)
|
|
42
|
-
assistant_messages = @assistant.run!
|
|
43
|
-
|
|
44
|
-
assistant_response = assistant_messages.select { |m| m.role == 'assistant' }.last
|
|
45
|
-
|
|
46
|
-
stop = Time.now
|
|
47
|
-
Response.new({ provider: @llm_instance.provider,
|
|
48
|
-
model: @llm_instance.model,
|
|
49
|
-
duration_seconds: stop.to_i - start.to_i,
|
|
50
|
-
source_code: @llm_instance.source_code(assistant_response.content),
|
|
51
|
-
total_tokens: @assistant.total_tokens })
|
|
22
|
+
module SourceCodeScrubbing
|
|
23
|
+
def source_code(content)
|
|
24
|
+
content.gsub('```', '').sub(/^(node(js)?|javascript|ruby|python(\d*)|elixir|bash|html|go|c(pp)?)([ \n])/, '')
|
|
52
25
|
end
|
|
53
26
|
end
|
|
54
27
|
|
|
55
28
|
class OpenAI
|
|
29
|
+
|
|
56
30
|
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
|
|
57
31
|
MAX_TOKENS = 8192
|
|
58
32
|
|
|
59
|
-
|
|
33
|
+
include SourceCodeScrubbing
|
|
34
|
+
private :source_code
|
|
60
35
|
|
|
61
36
|
def initialize(**args)
|
|
62
37
|
@logger = args.delete(:logger)
|
|
@@ -64,25 +39,28 @@ class LLMed
|
|
|
64
39
|
end
|
|
65
40
|
|
|
66
41
|
def chat(messages: [])
|
|
42
|
+
messages = messages.map do |m|
|
|
43
|
+
case m
|
|
44
|
+
when Message::System
|
|
45
|
+
{ role: 'system', content: m.content }
|
|
46
|
+
when Message::User
|
|
47
|
+
{ role: 'user', content: m.content }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
start = Time.now
|
|
67
52
|
llm_response = @llm.chat(messages: messages, max_tokens: MAX_TOKENS)
|
|
68
53
|
warn_token_limits(llm_response)
|
|
69
|
-
llm_response
|
|
70
|
-
end
|
|
71
54
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def source_code(content)
|
|
81
|
-
content.gsub('```', '').sub(/^(node(js)?|javascript|ruby|python(\d*)|elixir|bash|html|go|c(pp)?)([ \n])/, '')
|
|
55
|
+
stop = Time.now
|
|
56
|
+
Response.new({ provider: provider,
|
|
57
|
+
model: @llm.chat_parameters[:model],
|
|
58
|
+
duration_seconds: stop.to_i - start.to_i,
|
|
59
|
+
source_code: source_code(llm_response.chat_completion),
|
|
60
|
+
total_tokens: llm_response.total_tokens })
|
|
82
61
|
end
|
|
83
62
|
|
|
84
63
|
private
|
|
85
|
-
|
|
86
64
|
def warn_token_limits(llm_response)
|
|
87
65
|
if llm_response.completion_tokens >= MAX_TOKENS
|
|
88
66
|
@logger.warn("POSSIBLE INCONSISTENCY COMPLETED TOKENS REACHED MAX TOKENS #{MAX_TOKENS}")
|
|
@@ -92,57 +70,167 @@ class LLMed
|
|
|
92
70
|
def llm_arguments(args)
|
|
93
71
|
args
|
|
94
72
|
end
|
|
95
|
-
end
|
|
96
73
|
|
|
97
|
-
class Anthropic < OpenAI
|
|
98
74
|
def provider
|
|
99
|
-
:
|
|
75
|
+
:openai
|
|
100
76
|
end
|
|
77
|
+
end
|
|
101
78
|
|
|
79
|
+
class Anthropic < OpenAI
|
|
102
80
|
private
|
|
103
81
|
|
|
104
82
|
def llm_arguments(args)
|
|
105
83
|
@logger = args.delete(:logger)
|
|
106
84
|
args.merge({ llm_options: { uri_base: 'https://api.anthropic.com/v1/' } })
|
|
107
85
|
end
|
|
108
|
-
end
|
|
109
86
|
|
|
110
|
-
class LikeOpenAI < OpenAI
|
|
111
87
|
def provider
|
|
112
|
-
:
|
|
88
|
+
:anthropic
|
|
113
89
|
end
|
|
90
|
+
end
|
|
114
91
|
|
|
92
|
+
class LikeOpenAI < OpenAI
|
|
115
93
|
private
|
|
116
94
|
|
|
117
95
|
def llm_arguments(args)
|
|
118
96
|
args
|
|
119
97
|
end
|
|
98
|
+
|
|
99
|
+
def provider
|
|
100
|
+
:like_openai
|
|
101
|
+
end
|
|
120
102
|
end
|
|
121
103
|
|
|
122
|
-
class
|
|
123
|
-
|
|
124
|
-
|
|
104
|
+
class OpenCode
|
|
105
|
+
include SourceCodeScrubbing
|
|
106
|
+
private :source_code
|
|
107
|
+
|
|
108
|
+
DEFAULT_BIN = 'opencode'.freeze
|
|
109
|
+
DEFAULT_TIMEOUT = 900
|
|
110
|
+
PROMPT_SEPARATOR = "\n\n---\n\n".freeze
|
|
111
|
+
|
|
112
|
+
def initialize(**args)
|
|
113
|
+
@logger = args.delete(:logger)
|
|
114
|
+
args.delete(:api_key)
|
|
115
|
+
@model = args.dig(:default_options, :chat_model)
|
|
116
|
+
@llm_options = args.fetch(:llm_options, {}) || {}
|
|
117
|
+
@bin = @llm_options.fetch(:bin, DEFAULT_BIN)
|
|
118
|
+
@dir = @llm_options[:dir]
|
|
119
|
+
@timeout = @llm_options.fetch(:timeout, DEFAULT_TIMEOUT)
|
|
120
|
+
@flags = @llm_options[:flags]
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def chat(messages: [])
|
|
124
|
+
prompt = messages.map { |m| m[:content] }.join(PROMPT_SEPARATOR)
|
|
125
|
+
|
|
126
|
+
command = [@bin, 'run', '-m', @model]
|
|
127
|
+
command.concat(['--dir', @dir]) if @dir
|
|
128
|
+
command.concat(@flags) if @flags
|
|
129
|
+
|
|
130
|
+
start = Time.now
|
|
131
|
+
stdout, stderr, status = run(command, prompt)
|
|
132
|
+
stop = Time.now
|
|
133
|
+
|
|
134
|
+
cleaned = status.success? ? source_code(stdout) : ''
|
|
135
|
+
if !status.success? || cleaned.strip.empty?
|
|
136
|
+
@logger.error("opencode run failed (exit: #{status.success? ? 'success but empty output' : status.exitstatus}): #{stderr.to_s.lines.last(10).join}")
|
|
137
|
+
raise "opencode run failed for model #{@model}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
Response.new({ provider: :opencode,
|
|
141
|
+
model: @model,
|
|
142
|
+
duration_seconds: stop.to_i - start.to_i,
|
|
143
|
+
source_code: cleaned,
|
|
144
|
+
total_tokens: nil })
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
private
|
|
148
|
+
|
|
149
|
+
def run(command, prompt)
|
|
150
|
+
stdin, stdout, stderr, wait_thr = Open3.popen3(*command, spawn_options)
|
|
151
|
+
|
|
152
|
+
stdin_thread = prompt_writer(stdin, prompt)
|
|
153
|
+
stdout_thread = stream_reader(stdout)
|
|
154
|
+
stderr_thread = stream_reader(stderr)
|
|
155
|
+
|
|
156
|
+
unless wait_thr.join(@timeout)
|
|
157
|
+
kill_child_tree(wait_thr)
|
|
158
|
+
stdin_thread.join
|
|
159
|
+
stdout_thread.join
|
|
160
|
+
stderr_thread.join
|
|
161
|
+
|
|
162
|
+
@logger.error("opencode run timed out after #{@timeout} seconds")
|
|
163
|
+
raise "opencode run timed out after #{@timeout} seconds"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
stdin_thread.join
|
|
167
|
+
[stdout_thread.value, stderr_thread.value, wait_thr.value]
|
|
125
168
|
end
|
|
126
169
|
|
|
127
|
-
def
|
|
128
|
-
{
|
|
170
|
+
def spawn_options
|
|
171
|
+
windows? ? { new_pgroup: true } : { pgroup: true }
|
|
129
172
|
end
|
|
130
173
|
|
|
131
|
-
def
|
|
132
|
-
|
|
174
|
+
def windows?
|
|
175
|
+
RUBY_PLATFORM.match?(/mswin|mingw/)
|
|
133
176
|
end
|
|
134
177
|
|
|
135
|
-
def
|
|
136
|
-
|
|
137
|
-
|
|
178
|
+
def prompt_writer(stdin, prompt)
|
|
179
|
+
Thread.new do
|
|
180
|
+
begin
|
|
181
|
+
stdin.write(prompt)
|
|
182
|
+
rescue StandardError
|
|
183
|
+
nil
|
|
184
|
+
end
|
|
185
|
+
begin
|
|
186
|
+
stdin.close
|
|
187
|
+
rescue StandardError
|
|
188
|
+
nil
|
|
189
|
+
end
|
|
190
|
+
end
|
|
138
191
|
end
|
|
139
192
|
|
|
140
|
-
def
|
|
141
|
-
|
|
193
|
+
def stream_reader(stream)
|
|
194
|
+
Thread.new do
|
|
195
|
+
begin
|
|
196
|
+
stream.read
|
|
197
|
+
rescue StandardError
|
|
198
|
+
''
|
|
199
|
+
end
|
|
200
|
+
end
|
|
142
201
|
end
|
|
143
202
|
|
|
144
|
-
def
|
|
145
|
-
:
|
|
203
|
+
def kill_child_tree(wait_thr)
|
|
204
|
+
target = windows? ? wait_thr.pid : -wait_thr.pid
|
|
205
|
+
|
|
206
|
+
send_signal('TERM', target)
|
|
207
|
+
wait_thr.join(5)
|
|
208
|
+
send_signal('KILL', target) if wait_thr.alive?
|
|
209
|
+
wait_thr.join(5)
|
|
210
|
+
|
|
211
|
+
@logger.warn("opencode timeout kill left pid #{wait_thr.pid} alive") if wait_thr.alive?
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def send_signal(signal, target)
|
|
215
|
+
Process.kill(signal, target)
|
|
216
|
+
rescue Errno::ESRCH, Errno::EPERM => e
|
|
217
|
+
@logger.warn("opencode timeout kill #{signal} to #{target} failed: #{e.message}")
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
class Test
|
|
222
|
+
def initialize
|
|
223
|
+
@output = ''
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def chat(messages: [])
|
|
227
|
+
@output = messages.map { |m| m[:content] }.join("\n")
|
|
228
|
+
|
|
229
|
+
Response.new({ provider: :test,
|
|
230
|
+
model: 'test',
|
|
231
|
+
duration_seconds: 0,
|
|
232
|
+
source_code: @output,
|
|
233
|
+
total_tokens: 0 })
|
|
146
234
|
end
|
|
147
235
|
end
|
|
148
236
|
end
|
data/lib/llmed/application.rb
CHANGED
|
@@ -49,7 +49,6 @@ class LLMed
|
|
|
49
49
|
|
|
50
50
|
# Example:
|
|
51
51
|
# application { context "demo" { "content" } }
|
|
52
|
-
# DEPRECATED: Use spec instead of context
|
|
53
52
|
def context(name, **opts, &block)
|
|
54
53
|
opts[:release_dir] = @release_dir
|
|
55
54
|
ctx = Context.new(name: name, options: opts)
|
|
@@ -59,12 +58,6 @@ class LLMed
|
|
|
59
58
|
@contexts << ctx
|
|
60
59
|
end
|
|
61
60
|
|
|
62
|
-
# Example:
|
|
63
|
-
# application { spec "demo" { "content" } }
|
|
64
|
-
def spec(name, **opts, &block)
|
|
65
|
-
context(name, **opts, &block)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
61
|
def achieve(name, **opts, &block)
|
|
69
62
|
opts[:release_dir] = @release_dir
|
|
70
63
|
goal = Goal.new(name: name, options: opts)
|
|
@@ -92,7 +85,7 @@ class LLMed
|
|
|
92
85
|
end
|
|
93
86
|
|
|
94
87
|
def refresh(contexts)
|
|
95
|
-
@contexts = contexts.map
|
|
88
|
+
@contexts = contexts.map{ |ctx| [ctx.name, {'name' => ctx.name, 'message' => ctx.raw}]}.to_h
|
|
96
89
|
dump
|
|
97
90
|
end
|
|
98
91
|
|
|
@@ -105,7 +98,7 @@ class LLMed
|
|
|
105
98
|
else
|
|
106
99
|
line_diff(current_ctx['message'], other_ctx.raw)
|
|
107
100
|
end
|
|
108
|
-
if !result.all?
|
|
101
|
+
if !result.all?{|op, line| op == '=:'}
|
|
109
102
|
diffs[other_ctx.name] = result
|
|
110
103
|
end
|
|
111
104
|
end
|
|
@@ -140,8 +133,8 @@ class LLMed
|
|
|
140
133
|
i2 += 1
|
|
141
134
|
else
|
|
142
135
|
# Try to find if one of the lines matches later
|
|
143
|
-
idx1 = lines1[i1
|
|
144
|
-
idx2 = lines2[i2
|
|
136
|
+
idx1 = lines1[i1+1..-1]&.index(line2)
|
|
137
|
+
idx2 = lines2[i2+1..-1]&.index(line1)
|
|
145
138
|
|
|
146
139
|
if !idx1.nil? && (idx2.nil? || idx1 <= idx2)
|
|
147
140
|
result << ["-:", line1]
|
|
@@ -168,13 +161,14 @@ class LLMed
|
|
|
168
161
|
@contexts = JSON.load(f.read)['contexts']
|
|
169
162
|
end
|
|
170
163
|
else
|
|
171
|
-
@contexts = default.map
|
|
164
|
+
@contexts = default.map{ |ctx| [ctx.name, {'name' => ctx.name, 'message' => ctx.raw}]}.to_h
|
|
172
165
|
end
|
|
173
166
|
end
|
|
174
167
|
|
|
175
168
|
def dump
|
|
169
|
+
FileUtils.mkdir_p(File.dirname(@snapshot_file))
|
|
176
170
|
File.open(@snapshot_file, 'w') do |file|
|
|
177
|
-
file.write(JSON.dump({
|
|
171
|
+
file.write(JSON.dump({'contexts' => @contexts}))
|
|
178
172
|
end
|
|
179
173
|
end
|
|
180
174
|
end
|
|
@@ -223,6 +217,7 @@ class LLMed
|
|
|
223
217
|
end
|
|
224
218
|
|
|
225
219
|
def patch_or_create(output)
|
|
220
|
+
|
|
226
221
|
output_content = output
|
|
227
222
|
|
|
228
223
|
if @release && File.exist?(release_source_code) && !release_contexts.empty?
|
|
@@ -252,7 +247,6 @@ class LLMed
|
|
|
252
247
|
end
|
|
253
248
|
|
|
254
249
|
return unless @output_file.is_a?(String)
|
|
255
|
-
|
|
256
250
|
output_file = Pathname.new(@output_dir) + @output_file
|
|
257
251
|
FileUtils.cp(output_file, release_source_code)
|
|
258
252
|
FileUtils.cp(output_file, release_main_source_code)
|
data/lib/llmed/configuration.rb
CHANGED
|
@@ -50,7 +50,7 @@ class LLMed
|
|
|
50
50
|
|
|
51
51
|
<goals>
|
|
52
52
|
{goals}
|
|
53
|
-
|
|
53
|
+
</goals>
|
|
54
54
|
|
|
55
55
|
Goals and context are orthogonal: goals orient the context.
|
|
56
56
|
|
|
@@ -60,8 +60,7 @@ class LLMed
|
|
|
60
60
|
", input_variables: %w[language source_code code_comment_begin code_comment_end update_context_digests changes_of_contexts goals])
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
def prompt(language:, source_code:, code_comment_begin:, code_comment_end:, update_context_digests: [],
|
|
64
|
-
changes_of_contexts: '', goals: '')
|
|
63
|
+
def prompt(language:, source_code:, code_comment_begin:, code_comment_end:, update_context_digests: [], changes_of_contexts: '', goals: '')
|
|
65
64
|
@prompt.format(language: language,
|
|
66
65
|
source_code: source_code,
|
|
67
66
|
code_comment_begin: code_comment_begin,
|
|
@@ -75,9 +74,9 @@ class LLMed
|
|
|
75
74
|
# Example:
|
|
76
75
|
# set_prompt "my new prompt"
|
|
77
76
|
def set_prompt(*arg, input_variables: %w[language code_comment_begin code_comment_end source_code], **args)
|
|
78
|
-
input_variables = {} if args[:file]
|
|
79
77
|
prompt = File.read(args[:file]) if args[:file]
|
|
80
78
|
prompt ||= arg.first
|
|
79
|
+
input_variables = prompt.scan(/\{([a-z][a-z0-9_]*)\}/).flatten.uniq if args[:file]
|
|
81
80
|
@prompt = LLMed::LLM::Template.build(template: prompt, input_variables: input_variables)
|
|
82
81
|
end
|
|
83
82
|
|
|
@@ -103,35 +102,40 @@ class LLMed
|
|
|
103
102
|
end
|
|
104
103
|
|
|
105
104
|
def llm
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
105
|
+
case @provider
|
|
106
|
+
when :openai
|
|
107
|
+
LLMed::LLM::OpenAI.new(
|
|
108
|
+
logger: @logger,
|
|
109
|
+
api_key: @provider_api_key,
|
|
110
|
+
default_options: { max_tokens: nil, temperature: 0.7, chat_model: @provider_model }
|
|
111
|
+
)
|
|
112
|
+
when :anthropic
|
|
113
|
+
LLMed::LLM::Anthropic.new(
|
|
114
|
+
logger: @logger,
|
|
115
|
+
api_key: @provider_api_key,
|
|
116
|
+
default_options: { max_tokens: nil, temperature: 0.7, chat_model: @provider_model }
|
|
117
|
+
)
|
|
118
|
+
when :like_openai
|
|
119
|
+
LLMed::LLM::LikeOpenAI.new(
|
|
120
|
+
logger: @logger,
|
|
121
|
+
api_key: @provider_api_key,
|
|
122
|
+
default_options: { temperature: 0.7, chat_model: @provider_model },
|
|
123
|
+
llm_options: @provider_options
|
|
124
|
+
)
|
|
125
|
+
when :opencode
|
|
126
|
+
LLMed::LLM::OpenCode.new(
|
|
127
|
+
logger: @logger,
|
|
128
|
+
api_key: @provider_api_key,
|
|
129
|
+
default_options: { chat_model: @provider_model },
|
|
130
|
+
llm_options: @provider_options
|
|
131
|
+
)
|
|
132
|
+
when :test
|
|
133
|
+
LLMed::LLM::Test.new
|
|
134
|
+
when nil
|
|
135
|
+
raise 'Please set the provider with `set_llm(provider, api_key, model)`'
|
|
136
|
+
else
|
|
137
|
+
raise "not implemented provider #{@provider}"
|
|
138
|
+
end
|
|
135
139
|
end
|
|
136
140
|
end
|
|
137
141
|
end
|
data/lib/llmed/goal.rb
CHANGED
|
@@ -8,6 +8,7 @@ class LLMed
|
|
|
8
8
|
def initialize(name:, options: {})
|
|
9
9
|
@name = name
|
|
10
10
|
@options = options
|
|
11
|
+
|
|
11
12
|
end
|
|
12
13
|
|
|
13
14
|
def llm(message)
|
|
@@ -19,7 +20,7 @@ class LLMed
|
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
def message?
|
|
22
|
-
|
|
23
|
+
!(@message.nil? || @message.empty?)
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
end
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
#<llmed-code context='Library LLMed::LiterateProgramming::Markdown' digest='9c0e3f61ab4cdc3c56c29230a800487dd1a7ef0d929c843fd2461907d0831ab2' after=''>
|
|
2
2
|
class LLMed::LiterateProgramming::Markdown
|
|
3
3
|
def parse(input)
|
|
4
4
|
contexts = []
|
|
5
5
|
current_context = { type: :context, title: "_default", content: [] }
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
input.each_line do |line|
|
|
8
8
|
if line.strip =~ /^# (.+)$/
|
|
9
9
|
contexts << current_context unless current_context[:content].empty?
|
|
@@ -23,4 +23,4 @@ class LLMed::LiterateProgramming::Markdown
|
|
|
23
23
|
contexts
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
|
-
|
|
26
|
+
#</llmed-code>
|
metadata
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: llmed
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jovany Leandro G.C
|
|
8
|
+
autorequire:
|
|
8
9
|
bindir: exe
|
|
9
10
|
cert_chain: []
|
|
10
|
-
date:
|
|
11
|
+
date: 2026-11-21 00:00:00.000000000 Z
|
|
11
12
|
dependencies:
|
|
12
13
|
- !ruby/object:Gem::Dependency
|
|
13
14
|
name: langchainrb
|
|
@@ -111,6 +112,7 @@ licenses:
|
|
|
111
112
|
metadata:
|
|
112
113
|
source_code_uri: https://github.com/bit4bit/llmed
|
|
113
114
|
allowed_push_host: https://rubygems.org
|
|
115
|
+
post_install_message:
|
|
114
116
|
rdoc_options: []
|
|
115
117
|
require_paths:
|
|
116
118
|
- lib
|
|
@@ -125,7 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
125
127
|
- !ruby/object:Gem::Version
|
|
126
128
|
version: 1.3.7
|
|
127
129
|
requirements: []
|
|
128
|
-
rubygems_version: 3.
|
|
130
|
+
rubygems_version: 3.4.20
|
|
131
|
+
signing_key:
|
|
129
132
|
specification_version: 4
|
|
130
133
|
summary: LLM Execution Development
|
|
131
134
|
test_files: []
|