Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Basic functionality + some error handling. Nothing groundbreaking!
  • Loading branch information
penguoir committed Oct 29, 2023
0 parents commit 044a304
Show file tree
Hide file tree
Showing 63 changed files with 1,301 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/doc/
/log/*.log
/pkg/
/tmp/
/test/dummy/db/*.sqlite3
/test/dummy/db/*.sqlite3-*
/test/dummy/log/*.log
/test/dummy/storage/
/test/dummy/tmp/
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

# Specify your gem's dependencies in active_cortex.gemspec.
gemspec

gem "puma"
gem "sqlite3"
gem "debug"
gem "webmock"
210 changes: 210 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
PATH
remote: .
specs:
active_cortex (0.1.0)
dry-configurable (>= 1.0)
rails (>= 7.0.8)
ruby-openai (>= 5.1)

GEM
remote: https://rubygems.org/
specs:
actioncable (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.0.8)
actionpack (= 7.0.8)
actionview (= 7.0.8)
activejob (= 7.0.8)
activesupport (= 7.0.8)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.0)
actionpack (7.0.8)
actionview (= 7.0.8)
activesupport (= 7.0.8)
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (7.0.8)
actionpack (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.0.8)
activesupport (= 7.0.8)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (7.0.8)
activesupport (= 7.0.8)
globalid (>= 0.3.6)
activemodel (7.0.8)
activesupport (= 7.0.8)
activerecord (7.0.8)
activemodel (= 7.0.8)
activesupport (= 7.0.8)
activestorage (7.0.8)
actionpack (= 7.0.8)
activejob (= 7.0.8)
activerecord (= 7.0.8)
activesupport (= 7.0.8)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (7.0.8)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.1.1)
builder (3.2.4)
concurrent-ruby (1.2.2)
crack (0.4.5)
rexml
crass (1.0.6)
date (3.3.3)
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
dry-configurable (1.1.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
erubi (1.12.0)
faraday (2.7.11)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.0.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
irb (1.8.3)
rdoc
reline (>= 0.3.8)
loofah (2.21.4)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.5)
minitest (5.20.0)
multipart-post (2.3.0)
net-imap (0.4.1)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.1)
timeout
net-smtp (0.4.0)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4-arm64-darwin)
racc (~> 1.4)
psych (5.1.1.1)
stringio
public_suffix (5.0.3)
puma (6.3.1)
nio4r (~> 2.0)
racc (1.7.1)
rack (2.2.8)
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.8)
actioncable (= 7.0.8)
actionmailbox (= 7.0.8)
actionmailer (= 7.0.8)
actionpack (= 7.0.8)
actiontext (= 7.0.8)
actionview (= 7.0.8)
activejob (= 7.0.8)
activemodel (= 7.0.8)
activerecord (= 7.0.8)
activestorage (= 7.0.8)
activesupport (= 7.0.8)
bundler (>= 1.15.0)
railties (= 7.0.8)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.0.8)
actionpack (= 7.0.8)
activesupport (= 7.0.8)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.0.6)
rdoc (6.5.0)
psych (>= 4.0.0)
reline (0.3.9)
io-console (~> 0.5)
rexml (3.2.6)
ruby-openai (5.1.0)
faraday (>= 1)
faraday-multipart (>= 1)
ruby2_keywords (0.0.5)
sqlite3 (1.6.7-arm64-darwin)
stringio (3.0.8)
thor (1.2.2)
timeout (0.4.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
webmock (3.19.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.12)

PLATFORMS
arm64-darwin-21

DEPENDENCIES
active_cortex!
debug
puma
sqlite3
webmock

BUNDLED WITH
2.4.1
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2023 Ori Marash

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# ActiveCortex

Easily add AI-generated fields to your Rails models.

## Usage

```ruby
# app/models/document.rb
class Document < ApplicationRecord
include ActiveCortex::Model

ai_generated :summary, prompt: -> (doc) { "Summarize: #{doc.text}" }
end

# ... then ...
doc = Document.new(text: "Call me Ishmael...")
doc.generate_summary!
doc.summary # => an AI-generated summary of `text`
```

## Installation

Add this line to your application's Gemfile:

```ruby
gem "active_cortex"
```

And then execute:
```bash
$ bundle
```

Or install it yourself as:
```bash
$ gem install active_cortex
```

And set an OpenAI key

```ruby
# config/initializers/active_cortex.rb
ActiveCortex.config.openai_key = ENV.fetch("OPENAI_ACCESS_TOKEN")
```

## Contributing

Happy for you to open an issue if you have ideas to improve the gem.

## License

The gem is available as open source under the terms of the [MIT
License](https://opensource.org/licenses/MIT).
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/setup"
require "bundler/gem_tasks"
24 changes: 24 additions & 0 deletions active_cortex.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative "lib/active_cortex/version"

Gem::Specification.new do |spec|
spec.name = "active_cortex"
spec.version = ActiveCortex::VERSION
spec.authors = ["Ori Marash"]
spec.email = ["[email protected]"]
spec.homepage = "https://github.com/penguoir/active_cortex"
spec.summary = "Easily add AI-generated fields to your Rails models."
spec.description = "Easily add AI-generated fields to your Raila models."
spec.license = "MIT"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/penguoir/active_cortex"
spec.metadata["changelog_uri"] = "https://github.com/penguoir/active_cortex"

spec.files = Dir.chdir(File.expand_path(__dir__)) do
Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
end

spec.add_dependency "rails", ">= 7.0.8"
spec.add_dependency "dry-configurable", ">= 1.0"
spec.add_dependency "ruby-openai", ">= 5.1"
end
5 changes: 5 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
$: << File.expand_path("../test", __dir__)

require "bundler/setup"
require "rails/plugin/test"
8 changes: 8 additions & 0 deletions lib/active_cortex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "active_cortex/version"
require "active_cortex/config"
require "active_cortex/railtie"
require "active_cortex/model"

module ActiveCortex
class Error < StandardError; end
end
7 changes: 7 additions & 0 deletions lib/active_cortex/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "dry-configurable"

module ActiveCortex
extend Dry::Configurable

setting :openai_access_token
end
28 changes: 28 additions & 0 deletions lib/active_cortex/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "openai"

module ActiveCortex::Model
extend ActiveSupport::Concern

class_methods do
def ai_generated(field, prompt: nil)
define_method("generate_#{field}!") do
self[field] = generate_content_for_field(field, prompt:)
end
end
end

private

def generate_content_for_field(field, prompt: nil)
openai_client.chat(parameters: {
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt.call(self) }],
})["choices"][0]["message"]["content"]
rescue
raise ActiveCortex::Error
end

def openai_client
@openai_client ||= OpenAI::Client.new(access_token: ActiveCortex.config.openai_access_token)
end
end
4 changes: 4 additions & 0 deletions lib/active_cortex/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ActiveCortex
class Railtie < ::Rails::Railtie
end
end
3 changes: 3 additions & 0 deletions lib/active_cortex/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ActiveCortex
VERSION = "0.1.0"
end
Loading

0 comments on commit 044a304

Please sign in to comment.