Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

active_cortex

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

active_cortex

  • 0.3.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

ActiveCortex

Easily add AI-generated fields to your Rails models.

Motivation

ActiveCortex is born out of the need to easily integrate OpenAI into Rails.

Integrating with OpenAI is kind of a pain. It requires a lot of boilerplate: a service object, dealing with OpenAI errors, defining custom functions, etc. Also, OpenAI is constantly releasing new features, and keeping up-to-date is a hassle.

Many developers aren't following OpenAI's best practices because they don't have the time to develop the functionality required to follow them. For example, it's a best practice to split complicated tasks into multiple prompts, but some developers will avoid doing that to simplify their implementation. This results in worse outputs. What if you could effortlessly write multi-stage prompts and debug their performance?

We often write custom functions defining how to create a model we already have a schema for! What if you could tell ChatGPT to provide its response in a format that matches an ActiveRecord class?

Finally, we have to consider errors. OpenAI has downtime, sometimes returns server errors, and ChatGPT sometimes bugs out and returns "Sorry, I can't help you with that". What if you could remove the error handling logic from your system?

ActiveCortex cleans up Rails codebases by providing a macro that deals with the interface to OpenAI. (I'm still working on the above features, but that's the vision for this gem!)

Usage

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

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

  # Generate has_many associations
  has_many :reviews

  # This will look at the Review class and pass its schema to OpenAI.
  ai_generated :reviews,
    prompt: -> (doc) { "Register three reviews for #{doc.title}" },
    max_results: 3

  private

  def generate_summary_prompt
     "Summarize: #{text}"
  end
end

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

doc.generate_reviews!
doc.reviews # => [#<Review id: nil content: "Wonderful! The way...", rating: 5>, ...]

Installation

Add this line to your application's Gemfile:

gem "active_cortex"

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_cortex

And set an OpenAI key

# config/initializers/active_cortex.rb
ActiveCortex.config.openai_access_token = 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.

FAQs

Package last updated on 15 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc