ai-chat 0.0.3 → 0.0.4

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: 96adef3af8e7871b999d82885b74b35ed59da4fa91c90640fe8089fd63d87ca5
4
- data.tar.gz: 1becad90a613aff2c57b91b8d90700f6c7e48faf621ae0ffdc9a3f3e49870227
3
+ metadata.gz: '080a6986c9471fac9120a46be24abb034238d88093c63ec700b9f3e9e3a6b4fd'
4
+ data.tar.gz: 2bd5ce414fb2323a897594810802e61bf4d0918de8d050210e643458bca8d83c
5
5
  SHA512:
6
- metadata.gz: b3f703478eaf415e5b0e3976d86c9f8e773756b9a0a292f6f73a41da4d479b013366726da5335ba40b105981cc558d04f10b44d2d9f78dc513b10da039507a7c
7
- data.tar.gz: 903e3772c07a351f2435d990bb1bbb02f6223d982e76025afc5e97477f64cc9d356eca153d31e5a39031c4f21f381d3aab6c8e53da48a8d29f31843fa195a801
6
+ metadata.gz: 3dfa680c1d64dfab65bdfa0470a0bb48fc7301645dc102723af40d562ba4b3707a65c9cd0fccd5db824a470bb801675dcef1957c541fdf43d651fbdafa793a45
7
+ data.tar.gz: ad1c8453fd0b15e7abca23082b14b224ec1a41d4708da43a3cd520208386c09df2d9e939b3031a111933b955382c392abd240ea93af9a942d456a9dc686c207b
@@ -0,0 +1,2 @@
1
+ inherit_gem:
2
+ caliber: config/all.yml
data/.reek.yml ADDED
@@ -0,0 +1,7 @@
1
+ exclude_paths:
2
+ - tmp
3
+ - vendor
4
+
5
+ detectors:
6
+ LongParameterList:
7
+ enabled: false
data/.rspec_status ADDED
@@ -0,0 +1,16 @@
1
+ example_id | status | run_time |
2
+ --------------------------------------------- | ------ | --------------- |
3
+ ./spec/ai/chat/image_support_spec.rb[1:1:1:1] | passed | 0.00007 seconds |
4
+ ./spec/ai/chat/image_support_spec.rb[1:1:2:1] | passed | 0.0001 seconds |
5
+ ./spec/ai/chat/image_support_spec.rb[1:1:3:1] | passed | 0.00012 seconds |
6
+ ./spec/ai/chat/image_support_spec.rb[1:1:3:2] | passed | 0.00018 seconds |
7
+ ./spec/ai/chat/image_support_spec.rb[1:2:1:1] | passed | 0.0016 seconds |
8
+ ./spec/ai/chat/image_support_spec.rb[1:2:1:2] | passed | 0.0001 seconds |
9
+ ./spec/ai/chat/image_support_spec.rb[1:2:2:1] | passed | 0.00011 seconds |
10
+ ./spec/ai/chat/image_support_spec.rb[1:2:3:1] | passed | 0.00007 seconds |
11
+ ./spec/ai/chat/image_support_spec.rb[1:2:3:2] | passed | 0.00013 seconds |
12
+ ./spec/ai/chat/image_support_spec.rb[1:3:1] | passed | 0.00005 seconds |
13
+ ./spec/ai/chat/image_support_spec.rb[1:3:2] | passed | 0.00006 seconds |
14
+ ./spec/ai/chat/image_support_spec.rb[1:3:3] | passed | 0.00008 seconds |
15
+ ./spec/ai/chat/image_support_spec.rb[1:3:4] | passed | 0.00017 seconds |
16
+ ./spec/ai/chat/image_support_spec.rb[1:3:5] | passed | 0.00074 seconds |
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.6
@@ -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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in draft_chat.gemspec
6
+ gemspec
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 OpenAI's Chat Completions endpoint.
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 art the titans of the deep-dish realm. Lou Malnati's crust is praised for its buttery crispness, whilst Giordano's doth boast a stuffed creation that is nigh unto legendary. Yet, I encourage thee to embark upon thine own quest and savor the offerings of these famed establishments, for in the tasting lies the truth of which thy palate prefers. Enjoy the gastronomic adventure, my friend."
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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "standard/rake"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ desc "Run standardrb and rspec tests"
10
+ task default: [:standard, :spec]
data/ai_chat.gemspec ADDED
@@ -0,0 +1,47 @@
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 Chat Completions endpoint."
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 Chat Completions endpoint. Supports Structured Output."
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
+
37
+ # Development dependencies
38
+ spec.add_development_dependency "rake", "~> 13.0"
39
+ spec.add_development_dependency "rspec", "~> 3.12"
40
+ spec.add_development_dependency "factory_bot", "~> 6.2"
41
+ spec.add_development_dependency "webmock", "~> 3.18"
42
+ spec.add_development_dependency "vcr", "~> 6.1"
43
+ spec.add_development_dependency "standard", "~> 1.32"
44
+
45
+ # For more information and examples about making a new gem, check out our
46
+ # guide at: https://bundler.io/guides/creating_gem.html
47
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module AI
4
4
  class Chat
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4"
6
6
  end
7
7
  end
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(api_token: nil)
10
- @api_token = api_token || ENV.fetch("AI_TOKEN", ENV.fetch("OPENAI_TOKEN", nil))
9
+ def initialize(api_key: nil)
10
+ @api_key = api_key || ENV.fetch("OPENAI_API_KEY")
11
11
  @messages = []
12
- @model = "gpt-4o"
12
+ @model = "gpt-4.1-mini"
13
13
  end
14
14
 
15
15
  def system(content)
@@ -96,7 +96,7 @@ module AI
96
96
 
97
97
  def assistant!
98
98
  request_headers_hash = {
99
- "Authorization" => "Bearer #{@api_token}",
99
+ "Authorization" => "Bearer #{@api_key}",
100
100
  "content-type" => "application/json"
101
101
  }
102
102
 
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "dotenv"
4
+ gem "ai-chat", path: ".."
@@ -0,0 +1,96 @@
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 an image (using the fixture image from the spec directory)
60
+ puts "\nImage Support Test:"
61
+ puts "------------------"
62
+ begin
63
+ img_path = File.expand_path("../../spec/fixtures/test_image.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 "Image test error: #{e.message}"
74
+ puts "#{response}"
75
+ end
76
+
77
+ # Get messages example
78
+ puts "\nMessages Test:"
79
+ puts "--------------"
80
+ m = AI::Chat.new
81
+ m.system("You are a helpful assistant.")
82
+ m.user("Hello world!")
83
+ m.assistant!
84
+ puts "Messages: #{m.messages.inspect}"
85
+
86
+ # Manual assistant message example
87
+ puts "\nManual Assistant Message Test:"
88
+ puts "-----------------------------"
89
+ a = AI::Chat.new
90
+ a.system("You are a helpful assistant.")
91
+ a.assistant("Greetings! How can I assist you today?")
92
+ a.user("Tell me a joke.")
93
+ response = a.assistant!
94
+ puts "Assistant response after manual message: #{response}"
95
+
96
+ 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.3
4
+ version: 0.0.4
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-25 00:00:00.000000000 Z
11
+ date: 2025-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -118,12 +118,22 @@ executables: []
118
118
  extensions: []
119
119
  extra_rdoc_files: []
120
120
  files:
121
+ - ".config/rubocop/config.yml"
122
+ - ".reek.yml"
123
+ - ".rspec_status"
124
+ - ".ruby-version"
121
125
  - CHANGELOG.md
126
+ - CODE_OF_CONDUCT.md
127
+ - Gemfile
122
128
  - LICENSE.txt
123
129
  - README.md
130
+ - Rakefile
131
+ - ai_chat.gemspec
124
132
  - lib/ai-chat.rb
125
133
  - lib/ai/chat.rb
126
134
  - lib/ai/chat/version.rb
135
+ - test_program/Gemfile
136
+ - test_program/test_ai_chat.rb
127
137
  homepage: https://github.com/firstdraft/ai-chat
128
138
  licenses:
129
139
  - MIT