GALTdea / ask_chatgpt

AI-Powered Assistant Gem right in your Rails console.

Home Page:https://www.railsjazz.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ask ChatGPT

RailsJazz Listed on OpenSource-Heroes.com

AI-Powered Assistant Gem right in your Rails console.

AskChatGPT

A Gem that leverages the power of AI to make your development experience more efficient and enjoyable. With this gem, you can streamline your coding process, effortlessly refactor and improve your code, and even generate tests on the fly.

See more examples below.

Usage

Go to Rails console and run:

  gpt.ask("how to get max age of user with projects from Ukraine").with_model(User, Project, Country)
  gpt.ask("convert json to xml")
  gpt.payload(json).ask("extract emails from json")
  gpt.refactor("User.get_report")
  gpt.improve("User.get_report")
  gpt.rspec_test(User)
  gpt.unit_test(User)
  gpt.code_review(User.method(:get_report))
  gpt.find_bug('User#full_name')
  gpt.explain(User)
  gpt.improve %Q{
    def full_name
      [first_name, last_name].join
    end
  }

See some examples below. You can also create your own prompts with just few lines of code here.

Examples

Typical use-cases how you can use this plugin AskChatGPT

Ask for code ideas: AskChatGPT

Do you need help to write rspec test? AskChatGPT

What about unit tests? AskChatGPT

Ask ChatGPT to improve your code: AskChatGPT

Ask ChatGPT to parse you JSON: AskChatGPT

Or it can encode in Base64 your string: AskChatGPT

Create I18n YAML for your Model (custom prompt): AskChatGPT

Installation

Add this line to your application's Gemfile:

gem "ask_chatgpt"

And then execute:

$ bundle

Or install it yourself as:

$ gem install ask_chatgpt

Options & Configurations

Run rails g ask_chatgpt initializer.

And you can edit:

  AskChatGPT.setup do |config|
    # config.access_token    = ENV["OPENAI_API_KEY"]
    # config.debug           = false
    # config.model           = "gpt-3.5-turbo"
    # config.temperature     = 0.1
    # config.max_tokens      = 3000 # or nil by default
    # config.included_prompt = []

    # Examples of custom prompts:
    # you can use them `gpt.ask(:extract_email, "some string")`

    # config.register_prompt :extract_email do |arg|
    #   "Extract email from: #{arg} as JSON"
    # end

    # config.register_prompt :extract_constants do |arg|
    #   "Extract constants from class: #{AskChatGPT::Helpers.extract_source(arg)}"
    # end

    # config.register_prompt :i18n do |code|
    #   "Use I18n in this code:\n#{AskChatGPT::Helpers.extract_source(code)}"
    # end
  end

Note: that you need to setup your API Key https://platform.openai.com/account/api-keys. You can store it in the .env or .bash_profile. BUT make sure it won't be committed to the Github. Is must be private.

You can define you own prompts and use them using .register_prompt. For example:

  config.register_prompt :extract_email do |arg|
    "Extract email from: #{arg} as JSON"
  end

And later you can call it with gpt.extract_email("some text with email@site.com, user@email.com"). If you believe your custom promts will be useful - create a PR for this gem.

If you want to get source code use this helper AskChatGPT::Helpers.extract_source(str).

You can pass:

  AskChatGPT::Helpers.extract_source('User.some_class_method')
  AskChatGPT::Helpers.extract_source('User#instance_method')
  AskChatGPT::Helpers.extract_source('User')
  AskChatGPT::Helpers.extract_source(User)
  AskChatGPT::Helpers.extract_source("a = b")

Debug Mode

You can enable debug mode to see request/response from the OpenAI using two ways:

AskChatGPT.setup do |config|
  config.debug = false
end

or directly in console gpt.debug! (and finish gpt.debug!(:off))

TODO

  • cli app? ask_gpt <something> --file <file> ...
  • more prompts (cover controllers, sql, etc?), e.g. with_controller, with_class, ...
  • tests(rspec, vcr)
  • CI (but first specs)
  • can it be used with pry/byebug/etc?
  • print tokens usage? .with_usage
  • support org_id? in the configs
  • use gpt in the code of the main app (e.g. model/controller)
  • remove dependency on ruby-openai and just do a Net::HTTP call

Contributing

We are welcoming you to propose new prompts or adjust existing ones!

To start just clone this repo. Run bundle, and go to cd test/dummy/. Start rails c and test your prompts.

License

The gem is available as open source under the terms of the MIT License.

About

AI-Powered Assistant Gem right in your Rails console.

https://www.railsjazz.com/

License:MIT License


Languages

Language:Ruby 85.3%Language:HTML 14.7%Language:CSS 0.1%