ruby_llm-providers-lms 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.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.flayignore +1 -0
  3. data/.github/workflows/ci.yml +32 -0
  4. data/.github/workflows/gitleaks.yml +22 -0
  5. data/.github/workflows/release.yml +36 -0
  6. data/.overcommit.yml +31 -0
  7. data/.rspec +2 -0
  8. data/.rubocop.yml +29 -0
  9. data/Archspec.rb +14 -0
  10. data/LICENSE +21 -0
  11. data/README.md +87 -0
  12. data/lib/ruby_llm/providers/lms/models.rb +96 -0
  13. data/lib/ruby_llm/providers/lms.rb +55 -0
  14. data/models.json +603 -0
  15. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_openai_gpt_oss_20b_can_handle_a_multi_turn_conversation.yml +92 -0
  16. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_openai_gpt_oss_20b_can_have_a_basic_conversation.yml +45 -0
  17. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_openai_gpt_oss_20b_can_use_tools.yml +129 -0
  18. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_openai_gpt_oss_20b_returns_the_raw_response.yml +73 -0
  19. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_openai_gpt_oss_20b_supports_streaming_responses.yml +81 -0
  20. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_qwen3_0_6b_bible_assistant_can_have_a_basic_conversation.yml +73 -0
  21. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_qwen3_0_6b_bible_assistant_returns_structured_output.yml +73 -0
  22. data/spec/fixtures/vcr_cassettes/rubyllm_chat_lms_qwen3_0_6b_bible_assistant_returns_the_raw_response.yml +73 -0
  23. data/spec/fixtures/vcr_cassettes/rubyllm_embedding_lms_text_embedding_nomic_embed_text_v1_5_embeds_one_text.yml +828 -0
  24. data/spec/fixtures/vcr_cassettes/rubyllm_embedding_lms_text_embedding_nomic_embed_text_v1_5_embeds_several_texts.yml +2375 -0
  25. data/spec/ruby_llm/chat_schema_spec.rb +30 -0
  26. data/spec/ruby_llm/chat_spec.rb +35 -0
  27. data/spec/ruby_llm/chat_streaming_spec.rb +22 -0
  28. data/spec/ruby_llm/chat_tools_spec.rb +30 -0
  29. data/spec/ruby_llm/embedding_spec.rb +49 -0
  30. data/spec/ruby_llm/image_spec.rb +23 -0
  31. data/spec/ruby_llm/models_spec.rb +11 -0
  32. data/spec/ruby_llm/moderation_spec.rb +22 -0
  33. data/spec/ruby_llm/providers/lms_spec.rb +149 -0
  34. data/spec/ruby_llm/rerank_spec.rb +23 -0
  35. data/spec/ruby_llm/speech_spec.rb +25 -0
  36. data/spec/ruby_llm/video_spec.rb +27 -0
  37. data/spec/spec_helper.rb +26 -0
  38. data/spec/support/models.rb +29 -0
  39. data/spec/support/rubyllm_configuration.rb +14 -0
  40. data/spec/support/vcr_configuration.rb +16 -0
  41. metadata +99 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e6ecf4144c152ba49e31f4d1456e5998bc28db598698619a0be7c53bb1d45f01
4
+ data.tar.gz: c76b3200ac422e1ef9ce27344071b98206e1a1f7017f7e3a03e4f26bf5a5d6d3
5
+ SHA512:
6
+ metadata.gz: 669b63bf1a9f3685c4419620be7fcb3218007c2e6e620617f5852ae32a53a21f4b9ae7b4f77cc788c7c2e02265e71d0010ba399eae60392143254feecc857d8e
7
+ data.tar.gz: 919adff659f4c78b594c137a659074af4d35383201b0338e0163d4e5cc2402db3b72ec23e6eaefdcea0de7b60c23429f2223d5630a293512194297c5701d871d
data/.flayignore ADDED
@@ -0,0 +1 @@
1
+ spec/support/streaming_error_helpers.rb
@@ -0,0 +1,32 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ branches: ["main"]
6
+ push:
7
+ branches: ["main"]
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ test:
14
+ name: Ruby ${{ matrix.ruby-version }}
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ ruby-version: ["3.1", "3.2", "3.3", "3.4", "4.0"]
20
+ steps:
21
+ - uses: actions/checkout@v6
22
+
23
+ - uses: ruby/setup-ruby@v1
24
+ with:
25
+ ruby-version: ${{ matrix.ruby-version }}
26
+ bundler-cache: true
27
+
28
+ - run: bundle exec rake
29
+ if: matrix.ruby-version != '3.1'
30
+
31
+ - run: bundle exec rake rubocop flay spec
32
+ if: matrix.ruby-version == '3.1'
@@ -0,0 +1,22 @@
1
+ name: Gitleaks
2
+
3
+ on:
4
+ pull_request:
5
+ branches: ["main"]
6
+ push:
7
+ branches: ["main"]
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ scan:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v6
17
+ with:
18
+ fetch-depth: 0
19
+
20
+ - uses: gitleaks/gitleaks-action@v2
21
+ env:
22
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,36 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ publish:
13
+ name: Build and publish
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v6
17
+
18
+ - uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: "3.4"
21
+ bundler-cache: true
22
+
23
+ - run: bundle exec rake
24
+
25
+ - name: Build gem
26
+ run: gem build ruby_llm-providers-lms.gemspec
27
+
28
+ - name: Publish to RubyGems
29
+ run: |
30
+ mkdir -p "$HOME/.gem"
31
+ touch "$HOME/.gem/credentials"
32
+ chmod 0600 "$HOME/.gem/credentials"
33
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > "$HOME/.gem/credentials"
34
+ gem push ruby_llm-providers-lms-*.gem
35
+ env:
36
+ GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_AUTH_TOKEN }}
data/.overcommit.yml ADDED
@@ -0,0 +1,31 @@
1
+ PreCommit:
2
+ RuboCop:
3
+ enabled: true
4
+ auto_correct: true
5
+ on_warn: fail
6
+
7
+ Flay:
8
+ enabled: true
9
+ include:
10
+ - lib/**/*.rb
11
+ - spec/**/*.rb
12
+ mass_threshold: 70
13
+
14
+ ArchSpec:
15
+ enabled: true
16
+ command: ['bundle', 'exec', 'archspec', 'check']
17
+ on_warn: fail
18
+
19
+ RSpec:
20
+ enabled: true
21
+ command: ['bundle', 'exec', 'rspec']
22
+ on_warn: fail
23
+
24
+ TrailingWhitespace:
25
+ enabled: true
26
+ auto_correct: true
27
+
28
+ Gitleaks:
29
+ enabled: true
30
+ required_executable: gitleaks
31
+ command: ['gitleaks', 'protect', '--staged', '--redact', '--verbose']
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --format progress
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ plugins:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ NewCops: enable
8
+ TargetRubyVersion: 3.1
9
+ Exclude:
10
+ - Archspec.rb
11
+ - vendor/**/*
12
+ - gemfiles/**/*
13
+ SuggestExtensions: false
14
+
15
+ Metrics/AbcSize:
16
+ Max: 80
17
+ Metrics/MethodLength:
18
+ Max: 40
19
+ CountAsOne:
20
+ - array
21
+ - hash
22
+ - heredoc
23
+ Metrics/BlockLength:
24
+ Exclude:
25
+ - spec/**/*.rb
26
+ RSpec/ExampleLength:
27
+ Enabled: false
28
+ RSpec/MultipleExpectations:
29
+ Enabled: false
data/Archspec.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'lib/**/*.rb'
4
+
5
+ component :provider,
6
+ in: %w[
7
+ lib/ruby_llm/providers/lms.rb
8
+ lib/ruby_llm/providers/lms/**/*.rb
9
+ ],
10
+ namespace: 'RubyLLM::Providers::LMS'
11
+
12
+ provider.cannot_reference_constants 'RSpec', 'WebMock', 'VCR'
13
+
14
+ preset :ruby_conventions
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 madbomber
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,87 @@
1
+ # ruby_llm-providers-lms
2
+
3
+ [RubyLLM](https://rubyllm.com) provider gem for [LM Studio](https://lmstudio.ai) — run
4
+ local models through LM Studio's OpenAI-compatible server (`lms server start`,
5
+ `http://localhost:1234/v1` by default).
6
+
7
+ The provider works out of the box: no API key, no configuration. It speaks the
8
+ Chat Completions dialect by default and also registers the Responses protocol
9
+ (LM Studio serves `/v1/responses` too). Model listings are enriched with details
10
+ from LM Studio's native REST API (`/api/v0/models`): architecture, quantization,
11
+ load state, context length, and tool/vision capabilities.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'ruby_llm-providers-lms', require: 'ruby_llm/providers/lms'
19
+ ```
20
+
21
+ Configuration is optional. Override the defaults only when your server is not
22
+ on `http://localhost:1234/v1` or sits behind an auth proxy:
23
+
24
+ ```ruby
25
+ require 'ruby_llm/providers/lms'
26
+
27
+ RubyLLM.configure do |config|
28
+ config.lms_api_base = ENV.fetch('LMS_API_BASE', 'http://localhost:1234/v1')
29
+ config.lms_api_key = ENV['LMS_API_KEY'] # only if your server requires auth
30
+ end
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Use the model identifier shown in LM Studio (not an OpenAI model name):
36
+
37
+ ```ruby
38
+ chat = RubyLLM.chat(model: 'qwen/qwen3-4b', provider: :lms)
39
+ response = chat.ask('Hello')
40
+ puts response.content
41
+ ```
42
+
43
+ Because LM Studio is a local provider, RubyLLM assumes any model id you pass
44
+ exists — LM Studio will just-in-time load the model if it isn't loaded yet.
45
+ To browse what the server offers:
46
+
47
+ ```ruby
48
+ RubyLLM.models.refresh!
49
+ RubyLLM.models.by_provider(:lms).each { |model| puts model.id }
50
+ ```
51
+
52
+ Embeddings work the same way:
53
+
54
+ ```ruby
55
+ RubyLLM.embed('Hello world', model: 'text-embedding-nomic-embed-text-v1.5', provider: :lms)
56
+ ```
57
+
58
+ To use the Responses dialect instead of Chat Completions:
59
+
60
+ ```ruby
61
+ RubyLLM.configure { |config| config.lms_protocol = :responses }
62
+ ```
63
+
64
+ ## Development
65
+
66
+ Start the LM Studio server (`lms server start`), then:
67
+
68
+ ```sh
69
+ bundle exec rake models # refresh models.json from your local server
70
+ bundle exec rake # rubocop, flay, archspec, specs
71
+ ```
72
+
73
+ `rake models` calls the local server's model-listing endpoints and writes
74
+ `models.json` at the gem root. RubyLLM loads that catalog as a fallback when
75
+ this provider is registered. Since LM Studio catalogs are machine-specific
76
+ (they list whatever you have downloaded), the packaged catalog is only a
77
+ sample — live listings via `RubyLLM.models.refresh!` reflect your machine.
78
+
79
+ The suite always runs the provider integration specs. The first local run
80
+ calls the API and records VCR cassettes; CI only replays committed cassettes.
81
+ A failing example deletes its cassette so the next local run tests the live
82
+ API again.
83
+
84
+ After refreshing the catalog, put real model IDs from your machine into
85
+ `spec/support/models.rb`. Keep only the operation matrices LM Studio supports
86
+ (chat, tools, structured output, embeddings). The portable contract specs are
87
+ adapted from [RubyLLM's live specs](https://github.com/crmne/ruby_llm/tree/main/spec/ruby_llm).
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Providers
5
+ class LMS < Provider
6
+ # Model-listing methods for the LM Studio API integration.
7
+ # Enriches the OpenAI-compatible /v1/models listing with details from
8
+ # LM Studio's native REST API (/api/v0/models): model type, architecture,
9
+ # quantization, load state, and context length. The native endpoint is
10
+ # optional — when it is unavailable the plain listing still works.
11
+ module Models
12
+ NATIVE_MODELS_URL = '../api/v0/models'
13
+ CAPABILITY_MAP = {
14
+ 'tool_use' => 'function_calling',
15
+ 'vision' => 'vision'
16
+ }.freeze
17
+
18
+ def models_url
19
+ 'models'
20
+ end
21
+
22
+ def model_capabilities
23
+ %w[streaming structured_output]
24
+ end
25
+
26
+ def list_models
27
+ response = @connection.get models_url
28
+ parse_list_models_response(response, @provider.slug, details: native_model_details)
29
+ end
30
+
31
+ def parse_list_models_response(response, slug, details: {})
32
+ Array(response.body['data']).map do |model|
33
+ detail = details[model['id']] || {}
34
+ Model.new(
35
+ id: model['id'],
36
+ name: model['id'],
37
+ provider: slug,
38
+ family: detail['arch'] || 'lms',
39
+ created_at: model['created'] ? Time.at(model['created']) : nil,
40
+ context_window: detail['max_context_length'],
41
+ modalities: build_modalities(detail),
42
+ capabilities: build_capabilities(detail),
43
+ pricing: {},
44
+ metadata: build_metadata(model, detail)
45
+ )
46
+ end
47
+ end
48
+
49
+ def build_modalities(detail)
50
+ return { input: %w[text], output: %w[embeddings] } if embedding?(detail)
51
+
52
+ input = %w[text]
53
+ input << 'image' if vision?(detail)
54
+ { input: input, output: %w[text] }
55
+ end
56
+
57
+ def build_capabilities(detail)
58
+ return [] if embedding?(detail)
59
+
60
+ reported = Array(detail['capabilities'])
61
+ derived = CAPABILITY_MAP.filter_map { |native, capability| capability if reported.include?(native) }
62
+ derived << 'vision' if vision?(detail) && !derived.include?('vision')
63
+ model_capabilities + derived
64
+ end
65
+
66
+ def build_metadata(model, detail)
67
+ {
68
+ owned_by: model['owned_by'],
69
+ publisher: detail['publisher'],
70
+ arch: detail['arch'],
71
+ compatibility_type: detail['compatibility_type'],
72
+ quantization: detail['quantization'],
73
+ state: detail['state']
74
+ }.compact
75
+ end
76
+
77
+ def embedding?(detail)
78
+ detail['type'] == 'embeddings'
79
+ end
80
+
81
+ def vision?(detail)
82
+ detail['type'] == 'vlm' || Array(detail['capabilities']).include?('vision')
83
+ end
84
+
85
+ private
86
+
87
+ def native_model_details
88
+ Array(@connection.get(NATIVE_MODELS_URL).body['data']).to_h { |detail| [detail['id'], detail] }
89
+ rescue Error, Faraday::Error => e
90
+ RubyLLM.logger.debug "LM Studio native model details unavailable (#{e.message})."
91
+ {}
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_llm'
4
+ require 'ruby_llm/providers/lms/models'
5
+
6
+ module RubyLLM
7
+ module Providers
8
+ # LM Studio API integration. Talks to the local LM Studio server
9
+ # (`lms server start`) through its OpenAI-compatible endpoints.
10
+ # Works out of the box against http://localhost:1234/v1 — no API key
11
+ # required.
12
+ class LMS < Provider
13
+ DEFAULT_API_BASE = 'http://localhost:1234/v1'
14
+
15
+ # LM Studio's dialect of the Chat Completions API.
16
+ class ChatCompletions < Protocols::ChatCompletions
17
+ include LMS::Models
18
+ end
19
+
20
+ protocol :chat_completions, ChatCompletions
21
+ protocol :responses, Protocols::Responses
22
+
23
+ def api_base
24
+ @config.lms_api_base || DEFAULT_API_BASE
25
+ end
26
+
27
+ def headers
28
+ return {} unless @config.lms_api_key
29
+
30
+ { 'Authorization' => "Bearer #{@config.lms_api_key}" }
31
+ end
32
+
33
+ class << self
34
+ def display_name
35
+ 'LM Studio'
36
+ end
37
+
38
+ def configuration_options
39
+ %i[lms_api_base lms_api_key]
40
+ end
41
+
42
+ def configuration_requirements
43
+ []
44
+ end
45
+
46
+ def local?
47
+ true
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ RubyLLM::Provider.register :lms, RubyLLM::Providers::LMS,
55
+ models: File.expand_path('../../../models.json', __dir__)