rubyml-ai 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 (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/train/version.rb +5 -0
  3. data/lib/train.rb +99 -0
  4. metadata +42 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ab0d3ab12880405fa0846a88e13f2fc4ccd1a189d1e05bc4bafc2851bff95c1a
4
+ data.tar.gz: 7cdea16e0a71730a64a868a77b4c54c9d4bb959ff41763168a554e1ade761294
5
+ SHA512:
6
+ metadata.gz: eccb769dbb5341a6094bea951c06f5ad5fdc4f297067edb249ab7f1f31905036703245eb47c6734240653ebd645625cfed37b4caecf0478bc9dcd6457ed001af
7
+ data.tar.gz: 8c7a15fc5e5f57e64b07d0513b3f6f47e50a5cec19cce056e7db7ec5e91388c780d700e9ae0f3fea86068cd82088671b40bbeb6aba8fde19818268875e3f590e
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Train
4
+ VERSION = "0.1.0"
5
+ end
data/lib/train.rb ADDED
@@ -0,0 +1,99 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ require "net/http"
5
+ require "json"
6
+ require "uri"
7
+
8
+ module Train
9
+ module MODELS
10
+ @default_map = {
11
+ "GPT-3.5 Turbo" => "gpt-3.5-turbo",
12
+ "GPT-4o" => "gpt-4o",
13
+ "Gemini 2.5 Pro"=> "google/gemini-1.5-pro-latest",
14
+ "Gemini 1.5" => "google/gemini-1.5-flash-latest",
15
+ "DeepSeek R1" => "deepseek/deepseek-r1-0528-qwen3-8b:free"
16
+ }
17
+
18
+ def self.get(name)
19
+ @default_map[name]
20
+ end
21
+
22
+ def self.all
23
+ @default_map
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+
30
+ ##
31
+ # Train::Chat::INPUT for interactive terminal loop
32
+ module Chat
33
+ INPUT = :__interactive__
34
+
35
+ def self.start(model)
36
+ puts "🚂 Ruby AI Train Interactive Mode"
37
+ puts "Type a message. Empty input or Ctrl+C to exit.\n\n"
38
+
39
+ loop do
40
+ print "Prompt: "
41
+ prompt = $stdin.gets&.chomp
42
+ break if prompt.nil? || prompt.strip.empty?
43
+
44
+ puts "AI Answer: #{model.chat(prompt)}\n\n"
45
+ end
46
+ rescue Interrupt
47
+ puts "\nGoodbye!"
48
+ end
49
+ end
50
+
51
+ ##
52
+ # Train::Model represents a connection to an AI model endpoint
53
+ class Train::Model
54
+ attr_accessor :api_key, :url, :model_name
55
+
56
+ def initialize(api_key:, url:, model_name:, model_id_changed: nil)
57
+ @api_key = api_key
58
+ @url = URI.parse(url)
59
+
60
+ # 🔁 override if custom model ID provided
61
+ default_id = Train::MODELS.get(model_name)
62
+ @model_name = model_id_changed || default_id || model_name
63
+ end
64
+
65
+
66
+ ##
67
+ # Sends a chat message to the AI model
68
+ #
69
+ # @param prompt [String or Symbol] User message or Train::Chat::INPUT for interactive mode
70
+ # @param system [String] Optional system instruction
71
+ # @return [String] AI-generated response or fallback message
72
+ def chat(prompt, system: "You are a helpful AI.")
73
+ return Chat.start(self) if prompt == Chat::INPUT
74
+
75
+ http = Net::HTTP.new(url.host, url.port)
76
+ http.use_ssl = (url.scheme == "https")
77
+
78
+ headers = {
79
+ "Content-Type" => "application/json",
80
+ "Authorization" => "Bearer #{api_key}"
81
+ }
82
+
83
+ body = {
84
+ model: model_name,
85
+ messages: [
86
+ { role: "system", content: system },
87
+ { role: "user", content: prompt }
88
+ ]
89
+ }.to_json
90
+
91
+ response = http.post(url.path, body, headers)
92
+ json = JSON.parse(response.body)
93
+
94
+ json["choices"]&.first&.dig("message", "content") || "⚠️ No response"
95
+ rescue => e
96
+ "❌ Train error: #{e.message}"
97
+ end
98
+ end
99
+
metadata ADDED
@@ -0,0 +1,42 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyml-ai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cortlet
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A smarter ride into the AI future. 'train' brings Ruby back to domination.
13
+ email:
14
+ - kkuppuluri0@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/train.rb
20
+ - lib/train/version.rb
21
+ homepage: https://github.com/Cortlet/train
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.6'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.6.9
40
+ specification_version: 4
41
+ summary: Ruby AI Train – Chat with LLMs from Ruby
42
+ test_files: []