Created
February 18, 2025 20:58
-
-
Save budu/67c10669bfc0069e8adf8d805267d6a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if Rails.env.development? | |
require 'langchain' | |
LLM_CLIENT ||= Langchain::LLM::OpenAI.new( | |
api_key: ENV.fetch('CHATGPT_API_KEY'), | |
default_options: { | |
temperature: 0.2, # Lower temperature for deterministic output | |
chat_model: 'gpt-4o' | |
} | |
) | |
Langchain.logger.level = Logger::DEBUG | |
Langchain.logger = Logger.new("log/llm.log", **Langchain::LOGGER_OPTIONS) | |
## | |
# Quick and dirty `llm` helper | |
# | |
# Usage: | |
# > llm 'a ruby query to get the biggest row in the PgSearch::Document model' | |
# # => PgSearch::Document.order('length(content) DESC').first | |
# nil | |
# # Use console paste keybinding to paste the result | |
# > PgSearch::Document.order('length(content) DESC').first | |
# | |
def llm(prompt) | |
context = "Please be concise! In a Rails project, give a single line of " \ | |
"Ruby code that does the following:\n#{prompt}" | |
response = LLM_CLIENT.chat(messages: [ { role: "system", content: context } ]) | |
to_clean = /```ruby|\A`+|`+\z/ | |
result = response.chat_completion.gsub(to_clean, '').strip | |
case RUBY_PLATFORM | |
when /darwin/ # macOS: Use `pbcopy` | |
IO.popen('pbcopy', 'w') { |clipboard| clipboard << result } | |
when /linux/ # Linux: Use `xclip` or `xsel` | |
begin | |
IO.popen('xclip -selection clipboard', 'w') { |clipboard| clipboard << result } | |
rescue Errno::ENOENT | |
IO.popen('xsel --clipboard --input', 'w') { |clipboard| clipboard << result } | |
end | |
when /mswin|mingw|cygwin/ # Windows: Use `clip` | |
IO.popen('clip', 'w') { |clipboard| clipboard << result } | |
end | |
puts "# => #{result}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment