Skip to content

Instantly share code, notes, and snippets.

@ErikDeBruijn
Last active October 4, 2023 08:46
Show Gist options
  • Save ErikDeBruijn/bd454b2fd024c9a25d87ac7b428b2eac to your computer and use it in GitHub Desktop.
Save ErikDeBruijn/bd454b2fd024c9a25d87ac7b428b2eac to your computer and use it in GitHub Desktop.
bitgpt.py - a bitbar tool that runs prompts on your copy & paste buffer
#!/usr/bin/env python3
# <xbar.title>BitGPT</xbar.title>
# <xbar.version>v0.1</xbar.version>
# <xbar.author>Erik de Bruijn</xbar.author>
# <xbar.author.github>ErikDeBruijn</xbar.author.github>
# <xbar.desc>Performs ChatGPT prompts on the clipboard and returns the result on the paste buffer.</xbar.desc>
# <xbar.image>http://www.hosted-somewhere/pluginimage</xbar.image>
# <xbar.dependencies>python</xbar.dependencies>
# <xbar.abouturl>https://erikdebruijn.nl/</xbar.abouturl>
# <xbar.var>string(OPENAI_API_KEY="your-open-ai-key"): Your openAI key. Find it here: https://platform.openai.com/account/api-keys</xbar.var>
import os
import sys
import yaml
import openai
import subprocess
import shlex
# Read settings from the YAML file
settings_file = os.path.expanduser("~/.bitgpt/.openai.yml")
try:
with open(settings_file, 'r') as f:
settings = yaml.safe_load(f)
except FileNotFoundError:
print("BitGPT | color=red")
print("---")
print("Settings YAML not found | color=red")
print(f"Edit Settings | bash=nano param1={settings_file} terminal=true")
sys.exit(1)
# Read prompts from the YAML file
prompts_file = os.path.expanduser("~/.bitgpt/.prompts.yml")
try:
with open(prompts_file, 'r') as f:
prompts = yaml.safe_load(f)
except FileNotFoundError:
print("BitGPT | color=red")
print("---")
print("Prompts YAML not found | color=red")
print(f"Edit Prompts | bash=nano param1={prompts_file} terminal=true")
sys.exit(1)
# Fetch the clipboard content
clipboard_content = subprocess.getoutput("pbpaste")
# Display the Bitbar menu
print("BitGPT")
print("---")
# Loop through the prompts and display them in the dropdown
for i, prompt in enumerate(prompts):
print(f"{prompt['name']} | bash=/Users/erik/Dev/BitGPT/bitgpt.py param1={i} terminal=false")
# Menu item to edit YAML files
editor = settings.get('editor', os.environ.get('EDITOR'))
print("---")
print(f"Edit Prompts | bash={editor} param1={prompts_file} terminal=true")
print(f"Edit Settings | bash={editor} param1={settings_file} terminal=true")
# The actual GPT-3 call
if len(sys.argv) > 1:
try:
prompt_index = int(sys.argv[1])
prompt_text = prompts[prompt_index]
except (ValueError, IndexError):
print("Invalid index")
sys.exit(1)
if prompt_text:
final_prompt = prompt_text['prompt'].replace("{paste}", clipboard_content)
openai.api_key = settings.get('api_key', os.environ.get('OPENAI_API_KEY'))
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": final_prompt
}
],
temperature=0,
max_tokens=100
)
gpt_response = response["choices"][0]["message"]["content"]
#print(gpt_response) # Print to terminal (useful for debugging)
# Copy to clipboard
subprocess.run("pbcopy", universal_newlines=True, input=gpt_response)
# Show alert if the alert flag is true
if prompt_text.get('alert', False):
subprocess.run(["osascript", "-e", f'display notification "Clipboard updated with: {gpt_response}" with title "BitGPT clipboard"'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment