Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvements #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 57 additions & 50 deletions copy-n-prompt.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/usr/bin/env ruby
# encoding: UTF-8
# frozen_string_literal: true
# <swiftbar.hideAbout>true</swiftbar.hideAbout>
# <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
# <swiftbar.hideLastUpdated>true</swiftbar.hideLastUpdated>
# <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
# <swiftbar.hideSwiftBar>true</swiftbar.hideSwiftBar>

require 'yaml'
require 'json'
Expand Down Expand Up @@ -37,65 +43,66 @@ def debug_to_file(debug_message, file_path = '/tmp/xbar-gpt.log')

prompts.each_with_index do |prompt, i|
script_path = Shellwords.escape(File.expand_path(__FILE__))
image = prompt['image'] || "captions.bubble"
image = prompt['image'] || 'captions.bubble'
puts %(#{prompt['name']} | bash=#{script_path} param1=#{i} terminal=false sfimage=#{image})
end

editor = ENV['EDITOR'] || 'nano'
puts '---'
puts "Edit Prompts | bash=#{editor} param1=#{prompts_file} terminal=true"

if ARGV.length.positive?
begin
debug_to_file("ARGV.length: #{ARGV.length}")

begin
if ARGV.length > 0
prompt_index = ARGV[0].to_i
prompt_text = prompts[prompt_index]
rescue StandardError
puts 'Invalid index'
exit(1)
end


clipboard_content = `pbpaste`
uri = URI.parse('https://api.openai.com/v1/chat/completions')
request = Net::HTTP::Post.new(uri)
request.content_type = 'application/json'
request['Authorization'] = "Bearer #{api_key}"
request.body = JSON.dump({
'model' => 'gpt-3.5-turbo',
'messages' => [
{
'role' => 'system',
'content' => prompt_text['prompt']
},
{
'role' => 'user',
'content' => clipboard_content.strip
}
],
'temperature' => 1,
'max_tokens' => 256,
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0
})

req_options = {
use_ssl: uri.scheme == 'https'
}

debug_to_file("gpt request: #{request.body}")

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end

debug_to_file("gpt_response: #{response.body}")

gpt_response = JSON.parse(response.body)['choices'][0]['message']['content'].strip.force_encoding('UTF-8')

IO.popen('/usr/bin/pbcopy', 'w') { |f| f.puts gpt_response }

unless prompt_text['alert'] == false
system("afplay /System/Library/Sounds/Glass.aiff")
debug_to_file("gpt request")

clipboard_content = `pbpaste`.force_encoding('UTF-8')
uri = URI.parse('https://api.openai.com/v1/chat/completions')
request = Net::HTTP::Post.new(uri)
request.content_type = 'application/json'
request['Authorization'] = "Bearer #{api_key}"
request.body = JSON.dump({
'model' => 'gpt-3.5-turbo',
'messages' => [
{
'role' => 'system',
'content' => prompt_text['prompt']
},
{
'role' => 'user',
'content' => clipboard_content.strip
}
],
'temperature' => 1,
'max_tokens' => 256,
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0
})

req_options = {
use_ssl: uri.scheme == 'https'
}

debug_to_file("gpt request: #{request.body}")

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end

debug_to_file("gpt_response: #{response.body}")

gpt_response = JSON.parse(response.body)['choices'][0]['message']['content'].strip.force_encoding('UTF-8')

IO.popen('/usr/bin/pbcopy', 'w') { |f| f.puts gpt_response }

system('afplay /System/Library/Sounds/Glass.aiff') unless prompt_text['alert'] == false
end
rescue StandardError => e
debug_to_file("Error: #{e}")
%x[osascript -e 'display alert "Error" message "Copy-n-pompt raised an error" as warning buttons "OK"']
end