lex-mlx 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 +7 -0
- data/.github/workflows/ci.yml +16 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +66 -0
- data/CHANGELOG.md +13 -0
- data/CLAUDE.md +44 -0
- data/Gemfile +13 -0
- data/LICENSE +21 -0
- data/README.md +44 -0
- data/lex-mlx.gemspec +30 -0
- data/lib/legion/extensions/mlx/client.rb +35 -0
- data/lib/legion/extensions/mlx/helpers/client.rb +34 -0
- data/lib/legion/extensions/mlx/helpers/errors.rb +40 -0
- data/lib/legion/extensions/mlx/helpers/usage.rb +32 -0
- data/lib/legion/extensions/mlx/runners/chat.rb +64 -0
- data/lib/legion/extensions/mlx/runners/completions.rb +64 -0
- data/lib/legion/extensions/mlx/runners/embeddings.rb +25 -0
- data/lib/legion/extensions/mlx/runners/models.rb +24 -0
- data/lib/legion/extensions/mlx/version.rb +9 -0
- data/lib/legion/extensions/mlx.rb +19 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8c2365eeeb7c750cec33de25917a6f3b797378cfe1a23d14c72d168638dd0c52
|
|
4
|
+
data.tar.gz: 1e092244c8feb4b5a46cc316dbfce948cf3f1366644c2391b29b5b92786403b5
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: cd3d9d90dabd91955f44f7793bb43b35c6228bd009b58a36e6bd307ef0391ff61dcb6d2ebbe9f2b90fd1deb5dd6b7369aa673ce5597c5c33ac2cd03f180b286f
|
|
7
|
+
data.tar.gz: 2ee461a89bb99ab24cfb97c3331df5056cb649233c92b49e5295c578ef5a710f51d13a0860f60d735811e596b32fdeeef9ea3b8a8c6dbb220960f0a0c5aa3b6d
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches: [main]
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
ci:
|
|
9
|
+
uses: LegionIO/.github/.github/workflows/ci.yml@main
|
|
10
|
+
|
|
11
|
+
release:
|
|
12
|
+
needs: ci
|
|
13
|
+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
14
|
+
uses: LegionIO/.github/.github/workflows/release.yml@main
|
|
15
|
+
secrets:
|
|
16
|
+
rubygems-api-key: ${{ secrets.RUBYGEMS_API_KEY }}
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.4
|
|
3
|
+
NewCops: enable
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
|
|
6
|
+
Layout/LineLength:
|
|
7
|
+
Max: 160
|
|
8
|
+
|
|
9
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
|
10
|
+
EnforcedStyle: space
|
|
11
|
+
|
|
12
|
+
Layout/HashAlignment:
|
|
13
|
+
EnforcedHashRocketStyle: table
|
|
14
|
+
EnforcedColonStyle: table
|
|
15
|
+
|
|
16
|
+
Metrics/MethodLength:
|
|
17
|
+
Max: 50
|
|
18
|
+
|
|
19
|
+
Metrics/ClassLength:
|
|
20
|
+
Max: 1500
|
|
21
|
+
|
|
22
|
+
Metrics/ModuleLength:
|
|
23
|
+
Max: 1500
|
|
24
|
+
|
|
25
|
+
Metrics/BlockLength:
|
|
26
|
+
Max: 40
|
|
27
|
+
Exclude:
|
|
28
|
+
- 'spec/**/*'
|
|
29
|
+
|
|
30
|
+
Metrics/AbcSize:
|
|
31
|
+
Max: 60
|
|
32
|
+
|
|
33
|
+
Metrics/CyclomaticComplexity:
|
|
34
|
+
Max: 15
|
|
35
|
+
|
|
36
|
+
Metrics/PerceivedComplexity:
|
|
37
|
+
Max: 17
|
|
38
|
+
|
|
39
|
+
Metrics/ParameterLists:
|
|
40
|
+
Max: 10
|
|
41
|
+
|
|
42
|
+
Style/Documentation:
|
|
43
|
+
Enabled: false
|
|
44
|
+
|
|
45
|
+
Style/SymbolArray:
|
|
46
|
+
Enabled: true
|
|
47
|
+
|
|
48
|
+
Style/FrozenStringLiteralComment:
|
|
49
|
+
Enabled: true
|
|
50
|
+
EnforcedStyle: always
|
|
51
|
+
|
|
52
|
+
Naming/FileName:
|
|
53
|
+
Enabled: false
|
|
54
|
+
|
|
55
|
+
Naming/PredicateMethod:
|
|
56
|
+
Enabled: false
|
|
57
|
+
|
|
58
|
+
Naming/PredicatePrefix:
|
|
59
|
+
Enabled: false
|
|
60
|
+
|
|
61
|
+
Gemspec/DevelopmentDependencies:
|
|
62
|
+
Enabled: false
|
|
63
|
+
|
|
64
|
+
Lint/EmptyClass:
|
|
65
|
+
Exclude:
|
|
66
|
+
- 'spec/**/*'
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2026-04-13
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Initial release
|
|
8
|
+
- Chat completions via POST /v1/chat/completions (with SSE streaming)
|
|
9
|
+
- Text completions via POST /v1/completions (with SSE streaming)
|
|
10
|
+
- Embeddings via POST /v1/embeddings
|
|
11
|
+
- Model listing via GET /v1/models
|
|
12
|
+
- Retry logic with exponential backoff for transient failures
|
|
13
|
+
- Standalone Client class for use outside LegionIO
|
data/CLAUDE.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# lex-mlx: Apple MLX Integration for LegionIO
|
|
2
|
+
|
|
3
|
+
**Parent**: `/Users/miverso2/rubymine/legion/extensions-ai/CLAUDE.md`
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Legion Extension that connects LegionIO to Apple MLX local LLM server (mlx_lm.server). Provides chat completions, text completions, embeddings, and model listing via the OpenAI-compatible API.
|
|
8
|
+
|
|
9
|
+
**GitHub**: https://github.com/LegionIO/lex-mlx
|
|
10
|
+
**License**: MIT
|
|
11
|
+
|
|
12
|
+
## Architecture
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
Legion::Extensions::Mlx
|
|
16
|
+
├── Runners/
|
|
17
|
+
│ ├── Chat # POST /v1/chat/completions (with SSE streaming)
|
|
18
|
+
│ ├── Completions # POST /v1/completions (with SSE streaming)
|
|
19
|
+
│ ├── Embeddings # POST /v1/embeddings
|
|
20
|
+
│ └── Models # GET /v1/models
|
|
21
|
+
├── Helpers/
|
|
22
|
+
│ ├── Client # Faraday connection to MLX server
|
|
23
|
+
│ ├── Errors # Retry logic with exponential backoff
|
|
24
|
+
│ └── Usage # OpenAI-format usage extraction
|
|
25
|
+
└── Client # Standalone client class
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Dependencies
|
|
29
|
+
|
|
30
|
+
| Gem | Purpose |
|
|
31
|
+
|-----|---------|
|
|
32
|
+
| faraday | HTTP client for MLX REST API |
|
|
33
|
+
|
|
34
|
+
## Testing
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
bundle install
|
|
38
|
+
bundle exec rspec
|
|
39
|
+
bundle exec rubocop
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
**Maintained By**: Matthew Iverson (@Esity)
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LegionIO
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# lex-mlx
|
|
2
|
+
|
|
3
|
+
LegionIO extension for Apple MLX local LLM inference server (`mlx_lm.server`).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'lex-mlx'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require 'legion/extensions/mlx'
|
|
15
|
+
|
|
16
|
+
client = Legion::Extensions::Mlx::Client.new
|
|
17
|
+
# or with custom host:
|
|
18
|
+
# client = Legion::Extensions::Mlx::Client.new(host: 'http://localhost:8080')
|
|
19
|
+
|
|
20
|
+
# Chat
|
|
21
|
+
result = client.chat(model: 'mlx-community/Llama-3-8B-Instruct-4bit',
|
|
22
|
+
messages: [{ role: 'user', content: 'Hello!' }])
|
|
23
|
+
|
|
24
|
+
# Streaming chat
|
|
25
|
+
client.chat_stream(model: 'mlx-community/Llama-3-8B-Instruct-4bit',
|
|
26
|
+
messages: [{ role: 'user', content: 'Hello!' }]) do |event|
|
|
27
|
+
print event[:text] if event[:type] == :delta
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Completions
|
|
31
|
+
result = client.complete(model: 'mlx-community/Llama-3-8B-Instruct-4bit',
|
|
32
|
+
prompt: 'Once upon a time')
|
|
33
|
+
|
|
34
|
+
# Embeddings
|
|
35
|
+
result = client.embed(model: 'mlx-community/Llama-3-8B-Instruct-4bit',
|
|
36
|
+
input: 'Hello world')
|
|
37
|
+
|
|
38
|
+
# List models
|
|
39
|
+
result = client.list_models
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
data/lex-mlx.gemspec
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/legion/extensions/mlx/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'lex-mlx'
|
|
7
|
+
spec.version = Legion::Extensions::Mlx::VERSION
|
|
8
|
+
spec.authors = ['Esity']
|
|
9
|
+
spec.email = ['matthewdiverson@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'LEX MLX'
|
|
12
|
+
spec.description = 'Connects LegionIO to Apple MLX local LLM server'
|
|
13
|
+
spec.homepage = 'https://github.com/LegionIO/lex-mlx'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
spec.required_ruby_version = '>= 3.4'
|
|
16
|
+
|
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/LegionIO/lex-mlx'
|
|
19
|
+
spec.metadata['documentation_uri'] = 'https://github.com/LegionIO/lex-mlx'
|
|
20
|
+
spec.metadata['changelog_uri'] = 'https://github.com/LegionIO/lex-mlx/blob/main/CHANGELOG.md'
|
|
21
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/LegionIO/lex-mlx/issues'
|
|
22
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
23
|
+
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
+
end
|
|
27
|
+
spec.require_paths = ['lib']
|
|
28
|
+
|
|
29
|
+
spec.add_dependency 'faraday', '>= 2.0'
|
|
30
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'helpers/client'
|
|
4
|
+
require_relative 'runners/completions'
|
|
5
|
+
require_relative 'runners/chat'
|
|
6
|
+
require_relative 'runners/models'
|
|
7
|
+
require_relative 'runners/embeddings'
|
|
8
|
+
|
|
9
|
+
module Legion
|
|
10
|
+
module Extensions
|
|
11
|
+
module Mlx
|
|
12
|
+
class Client
|
|
13
|
+
include Helpers::Client
|
|
14
|
+
include Runners::Completions
|
|
15
|
+
include Runners::Chat
|
|
16
|
+
include Runners::Models
|
|
17
|
+
include Runners::Embeddings
|
|
18
|
+
|
|
19
|
+
attr_reader :opts
|
|
20
|
+
|
|
21
|
+
def initialize(host: Helpers::Client::DEFAULT_HOST, **)
|
|
22
|
+
@opts = { host: host }.compact
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def client(**override)
|
|
26
|
+
super(**@opts, **override)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def streaming_client(**override)
|
|
30
|
+
super(**@opts, **override)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Mlx
|
|
8
|
+
module Helpers
|
|
9
|
+
module Client
|
|
10
|
+
DEFAULT_HOST = 'http://localhost:8080'
|
|
11
|
+
|
|
12
|
+
def client(host: DEFAULT_HOST, **)
|
|
13
|
+
Faraday.new(url: host) do |conn|
|
|
14
|
+
conn.request :json
|
|
15
|
+
conn.response :json, content_type: /\bjson$/
|
|
16
|
+
conn.headers['Content-Type'] = 'application/json'
|
|
17
|
+
conn.options.timeout = 300
|
|
18
|
+
conn.options.open_timeout = 10
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def streaming_client(host: DEFAULT_HOST, **)
|
|
23
|
+
Faraday.new(url: host) do |conn|
|
|
24
|
+
conn.request :json
|
|
25
|
+
conn.headers['Content-Type'] = 'application/json'
|
|
26
|
+
conn.options.timeout = 300
|
|
27
|
+
conn.options.open_timeout = 10
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Mlx
|
|
6
|
+
module Helpers
|
|
7
|
+
module Errors
|
|
8
|
+
MAX_RETRIES = 3
|
|
9
|
+
BASE_DELAY = 0.5
|
|
10
|
+
MAX_DELAY = 16
|
|
11
|
+
|
|
12
|
+
RETRYABLE_EXCEPTIONS = [
|
|
13
|
+
Faraday::TimeoutError,
|
|
14
|
+
Faraday::ConnectionFailed
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def retryable?(exception)
|
|
20
|
+
RETRYABLE_EXCEPTIONS.any? { |klass| exception.is_a?(klass) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def with_retry(max_retries: MAX_RETRIES)
|
|
24
|
+
retries = 0
|
|
25
|
+
begin
|
|
26
|
+
yield
|
|
27
|
+
rescue *RETRYABLE_EXCEPTIONS
|
|
28
|
+
retries += 1
|
|
29
|
+
raise if retries > max_retries
|
|
30
|
+
|
|
31
|
+
delay = [BASE_DELAY * (2**(retries - 1)), MAX_DELAY].min
|
|
32
|
+
sleep(delay)
|
|
33
|
+
retry
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Mlx
|
|
6
|
+
module Helpers
|
|
7
|
+
module Usage
|
|
8
|
+
EMPTY_USAGE = {
|
|
9
|
+
input_tokens: 0,
|
|
10
|
+
output_tokens: 0,
|
|
11
|
+
total_tokens: 0
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def from_response(body)
|
|
17
|
+
return EMPTY_USAGE.dup unless body.is_a?(Hash)
|
|
18
|
+
|
|
19
|
+
usage = body['usage']
|
|
20
|
+
return EMPTY_USAGE.dup unless usage.is_a?(Hash)
|
|
21
|
+
|
|
22
|
+
{
|
|
23
|
+
input_tokens: usage['prompt_tokens'] || 0,
|
|
24
|
+
output_tokens: usage['completion_tokens'] || 0,
|
|
25
|
+
total_tokens: usage['total_tokens'] || 0
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'legion/extensions/mlx/helpers/client'
|
|
5
|
+
require 'legion/extensions/mlx/helpers/errors'
|
|
6
|
+
require 'legion/extensions/mlx/helpers/usage'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Mlx
|
|
11
|
+
module Runners
|
|
12
|
+
module Chat
|
|
13
|
+
extend Legion::Extensions::Mlx::Helpers::Client
|
|
14
|
+
|
|
15
|
+
def chat(model:, messages:, temperature: nil, max_tokens: nil, top_p: nil, stream: false, **)
|
|
16
|
+
body = { model: model, messages: messages, temperature: temperature,
|
|
17
|
+
max_tokens: max_tokens, top_p: top_p, stream: stream }.compact
|
|
18
|
+
response = Helpers::Errors.with_retry { client(**).post('/v1/chat/completions', body) }
|
|
19
|
+
{ result: response.body, usage: Helpers::Usage.from_response(response.body), status: response.status }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def chat_stream(model:, messages:, temperature: nil, max_tokens: nil, top_p: nil, **, &block)
|
|
23
|
+
body = { model: model, messages: messages, temperature: temperature,
|
|
24
|
+
max_tokens: max_tokens, top_p: top_p, stream: true }.compact
|
|
25
|
+
accumulated = +''
|
|
26
|
+
usage = nil
|
|
27
|
+
buffer = +''
|
|
28
|
+
|
|
29
|
+
Helpers::Errors.with_retry do
|
|
30
|
+
streaming_client(**).post('/v1/chat/completions', body) do |req|
|
|
31
|
+
req.options.on_data = proc do |chunk, _size|
|
|
32
|
+
buffer << chunk
|
|
33
|
+
while (idx = buffer.index("\n\n"))
|
|
34
|
+
line = buffer.slice!(0, idx + 2).strip
|
|
35
|
+
next if line.empty?
|
|
36
|
+
next unless line.start_with?('data: ')
|
|
37
|
+
|
|
38
|
+
payload = line.sub('data: ', '')
|
|
39
|
+
break if payload == '[DONE]'
|
|
40
|
+
|
|
41
|
+
parsed = ::JSON.parse(payload)
|
|
42
|
+
usage = Helpers::Usage.from_response(parsed) if parsed['usage']
|
|
43
|
+
text = parsed.dig('choices', 0, 'delta', 'content') || ''
|
|
44
|
+
unless text.empty?
|
|
45
|
+
accumulated << text
|
|
46
|
+
block&.call({ type: :delta, text: text })
|
|
47
|
+
end
|
|
48
|
+
block&.call({ type: :done, data: parsed }) if parsed.dig('choices', 0, 'finish_reason')
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
usage ||= Helpers::Usage::EMPTY_USAGE.dup
|
|
55
|
+
{ result: accumulated, usage: usage, status: 200 }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
59
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'legion/extensions/mlx/helpers/client'
|
|
5
|
+
require 'legion/extensions/mlx/helpers/errors'
|
|
6
|
+
require 'legion/extensions/mlx/helpers/usage'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module Mlx
|
|
11
|
+
module Runners
|
|
12
|
+
module Completions
|
|
13
|
+
extend Legion::Extensions::Mlx::Helpers::Client
|
|
14
|
+
|
|
15
|
+
def complete(model:, prompt:, temperature: nil, max_tokens: nil, top_p: nil, stream: false, **)
|
|
16
|
+
body = { model: model, prompt: prompt, temperature: temperature,
|
|
17
|
+
max_tokens: max_tokens, top_p: top_p, stream: stream }.compact
|
|
18
|
+
response = Helpers::Errors.with_retry { client(**).post('/v1/completions', body) }
|
|
19
|
+
{ result: response.body, usage: Helpers::Usage.from_response(response.body), status: response.status }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def complete_stream(model:, prompt:, temperature: nil, max_tokens: nil, top_p: nil, **, &block)
|
|
23
|
+
body = { model: model, prompt: prompt, temperature: temperature,
|
|
24
|
+
max_tokens: max_tokens, top_p: top_p, stream: true }.compact
|
|
25
|
+
accumulated = +''
|
|
26
|
+
usage = nil
|
|
27
|
+
buffer = +''
|
|
28
|
+
|
|
29
|
+
Helpers::Errors.with_retry do
|
|
30
|
+
streaming_client(**).post('/v1/completions', body) do |req|
|
|
31
|
+
req.options.on_data = proc do |chunk, _size|
|
|
32
|
+
buffer << chunk
|
|
33
|
+
while (idx = buffer.index("\n\n"))
|
|
34
|
+
line = buffer.slice!(0, idx + 2).strip
|
|
35
|
+
next if line.empty?
|
|
36
|
+
next unless line.start_with?('data: ')
|
|
37
|
+
|
|
38
|
+
payload = line.sub('data: ', '')
|
|
39
|
+
break if payload == '[DONE]'
|
|
40
|
+
|
|
41
|
+
parsed = ::JSON.parse(payload)
|
|
42
|
+
usage = Helpers::Usage.from_response(parsed) if parsed['usage']
|
|
43
|
+
text = parsed.dig('choices', 0, 'text') || ''
|
|
44
|
+
unless text.empty?
|
|
45
|
+
accumulated << text
|
|
46
|
+
block&.call({ type: :delta, text: text })
|
|
47
|
+
end
|
|
48
|
+
block&.call({ type: :done, data: parsed }) if parsed.dig('choices', 0, 'finish_reason')
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
usage ||= Helpers::Usage::EMPTY_USAGE.dup
|
|
55
|
+
{ result: accumulated, usage: usage, status: 200 }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
59
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/mlx/helpers/client'
|
|
4
|
+
require 'legion/extensions/mlx/helpers/errors'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Extensions
|
|
8
|
+
module Mlx
|
|
9
|
+
module Runners
|
|
10
|
+
module Embeddings
|
|
11
|
+
extend Legion::Extensions::Mlx::Helpers::Client
|
|
12
|
+
|
|
13
|
+
def embed(model:, input:, **)
|
|
14
|
+
body = { model: model, input: input }.compact
|
|
15
|
+
response = Helpers::Errors.with_retry { client(**).post('/v1/embeddings', body) }
|
|
16
|
+
{ result: response.body, status: response.status }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
20
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/mlx/helpers/client'
|
|
4
|
+
require 'legion/extensions/mlx/helpers/errors'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Extensions
|
|
8
|
+
module Mlx
|
|
9
|
+
module Runners
|
|
10
|
+
module Models
|
|
11
|
+
extend Legion::Extensions::Mlx::Helpers::Client
|
|
12
|
+
|
|
13
|
+
def list_models(**)
|
|
14
|
+
response = Helpers::Errors.with_retry { client(**).get('/v1/models') }
|
|
15
|
+
{ result: response.body, status: response.status }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
19
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/mlx/version'
|
|
4
|
+
require 'legion/extensions/mlx/helpers/client'
|
|
5
|
+
require 'legion/extensions/mlx/helpers/errors'
|
|
6
|
+
require 'legion/extensions/mlx/helpers/usage'
|
|
7
|
+
require 'legion/extensions/mlx/runners/completions'
|
|
8
|
+
require 'legion/extensions/mlx/runners/chat'
|
|
9
|
+
require 'legion/extensions/mlx/runners/models'
|
|
10
|
+
require 'legion/extensions/mlx/runners/embeddings'
|
|
11
|
+
require 'legion/extensions/mlx/client'
|
|
12
|
+
|
|
13
|
+
module Legion
|
|
14
|
+
module Extensions
|
|
15
|
+
module Mlx
|
|
16
|
+
extend Legion::Extensions::Core if Legion::Extensions.const_defined? :Core
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lex-mlx
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Esity
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: faraday
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '2.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '2.0'
|
|
26
|
+
description: Connects LegionIO to Apple MLX local LLM server
|
|
27
|
+
email:
|
|
28
|
+
- matthewdiverson@gmail.com
|
|
29
|
+
executables: []
|
|
30
|
+
extensions: []
|
|
31
|
+
extra_rdoc_files: []
|
|
32
|
+
files:
|
|
33
|
+
- ".github/workflows/ci.yml"
|
|
34
|
+
- ".gitignore"
|
|
35
|
+
- ".rspec"
|
|
36
|
+
- ".rubocop.yml"
|
|
37
|
+
- CHANGELOG.md
|
|
38
|
+
- CLAUDE.md
|
|
39
|
+
- Gemfile
|
|
40
|
+
- LICENSE
|
|
41
|
+
- README.md
|
|
42
|
+
- lex-mlx.gemspec
|
|
43
|
+
- lib/legion/extensions/mlx.rb
|
|
44
|
+
- lib/legion/extensions/mlx/client.rb
|
|
45
|
+
- lib/legion/extensions/mlx/helpers/client.rb
|
|
46
|
+
- lib/legion/extensions/mlx/helpers/errors.rb
|
|
47
|
+
- lib/legion/extensions/mlx/helpers/usage.rb
|
|
48
|
+
- lib/legion/extensions/mlx/runners/chat.rb
|
|
49
|
+
- lib/legion/extensions/mlx/runners/completions.rb
|
|
50
|
+
- lib/legion/extensions/mlx/runners/embeddings.rb
|
|
51
|
+
- lib/legion/extensions/mlx/runners/models.rb
|
|
52
|
+
- lib/legion/extensions/mlx/version.rb
|
|
53
|
+
homepage: https://github.com/LegionIO/lex-mlx
|
|
54
|
+
licenses:
|
|
55
|
+
- MIT
|
|
56
|
+
metadata:
|
|
57
|
+
homepage_uri: https://github.com/LegionIO/lex-mlx
|
|
58
|
+
source_code_uri: https://github.com/LegionIO/lex-mlx
|
|
59
|
+
documentation_uri: https://github.com/LegionIO/lex-mlx
|
|
60
|
+
changelog_uri: https://github.com/LegionIO/lex-mlx/blob/main/CHANGELOG.md
|
|
61
|
+
bug_tracker_uri: https://github.com/LegionIO/lex-mlx/issues
|
|
62
|
+
rubygems_mfa_required: 'true'
|
|
63
|
+
rdoc_options: []
|
|
64
|
+
require_paths:
|
|
65
|
+
- lib
|
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '3.4'
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
requirements: []
|
|
77
|
+
rubygems_version: 3.6.9
|
|
78
|
+
specification_version: 4
|
|
79
|
+
summary: LEX MLX
|
|
80
|
+
test_files: []
|