simple_llm 0.0.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 (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/lib/simple_llm.rb +94 -0
  4. data/simple_llm.gemspec +15 -0
  5. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41fa4b4f324b9c18eea8b5325d740b3575148f636fc681479a99d656d2b292a7
4
+ data.tar.gz: 02b7447deb5930df1119b7576495e92ac93e502bd2d8a83690a83ddd384be4f9
5
+ SHA512:
6
+ metadata.gz: a49b36d277ed58c279094c4af2e2af35af02c8788a6709bb1c1df108e66cdcff0fc7ddccdc2b86c94eeec54d3eaf5e022bc712de16ec68ba40528fb2894f9d27
7
+ data.tar.gz: 47538a761188bb5364d851e80a18b8a261272f36e97fa814d24a92b152678e4e5e7338cc433a2b5b4d4ced7a647d69b8587314e8ad9b4fdc942a95b879a23efd
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Victor Maslov
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/lib/simple_llm.rb ADDED
@@ -0,0 +1,94 @@
1
+ require_relative "refinement_array"
2
+ using ::RefinementArray
3
+
4
+ module SimpleLLM
5
+ require "nethttputils"
6
+
7
+ Error = ::Class.new ::RuntimeError
8
+
9
+ module Common
10
+
11
+ require "skjvs"
12
+ # @param cache_filename [String, nil, false] если передать `nil` или `false`, кеширования не будет
13
+ def initialize cache_filename = "simple_llm_cache.jsonl", &cache_key_string_function
14
+ @cache_key_string_function = cache_key_string_function || lambda do |
15
+ cls,
16
+ time,
17
+ auth,
18
+ pid,
19
+ model,
20
+ input,
21
+ max_output_tokens,
22
+ temperature,
23
+ instructions,
24
+ |
25
+ "#{instructions} #{input}"
26
+ end
27
+ @cache = ::SKJVS::OneFile.new cache_filename if cache_filename
28
+ end
29
+
30
+ end
31
+
32
+ module Yandex
33
+
34
+ class Sync
35
+ require "json"
36
+ require "digest"
37
+ include Common
38
+ # TODO: ассертить размер input-а при image_input как-то иначе
39
+ # https://aistudio.yandex.ru/docs/ru/ai-studio/api/Responses/createResponse.html
40
+ # @param project
41
+ # @param api_key
42
+ # @param input [Object] https://aistudio.yandex.ru/docs/ru/ai-studio/api/Responses/createResponse.html#entity-InputParam
43
+ # @param max_output_tokens [Integer] "An upper bound for the number of tokens that can be generated for a response, including visible output tokens and reasoning tokens."
44
+ # @param temperature [Integer] "What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic."
45
+ # @param model [String]
46
+ # @param instructions
47
+ # @param max_input_chars [Integer] предохранительный ассерт, чтобы метод не вызвали со слишком тяжелым большим input-ом
48
+ # @param debug [true, false] печатать запросы и ответы
49
+ # @return [String] текст из `["output"]["content"][0]["text"]`
50
+ def call project, api_key, input, max_output_tokens = 500, temperature = 0, model: "yandexgpt/rc", instructions: nil, max_input_chars: 1000, debug: false
51
+ raise ::ArgumentError, "max_input_chars assertion: #{input.to_s.size}" if input.to_s.size > max_input_chars
52
+ request = lambda do
53
+ ::NetHTTPUtils.request_data(
54
+ "https://ai.api.cloud.yandex.net/v1/responses", :post, :json, header: {
55
+ "OpenAI-Project" => project,
56
+ "Authorization" => "Api-Key #{api_key}",
57
+ "x-data-logging-enabled" => "false", # https://aistudio.yandex.ru/docs/ru/ai-studio/operations/disable-logging.html
58
+ }, form: {
59
+ model: "gpt://#{project}/#{model}",
60
+ instructions: instructions,
61
+ input: input,
62
+ temperature: temperature,
63
+ max_output_tokens: max_output_tokens,
64
+ # reasoning: {effort: "low"},
65
+ }.compact.tap{ |_| ::STDERR.puts ::JSON.pretty_generate _ if debug }
66
+ ).force_encoding("utf-8")
67
+ end
68
+ ::JSON.parse(
69
+ if @cache
70
+ @cache[ @cache_key_string_function.(
71
+ self.class,
72
+ ::Time.now,
73
+ ::Digest::MD5.hexdigest(api_key),
74
+ ::Process::pid,
75
+ model,
76
+ input,
77
+ max_output_tokens,
78
+ temperature,
79
+ instructions,
80
+ ) ] ||= request.call
81
+ else
82
+ request.call
83
+ end
84
+ ).tap{ |_| ::STDERR.puts ::JSON.pretty_generate _ if debug }["output"].select{ |_| "message" == _["type"] }.map do |message|
85
+ message["content"].assert_one.fetch("text").strip
86
+ end.assert_one_or_less or raise Error, "empty message assertion"
87
+ end
88
+ end
89
+
90
+ # class Async
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "simple_llm"
3
+ spec.version = "0.0.0"
4
+ spec.summary = "common wrapper for all LLM providers"
5
+
6
+ spec.author = "Victor Maslov aka Nakilon"
7
+ spec.email = "nakilon@gmail.com"
8
+ spec.license = "MIT"
9
+ spec.metadata = {"source_code_uri" => "https://github.com/nakilon/simple_llm"}
10
+
11
+ spec.add_dependency "nethttputils"
12
+ spec.add_dependency "skjvs"
13
+
14
+ spec.files = %w{ LICENSE simple_llm.gemspec lib/simple_llm.rb }
15
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_llm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Maslov aka Nakilon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nethttputils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: skjvs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: nakilon@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - lib/simple_llm.rb
49
+ - simple_llm.gemspec
50
+ homepage:
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ source_code_uri: https://github.com/nakilon/simple_llm
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: common wrapper for all LLM providers
74
+ test_files: []