-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathprompt_note.py
84 lines (65 loc) · 2.38 KB
/
prompt_note.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
83
84
def python_str_format_example(task_desc_str: str, input_str: str):
# percent(%) formatting
print("<SYS>%s</SYS> User: %s" % (task_desc_str, input_str))
# format() method with kwargs
print(
"<SYS>{task_desc_str}</SYS> User: {input_str}".format(
task_desc_str=task_desc_str, input_str=input_str
)
)
# f-string
print(f"<SYS>{task_desc_str}</SYS> User: {input_str}")
# Templates
from string import Template
t = Template("<SYS>$task_desc_str</SYS> User: $input_str")
print(t.substitute(task_desc_str=task_desc_str, input_str=input_str))
def jinja2_template_example(template, **kwargs):
from jinja2 import Template
t = Template(template, trim_blocks=True, lstrip_blocks=True)
print(t.render(**kwargs))
def adalflow_prompt(template, task_desc_str, input_str, tools=None):
from adalflow.core.prompt_builder import Prompt
prompt = Prompt(
template=template,
prompt_kwargs={
"task_desc_str": task_desc_str,
"tools": tools,
},
)
print(prompt)
print(prompt(input_str=input_str))
saved_prompt = prompt.to_dict()
restored_prompt = Prompt.from_dict(saved_prompt)
print(
restored_prompt == prompt
) # False as the jinja2 template can not be serialized, but we recreated the template from the string at the time of restoration, so it works the same
print(restored_prompt)
def adalflow_default_prompt():
from adalflow.core.prompt_builder import Prompt
prompt = Prompt()
input_str = "What is the capital of France?"
output = prompt(input_str=input_str)
print(output)
if __name__ == "__main__":
task_desc_str = "You are a helpful assitant"
input_str = "What is the capital of France?"
tools = ["google", "wikipedia", "wikidata"]
template = r"""<SYS>{{ task_desc_str }}</SYS>
{# tools #}
{% if tools %}
<TOOLS>
{% for tool in tools %}
{{loop.index}}. {{ tool }}
{% endfor %}
</TOOLS>
{% endif %}
User: {{ input_str }}"""
python_str_format_example(task_desc_str, input_str)
jinja2_template_example(template, task_desc_str=task_desc_str, input_str=input_str)
jinja2_template_example(
template, task_desc_str=task_desc_str, input_str=input_str, tools=tools
)
adalflow_prompt(
template, task_desc_str=task_desc_str, input_str=input_str, tools=tools
)
adalflow_default_prompt()