ai-chat 0.5.6 → 0.5.7

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/ai-chat.gemspec +1 -1
  4. data/lib/ai/chat.rb +14 -4
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8573f38afcb8919bc8a4c7612cd8d99ee54fa9271ee974fc1c266af3a6daaa5
4
- data.tar.gz: 021e2c5e81597b2154142f65b4771932b615361b40c41d8e855675fb0db88a90
3
+ metadata.gz: bdaa537e8986ff0070250da8e7c6cc0a2b39fccab163aa4f2c858b3217bb6da3
4
+ data.tar.gz: 6e2077b0e87d86968d43c57b0f5bae1db7341b9fa16aafc1652712b87b67c438
5
5
  SHA512:
6
- metadata.gz: 1ccf46c12086468abc44ecc967ed7ef3c52f19661ebe862f1516b79fd504e8b801300785cb39f7e8195443b55b8e101beb01955ca66bf43550f6f14d261e3fd6
7
- data.tar.gz: 74dcc5634d3eaaa4d9f6b3e123e91b016385883e9c4a3bfb4d71904fde07651d79bad711dfe1f3e2735d31a153e8d0d9efc0a6593b887be2d728364c99160244
6
+ metadata.gz: 145f78aa4de9dd582d2772ab576c3bbe2c3a30623f6f8172eb2189edbdb198132778784855c498d7f28e0368f16ba8b5b1d7d258085b3c3645b833bd1fbaabd4
7
+ data.tar.gz: 48cfe57e2bffdcb039ce0177dd5961edba51f9df8e6c144bdcba8bea1dfc4902365fcfc43bb3cb6c76b11396c9e9e7383d0af327fe1ac82e3e08a45c15e84677
data/README.md CHANGED
@@ -238,7 +238,7 @@ See [OpenAI's model documentation](https://platform.openai.com/docs/models) for
238
238
 
239
239
  ### API key
240
240
 
241
- The gem by default looks for an environment variable called `OPENAI_API_KEY` and uses that if it finds it.
241
+ The gem by default looks for `AICHAT_API_KEY` first. If that is missing (or empty), it falls back to `OPENAI_API_KEY`.
242
242
 
243
243
  You can specify a different environment variable name:
244
244
 
@@ -404,7 +404,7 @@ AI::Chat.generate_schema!("A user profile with name (required), email (required)
404
404
 
405
405
  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.
406
406
 
407
- 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.
407
+ Similar to generating messages with `AI::Chat` objects, this class method will look for `AICHAT_API_KEY` first, then fall back to `OPENAI_API_KEY` if needed. You can also pass the API key directly or choose a different environment variable key for it to use.
408
408
 
409
409
  ```rb
410
410
  # Passing the API key directly
@@ -748,7 +748,9 @@ This is particularly useful for background mode workflows. If you want to retrie
748
748
  ```ruby
749
749
  require "openai"
750
750
 
751
- client = OpenAI::Client.new(api_key: ENV.fetch("OPENAI_API_KEY"))
751
+ api_key = ENV["AICHAT_API_KEY"]
752
+ api_key = ENV.fetch("OPENAI_API_KEY") if api_key.nil? || api_key.empty?
753
+ client = OpenAI::Client.new(api_key: api_key)
752
754
 
753
755
  response_id = "resp_abc123..." # e.g., load from your database
754
756
  response = client.responses.retrieve(response_id)
data/ai-chat.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "ai-chat"
5
- spec.version = "0.5.6"
5
+ spec.version = "0.5.7"
6
6
  spec.authors = ["Raghu Betina", "Jelani Woods"]
7
7
  spec.email = ["raghu@firstdraft.com", "jelani@firstdraft.com"]
8
8
  spec.homepage = "https://github.com/firstdraft/ai-chat"
data/lib/ai/chat.rb CHANGED
@@ -23,8 +23,8 @@ module AI
23
23
 
24
24
  BASE_PROXY_URL = "https://prepend.me/api.openai.com/v1"
25
25
 
26
- def initialize(api_key: nil, api_key_env_var: "OPENAI_API_KEY")
27
- @api_key = api_key || ENV.fetch(api_key_env_var)
26
+ def initialize(api_key: nil, api_key_env_var: nil)
27
+ @api_key = self.class.resolve_api_key(api_key: api_key, api_key_env_var: api_key_env_var)
28
28
  @proxy = ENV["AICHAT_PROXY"] == "true"
29
29
  @messages = []
30
30
  @reasoning_effort = nil
@@ -39,8 +39,8 @@ module AI
39
39
  @verbosity = :medium
40
40
  end
41
41
 
42
- def self.generate_schema!(description, location: "schema.json", api_key: nil, api_key_env_var: "OPENAI_API_KEY", proxy: nil)
43
- api_key ||= ENV.fetch(api_key_env_var)
42
+ def self.generate_schema!(description, location: "schema.json", api_key: nil, api_key_env_var: nil, proxy: nil)
43
+ api_key = resolve_api_key(api_key: api_key, api_key_env_var: api_key_env_var)
44
44
  proxy = ENV["AICHAT_PROXY"] == "true" if proxy.nil?
45
45
  prompt_path = File.expand_path("../prompts/schema_generator.md", __dir__)
46
46
  system_prompt = File.read(prompt_path)
@@ -73,6 +73,16 @@ module AI
73
73
  content
74
74
  end
75
75
 
76
+ def self.resolve_api_key(api_key: nil, api_key_env_var: nil)
77
+ return api_key if api_key
78
+ return ENV.fetch(api_key_env_var) if api_key_env_var
79
+
80
+ aichat_api_key = ENV["AICHAT_API_KEY"]
81
+ return aichat_api_key if aichat_api_key && !aichat_api_key.empty?
82
+
83
+ ENV.fetch("OPENAI_API_KEY")
84
+ end
85
+
76
86
  # :reek:TooManyStatements
77
87
  # :reek:NilCheck
78
88
  def add(content, role: "user", response: nil, status: nil, image: nil, images: nil, file: nil, files: nil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raghu Betina