active_cortex 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca05346fe4261874164f6fbf48a473b4e310f2cafa206721b2f0ddb2dcfdff5c
4
- data.tar.gz: 2a0163d3e56f87d416a56a105ad3c29af488de955445180d0adafa347d7d1c99
3
+ metadata.gz: c1020861a0627f8c3df3db06459b51287e067cb8ecee1a3c9ee412e80aa374d9
4
+ data.tar.gz: 6e6c36fff9beec9b256f8bae8b72e118da33b4b6a4535cbe6574c09d8561489f
5
5
  SHA512:
6
- metadata.gz: 5f4b74059e5887c179f7c330dd1a47aa87e68521b3e63a1283359ae4067cac187097890a52bd4b931b5b0cbcb6bdbecae16e985a9c16a708ecc3b78b77be6554
7
- data.tar.gz: c585a12f113acf4086389fe67f20a1558e054c9f1ad3aa6e87ffb22de42f31e385ce9c883b2041a3a619bfdb95feab2d4ada8f54e0ea6b43f53713742d3d24f8
6
+ metadata.gz: 6536b4dc37ff820006cc77d9d0f15255640dc6be9732bdbcbf1e7e1262f33b650f42c174f593592db979cc0fc8a9e47fbdbc6733c824a9fd50676a44bb42d21e
7
+ data.tar.gz: ea9d43ad9662b87f6262d2df69fedfbead2e5a171a6d7e8effd1f64d8043e8f3ab185eefc34e6d43dfd63c7c2fd94883c4f9137ec531ea1082156fbf71b9cd95
@@ -0,0 +1,45 @@
1
+ class ActiveCortex::Generator::Boolean < ActiveCortex::Generator
2
+ def self.accepts?(record:, field_name:)
3
+ record.class.attribute_types[field_name.to_s].type == :boolean
4
+ end
5
+
6
+ def self.generate(record:, field_name:)
7
+ record.send("#{field_name}=", generation)
8
+ end
9
+
10
+ def generation
11
+ openai_content || raise(ActiveCortex::Error, openai_error_message)
12
+ end
13
+
14
+ private
15
+
16
+ def convert_to_boolean(content)
17
+ case content
18
+ when "Yes", "yes", "True", "true", "1"
19
+ true
20
+ when "No", "no", "False", "false", "0"
21
+ false
22
+ else
23
+ raise ActiveCortex::Error, "Could not convert content to boolean: #{content}"
24
+ end
25
+ end
26
+
27
+ def openai_content
28
+ convert_to_boolean(openai_response["choices"][0]["message"]["content"])
29
+ rescue
30
+ nil
31
+ end
32
+
33
+ def openai_error_message
34
+ "Error from OpenAI. " + { response: openai_response }.to_json
35
+ end
36
+
37
+ def openai_response
38
+ @openai_response ||= openai_client.chat(parameters: {
39
+ model: model,
40
+ messages: [
41
+ { role: "user", content: prompt }
42
+ ],
43
+ })
44
+ end
45
+ end
@@ -53,7 +53,7 @@ module ActiveCortex::Model
53
53
 
54
54
  def validate_arguments!(field_name, prompt, max_results, model)
55
55
  raise ArgumentError, "field_name must be a symbol or string" unless field_name.is_a?(Symbol) || field_name.is_a?(String)
56
- raise ArgumentError, "prompt must be a proc" unless prompt.is_a?(Proc)
56
+ raise ArgumentError, "prompt must be a proc" unless prompt.is_a?(Proc) || prompt.is_a?(Symbol)
57
57
  raise ArgumentError, "max_results must be a number" unless max_results.nil? || max_results.is_a?(Integer)
58
58
  raise ArgumentError, "model must be a string" unless model.is_a?(String)
59
59
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveCortex
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/active_cortex.rb CHANGED
@@ -4,6 +4,7 @@ require "active_cortex/railtie"
4
4
  require "active_cortex/generator"
5
5
  require "active_cortex/generator/text"
6
6
  require "active_cortex/generator/has_many"
7
+ require "active_cortex/generator/boolean"
7
8
  require "active_cortex/model"
8
9
 
9
10
  module ActiveCortex
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_cortex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ori Marash
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-15 00:00:00.000000000 Z
11
+ date: 2024-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -65,6 +65,7 @@ files:
65
65
  - lib/active_cortex.rb
66
66
  - lib/active_cortex/config.rb
67
67
  - lib/active_cortex/generator.rb
68
+ - lib/active_cortex/generator/boolean.rb
68
69
  - lib/active_cortex/generator/has_many.rb
69
70
  - lib/active_cortex/generator/text.rb
70
71
  - lib/active_cortex/model.rb