active_cortex 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 378a610d894ec96ee6766021538255b8498db655792a1b6878d0720e09e0bb44
4
+ data.tar.gz: 0a0091e2980aa8326f26e4f0935fd7169cfda06627afa15c1f8014a587ef6fa6
5
+ SHA512:
6
+ metadata.gz: ee5d5959224b4e2fe7cb4bcb68f1a430320fa7a9071c85b87a10f14048a6d549f7f40fba24a8a497ebfaf8b6d94192c90e8bdfe6b4ab698a1bd84f767844a749
7
+ data.tar.gz: 0ab90dea7be949e3efe2280280aafcee93a2a66acc7ca63df8a2024097f54a1419d4377316243d58de96f37bb695f075e375cc16a4fd23271c9c5cfa36cf9784
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Ori Marash
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # ActiveCortex
2
+
3
+ Easily add AI-generated fields to your Rails models.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ # app/models/document.rb
9
+ class Document < ApplicationRecord
10
+ include ActiveCortex::Model
11
+
12
+ ai_generated :summary, prompt: -> (doc) { "Summarize: #{doc.text}" }
13
+ # (or)
14
+ ai_generated :summary, prompt: :generate_summary_prompt
15
+
16
+ private
17
+
18
+ def generate_summary_prompt
19
+ "Summarize: #{text}"
20
+ end
21
+ end
22
+
23
+ # ... then ...
24
+ doc = Document.new(text: "Call me Ishmael...")
25
+ doc.generate_summary!
26
+ doc.summary # => an AI-generated summary of `text`
27
+ ```
28
+
29
+ ## Installation
30
+
31
+ Add this line to your application's Gemfile:
32
+
33
+ ```ruby
34
+ gem "active_cortex"
35
+ ```
36
+
37
+ And then execute:
38
+ ```bash
39
+ $ bundle
40
+ ```
41
+
42
+ Or install it yourself as:
43
+ ```bash
44
+ $ gem install active_cortex
45
+ ```
46
+
47
+ And set an OpenAI key
48
+
49
+ ```ruby
50
+ # config/initializers/active_cortex.rb
51
+ ActiveCortex.config.openai_key = ENV.fetch("OPENAI_ACCESS_TOKEN")
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ Happy for you to open an issue if you have ideas to improve the gem.
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT
61
+ License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/setup"
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ require "dry-configurable"
2
+
3
+ module ActiveCortex
4
+ extend Dry::Configurable
5
+
6
+ setting :openai_access_token
7
+ end
@@ -0,0 +1,39 @@
1
+ require "openai"
2
+
3
+ module ActiveCortex::Model
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ def ai_generated(field, prompt: nil)
8
+ define_method("generate_#{field}!") do
9
+ self[field] = generate_content_for_field(field, prompt:)
10
+ end
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def generate_content_for_field(field, prompt: nil)
17
+ content = case prompt
18
+ when Symbol then send(prompt)
19
+ when Proc then prompt.call(self)
20
+ else
21
+ raise ActiveCortex::Error, "prompt must be a symbol or a proc"
22
+ end
23
+
24
+ query_chatgpt_with(content)
25
+ rescue => e
26
+ raise ActiveCortex::Error, e.message
27
+ end
28
+
29
+ def query_chatgpt_with(content)
30
+ openai_client.chat(parameters: {
31
+ model: "gpt-3.5-turbo",
32
+ messages: [{ role: "user", content: content }],
33
+ })["choices"][0]["message"]["content"]
34
+ end
35
+
36
+ def openai_client
37
+ @openai_client ||= OpenAI::Client.new(access_token: ActiveCortex.config.openai_access_token)
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveCortex
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveCortex
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "active_cortex/version"
2
+ require "active_cortex/config"
3
+ require "active_cortex/railtie"
4
+ require "active_cortex/model"
5
+
6
+ module ActiveCortex
7
+ class Error < StandardError; end
8
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_cortex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ori Marash
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.8
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-configurable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-openai
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
55
+ description: Easily add AI-generated fields to your Raila models.
56
+ email:
57
+ - ori@marash.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/active_cortex.rb
66
+ - lib/active_cortex/config.rb
67
+ - lib/active_cortex/model.rb
68
+ - lib/active_cortex/railtie.rb
69
+ - lib/active_cortex/version.rb
70
+ homepage: https://github.com/penguoir/active_cortex
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ homepage_uri: https://github.com/penguoir/active_cortex
75
+ source_code_uri: https://github.com/penguoir/active_cortex
76
+ changelog_uri: https://github.com/penguoir/active_cortex
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.3.7
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Easily add AI-generated fields to your Rails models.
96
+ test_files: []