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 +4 -4
- data/README.md +3 -5
- data/lib/json/schema/array.rb +1 -2
- data/lib/json/schema/leaf.rb +27 -0
- data/lib/json/schema/version.rb +6 -0
- data/lib/json/schema.rb +24 -13
- data/lib/llm/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ae98573410bfc74a61f0bf15f811c30800b6567c9e5fc9e4cbe50f4994a200f
|
4
|
+
data.tar.gz: 9776839912017f5f3bac8452670c60b3bc9eabecebe11c402458a36b78d958e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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")
|
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`
|
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
|
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
|
data/lib/json/schema/array.rb
CHANGED
data/lib/json/schema/leaf.rb
CHANGED
@@ -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
|
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
|
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
|
22
|
-
Object.new(properties
|
38
|
+
def object(properties)
|
39
|
+
Object.new(properties)
|
23
40
|
end
|
24
41
|
|
25
42
|
##
|
26
43
|
# Returns an array
|
27
|
-
# @param
|
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
|
31
|
-
Array.new(items
|
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
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.
|
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-
|
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
|