ai-chat 0.3.0 → 0.3.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 +34 -0
- data/ai-chat.gemspec +1 -1
- data/lib/ai/chat.rb +20 -6
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4dd73c65bb0fa8183801233a76038c8e9a32f86268ab113311317a47f72504db
|
|
4
|
+
data.tar.gz: 8a6541039268eee87a035f7547d767919962b8f20573e06618700e05ea72ffe6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b2564b32f5f1c69e8749eb079339d120cbfbf71ce9c5870cfb7b47ea81891ec11dbc9290b59f2b49b35830f59d7f0817d3dcaa558bc5ef1d97309a0a26da3733
|
|
7
|
+
data.tar.gz: ae9ebf5fd4b0a1001e6fb047585c852055c43cfc2b7af7fc3385ed8df355aec260e2ba6e80a2443b9104648a49356124974353317d3818449413fe923ab27a0a
|
data/README.md
CHANGED
|
@@ -362,6 +362,40 @@ i.schema = '{"name":"nutrition_values","strict":true,"schema":{...}}'
|
|
|
362
362
|
i.schema = '{"type":"object","properties":{...}}'
|
|
363
363
|
```
|
|
364
364
|
|
|
365
|
+
### Generating a Schema
|
|
366
|
+
|
|
367
|
+
You can call the class method `AI::Chat.generate_schema!` to use OpenAI to generate a JSON schema for you given a `String` describing the schema you want.
|
|
368
|
+
|
|
369
|
+
```rb
|
|
370
|
+
AI::Chat.generate_schema!("A user profile with name (required), email (required), age (number), and bio (optional text).")
|
|
371
|
+
# => "{ ... }"
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
This method returns a String containing the JSON schema. The JSON schema also writes (or overwrites) to `schema.json` at the root of the project.
|
|
375
|
+
|
|
376
|
+
Similar to generating messages with `AI::Chat` objects, this class method will assume that you have an API key called `OPENAI_API_KEY` defined. You can also pass the API key directly or choose a different environment variable key for it to use.
|
|
377
|
+
|
|
378
|
+
```rb
|
|
379
|
+
# Passing the API key directly
|
|
380
|
+
AI::Chat.generate_schema!("A user with full name (required), first_name (required), and last_name (required).", api_key: "MY_SECRET_API_KEY")
|
|
381
|
+
|
|
382
|
+
# Choosing a different API key name
|
|
383
|
+
AI::Chat.generate_schema!("A user with full name (required), first_name (required), and last_name (required).", api_key_env_var: "CUSTOM_KEY")
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
You can choose a location for the schema to save by using the `location` keyword argument.
|
|
387
|
+
|
|
388
|
+
```rb
|
|
389
|
+
AI::Chat.generate_schema!("A user with full name (required), first_name (required), and last_name (required).", location: "my_schemas/user.json")
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
If you don't want to write the output to a file, you can pass `false` to `location`.
|
|
393
|
+
|
|
394
|
+
```rb
|
|
395
|
+
AI::Chat.generate_schema!("A user with full name (required), first_name (required), and last_name (required).", location: false)
|
|
396
|
+
# => { ... }
|
|
397
|
+
```
|
|
398
|
+
|
|
365
399
|
### Schema Notes
|
|
366
400
|
|
|
367
401
|
- The keys can be `String`s or `Symbol`s.
|
data/ai-chat.gemspec
CHANGED
data/lib/ai/chat.rb
CHANGED
|
@@ -23,7 +23,7 @@ module AI
|
|
|
23
23
|
class Chat
|
|
24
24
|
# :reek:Attribute
|
|
25
25
|
attr_accessor :background, :code_interpreter, :conversation_id, :image_generation, :image_folder, :messages, :model, :proxy, :previous_response_id, :web_search
|
|
26
|
-
attr_reader :reasoning_effort, :client, :schema
|
|
26
|
+
attr_reader :reasoning_effort, :client, :schema, :schema_file
|
|
27
27
|
|
|
28
28
|
VALID_REASONING_EFFORTS = [:low, :medium, :high].freeze
|
|
29
29
|
PROXY_URL = "https://prepend.me/".freeze
|
|
@@ -40,15 +40,15 @@ module AI
|
|
|
40
40
|
@image_folder = "./images"
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
def self.generate_schema!(description, api_key: nil, api_key_env_var: "OPENAI_API_KEY", proxy: false)
|
|
44
|
-
|
|
43
|
+
def self.generate_schema!(description, location: "schema.json", api_key: nil, api_key_env_var: "OPENAI_API_KEY", proxy: false)
|
|
44
|
+
api_key ||= ENV.fetch(api_key_env_var)
|
|
45
45
|
prompt_path = File.expand_path("../prompts/schema_generator.md", __dir__)
|
|
46
46
|
system_prompt = File.open(prompt_path).read
|
|
47
47
|
|
|
48
48
|
json = if proxy
|
|
49
49
|
uri = URI(PROXY_URL + "api.openai.com/v1/responses")
|
|
50
50
|
parameters = {
|
|
51
|
-
model: "
|
|
51
|
+
model: "gpt-5.1",
|
|
52
52
|
input: [
|
|
53
53
|
{role: :system, content: system_prompt},
|
|
54
54
|
{role: :user, content: description},
|
|
@@ -61,7 +61,7 @@ module AI
|
|
|
61
61
|
else
|
|
62
62
|
client = OpenAI::Client.new(api_key: api_key)
|
|
63
63
|
response = client.responses.create(
|
|
64
|
-
model: "
|
|
64
|
+
model: "gpt-5.1",
|
|
65
65
|
input: [
|
|
66
66
|
{role: :system, content: system_prompt},
|
|
67
67
|
{role: :user, content: description}
|
|
@@ -73,7 +73,15 @@ module AI
|
|
|
73
73
|
output_text = response.output_text
|
|
74
74
|
JSON.parse(output_text)
|
|
75
75
|
end
|
|
76
|
-
JSON.pretty_generate(json)
|
|
76
|
+
content = JSON.pretty_generate(json)
|
|
77
|
+
if location
|
|
78
|
+
path = Pathname.new(location)
|
|
79
|
+
FileUtils.mkdir_p(path.dirname) if path.dirname != "."
|
|
80
|
+
File.open(location, "wb") do |file|
|
|
81
|
+
file.write(content)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
content
|
|
77
85
|
end
|
|
78
86
|
|
|
79
87
|
# :reek:TooManyStatements
|
|
@@ -192,6 +200,12 @@ module AI
|
|
|
192
200
|
end
|
|
193
201
|
end
|
|
194
202
|
|
|
203
|
+
def schema_file=(path)
|
|
204
|
+
content = File.open(path).read
|
|
205
|
+
@schema_file = path
|
|
206
|
+
self.schema = content
|
|
207
|
+
end
|
|
208
|
+
|
|
195
209
|
def last
|
|
196
210
|
messages.last
|
|
197
211
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ai-chat
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Raghu Betina
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: openai
|
|
@@ -146,8 +146,8 @@ email:
|
|
|
146
146
|
executables: []
|
|
147
147
|
extensions: []
|
|
148
148
|
extra_rdoc_files:
|
|
149
|
-
- README.md
|
|
150
149
|
- LICENSE
|
|
150
|
+
- README.md
|
|
151
151
|
files:
|
|
152
152
|
- LICENSE
|
|
153
153
|
- README.md
|
|
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
181
181
|
- !ruby/object:Gem::Version
|
|
182
182
|
version: '0'
|
|
183
183
|
requirements: []
|
|
184
|
-
rubygems_version: 3.6.
|
|
184
|
+
rubygems_version: 3.6.7
|
|
185
185
|
specification_version: 4
|
|
186
186
|
summary: A beginner-friendly Ruby interface for OpenAI's API
|
|
187
187
|
test_files: []
|