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
|
import os
from typing import Callable, Dict, List, Union
import chevron
from ...core.main import ChatMessage
def get_vars_in_template(template):
"""
Get the variables in a template
"""
return [
token[1]
for token in chevron.tokenizer.tokenize(template)
if token[0] == "variable"
]
def escape_var(var: str) -> str:
"""
Escape a variable so it can be used in a template
"""
return var.replace(os.path.sep, "").replace(".", "")
def render_templated_string(template: str) -> str:
"""
Render system message or other templated string with mustache syntax.
Right now it only supports rendering absolute file paths as their contents.
"""
vars = get_vars_in_template(template)
args = {}
for var in vars:
if var.startswith(os.path.sep):
# Escape vars which are filenames, because mustache doesn't allow / in variable names
escaped_var = escape_var(var)
template = template.replace(var, escaped_var)
if os.path.exists(var):
args[escaped_var] = open(var, "r").read()
else:
args[escaped_var] = ""
return chevron.render(template, args)
"""
A PromptTemplate can either be a template string (mustache syntax, e.g. {{user_input}}) or
a function which takes the history and a dictionary of additional key-value pairs and returns
either a string or a list of ChatMessages.
If a string is returned, it will be assumed that the chat history should be ignored
"""
PromptTemplate = Union[
str, Callable[[ChatMessage, Dict[str, str]], Union[str, List[ChatMessage]]]
]
def render_prompt_template(
template: PromptTemplate, history: List[ChatMessage], other_data: Dict[str, str]
) -> str:
"""
Render a prompt template.
"""
if isinstance(template, str):
data = {
"history": history,
**other_data,
}
if len(history) > 0 and history[0].role == "system":
data["system_message"] = history.pop(0).content
return chevron.render(template, data)
else:
return template(history, other_data)
|