Skip to content

Instantly share code, notes, and snippets.

@wallentx
Created March 7, 2024 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallentx/2692a96fd5b7d3c1832ee1cf2a9cf005 to your computer and use it in GitHub Desktop.
Save wallentx/2692a96fd5b7d3c1832ee1cf2a9cf005 to your computer and use it in GitHub Desktop.
generate gist descriptions for gists that have no description that currently costs way too many tokens, and sometimes provides shitty descriptions.
OPENAI_API_KEY=beans2000
GH_TOKEN=beans2001
import argparse
import requests
from dotenv import load_dotenv
import os
from openai import OpenAI
# Load environment variables from .env file
load_dotenv()
# Setup command line argument parsing
parser = argparse.ArgumentParser(description='Generate and optionally update GitHub Gist descriptions using ChatGPT.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-u', '--url', type=str, help='The URL of the GitHub Gist')
group.add_argument('-i', '--iterate', action='store_true', help='Iterate over all gists without descriptions')
parser.add_argument('-e', '--edit', action='store_true', help='Edit the gist with the generated description')
args = parser.parse_args()
# Initialize OpenAI client with API key
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def list_gists_without_description(token):
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github+json'
}
gists_without_description = []
response = requests.get("https://api.github.com/gists", headers=headers, params={'per_page': 200})
while response.status_code == 200:
gists = response.json()
for gist in gists:
if not gist.get('description'):
gists_without_description.append(gist['html_url'])
if 'next' in response.links:
next_page_url = response.links['next']['url']
response = requests.get(next_page_url, headers=headers)
else:
break
return gists_without_description
# Function to fetch Gist content
def fetch_gist_content(gist_url):
response = requests.get(gist_url)
if response.status_code == 200:
return response.text
else:
return "Error fetching Gist."
# Adjusted Function to generate description using OpenAI Chat Completion
def generate_description(client, content, initial=False):
# If initial call, set up the first set of messages
if initial:
messages = [
# This is terrible.
{"role": "system", "content": "You are an assistant that will be interacted with via a python script. You will be given a Github Gist URL. Your task is to parse the content within the Gist, and if there are multiple files, determine the context in which they are related, and then determine the purpose of the Gist as a whole, given the function of the code/diagrams/files, etc., and develop a summary of the Gist emphasizing on what it represents. You may be asked to regenerate your response. The only provided input that can be given to you is either the Gist URL, or a request to regenerate your response. Assume you're examining a piece of content intended to illustrate or explain concepts visually, such as diagrams or code snippets that create diagrams. You may sometimes get hints as to the context/purpose of the gist from the filename itself. Your task is to succinctly summarize the visual or conceptual purpose of this content, highlighting what it aims to represent or convey. Avoid focusing on the format or embedding elements like HTML or CSS, and concentrate instead on the essence or primary function of the diagram or code itself. For example, a gist might contain a .md document. Know that github can render many things in a .md doc, such as a mermaid diagram. If this is the case, then the description would be like 'A mermaid diagram that represents...'. Remember, this needs to be a brief note, no longer than 72 characters, written as if explaining the creator's intention behind it, like a git commit message. The response needs to be super short, single-thought responses, that aren't attempting to be verbose, or super fancy."},
{"role": "user", "content": f"Gist content: {gist_content}"}
]
else:
# Use the existing global or passed messages list for subsequent calls
messages = [
{"role": "user", "content": "This description doesn't seem to fit. Please regenerate."}
]
response = client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=messages
)
return response.choices[0].message.content
# Function to update gist description
def update_gist_description(gist_id, description, token):
api_url = f"https://api.github.com/gists/{gist_id}"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
data = {"description": description}
response = requests.patch(api_url, headers=headers, json=data)
return response.status_code == 200
if args.edit or args.iterate:
token = os.getenv('GH_TOKEN') or os.getenv('GITHUB_TOKEN')
if not token:
print("A GitHub token is required. Please set GH_TOKEN or GITHUB_TOKEN in your environment.")
exit(1)
gist_urls = []
if args.iterate:
if not token:
print("Iteration over gists requires a GitHub token.")
exit(1)
gist_urls.extend(list_gists_without_description(token))
elif args.url:
gist_urls.append(args.url)
for gist_url in gist_urls:
gist_id = gist_url.split("/")[-1]
gist_content = fetch_gist_content(gist_url)
description = generate_description(client, gist_content, initial=True)
print(f"✨Description for {gist_url}:\n{description}")
if args.edit:
while True:
user_choice = input("Update gist with this description? (u)pdate, (r)egenerate, (s)kip, (q)uit: ")
if user_choice.lower() == 'u' and token:
if update_gist_description(gist_id, description, token):
print("Gist description updated successfully.")
break
else:
print("Failed to update gist description.")
break
elif user_choice.lower() == 'r':
description = generate_description(client, gist_content, initial=False)
print(f"✨Regenerated Description for {gist_url}:\n{description}")
elif user_choice.lower() == 's':
print("Skipping gist update.")
break
elif user_choice.lower() == 'q':
exit(0)
else:
# If not editing or iterating, just show the generated description
print(f"✨Generated Description:\n{description}")
python-dotenv
openai
requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment