Pclanglais commited on
Commit
be38993
1 Parent(s): 9d55aff

Create prompt_demo.py

Browse files
Files changed (1) hide show
  1. prompt_demo.py +57 -0
prompt_demo.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/python
2
+
3
+ import sys, os
4
+ from pprint import pprint
5
+ from jinja2 import Environment, FileSystemLoader, meta
6
+ import yaml
7
+
8
+
9
+ sys.path.append(".")
10
+ os.chdir(os.path.dirname(os.path.abspath(__file__)))
11
+
12
+
13
+ if __name__ == "__main__":
14
+
15
+ with open('prompt_config.yaml') as f:
16
+ config = yaml.safe_load(f)
17
+
18
+ print("prompt format:", config.get("prompt_format"))
19
+ print(config)
20
+ print()
21
+ for prompt in config["prompts"]:
22
+ print(f'--- prompt mode: {prompt["mode"]} ---')
23
+ env = Environment(loader=FileSystemLoader("."))
24
+ template = env.get_template(prompt["template"])
25
+
26
+ source = template.environment.loader.get_source(template.environment, template.name)
27
+ variables = meta.find_undeclared_variables(env.parse(source[0]))
28
+
29
+ print("variables:", variables)
30
+ print("---")
31
+
32
+ data = {
33
+ "query": "Comment est votre blanquette ?",
34
+ "chunks" : [
35
+ {
36
+ "url": "http://data.gouv.fr",
37
+ "h": "hash49080805",
38
+ "title": "A chunk title",
39
+ "text": "text texs\ntext again ",
40
+ },
41
+ {
42
+ "url": "http://...",
43
+ "h": "hash49080806",
44
+ "title": "A chunk title",
45
+ "text": "text texs\ntext again ",
46
+ "context": "I > am > a > context"
47
+ },
48
+
49
+ ]
50
+ }
51
+
52
+ if "system_prompt" in variables:
53
+ data["system_prompt"] = prompt["system_prompt"]
54
+
55
+ rendered_template = template.render(**data)
56
+ print(rendered_template)
57
+ print("---")