add prompt templates as j2 templates

This commit is contained in:
Michael Yang
2023-06-27 15:46:05 -07:00
parent ce7fe42345
commit dd4448f787
16 changed files with 194 additions and 440 deletions

19
ollama/prompt.py Normal file
View File

@@ -0,0 +1,19 @@
from os import path
from difflib import SequenceMatcher
from jinja2 import Environment, PackageLoader
def template(model, prompt):
best_ratio = 0
best_template = ''
environment = Environment(loader=PackageLoader(__name__, 'templates'))
for template in environment.list_templates():
base, _ = path.splitext(template)
ratio = SequenceMatcher(None, model.lower(), base).ratio()
if ratio > best_ratio:
best_ratio = ratio
best_template = template
template = environment.get_template(best_template)
return template.render(prompt=prompt)