filmov
tv
Prompt Engineering With ChatGPT API | Build Natural Language Python Functions Quickly

Показать описание
import os
import PyPDF2
import re
import openai
import warnings
def get_page(pdf_file): #get the first 400 characters from the first page of a PDF, remove newlines and special characters
pdf_reader = PyPDF2.PdfReader(pdf_file)
page_1_top = page_1[0:400].replace("\n", " ")
return page_1_top
def get_title(prompt): #call chatgpt with few shot prompting, followed by new prompt injection, return title
model="gpt-3.5-turbo",
messages=[
{"role":"user", "content": "I will give you the text of the first page of a scientfic paper or article. You will reply only with the title of the article, do not include any punctuation or special characters in your response."},
{"role":"assistant", "content": "OK, I understand. I am ready for the transcript."},
{"role":"user", "content": "genesG C A T T A C G G C A T Review Alternative Splicing Role in New Therapies of Spinal Muscular Atrophy Jan Lejman1 Grzegorz Zieli nski2 Piotr Gawda2 and Monika Lejman3 gid00030gid00035gid00032gid00030gid00038gid00001gid00033gid00042gid00045 gid00001 gid00048gid00043gid00031gi"},
{"role":"assistant", "content": "Alternative Splicing Role in New Therapies of Spinal Muscular Atrophy"},
{"role":"user", "content": "Invasiv e Californian death caps develop mushrooms unisexuallyand bisexually YenWen Wang1 Megan C. McKeon23 Holly Elmore4 Jaqueline Hess5 Jacob Golan1 Hunter Gage6 William Mao1 Lynn Harrow1 Susana C. Gonalves7 Christina M. Hull36 Anne Pringle18 5 Abstract Canonical sexual"},
{"role":"assistant", "content": "Invasive Californian death caps develop mushrooms unisexually and bisexually"},
{"role":"user", "content": "Molecular Ecology 2009 18 817 833 doi 10.1111 j. 1365 294X.2008.04030.x 2009 The Authors Journal compilation 2009 Blackwell Publishing LtdBlackwell Publishing LtdThe ectomycorrhizal fungus Amanita phalloides was introduced and is expanding its range on the west coast of North America ANNE PRINGLE RACHEL I. ADAMS HUGH B. CROSS a nd THOMAS D. BRUNS Department of Organismic an"},
{"role":"assistant", "content": "The ectomycorrhizal fungus Amanita phalloides was introduced and is expanding its range on the west coast of North America"},
{"role":"assistant", "content": "Metabolic engineering of Saccharomyces cerevisiae for the de novo production of psilocybin and related tryptamine derivatives"},
{"role":"assistant", "content": "An overview of an artificial nose system"},
{"role":"user", "content": clean_prompt}
]
)
return(reply_content)
def rename_pdf(dir): #rename all pdfs in a directory after getting title
title = get_title(get_page(path)) + ".pdf"
rename_pdf("Path to directory with PDFs")
import PyPDF2
import re
import openai
import warnings
def get_page(pdf_file): #get the first 400 characters from the first page of a PDF, remove newlines and special characters
pdf_reader = PyPDF2.PdfReader(pdf_file)
page_1_top = page_1[0:400].replace("\n", " ")
return page_1_top
def get_title(prompt): #call chatgpt with few shot prompting, followed by new prompt injection, return title
model="gpt-3.5-turbo",
messages=[
{"role":"user", "content": "I will give you the text of the first page of a scientfic paper or article. You will reply only with the title of the article, do not include any punctuation or special characters in your response."},
{"role":"assistant", "content": "OK, I understand. I am ready for the transcript."},
{"role":"user", "content": "genesG C A T T A C G G C A T Review Alternative Splicing Role in New Therapies of Spinal Muscular Atrophy Jan Lejman1 Grzegorz Zieli nski2 Piotr Gawda2 and Monika Lejman3 gid00030gid00035gid00032gid00030gid00038gid00001gid00033gid00042gid00045 gid00001 gid00048gid00043gid00031gi"},
{"role":"assistant", "content": "Alternative Splicing Role in New Therapies of Spinal Muscular Atrophy"},
{"role":"user", "content": "Invasiv e Californian death caps develop mushrooms unisexuallyand bisexually YenWen Wang1 Megan C. McKeon23 Holly Elmore4 Jaqueline Hess5 Jacob Golan1 Hunter Gage6 William Mao1 Lynn Harrow1 Susana C. Gonalves7 Christina M. Hull36 Anne Pringle18 5 Abstract Canonical sexual"},
{"role":"assistant", "content": "Invasive Californian death caps develop mushrooms unisexually and bisexually"},
{"role":"user", "content": "Molecular Ecology 2009 18 817 833 doi 10.1111 j. 1365 294X.2008.04030.x 2009 The Authors Journal compilation 2009 Blackwell Publishing LtdBlackwell Publishing LtdThe ectomycorrhizal fungus Amanita phalloides was introduced and is expanding its range on the west coast of North America ANNE PRINGLE RACHEL I. ADAMS HUGH B. CROSS a nd THOMAS D. BRUNS Department of Organismic an"},
{"role":"assistant", "content": "The ectomycorrhizal fungus Amanita phalloides was introduced and is expanding its range on the west coast of North America"},
{"role":"assistant", "content": "Metabolic engineering of Saccharomyces cerevisiae for the de novo production of psilocybin and related tryptamine derivatives"},
{"role":"assistant", "content": "An overview of an artificial nose system"},
{"role":"user", "content": clean_prompt}
]
)
return(reply_content)
def rename_pdf(dir): #rename all pdfs in a directory after getting title
title = get_title(get_page(path)) + ".pdf"
rename_pdf("Path to directory with PDFs")
Комментарии