elelem 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 06a7abdf8e0f812a68d33858858674b65641c32df197305f3baff807bc7faa40
4
+ data.tar.gz: fea3e4bf3d3c8fc2c8a9da954b704e8bb3542c5e003e5a8d4f9fcbe6a178152a
5
+ SHA512:
6
+ metadata.gz: 6790c0d2703328168d5abafa4b0b1e25987b771e60512edeb40e37f4b43cf10b4bd1e4e7ed9222c261238821f6610d9ba1f810c0f45bf32e7ef1a3b14dd4a553
7
+ data.tar.gz: 85f83cb98ab8d2895c77910d36b2ba3b705f614bf3de0bccb48f41b48033f248edd316cc0219c4ff5b9a4fe3a10845f4280cc2723bc6841cbbbd1ea2ff97c7cf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-08-08
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 mo khan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Elelem
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elelem`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xlgmokha/elelem.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
data/exe/elelem ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "elelem"
5
+
6
+ Signal.trap('INT') do
7
+ exit(1)
8
+ end
9
+
10
+ Elelem::Application.start
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Agent
5
+ attr_reader :configuration, :conversation, :tools
6
+
7
+ def initialize(configuration)
8
+ @configuration = configuration
9
+ @conversation = configuration.conversation
10
+ @tools = configuration.tools
11
+ end
12
+
13
+ def repl
14
+ loop do
15
+ print "\n> "
16
+ user = STDIN.gets&.chomp
17
+ break if user.nil? || user.empty? || user == 'exit'
18
+ process_input(user)
19
+ puts("\u001b[32mDone!\u001b[0m")
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def process_input(text)
26
+ conversation.add(role: 'user', content: text)
27
+
28
+ done = false
29
+ loop do
30
+ call_api(conversation.history) do |chunk|
31
+ debug_print(chunk)
32
+
33
+ response = JSON.parse(chunk)
34
+ done = response['done']
35
+ message = response['message'] || {}
36
+
37
+ if message['thinking']
38
+ print("\u001b[90m#{message['thinking']}\u001b[0m")
39
+ elsif message['tool_calls']&.any?
40
+ message['tool_calls'].each do |t|
41
+ conversation.add(role: 'tool', content: tools.execute(t))
42
+ end
43
+ done = false
44
+ elsif message['content'].to_s.strip
45
+ print message['content'].to_s.strip
46
+ else
47
+ raise chunk.inspect
48
+ end
49
+ end
50
+
51
+ break if done
52
+ end
53
+ end
54
+
55
+ def call_api(messages)
56
+ body = {
57
+ messages: messages,
58
+ model: configuration.model,
59
+ stream: true,
60
+ keep_alive: '5m',
61
+ options: { temperature: 0.1 },
62
+ tools: tools.to_h
63
+ }
64
+ json_body = body.to_json
65
+ debug_print(json_body)
66
+
67
+ req = Net::HTTP::Post.new(configuration.uri)
68
+ req['Content-Type'] = 'application/json'
69
+ req.body = json_body
70
+ req['Authorization'] = "Bearer #{configuration.token}" if configuration.token
71
+
72
+ configuration.http.request(req) do |response|
73
+ raise response.inspect unless response.code == "200"
74
+
75
+ response.read_body do |chunk|
76
+ debug_print(chunk)
77
+ yield(chunk) if block_given?
78
+ $stdout.flush
79
+ end
80
+ end
81
+ end
82
+
83
+ def debug_print(body = nil)
84
+ configuration.logger.debug(body) if configuration.debug && body
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Application < Thor
5
+ desc 'chat', 'Start the REPL'
6
+ method_option :help, aliases: '-h', type: :boolean, desc: 'Display usage information'
7
+ method_option :host, aliases: '--host', type: :string, desc: 'Ollama host', default: ENV.fetch('OLLAMA_HOST', 'localhost:11434')
8
+ method_option :model, aliases: '--model', type: :string, desc: 'Ollama model', default: ENV.fetch('OLLAMA_MODEL', 'gpt-oss')
9
+ method_option :token, aliases: '--token', type: :string, desc: 'Ollama token', default: ENV.fetch('OLLAMA_API_KEY', nil)
10
+ method_option :debug, aliases: '--debug', type: :boolean, desc: 'Debug mode', default: false
11
+ def chat(*)
12
+ if options[:help]
13
+ invoke :help, ['chat']
14
+ else
15
+ configuration = Configuration.new(
16
+ host: options[:host],
17
+ model: options[:model],
18
+ token: options[:token],
19
+ debug: options[:debug],
20
+ )
21
+ say "Ollama Agent (#{configuration.model})", :green
22
+ say "Tools:\n #{configuration.tools.banner}", :green
23
+
24
+ agent = Agent.new(configuration)
25
+ agent.repl
26
+ end
27
+ end
28
+
29
+ desc 'version', 'spandx version'
30
+ def version
31
+ puts "v#{Spandx::VERSION}"
32
+ end
33
+ map %w[--version -v] => :version
34
+ end
35
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Configuration
5
+ attr_reader :host, :model, :token, :debug
6
+
7
+ def initialize(host:, model:, token:, debug: false)
8
+ @host = host
9
+ @model = model
10
+ @token = token
11
+ @debug = debug
12
+ end
13
+
14
+ def http
15
+ @http ||= Net::HTTP.new(uri.host, uri.port).tap do |h|
16
+ h.read_timeout = 3_600
17
+ h.open_timeout = 10
18
+ end
19
+ end
20
+
21
+ def logger
22
+ @logger ||= begin
23
+ Logger.new(debug ? $stderr : "/dev/null").tap do |logger|
24
+ logger.formatter = ->(_, _, _, msg) { msg }
25
+ end
26
+ end
27
+ end
28
+
29
+ def uri
30
+ @uri ||= URI("#{scheme}://#{host}/api/chat")
31
+ end
32
+
33
+ def conversation
34
+ @conversation ||= Conversation.new
35
+ end
36
+
37
+ def tools
38
+ @tools ||= Tools.new
39
+ end
40
+
41
+ private
42
+
43
+ def scheme
44
+ host.match?(/\A(?:localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?\z/) ? 'http' : 'https'
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Conversation
5
+ SYSTEM_MESSAGE = <<~SYS
6
+ You are ChatGPT, a helpful assistant with reasoning capabilities.
7
+ Current date: #{Time.now.strftime('%Y-%m-%d')}.
8
+ System info: `uname -a` output: #{`uname -a`.strip}
9
+ Reasoning: high
10
+ SYS
11
+
12
+ ROLES = ['system', 'user', 'tool'].freeze
13
+
14
+ def initialize(items = [{ role: 'system', content: SYSTEM_MESSAGE }])
15
+ @items = items
16
+ end
17
+
18
+ def history
19
+ @items
20
+ end
21
+
22
+ # :TODO truncate conversation history
23
+ def add(role: 'user', content: '')
24
+ raise "unknown role: #{role}" unless ROLES.include?(role)
25
+
26
+ @items << { role: role, content: content }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Tools
5
+ DEFAULT_TOOLS = [
6
+ {
7
+ type: 'function',
8
+ function: {
9
+ name: 'execute_command',
10
+ description: 'Execute a shell command.',
11
+ parameters: {
12
+ type: 'object',
13
+ properties: { command: { type: 'string' } },
14
+ required: ['command']
15
+ }
16
+ },
17
+ handler: -> (args) {
18
+ stdout, stderr, _status = Open3.capture3('/bin/sh', '-c', args['command'])
19
+ stdout + stderr
20
+ }
21
+ },
22
+ {
23
+ type: 'function',
24
+ function: {
25
+ name: 'ask_user',
26
+ description: 'Ask the user to answer a question.',
27
+ parameters: {
28
+ type: 'object',
29
+ properties: { question: { type: 'string' } },
30
+ required: ['question']
31
+ }
32
+ },
33
+ handler: ->(args) {
34
+ puts("\u001b[35m#{args['question']}\u001b[0m")
35
+ print "> "
36
+ STDIN.gets&.chomp
37
+ }
38
+ }
39
+ ]
40
+
41
+ def initialize(tools = DEFAULT_TOOLS)
42
+ @tools = tools
43
+ end
44
+
45
+ def banner
46
+ @tools.map do |h|
47
+ [
48
+ h.dig(:function, :name),
49
+ h.dig(:function, :description)
50
+ ].join(": ")
51
+ end.sort.join("\n ")
52
+ end
53
+
54
+ def execute(tool_call)
55
+ name = tool_call.dig('function', 'name')
56
+ args = tool_call.dig('function', 'arguments')
57
+
58
+ tool = @tools.find do |tool|
59
+ tool.dig(:function, :name) == name
60
+ end
61
+ tool.fetch(:handler).call(args)
62
+ end
63
+
64
+ def to_h
65
+ @tools.map do |tool|
66
+ {
67
+ type: tool[:type],
68
+ function: {
69
+ name: tool.dig(:function, :name),
70
+ description: tool.dig(:function, :description),
71
+ parameters: tool.dig(:function, :parameters)
72
+ }
73
+ }
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ VERSION = "0.1.0"
5
+ end
data/lib/elelem.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "logger"
5
+ require "net/http"
6
+ require "open3"
7
+ require "thor"
8
+ require "uri"
9
+
10
+ require_relative "elelem/agent"
11
+ require_relative "elelem/application"
12
+ require_relative "elelem/configuration"
13
+ require_relative "elelem/conversation"
14
+ require_relative "elelem/tools"
15
+ require_relative "elelem/version"
16
+
17
+ module Elelem
18
+ class Error < StandardError; end
19
+ end
data/mise.toml ADDED
@@ -0,0 +1,2 @@
1
+ [tools]
2
+ ruby = "latest"
data/sig/elelem.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Elelem
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elelem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mo khan
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: json
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: logger
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: net-http
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: open3
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: thor
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ - !ruby/object:Gem::Dependency
83
+ name: uri
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ description: A REPL for Ollama.
97
+ email:
98
+ - mo@mokhan.ca
99
+ executables:
100
+ - elelem
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".rspec"
105
+ - ".rubocop.yml"
106
+ - CHANGELOG.md
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - exe/elelem
111
+ - lib/elelem.rb
112
+ - lib/elelem/agent.rb
113
+ - lib/elelem/application.rb
114
+ - lib/elelem/configuration.rb
115
+ - lib/elelem/conversation.rb
116
+ - lib/elelem/tools.rb
117
+ - lib/elelem/version.rb
118
+ - mise.toml
119
+ - sig/elelem.rbs
120
+ homepage: https://www.mokhan.ca
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ allowed_push_host: https://rubygems.org
125
+ homepage_uri: https://www.mokhan.ca
126
+ source_code_uri: https://gitlab.com/mokhax/elelem
127
+ changelog_uri: https://gitlab.com/mokhax/elelem/-/blob/main/CHANGELOG.md
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: 3.1.0
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 3.3.11
141
+ requirements: []
142
+ rubygems_version: 3.6.9
143
+ specification_version: 4
144
+ summary: A REPL for Ollama.
145
+ test_files: []