Skip to content

Instantly share code, notes, and snippets.

@F-3r
Last active January 27, 2025 13:28
Show Gist options
  • Save F-3r/b2c582a3a1737f01d2b816fa02a5d99d to your computer and use it in GitHub Desktop.
Save F-3r/b2c582a3a1737f01d2b816fa02a5d99d to your computer and use it in GitHub Desktop.
"Intelligent" Man page summarizer using ollama or openai
#!/bin/env ruby
require "fileutils"
model = nil
client = nil
use_openai = ENV["OPENAI_ACCESS_TOKEN"]
client, model = if use_openai
require "openai"
OpenAI.configure do |config|
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
config.log_errors = true
end
[OpenAI::Client.new, ENV.fetch("IMAN_MODEL", "gpt-4o-mini")]
else
require 'ollama-ai'
client = Ollama.new(
credentials: {
address: 'http://localhost:11434',
bearer_token: ENV['OLLAMA_BEARER_TOKEN']
},
options: { server_sent_events: false }
)
[client, ENV.fetch("IMAN_MODEL", "qwen2.5:3b")]
end
command = ARGV.first
if command.nil?
puts "Usage: #{$0} <command>"
exit 1
end
IMAN_DIR = File.expand_path("~/.iman")
FileUtils.mkdir_p(IMAN_DIR)
iman_file = "#{IMAN_DIR}/#{command}"
if File.exist?(iman_file)
puts "Using cached file: #{iman_file}"
print File.read(iman_file)
exit 0
end
prompt = <<~PROMPT
You are a technical writer who excels at explaining complex topics through examples.
I need you to summarize the following man page using an example-driven structure.
This will be rendered in a terminal using `echo -e` which doesnt understand markdown,
Use plain-text only. this is a MUST.
\n\n
man page:
#{`man -P cat #{command}`}
PROMPT
params = {
model: model,
messages: [
{
role: "user",
content: prompt
}
]
}
content = if use_openai
response = client.chat(parameters: params)
response.dig("choices", 0, "message", "content")
else
params.merge!("stream" => false)
response = client.generate(params)
response.first.dig("response")
end
File.write(iman_file, content)
print content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment