active_cortex 0.2.0 → 0.3.0
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/lib/active_cortex/generator/boolean.rb +45 -0
 - data/lib/active_cortex/model.rb +1 -1
 - data/lib/active_cortex/version.rb +1 -1
 - data/lib/active_cortex.rb +1 -0
 - metadata +3 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: c1020861a0627f8c3df3db06459b51287e067cb8ecee1a3c9ee412e80aa374d9
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 6e6c36fff9beec9b256f8bae8b72e118da33b4b6a4535cbe6574c09d8561489f
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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
         
     | 
    
        data/lib/active_cortex/model.rb
    CHANGED
    
    | 
         @@ -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
         
     | 
    
        data/lib/active_cortex.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 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:  
     | 
| 
      
 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
         
     |