Last active
March 15, 2023 21:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@random_walker / GPT-4