llmed 0.6.1 → 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 +4 -5
- data/exe/llmed.literate +11 -14
- data/lib/llm.rb +127 -4
- data/lib/llmed/application.rb +1 -0
- data/lib/llmed/configuration.rb +9 -2
- metadata +6 -5
- data/lib/#llm.rb# +0 -114
- data/lib/llm.rb~ +0 -114
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
|
@@ -24,8 +24,8 @@ OptionParser.new do |parser|
|
|
|
24
24
|
parser.banner = "Usage: llmed [options] <application file .llmed or stdin>"
|
|
25
25
|
parser.on_tail("-h", "--help", "Show this message") do
|
|
26
26
|
puts parser
|
|
27
|
-
puts "\n# Website\nhttps://github.com/bit4bit/
|
|
28
|
-
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"
|
|
29
29
|
exit
|
|
30
30
|
end
|
|
31
31
|
|
|
@@ -47,10 +47,9 @@ OptionParser.new do |parser|
|
|
|
47
47
|
end
|
|
48
48
|
end.parse!
|
|
49
49
|
|
|
50
|
+
source_file = ARGV.first
|
|
50
51
|
source_code = ARGF.read
|
|
51
|
-
|
|
52
|
-
release_dir = File.dirname(ARGF.path)
|
|
53
|
-
end
|
|
52
|
+
release_dir = File.dirname(source_file) unless source_file.nil?
|
|
54
53
|
|
|
55
54
|
llmed = LLMed.new(logger: logger, output_dir: output_dir, release_dir: release_dir)
|
|
56
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,11 +19,20 @@ class LLMed
|
|
|
18
19
|
|
|
19
20
|
Response = Struct.new(:provider, :model, :source_code, :duration_seconds, :total_tokens, keyword_init: true)
|
|
20
21
|
|
|
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])/, '')
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
21
28
|
class OpenAI
|
|
22
29
|
|
|
23
30
|
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
|
|
24
31
|
MAX_TOKENS = 8192
|
|
25
32
|
|
|
33
|
+
include SourceCodeScrubbing
|
|
34
|
+
private :source_code
|
|
35
|
+
|
|
26
36
|
def initialize(**args)
|
|
27
37
|
@logger = args.delete(:logger)
|
|
28
38
|
@llm = Langchain::LLM::OpenAI.new(**llm_arguments(args))
|
|
@@ -64,10 +74,6 @@ class LLMed
|
|
|
64
74
|
def provider
|
|
65
75
|
:openai
|
|
66
76
|
end
|
|
67
|
-
|
|
68
|
-
def source_code(content)
|
|
69
|
-
content.gsub('```', '').sub(/^(node(js)?|javascript|ruby|python(\d*)|elixir|bash|html|go|c(pp)?)([ \n])/, '')
|
|
70
|
-
end
|
|
71
77
|
end
|
|
72
78
|
|
|
73
79
|
class Anthropic < OpenAI
|
|
@@ -95,6 +101,123 @@ class LLMed
|
|
|
95
101
|
end
|
|
96
102
|
end
|
|
97
103
|
|
|
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]
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def spawn_options
|
|
171
|
+
windows? ? { new_pgroup: true } : { pgroup: true }
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def windows?
|
|
175
|
+
RUBY_PLATFORM.match?(/mswin|mingw/)
|
|
176
|
+
end
|
|
177
|
+
|
|
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
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def stream_reader(stream)
|
|
194
|
+
Thread.new do
|
|
195
|
+
begin
|
|
196
|
+
stream.read
|
|
197
|
+
rescue StandardError
|
|
198
|
+
''
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
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
|
+
|
|
98
221
|
class Test
|
|
99
222
|
def initialize
|
|
100
223
|
@output = ''
|
data/lib/llmed/application.rb
CHANGED
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
|
|
|
@@ -74,9 +74,9 @@ class LLMed
|
|
|
74
74
|
# Example:
|
|
75
75
|
# set_prompt "my new prompt"
|
|
76
76
|
def set_prompt(*arg, input_variables: %w[language code_comment_begin code_comment_end source_code], **args)
|
|
77
|
-
input_variables = {} if args[:file]
|
|
78
77
|
prompt = File.read(args[:file]) if args[:file]
|
|
79
78
|
prompt ||= arg.first
|
|
79
|
+
input_variables = prompt.scan(/\{([a-z][a-z0-9_]*)\}/).flatten.uniq if args[:file]
|
|
80
80
|
@prompt = LLMed::LLM::Template.build(template: prompt, input_variables: input_variables)
|
|
81
81
|
end
|
|
82
82
|
|
|
@@ -122,6 +122,13 @@ class LLMed
|
|
|
122
122
|
default_options: { temperature: 0.7, chat_model: @provider_model },
|
|
123
123
|
llm_options: @provider_options
|
|
124
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
|
+
)
|
|
125
132
|
when :test
|
|
126
133
|
LLMed::LLM::Test.new
|
|
127
134
|
when nil
|
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
|
|
@@ -93,9 +94,7 @@ files:
|
|
|
93
94
|
- README.md
|
|
94
95
|
- exe/llmed
|
|
95
96
|
- exe/llmed.literate
|
|
96
|
-
- lib/#llm.rb#
|
|
97
97
|
- lib/llm.rb
|
|
98
|
-
- lib/llm.rb~
|
|
99
98
|
- lib/llmed.rb
|
|
100
99
|
- lib/llmed/application.rb
|
|
101
100
|
- lib/llmed/configuration.rb
|
|
@@ -113,6 +112,7 @@ licenses:
|
|
|
113
112
|
metadata:
|
|
114
113
|
source_code_uri: https://github.com/bit4bit/llmed
|
|
115
114
|
allowed_push_host: https://rubygems.org
|
|
115
|
+
post_install_message:
|
|
116
116
|
rdoc_options: []
|
|
117
117
|
require_paths:
|
|
118
118
|
- lib
|
|
@@ -127,7 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
127
127
|
- !ruby/object:Gem::Version
|
|
128
128
|
version: 1.3.7
|
|
129
129
|
requirements: []
|
|
130
|
-
rubygems_version: 3.
|
|
130
|
+
rubygems_version: 3.4.20
|
|
131
|
+
signing_key:
|
|
131
132
|
specification_version: 4
|
|
132
133
|
summary: LLM Execution Development
|
|
133
134
|
test_files: []
|
data/lib/#llm.rb#
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
require 'openai'
|
|
2
|
-
require 'langchain'
|
|
3
|
-
|
|
4
|
-
Langchain.logger.level = Logger::ERROR
|
|
5
|
-
|
|
6
|
-
class LLMed
|
|
7
|
-
module LLM
|
|
8
|
-
module Message
|
|
9
|
-
System = Struct.new(:content)
|
|
10
|
-
User = Struct.new(:content)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
module Template
|
|
14
|
-
def self.build(template:, input_variables:)
|
|
15
|
-
Langchain::Prompt::PromptTemplate.new(template: template, input_variables: input_variables)
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
Response = Struct.new(:provider, :model, :source_code, :duration_seconds, :total_tokens, keyword_init: true)
|
|
20
|
-
|
|
21
|
-
class OpenAI
|
|
22
|
-
|
|
23
|
-
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
|
|
24
|
-
MAX_TOKENS = 8192
|
|
25
|
-
|
|
26
|
-
def initialize(**args)
|
|
27
|
-
@logger = args.delete(:logger)
|
|
28
|
-
@llm = Langchain::LLM::OpenAI.new(**llm_arguments(args))
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def chat(messages: [])
|
|
32
|
-
messages = messages.map do |m|
|
|
33
|
-
case messages
|
|
34
|
-
when Message::System
|
|
35
|
-
{ role: 'system', content: m.content }
|
|
36
|
-
when Message::User
|
|
37
|
-
{ role: 'user', content: m.content }
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
messages.e
|
|
41
|
-
start = Time.now
|
|
42
|
-
llm_response = @llm.chat(messages: messages, max_tokens: MAX_TOKENS)
|
|
43
|
-
warn_token_limits(llm_response)
|
|
44
|
-
|
|
45
|
-
stop = Time.now
|
|
46
|
-
Response.new({ provider: provider,
|
|
47
|
-
model: @llm.chat_parameters[:model],
|
|
48
|
-
duration_seconds: stop.to_i - start.to_i,
|
|
49
|
-
source_code: source_code(llm_response.chat_completion),
|
|
50
|
-
total_tokens: llm_response.total_tokens })
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
private
|
|
54
|
-
def warn_token_limits(llm_response)
|
|
55
|
-
if llm_response.completion_tokens >= MAX_TOKENS
|
|
56
|
-
@logger.warn("POSSIBLE INCONSISTENCY COMPLETED TOKENS REACHED MAX TOKENS #{MAX_TOKENS}")
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def llm_arguments(args)
|
|
61
|
-
args
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def provider
|
|
65
|
-
:openai
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def source_code(content)
|
|
69
|
-
content.gsub('```', '').sub(/^(node(js)?|javascript|ruby|python(\d*)|elixir|bash|html|go|c(pp)?)([ \n])/, '')
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
class Anthropic < OpenAI
|
|
74
|
-
private
|
|
75
|
-
|
|
76
|
-
def llm_arguments(args)
|
|
77
|
-
@logger = args.delete(:logger)
|
|
78
|
-
args.merge({ llm_options: { uri_base: 'https://api.anthropic.com/v1/' } })
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def provider
|
|
82
|
-
:anthropic
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
class LikeOpenAI < OpenAI
|
|
87
|
-
private
|
|
88
|
-
|
|
89
|
-
def llm_arguments(args)
|
|
90
|
-
args
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def provider
|
|
94
|
-
:like_openai
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
class Test
|
|
99
|
-
def initialize
|
|
100
|
-
@output = ''
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def chat(messages: [])
|
|
104
|
-
@output = messages.map { |m| m[:content] }.join("\n")
|
|
105
|
-
|
|
106
|
-
Response.new({ provider: :test,
|
|
107
|
-
model: 'test',
|
|
108
|
-
duration_seconds: 0,
|
|
109
|
-
source_code: @output,
|
|
110
|
-
total_tokens: 0 })
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
end
|
data/lib/llm.rb~
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
require 'openai'
|
|
2
|
-
require 'langchain'
|
|
3
|
-
|
|
4
|
-
Langchain.logger.level = Logger::ERROR
|
|
5
|
-
|
|
6
|
-
class LLMed
|
|
7
|
-
module LLM
|
|
8
|
-
module Message
|
|
9
|
-
System = Struct.new(:content)
|
|
10
|
-
User = Struct.new(:content)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
module Template
|
|
14
|
-
def self.build(template:, input_variables:)
|
|
15
|
-
Langchain::Prompt::PromptTemplate.new(template: template, input_variables: input_variables)
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
Response = Struct.new(:provider, :model, :source_code, :duration_seconds, :total_tokens, keyword_init: true)
|
|
20
|
-
|
|
21
|
-
class OpenAI
|
|
22
|
-
|
|
23
|
-
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
|
|
24
|
-
MAX_TOKENS = 8192
|
|
25
|
-
|
|
26
|
-
def initialize(**args)
|
|
27
|
-
@logger = args.delete(:logger)
|
|
28
|
-
@llm = Langchain::LLM::OpenAI.new(**llm_arguments(args))
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def chat(messages: [])
|
|
32
|
-
messages = messages.map do |m|
|
|
33
|
-
case m
|
|
34
|
-
when Message::System
|
|
35
|
-
{ role: 'system', content: m.content }
|
|
36
|
-
when Message::User
|
|
37
|
-
{ role: 'user', content: m.content }
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
start = Time.now
|
|
42
|
-
llm_response = @llm.chat(messages: messages, max_tokens: MAX_TOKENS)
|
|
43
|
-
warn_token_limits(llm_response)
|
|
44
|
-
|
|
45
|
-
stop = Time.now
|
|
46
|
-
Response.new({ provider: provider,
|
|
47
|
-
model: @llm.chat_parameters[:model],
|
|
48
|
-
duration_seconds: stop.to_i - start.to_i,
|
|
49
|
-
source_code: source_code(llm_response.chat_completion),
|
|
50
|
-
total_tokens: llm_response.total_tokens })
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
private
|
|
54
|
-
def warn_token_limits(llm_response)
|
|
55
|
-
if llm_response.completion_tokens >= MAX_TOKENS
|
|
56
|
-
@logger.warn("POSSIBLE INCONSISTENCY COMPLETED TOKENS REACHED MAX TOKENS #{MAX_TOKENS}")
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def llm_arguments(args)
|
|
61
|
-
args
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def provider
|
|
65
|
-
:openai
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def source_code(content)
|
|
69
|
-
content.gsub('```', '').sub(/^(node(js)?|javascript|ruby|python(\d*)|elixir|bash|html|go|c(pp)?)([ \n])/, '')
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
class Anthropic < OpenAI
|
|
74
|
-
private
|
|
75
|
-
|
|
76
|
-
def llm_arguments(args)
|
|
77
|
-
@logger = args.delete(:logger)
|
|
78
|
-
args.merge({ llm_options: { uri_base: 'https://api.anthropic.com/v1/' } })
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def provider
|
|
82
|
-
:anthropic
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
class LikeOpenAI < OpenAI
|
|
87
|
-
private
|
|
88
|
-
|
|
89
|
-
def llm_arguments(args)
|
|
90
|
-
args
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def provider
|
|
94
|
-
:like_openai
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
class Test
|
|
99
|
-
def initialize
|
|
100
|
-
@output = ''
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def chat(messages: [])
|
|
104
|
-
@output = messages.map { |m| m[:content] }.join("\n")
|
|
105
|
-
|
|
106
|
-
Response.new({ provider: :test,
|
|
107
|
-
model: 'test',
|
|
108
|
-
duration_seconds: 0,
|
|
109
|
-
source_code: @output,
|
|
110
|
-
total_tokens: 0 })
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
end
|