ai-chat 0.0.3 → 0.0.6
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/.config/rubocop/config.yml +2 -0
- data/.reek.yml +7 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +6 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +6 -0
- data/README.md +46 -17
- data/Rakefile +10 -0
- data/ai_chat.gemspec +48 -0
- data/lib/ai/chat/version.rb +1 -1
- data/lib/ai/chat.rb +70 -34
- data/test_program/Gemfile +4 -0
- data/test_program/test_ai_chat.rb +115 -0
- metadata +28 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4875edb9f6b70da87fbdabfc11fa25be2e76d51496a0ba369e2cf360511abd5
|
4
|
+
data.tar.gz: a062324e299eea25b68696d1d292f2f0b9fe217b3adaa7068020b6db7d198e53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd196bf575fdb10389c93d52e29596bfe68c5a48c49e81b2f5efe377ab91c55b4a2533bb0d09912d06c2400bee688f98f76605161dca9fcf4cab34ebad3067e5
|
7
|
+
data.tar.gz: d973e4d78ef6fe30333f494d60cd590d523727bf1a175c6fb6ce1832c26dd03dd52bbff8681ad15aec4a19afda2b5616ac9fdda21499ec86e635a106381af9d1
|
data/.reek.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.6
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.0.5] - 2025-04-25
|
4
|
+
|
5
|
+
- Updated to use OpenAI's Responses API instead of Chat Completions API
|
6
|
+
- Fixed image processing to work with the new API format
|
7
|
+
- Improved error handling and response parsing
|
8
|
+
|
3
9
|
## [0.0.1] - 2025-02-27
|
4
10
|
|
5
11
|
- Initial release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jelani@firstdraft.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
# AI::Chat
|
2
2
|
|
3
|
-
This gem provides a class called `AI::Chat` that is intended to make it as easy as possible to use
|
3
|
+
This gem provides a class called `AI::Chat` that is intended to make it as easy as possible to use cutting-edge Large Language Models.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
+
### Gemfile way (preferred)
|
8
|
+
|
7
9
|
Add this line to your application's Gemfile:
|
8
10
|
|
9
11
|
```ruby
|
10
|
-
gem "ai-chat"
|
12
|
+
gem "ai-chat", "< 1.0.0"
|
11
13
|
```
|
12
14
|
|
13
15
|
And then, at a command prompt:
|
@@ -16,37 +18,60 @@ And then, at a command prompt:
|
|
16
18
|
bundle install
|
17
19
|
```
|
18
20
|
|
21
|
+
### Direct way
|
22
|
+
|
19
23
|
Or, install it directly with:
|
20
24
|
|
21
25
|
```
|
22
26
|
gem install ai-chat
|
23
27
|
```
|
24
28
|
|
25
|
-
## Configuration
|
26
|
-
|
27
|
-
- By default, the gem checks for an environment variable called `AI_TOKEN`, then falls back to `OPENAI_TOKEN` if available. You can also provide your token directly when instantiating a chat:
|
28
|
-
|
29
|
-
```ruby
|
30
|
-
x = AI::Chat.new(api_token: "your-token-goes-here")
|
31
|
-
```
|
32
|
-
- By default, the gem uses the `gpt-4o` model. If you want something else, you can set it:
|
33
|
-
|
34
|
-
```ruby
|
35
|
-
x.model = "o3-mini"
|
36
|
-
```
|
37
|
-
|
38
29
|
## Simplest usage
|
39
30
|
|
31
|
+
In your Ruby program:
|
32
|
+
|
40
33
|
```ruby
|
34
|
+
require "ai-chat"
|
35
|
+
|
36
|
+
# Create an instance of AI::Chat
|
41
37
|
x = AI::Chat.new
|
38
|
+
|
39
|
+
# Add system-level instructions
|
42
40
|
x.system("You are a helpful assistant that speaks like Shakespeare.")
|
41
|
+
|
42
|
+
# Add a user message to the chat
|
43
43
|
x.user("Hi there!")
|
44
|
+
|
45
|
+
# Get the next message from the model
|
44
46
|
x.assistant!
|
45
47
|
# => "Greetings, good sir or madam! How dost thou fare on this fine day? Pray, tell me how I may be of service to thee."
|
46
48
|
|
49
|
+
# Rinse and repeat
|
47
50
|
x.user("What's the best pizza in Chicago?")
|
48
51
|
x.assistant!
|
49
|
-
# => "Ah, the fair and bustling city of Chicago, renowned for its deep-dish delight that hath captured hearts and stomachs aplenty. Amongst the many offerings of this great city, 'tis often said that Lou Malnati's and Giordano's
|
52
|
+
# => "Ah, the fair and bustling city of Chicago, renowned for its deep-dish delight that hath captured hearts and stomachs aplenty. Amongst the many offerings of this great city, 'tis often said that Lou Malnati's and Giordano's...."
|
53
|
+
```
|
54
|
+
|
55
|
+
## Configuration
|
56
|
+
|
57
|
+
By default, the gem uses OpenAI's `gpt-4.1-mini` model. If you want to use a different model, you can set it:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
x.model = "o3"
|
61
|
+
```
|
62
|
+
|
63
|
+
The gem by default looks for an environment variable called `OPENAI_API_KEY` and uses that if it finds it.
|
64
|
+
|
65
|
+
You can specify a different environment variable name:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
x = AI::Chat.new(api_key_env_var: "OPENAI_TOKEN")
|
69
|
+
```
|
70
|
+
|
71
|
+
Or, you can pass an API key in directly:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
x = AI::Chat.new(api_key: "your-api-key-goes-here")
|
50
75
|
```
|
51
76
|
|
52
77
|
## Structured Output
|
@@ -55,9 +80,13 @@ Get back Structured Output by setting the `schema` attribute (I suggest using [O
|
|
55
80
|
|
56
81
|
```ruby
|
57
82
|
x = AI::Chat.new
|
83
|
+
|
58
84
|
x.system("You are an expert nutritionist. The user will describe a meal. Estimate the calories, carbs, fat, and protein.")
|
85
|
+
|
59
86
|
x.schema = '{"name": "nutrition_values","strict": true,"schema": {"type": "object","properties": { "fat": { "type": "number", "description": "The amount of fat in grams." }, "protein": { "type": "number", "description": "The amount of protein in grams." }, "carbs": { "type": "number", "description": "The amount of carbohydrates in grams." }, "total_calories": { "type": "number", "description": "The total calories calculated based on fat, protein, and carbohydrates." }},"required": [ "fat", "protein", "carbs", "total_calories"],"additionalProperties": false}}'
|
87
|
+
|
60
88
|
x.user("1 slice of pizza")
|
89
|
+
|
61
90
|
x.assistant!
|
62
91
|
# => {"fat"=>15, "protein"=>5, "carbs"=>50, "total_calories"=>350}
|
63
92
|
```
|
@@ -142,4 +171,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
142
171
|
|
143
172
|
## Code of Conduct
|
144
173
|
|
145
|
-
Everyone interacting in the AI Chat project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/firstdraft/ai-chat/blob/main/CODE_OF_CONDUCT.md).
|
174
|
+
Everyone interacting in the AI Chat project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/firstdraft/ai-chat/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/ai_chat.gemspec
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ai/chat/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ai-chat"
|
7
|
+
spec.version = AI::Chat::VERSION
|
8
|
+
spec.authors = ["Raghu Betina", "Jelani Woods"]
|
9
|
+
spec.email = ["raghu@firstdraft.com", "jelani@firstdraft.com"]
|
10
|
+
|
11
|
+
spec.summary = "This gem provides a class called `AI::Chat` that is intended to make it as easy as possible to use OpenAI's Responses API."
|
12
|
+
spec.description = "This gem provides a class called `AI::Chat` that is intended to make it as easy as possible to use OpenAI's Responses API. Supports Structured Output and Image Processing."
|
13
|
+
spec.homepage = "https://github.com/firstdraft/ai-chat"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.0.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/firstdraft/ai-chat"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/firstdraft/ai-chat/blob/main/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(git|travis|circleci)|appveyor)})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
# Register dependencies of the gem
|
35
|
+
spec.add_runtime_dependency "mime-types", "~> 3.0"
|
36
|
+
spec.add_runtime_dependency "base64" # Works for all Ruby versions
|
37
|
+
|
38
|
+
# Development dependencies
|
39
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
40
|
+
spec.add_development_dependency "rspec", "~> 3.12"
|
41
|
+
spec.add_development_dependency "factory_bot", "~> 6.2"
|
42
|
+
spec.add_development_dependency "webmock", "~> 3.18"
|
43
|
+
spec.add_development_dependency "vcr", "~> 6.1"
|
44
|
+
spec.add_development_dependency "standard", "~> 1.32"
|
45
|
+
|
46
|
+
# For more information and examples about making a new gem, check out our
|
47
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
48
|
+
end
|
data/lib/ai/chat/version.rb
CHANGED
data/lib/ai/chat.rb
CHANGED
@@ -6,10 +6,10 @@ module AI
|
|
6
6
|
class Chat
|
7
7
|
attr_accessor :messages, :schema, :model
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
@
|
9
|
+
def initialize(api_key: nil)
|
10
|
+
@api_key = api_key || ENV.fetch("OPENAI_API_KEY")
|
11
11
|
@messages = []
|
12
|
-
@model = "gpt-
|
12
|
+
@model = "gpt-4.1-mini"
|
13
13
|
end
|
14
14
|
|
15
15
|
def system(content)
|
@@ -22,15 +22,13 @@ module AI
|
|
22
22
|
if item.key?("image") || item.key?(:image)
|
23
23
|
image_value = item.fetch("image") { item.fetch(:image) }
|
24
24
|
{
|
25
|
-
type: "
|
26
|
-
image_url:
|
27
|
-
url: process_image(image_value)
|
28
|
-
}
|
25
|
+
type: "input_image",
|
26
|
+
image_url: process_image(image_value)
|
29
27
|
}
|
30
28
|
elsif item.key?("text") || item.key?(:text)
|
31
29
|
text_value = item.fetch("text") { item.fetch(:text) }
|
32
30
|
{
|
33
|
-
type: "
|
31
|
+
type: "input_text",
|
34
32
|
text: text_value
|
35
33
|
}
|
36
34
|
else
|
@@ -54,7 +52,7 @@ module AI
|
|
54
52
|
else
|
55
53
|
text_and_images_array = [
|
56
54
|
{
|
57
|
-
type: "
|
55
|
+
type: "input_text",
|
58
56
|
text: content
|
59
57
|
}
|
60
58
|
]
|
@@ -62,10 +60,8 @@ module AI
|
|
62
60
|
if images && !images.empty?
|
63
61
|
images_array = images.map do |image|
|
64
62
|
{
|
65
|
-
type: "
|
66
|
-
image_url:
|
67
|
-
url: process_image(image)
|
68
|
-
}
|
63
|
+
type: "input_image",
|
64
|
+
image_url: process_image(image)
|
69
65
|
}
|
70
66
|
end
|
71
67
|
|
@@ -73,10 +69,8 @@ module AI
|
|
73
69
|
else
|
74
70
|
text_and_images_array.push(
|
75
71
|
{
|
76
|
-
type: "
|
77
|
-
image_url:
|
78
|
-
url: process_image(image)
|
79
|
-
}
|
72
|
+
type: "input_image",
|
73
|
+
image_url: process_image(image)
|
80
74
|
}
|
81
75
|
)
|
82
76
|
end
|
@@ -96,39 +90,81 @@ module AI
|
|
96
90
|
|
97
91
|
def assistant!
|
98
92
|
request_headers_hash = {
|
99
|
-
"Authorization" => "Bearer #{@
|
93
|
+
"Authorization" => "Bearer #{@api_key}",
|
100
94
|
"content-type" => "application/json"
|
101
95
|
}
|
102
96
|
|
103
|
-
response_format = if schema.nil?
|
104
|
-
{
|
105
|
-
"type" => "text"
|
106
|
-
}
|
107
|
-
else
|
108
|
-
{
|
109
|
-
"type" => "json_schema",
|
110
|
-
"json_schema" => JSON.parse(schema)
|
111
|
-
}
|
112
|
-
end
|
113
|
-
|
114
97
|
request_body_hash = {
|
115
98
|
"model" => model,
|
116
|
-
"
|
117
|
-
"messages" => messages
|
99
|
+
"input" => messages
|
118
100
|
}
|
119
101
|
|
102
|
+
# Handle structured output (JSON schema)
|
103
|
+
if !schema.nil?
|
104
|
+
# Parse the schema and use it with Structured Output (json_schema)
|
105
|
+
schema_obj = JSON.parse(schema)
|
106
|
+
|
107
|
+
# Extract schema name from the parsed schema, or use a default
|
108
|
+
schema_name = schema_obj["name"] || "output_object"
|
109
|
+
|
110
|
+
# Responses API uses proper Structured Output with schema
|
111
|
+
request_body_hash["text"] = {
|
112
|
+
"format" => {
|
113
|
+
"type" => "json_schema",
|
114
|
+
"schema" => schema_obj["schema"] || schema_obj,
|
115
|
+
"name" => schema_name,
|
116
|
+
"strict" => true
|
117
|
+
}
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
120
121
|
request_body_json = JSON.generate(request_body_hash)
|
121
122
|
|
122
|
-
uri = URI("https://api.openai.com/v1/
|
123
|
+
uri = URI("https://api.openai.com/v1/responses")
|
123
124
|
raw_response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
124
125
|
request = Net::HTTP::Post.new(uri, request_headers_hash)
|
125
126
|
request.body = request_body_json
|
126
127
|
http.request(request)
|
127
128
|
end
|
128
129
|
|
129
|
-
|
130
|
+
# Handle empty responses or HTTP errors
|
131
|
+
if raw_response.code.to_i >= 400
|
132
|
+
raise "HTTP Error #{raw_response.code}: #{raw_response.message}\n#{raw_response.body}"
|
133
|
+
end
|
134
|
+
|
135
|
+
if raw_response.body.nil? || raw_response.body.empty?
|
136
|
+
raise "Empty response received from OpenAI API"
|
137
|
+
end
|
130
138
|
|
131
|
-
|
139
|
+
parsed_response = JSON.parse(raw_response.body)
|
140
|
+
|
141
|
+
# Check for API errors
|
142
|
+
if parsed_response.key?("error") && parsed_response["error"].is_a?(Hash)
|
143
|
+
error_message = parsed_response["error"]["message"] || parsed_response["error"].inspect
|
144
|
+
raise "OpenAI API Error: #{error_message}"
|
145
|
+
end
|
146
|
+
|
147
|
+
# Extract the text content from the response
|
148
|
+
content = ""
|
149
|
+
|
150
|
+
# Parse response according to the documented structure
|
151
|
+
if parsed_response.key?("output") && parsed_response["output"].is_a?(Array) && !parsed_response["output"].empty?
|
152
|
+
output_item = parsed_response["output"][0]
|
153
|
+
|
154
|
+
if output_item["type"] == "message" && output_item.key?("content")
|
155
|
+
content_items = output_item["content"]
|
156
|
+
output_text_item = content_items.find { |item| item["type"] == "output_text" }
|
157
|
+
|
158
|
+
if output_text_item && output_text_item.key?("text")
|
159
|
+
content = output_text_item["text"]
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# If no content is found, throw an error
|
165
|
+
if content.empty?
|
166
|
+
raise "Failed to extract content from response: #{parsed_response.inspect}"
|
167
|
+
end
|
132
168
|
|
133
169
|
messages.push({role: "assistant", content: content})
|
134
170
|
|
@@ -0,0 +1,115 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dotenv/load"
|
5
|
+
require "ai-chat"
|
6
|
+
|
7
|
+
puts "Testing AI::Chat gem with examples from README"
|
8
|
+
puts "=============================================="
|
9
|
+
|
10
|
+
# Simplest usage example
|
11
|
+
puts "\nSimplest Usage Test:"
|
12
|
+
puts "-------------------"
|
13
|
+
x = AI::Chat.new
|
14
|
+
|
15
|
+
# Add system-level instructions
|
16
|
+
x.system("You are a helpful assistant that speaks like Shakespeare.")
|
17
|
+
|
18
|
+
# Add a user message to the chat
|
19
|
+
x.user("Hi there!")
|
20
|
+
|
21
|
+
# Get the next message from the model
|
22
|
+
begin
|
23
|
+
response = x.assistant!
|
24
|
+
puts "Assistant response: #{response}"
|
25
|
+
rescue => e
|
26
|
+
puts "Error: #{e.message}"
|
27
|
+
puts "API response: #{e.backtrace.first(3).join("\n")}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# Follow-up question
|
31
|
+
x.user("What's the best pizza in Chicago?")
|
32
|
+
response = x.assistant!
|
33
|
+
puts "Assistant response: #{response}"
|
34
|
+
|
35
|
+
# Configuration example
|
36
|
+
puts "\nConfiguration Test:"
|
37
|
+
puts "-------------------"
|
38
|
+
# Using a different model
|
39
|
+
y = AI::Chat.new
|
40
|
+
y.model = "gpt-4o-mini" # Using a model that should be available
|
41
|
+
y.system("You are a helpful assistant.")
|
42
|
+
y.user("Write a haiku about programming.")
|
43
|
+
response = y.assistant!
|
44
|
+
puts "Response with model #{y.model}: #{response}"
|
45
|
+
|
46
|
+
# Structured Output example
|
47
|
+
puts "\nStructured Output Test:"
|
48
|
+
puts "-----------------------"
|
49
|
+
z = AI::Chat.new
|
50
|
+
|
51
|
+
z.system("You are an expert nutritionist. The user will describe a meal. Estimate the calories, carbs, fat, and protein.")
|
52
|
+
|
53
|
+
z.schema = '{"name": "nutrition_values","strict": true,"schema": {"type": "object","properties": { "fat": { "type": "number", "description": "The amount of fat in grams." }, "protein": { "type": "number", "description": "The amount of protein in grams." }, "carbs": { "type": "number", "description": "The amount of carbohydrates in grams." }, "total_calories": { "type": "number", "description": "The total calories calculated based on fat, protein, and carbohydrates." }},"required": [ "fat", "protein", "carbs", "total_calories"],"additionalProperties": false}}'
|
54
|
+
|
55
|
+
z.user("1 slice of pizza")
|
56
|
+
response = z.assistant!
|
57
|
+
puts "Nutrition values: #{response.inspect}"
|
58
|
+
|
59
|
+
# Test with a single image
|
60
|
+
puts "\nSingle Image Support Test:"
|
61
|
+
puts "------------------------"
|
62
|
+
begin
|
63
|
+
img_path = File.expand_path("../../spec/fixtures/test1.jpg", __FILE__)
|
64
|
+
if File.exist?(img_path)
|
65
|
+
i = AI::Chat.new
|
66
|
+
i.user("What's in this image?", image: img_path)
|
67
|
+
response = i.assistant!
|
68
|
+
puts "Image description: #{response}"
|
69
|
+
else
|
70
|
+
puts "Test image not found at #{img_path}"
|
71
|
+
end
|
72
|
+
rescue => e
|
73
|
+
puts "Single image test error: #{e.message}"
|
74
|
+
end
|
75
|
+
|
76
|
+
# Test with multiple images
|
77
|
+
puts "\nMultiple Images Support Test:"
|
78
|
+
puts "--------------------------"
|
79
|
+
begin
|
80
|
+
img_path1 = File.expand_path("../../spec/fixtures/test1.jpg", __FILE__)
|
81
|
+
img_path2 = File.expand_path("../../spec/fixtures/test2.jpg", __FILE__)
|
82
|
+
img_path3 = File.expand_path("../../spec/fixtures/test3.jpg", __FILE__)
|
83
|
+
|
84
|
+
if File.exist?(img_path1) && File.exist?(img_path2) && File.exist?(img_path3)
|
85
|
+
i = AI::Chat.new
|
86
|
+
i.user("Compare these images and tell me what you see.", images: [img_path1, img_path2, img_path3])
|
87
|
+
response = i.assistant!
|
88
|
+
puts "Multiple images description: #{response}"
|
89
|
+
else
|
90
|
+
puts "One or more test images not found"
|
91
|
+
end
|
92
|
+
rescue => e
|
93
|
+
puts "Multiple images test error: #{e.message}"
|
94
|
+
end
|
95
|
+
|
96
|
+
# Get messages example
|
97
|
+
puts "\nMessages Test:"
|
98
|
+
puts "--------------"
|
99
|
+
m = AI::Chat.new
|
100
|
+
m.system("You are a helpful assistant.")
|
101
|
+
m.user("Hello world!")
|
102
|
+
m.assistant!
|
103
|
+
puts "Messages: #{m.messages.inspect}"
|
104
|
+
|
105
|
+
# Manual assistant message example
|
106
|
+
puts "\nManual Assistant Message Test:"
|
107
|
+
puts "-----------------------------"
|
108
|
+
a = AI::Chat.new
|
109
|
+
a.system("You are a helpful assistant.")
|
110
|
+
a.assistant("Greetings! How can I assist you today?")
|
111
|
+
a.user("Tell me a joke.")
|
112
|
+
response = a.assistant!
|
113
|
+
puts "Assistant response after manual message: #{response}"
|
114
|
+
|
115
|
+
puts "\nTests completed!"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ai-chat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Raghu Betina
|
8
8
|
- Jelani Woods
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-04-
|
11
|
+
date: 2025-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: base64
|
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'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,8 +123,8 @@ dependencies:
|
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '1.32'
|
111
125
|
description: This gem provides a class called `AI::Chat` that is intended to make
|
112
|
-
it as easy as possible to use OpenAI's
|
113
|
-
|
126
|
+
it as easy as possible to use OpenAI's Responses API. Supports Structured Output
|
127
|
+
and Image Processing.
|
114
128
|
email:
|
115
129
|
- raghu@firstdraft.com
|
116
130
|
- jelani@firstdraft.com
|
@@ -118,12 +132,21 @@ executables: []
|
|
118
132
|
extensions: []
|
119
133
|
extra_rdoc_files: []
|
120
134
|
files:
|
135
|
+
- ".config/rubocop/config.yml"
|
136
|
+
- ".reek.yml"
|
137
|
+
- ".ruby-version"
|
121
138
|
- CHANGELOG.md
|
139
|
+
- CODE_OF_CONDUCT.md
|
140
|
+
- Gemfile
|
122
141
|
- LICENSE.txt
|
123
142
|
- README.md
|
143
|
+
- Rakefile
|
144
|
+
- ai_chat.gemspec
|
124
145
|
- lib/ai-chat.rb
|
125
146
|
- lib/ai/chat.rb
|
126
147
|
- lib/ai/chat/version.rb
|
148
|
+
- test_program/Gemfile
|
149
|
+
- test_program/test_ai_chat.rb
|
127
150
|
homepage: https://github.com/firstdraft/ai-chat
|
128
151
|
licenses:
|
129
152
|
- MIT
|
@@ -148,5 +171,5 @@ requirements: []
|
|
148
171
|
rubygems_version: 3.6.5
|
149
172
|
specification_version: 4
|
150
173
|
summary: This gem provides a class called `AI::Chat` that is intended to make it as
|
151
|
-
easy as possible to use OpenAI's
|
174
|
+
easy as possible to use OpenAI's Responses API.
|
152
175
|
test_files: []
|