-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
82 lines (69 loc) · 2.61 KB
/
template.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
70
71
72
73
74
75
76
77
78
79
80
81
82
import string
from variable import Variable
from open_ai import run_chatgpt
class Template:
"""Template for ChatGPT prompts"""
def __init__(self, title):
"""Initializes the template"""
self.title = title
self.content = []
self.string = ""
def get_content(self):
"""Returns the content of the template"""
return self.content
def get_variable(self, name):
"""Returns the variable with the given name"""
for var in self.get_variables():
if var.name == name:
return var
return None
def get_variables(self):
"""Returns the variables of the template"""
out = []
for word in self.content:
if isinstance(word, Variable):
out.append(word)
return out
def set_variable(self, name, value):
"""Sets the variable with the given name to the given value"""
for var in self.get_variables():
if var.name == name:
var.value = value
def add_content(self, content):
"""Adds strings and variables to the template"""
for word in content.split():
punc = None
if word[-1] in string.punctuation and word[-1] not in ['$', '#', '*']:
punc = word[-1]
word = word[:-1]
if word[0] == '$' and word[-1] == '$':
word = Variable(word[1:-1], "text")
elif word[0] == '#' and word[-1] == '#':
word = Variable(word[1:-1], "number")
elif word[0] == '*' and word[-1] == '*':
word = Variable(word[1:-1], "date")
if punc is not None and isinstance(word, Variable):
word.punc = punc
elif punc is not None:
word += punc
self.content.append(word)
def prepare_string(self):
"""Prepares the string for ChatGPT"""
out = ''
for word in self.content:
if isinstance(word, Variable):
if word.punc is not None:
out += word.value + word.punc + ' '
else:
out += word.value + ' '
else:
out += word + ' '
return out.rstrip()
def execute_template(self):
"""Runs the template in ChatGPT"""
# Check that variables are defined
for var in self.get_variables():
if var.value is None:
raise ValueError('Variable %s is not defined' % var.name)
prompt = self.prepare_string()
return run_chatgpt(prompt)