-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
55 lines (47 loc) · 1.58 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import anthropic
from PyPDF2 import PdfReader
from dotenv import load_dotenv
# Load the environment variables
load_dotenv()
# Get the answer to a question from the AI model in use
def get_answer(question, client):
message = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
temperature=0,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": question
}
]
}
]
)
return message.content[0].text
# Gather context from a PDF file
def extract_text_from_pdf(pdf_file_path):
text = ""
with open(pdf_file_path, "rb") as f:
pdf = PdfReader(f)
for page in pdf.pages:
text += page.extract_text()
return text
# Read questions from a text file
def extract_questions_from_txt(txt_file_path):
questions = []
with open(txt_file_path, "r", encoding="utf8") as f:
questions = [question.strip() for question in f.read().split("\n\n")]
return questions
if __name__ == "__main__":
# Temporarily disable context usage as it is too long to pass to the API
# context = extract_text_from_pdf("textbook.pdf")
questions = extract_questions_from_txt("questions.txt")
client = anthropic.Anthropic() # ANTHROPIC_API_KEY should contain the API key
for task_number, question in enumerate(questions, start=1):
answer = get_answer(question, client)
print(f"{task_number}: {answer}")
client.close()