-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_retrieval.py
69 lines (53 loc) · 2.54 KB
/
llm_retrieval.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from config import OPENAI_API_KEY
import os
from openai import OpenAI
import re
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
def sanitize_reference(input_str):
return re.sub("\s", "", input_str).lower()
def get_all_scriptures(fp):
"""Parse the scriptures.txt file into a dict"""
scripture_dict = dict()
file = open(fp, 'r')
for line in file:
current_id = line.split(" ")[0]
scripture_dict[sanitize_reference(current_id)] = line.split(" ")[1]
return scripture_dict
def get_scriptures_string(scripture_dict, question, generative_model):
verses = get_verse_references(question, generative_model)
output_string = ""
for v in verses:
if sanitize_reference(v) in scripture_dict.keys():
output_string += v + "\t" + scripture_dict[sanitize_reference(v)] + "\n"
elif v.find("-") >=0:
cur_verse = int(v.split(":")[1].split("-")[0])
last_verse = int(v.split(":")[1].split("-")[1])
if cur_verse > last_verse:
continue
while cur_verse <= last_verse:
sub_v = v.split(":")[0] + ":" + str(cur_verse)
if sanitize_reference(sub_v) in scripture_dict.keys():
output_string += sub_v + "\t" + scripture_dict[sanitize_reference(sub_v)] + "\n"
cur_verse +=1
return output_string
def get_verse_references(question, generative_model):
"""Get a list of verses that refer to the question of interest
Args:
question: the question someone asked
generative_model: the generative model to use
"""
sys_prompt = f"""
You are a concise, faithful, and thoughtful latter-day saint. Your role is to suggest scriptures that will be relevant to answering gospel questions.
These scriptures may come from the Old Testament, the New Testament, the Bible, the Book of Mormon, the Doctrine of Covenants, or the Pearl of Great Price.
Choose scriptures that are highly relevant to the question at hand.
Return a list of five to ten scripture references to answer the question. Each reference should be on its own line in the format <book> <chapter>:<verse>. Do not include the actual text of the scripture in your response.
"""
client = OpenAI()
response = client.chat.completions.create(
model=generative_model,
messages=[
{"role": "system", "content": sys_prompt},
{"role": "user", "content": question}
]
)
return response.choices[0].message.content.split("\n")