Skip to content

Instantly share code, notes, and snippets.

@DJRHails
Last active March 15, 2023 21:57
Show Gist options
  • Save DJRHails/3652e5bcd56a3b07b405b152ce13a373 to your computer and use it in GitHub Desktop.
Save DJRHails/3652e5bcd56a3b07b405b152ce13a373 to your computer and use it in GitHub Desktop.
Use GPT-3.5 (davinci-003) to extract a bibtex entry from a URL
import sys
import openai
import subprocess
# Replace YOUR_API_KEY with your actual OpenAI API key
openai.api_key = "YOUR_API_KEY"
def get_page_contents(url):
try:
result = subprocess.run(["lynx", "-dump", url], capture_output=True, check=True)
content = result.stdout.decode("utf-8", errors="replace")
return content[:1000]
except subprocess.CalledProcessError as e:
print(f"Error: Failed to retrieve content from {url}")
sys.exit(1)
def generate_bibtex_entry(url, content):
prompt = f"I want to cite this webpage. Give me a bibtex entry: {url}\n\nHere are the contents of the page:\n\n{content}\n"
response = openai.Completion.create(
engine="text—davinci-003",
prompt=prompt,
max_tokens=200
)
return response.choices[0].text.strip()
if __name__ == "_ main_":
if len(sys.argv) != 2:
print("Usage: python url_to_bibtex.py <URL>")
sys.exit(1)
url = sys.argv[1]
content = get_page_contents(url)
bibtex_entry = generate_bibtex_entry(url, content)
print(bibtex_entry)
@DJRHails
Copy link
Author

@random_walker / GPT-4

Write a Python script that uses the GPT-3 API to convert a URL into a bibtex entry. Specifically, it should:

  • accept a URL as a command line argument
  • grab the first 1000 characters of the web page contents
    (not the source code); you can use python libraries for this
    or dump it using the lynx tool
  • send the URL and the extracted page contents to the API
    and ask GPT-3 to generate a bibtex entry for it.
  • output the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment