Last active
November 10, 2023 20:58
-
-
Save theoknock/c30e2f59d35eeec9e3df3513e70ba46f to your computer and use it in GitHub Desktop.
A Python script that interacts with the OpenAI API to generate study guides for Psalms from the Bible. It imports necessary libraries, creates prompts for generating the study guides, and then uses the OpenAI API to get responses based on those prompts. The responses are then processed and formatted into a Word document, and each study guide is …
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 os | |
import re | |
import sys | |
from docx import Document | |
from openai import OpenAI | |
# import OpenAI | |
try: | |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | |
except KeyError: | |
sys.stderr.write("""\nSet up OPENAI_API_KEY_REPLIT as a secret.\n""") | |
exit(1) | |
psalms = [{ | |
'psalm': 40 | |
}, { | |
'psalm': 10 | |
}, { | |
'psalm': 12 | |
}, { | |
'psalm': 23 | |
}, { | |
'psalm': 35 | |
}, { | |
'psalm': 41 | |
}, { | |
'psalm': 88 | |
}, { | |
'psalm': 139 | |
}, { | |
'psalm': 141 | |
}] | |
def create_prompts(psalm): | |
return ( | |
f"Starting a new study guide for Psalm {psalm}. Speak to non-academics, who are spiritualists — not theologians. You are speaking to people who are not interested in a history lesson or the origins of the psalm; rather, they are looking for deeper meaning and spiritual understanding. Write a 5 to 6 sentence introduction to the spiritual significance of Psalm {psalm}, making corollaries between its meaning and purpose and the Christian faith.", | |
f"Provide the full text of Psalm {psalm}. Use the King James Version.", | |
f"Create a study guide for Psalm {psalm} that covers the entire psalm with verses being grouped by relationship; precede the study guide with “Study Guide”. Write a concise, one-line header that describes the topical relationship of groups of related verses (set the text style of the header to bold). Next, write a two-part, one paragraph summary that 1) begins with a single topic sentence, which expands on the header and summarizes the main point presented by the group of verses (set the topic sentence text style to bold), and then reference the Bible book, chapter and verses that comprise the main point (put the reference in parentheses).; and, 2) continuing in the same paragraph, a more detailed summary (5 to 6 sentences in length) that further clarifies the intent and meaning of the main point ((set the text style of the header to regular). Include the entire psalm (all verses) in the series of main points. After each main point, present one to three questions related to the verses that focus their attention to the most significant aspect(s) of the main point (precede the questions with “Focus”). Follow each question with an answer. Following the questions and answers “Focus” section, ask one to three questions that invite and encourage the reader to reflect on the material and its applicability to their personal experiences, and to relay those experiences by and through their answers to each; precede the questions with “Reflect”. There should be no punctuation after any of these. Do not number anything, but do bullet the reflection questions. After all the main points are written, write a summary of the main points (or psalm) overall, highlighting their overall (or collective) meaning and purpose, and connect that meaning and purpose with that of Christian faith and/or divine attributes of God (both communicable and incommunicable), as well as His purpose for man. After each main point, present one to three questions related to the verses that focus their attention to the most significant aspect(s) of the main point (precede the questions with “Focus”). Follow each question with an answer. Following the questions and answers “Focus” section, ask one to three questions that invite and encourage the reader to reflect on the material and its applicability to their personal experiences, and to relay those experiences by and through their answers to each; precede the questions with “Reflect”. There should be no punctuation after any of these. Do not number anything, but do bullet the reflection questions.r answers to each; precede the questions with “Reflect”. There should be no punctuation after any of these. Do not number anything, but do bullet the reflection questions. Use the King James Version.", | |
f"Describe all the ways Psalm {psalm} embodies or reflects God’s nature in two sections: 1. divine (incommunicable) attributes; and, 2. communicable attributes. Include biblical references, if applicable. Use the King James Version.", | |
f"Relate the person and teachings of Jesus Christ and Psalm {psalm}, particularly, as they pertain to the gospel. Include supporting Bible verses for every connection made, especially if there is a match between the words of Jesus and verses in this psalm. Use the King James Version.", | |
f"List all of the psalms that are identical or highly similar to Psalm {psalm}, whether in part or in whole. Explain the similarities in as much detail as possible. Use the King James Version." | |
) | |
def remove_number_or_bullet(paragraph): | |
# This regex will match any digit followed by a period and any space, or a hyphen and space | |
return re.sub(r'^(\-\s*|\d+\.\s*)', '', paragraph) | |
# return re.sub(r'^(\d+\.\s+|\-\s+)', '', paragraph) | |
def apply_paragraph_style(doc, style, paragraph): | |
doc.add_paragraph(str(f"{paragraph}"), style=doc.styles[style]) | |
def starts_with_number_period_space(paragraph): | |
# Split the paragraph at the first space | |
parts = paragraph.split(' ', 1) | |
# Check if the first part is a number followed by a period | |
if len(parts) > 1 and parts[0].replace( | |
'.', '').isdigit() and parts[0].endswith('.'): | |
return True | |
return False | |
def process_chatgpt_response(doc, response): | |
# Split the content into paragraphs and remove any empty paragraphs | |
paragraphs = [ | |
paragraph | |
for paragraph in response.choices[0].message.content.split('\n') | |
if paragraph.strip() | |
] | |
for paragraph in paragraphs: | |
if 'Study Guide' in paragraph: | |
# Execute block of code for Study Guide | |
apply_paragraph_style(doc, "Heading 1", paragraph) | |
elif paragraph.startswith('**'): | |
# Execute block of code for Header | |
apply_paragraph_style(doc, "Heading 2", paragraph[2:-2]) | |
elif starts_with_number_period_space(paragraph): | |
# Execute block of code for numbered paragraph | |
apply_paragraph_style(doc, "List Paragraph", paragraph) | |
elif paragraph.startswith('- '): | |
# Execute block of code for bulleted paragraph | |
apply_paragraph_style(doc, "List Bullet", paragraph) | |
elif any(keyword in paragraph | |
for keyword in ['Focus', 'Reflect', 'Answers']): | |
# Execute block of code for Focus, Reflect, or Answers | |
apply_paragraph_style(doc, "Subtitle", paragraph) | |
else: | |
# Execute block of code for paragraphs that do not match any condition | |
apply_paragraph_style(doc, "Normal", paragraph) | |
return "Processing complete." | |
def main(): | |
for psalm in psalms: | |
response = client.chat.completions.create( | |
model="gpt-4-1106-preview", | |
messages=[{ | |
"role": | |
"system", | |
"content": | |
"Starting a new study guide for Psalm " + str(psalm['psalm']) + ". Speak to non-academics, who are spiritualists — not theologians. You are speaking to people who are not interested in a history lesson or the origins of the psalm; rather, they are looking for deeper meaning and spiritual understanding." | |
}]) | |
# n=1, | |
# temperature=1, | |
# top_p=1) | |
doc = Document() | |
for prompt in create_prompts(psalm['psalm']): | |
response = client.chat.completions.create(model="gpt-4-1106-preview", | |
messages=[{ | |
"role": "user", | |
"content": prompt | |
}]) | |
# n=1, | |
# temperature=1, | |
# top_p=1) | |
process_chatgpt_response(doc, response) | |
print( | |
str("\n\n" + prompt + "\n\n" + response.choices[0].message.content + | |
"\n")) | |
doc.save(str("/Users/xcodedeveloper/Desktop/pip_install/Psalm_" + str(psalm['psalm']) + "_study_guide.docx")) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A Python script that interacts with the OpenAI API to generate study guides for Psalms from the Bible. It imports necessary libraries, creates prompts for generating the study guides, and then uses the OpenAI API to get responses based on those prompts. The responses are then processed and formatted into a Word document, and each study guide is saved as a separate file. This script could be used to automate the creation of study guides for Psalms, which could be helpful for educational or spiritual purposes.