Skip to content

Commit

Permalink
refactor(prompt): improve handling of variable templates in advanced …
Browse files Browse the repository at this point in the history
…prompt transform
  • Loading branch information
laipz8200 committed Oct 14, 2024
1 parent b7370a1 commit 73f30df
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions api/core/prompt/advanced_prompt_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,20 @@ def _get_chat_model_prompt_messages(
raw_prompt = prompt_item.text

if prompt_item.edition_type == "basic" or not prompt_item.edition_type:
vp = VariablePool()
for k, v in inputs.items():
vp.add(k[1:-1].split("."), v)
raw_prompt.replace("{{#context#}}", context or "")
prompt = vp.convert_template(raw_prompt).text
if self.with_variable_tmpl:
vp = VariablePool()
for k, v in inputs.items():
if k.startswith("#"):
vp.add(k[1:-1].split("."), v)
raw_prompt.replace("{{#context#}}", context or "")
prompt = vp.convert_template(raw_prompt).text
else:
parser = PromptTemplateParser(template=raw_prompt, with_variable_tmpl=self.with_variable_tmpl)
prompt_inputs = {k: inputs[k] for k in parser.variable_keys if k in inputs}
prompt_inputs = self._set_context_variable(
context=context, parser=parser, prompt_inputs=prompt_inputs
)
prompt = parser.format(prompt_inputs)
elif prompt_item.edition_type == "jinja2":
prompt = raw_prompt
prompt_inputs = inputs
Expand Down

0 comments on commit 73f30df

Please sign in to comment.