llm.rb 0.4.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46af7f9487355b1ad33501bdc2602f2bfbb9ac3a7a3377eb601d367167373b69
4
- data.tar.gz: f1bb4dc1b423e5335d19540c664af714d523ecded97476fb379711f2015fa93a
3
+ metadata.gz: 2ae98573410bfc74a61f0bf15f811c30800b6567c9e5fc9e4cbe50f4994a200f
4
+ data.tar.gz: 9776839912017f5f3bac8452670c60b3bc9eabecebe11c402458a36b78d958e7
5
5
  SHA512:
6
- metadata.gz: b249df823bedda3041df65b4f17b861d7529737855c2395dbcb978838098eecb0950ec935e06f290d9737f7b780c64a42cd21bf54e8077360947c1e70c3bad04
7
- data.tar.gz: 0a35370e5cd41488dfcbc05259c28ac906e1a3b217f4882aebfb87a8ee5b9d04ca771df9ed77d32efc0782510bc35c33db7a0704a81841ae1b8f9d832d5f3af7
6
+ metadata.gz: 4252d2d861d409428067415340c2d36f2e75f34f3be9905a576a63455902d1faf4100d8c8c7060e683fe922ff095cc00fd56fd135b9589a746d9de716c66e5e5
7
+ data.tar.gz: 819676961e401aa5e27678e3d36af406362f0a68b0243e1e1f71f5f135286830dd1c009c47c69c26b6789a771da6a1fe72eb023f0f1833c20ffadac37f58ed1b
data/README.md CHANGED
@@ -133,7 +133,7 @@ The interface is designed so you could drop in any other library in its place:
133
133
  require "llm"
134
134
 
135
135
  llm = LLM.openai(ENV["KEY"])
136
- schema = llm.schema.object({os: llm.schema.string.enum("OpenBSD", "FreeBSD", "NetBSD").required})
136
+ schema = llm.schema.object({os: llm.schema.string.enum("OpenBSD", "FreeBSD", "NetBSD")})
137
137
  bot = LLM::Chat.new(llm, schema:)
138
138
  bot.chat "You secretly love NetBSD", :system
139
139
  bot.chat "What operating system is the best?", :user
@@ -364,7 +364,7 @@ bot.messages.select(&:assistant?).each { print "[#{_1.role}] ", _1.content, "\n"
364
364
  Generally all providers accept text prompts but some providers can
365
365
  also understand URLs, and various file types (eg images, audio, video,
366
366
  etc). The llm.rb approach to multimodal prompts is to let you pass `URI`
367
- objects to describe links, `LLM::File` / `LLM::Response::File` objects
367
+ objects to describe links, `LLM::File` | `LLM::Response::File` objects
368
368
  to describe files, `String` objects to describe text blobs, or an array
369
369
  of the aforementioned objects to describe multiple objects in a single
370
370
  prompt. Each object is a first class citizen that can be passed directly
@@ -372,9 +372,7 @@ to a prompt.
372
372
 
373
373
  For more depth and examples on how to use the multimodal API, please see
374
374
  the [provider-specific documentation](https://0x1eef.github.io/x/llm.rb/)
375
- for more provider-specific examples – there can be subtle differences
376
- between providers and even between APIs from the same provider that are
377
- not covered in the README:
375
+ for more provider-specific examples:
378
376
 
379
377
  ```ruby
380
378
  #!/usr/bin/env ruby
@@ -2,9 +2,8 @@
2
2
 
3
3
  class JSON::Schema
4
4
  class Array < Leaf
5
- def initialize(items, **rest)
5
+ def initialize(*items)
6
6
  @items = items
7
- super(**rest)
8
7
  end
9
8
 
10
9
  def to_h
@@ -1,6 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JSON::Schema
4
+ ##
5
+ # The {JSON::Schema::Leaf JSON::Schema::Leaf} class is the
6
+ # superclass of all values that can appear in a JSON schema.
7
+ # See the instance methods of {JSON::Schema JSON::Schema} for
8
+ # an example of how to create instances of {JSON::Schema::Leaf JSON::Schema::Leaf}
9
+ # through its subclasses.
4
10
  class Leaf
5
11
  def initialize
6
12
  @description = nil
@@ -9,30 +15,51 @@ class JSON::Schema
9
15
  @required = nil
10
16
  end
11
17
 
18
+ ##
19
+ # Set the description of a leaf
20
+ # @param [String] str The description
21
+ # @return [JSON::Schema::Leaf]
12
22
  def description(str)
13
23
  tap { @description = str }
14
24
  end
15
25
 
26
+ ##
27
+ # Set the default value of a leaf
28
+ # @param [Object] value The default value
29
+ # @return [JSON::Schema::Leaf]
16
30
  def default(value)
17
31
  tap { @default = value }
18
32
  end
19
33
 
34
+ ##
35
+ # Set the allowed values of a leaf
36
+ # @param [Array] values The allowed values
37
+ # @return [JSON::Schema::Leaf]
20
38
  def enum(*values)
21
39
  tap { @enum = values }
22
40
  end
23
41
 
42
+ ##
43
+ # Denote a leaf as required
44
+ # @return [JSON::Schema::Leaf]
24
45
  def required
25
46
  tap { @required = true }
26
47
  end
27
48
 
49
+ ##
50
+ # @return [Hash]
28
51
  def to_h
29
52
  {description: @description, default: @default, enum: @enum}.compact
30
53
  end
31
54
 
55
+ ##
56
+ # @return [String]
32
57
  def to_json(options = {})
33
58
  to_h.to_json(options)
34
59
  end
35
60
 
61
+ ##
62
+ # @return [Boolean]
36
63
  def required?
37
64
  @required
38
65
  end
@@ -0,0 +1,6 @@
1
+ module JSON
2
+ end unless defined?(JSON)
3
+
4
+ class JSON::Schema
5
+ VERSION = "0.1.0"
6
+ end
data/lib/json/schema.rb CHANGED
@@ -3,7 +3,25 @@
3
3
  module JSON
4
4
  end unless defined?(JSON)
5
5
 
6
+ ##
7
+ # The {JSON::Schema JSON::Schema} class represents a JSON schema,
8
+ # and provides methods that let you describe and produce a schema
9
+ # that can be used in various contexts that include the validation
10
+ # and generation of JSON data.
11
+ #
12
+ # @see https://json-schema.org/ JSON Schema Specification
13
+ # @see https://tour.json-schema.org/ JSON Schema Tour
14
+ #
15
+ # @example
16
+ # schema = JSON::Schema.new
17
+ # schema.object({
18
+ # name: schema.string.enum("John", "Jane").required,
19
+ # age: schema.integer.required,
20
+ # hobbies: schema.array(schema.string, schema.null).required,
21
+ # address: schema.object({street: schema.string}).required,
22
+ # })
6
23
  class JSON::Schema
24
+ require_relative "schema/version"
7
25
  require_relative "schema/leaf"
8
26
  require_relative "schema/object"
9
27
  require_relative "schema/array"
@@ -15,25 +33,22 @@ class JSON::Schema
15
33
 
16
34
  ##
17
35
  # Returns an object
18
- # @param properties [Hash] A hash of properties
19
- # @param rest [Hash] Any other options
36
+ # @param [Hash] properties A hash of properties
20
37
  # @return [JSON::Schema::Object]
21
- def object(properties, **rest)
22
- Object.new(properties, **rest)
38
+ def object(properties)
39
+ Object.new(properties)
23
40
  end
24
41
 
25
42
  ##
26
43
  # Returns an array
27
- # @param items [Array] An array of items
28
- # @param rest [Hash] Any other options
44
+ # @param [Array] items An array of items
29
45
  # @return [JSON::Schema::Array]
30
- def array(items, **rest)
31
- Array.new(items, **rest)
46
+ def array(*items)
47
+ Array.new(*items)
32
48
  end
33
49
 
34
50
  ##
35
51
  # Returns a string
36
- # @param rest [Hash] Any other options
37
52
  # @return [JSON::Schema::String]
38
53
  def string(...)
39
54
  String.new(...)
@@ -41,7 +56,6 @@ class JSON::Schema
41
56
 
42
57
  ##
43
58
  # Returns a number
44
- # @param rest [Hash] Any other options
45
59
  # @return [JSON::Schema::Number] a number
46
60
  def number(...)
47
61
  Number.new(...)
@@ -49,7 +63,6 @@ class JSON::Schema
49
63
 
50
64
  ##
51
65
  # Returns an integer
52
- # @param rest [Hash] Any other options
53
66
  # @return [JSON::Schema::Integer]
54
67
  def integer(...)
55
68
  Integer.new(...)
@@ -57,7 +70,6 @@ class JSON::Schema
57
70
 
58
71
  ##
59
72
  # Returns a boolean
60
- # @param rest [Hash] Any other options
61
73
  # @return [JSON::Schema::Boolean]
62
74
  def boolean(...)
63
75
  Boolean.new(...)
@@ -65,7 +77,6 @@ class JSON::Schema
65
77
 
66
78
  ##
67
79
  # Returns null
68
- # @param rest [Hash] Any other options
69
80
  # @return [JSON::Schema::Null]
70
81
  def null(...)
71
82
  Null.new(...)
data/lib/llm/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LLM
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: llm.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antar Azri
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-04-29 00:00:00.000000000 Z
12
+ date: 2025-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: webmock
@@ -158,6 +158,7 @@ files:
158
158
  - lib/json/schema/number.rb
159
159
  - lib/json/schema/object.rb
160
160
  - lib/json/schema/string.rb
161
+ - lib/json/schema/version.rb
161
162
  - lib/llm.rb
162
163
  - lib/llm/buffer.rb
163
164
  - lib/llm/chat.rb