Skip to content

Commit

Permalink
pre-load all prompt file contents instead of just storing a path and …
Browse files Browse the repository at this point in the history
…load on demand
  • Loading branch information
Benjoyo committed Apr 28, 2024
1 parent 0f4c511 commit 1b78111
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
17 changes: 10 additions & 7 deletions bpm-ai-core/bpm_ai_core/prompt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def from_string(cls, template: str, **kwargs):
return cls(kwargs, template_str=template)

def format(self, llm_name: str = "") -> List[ChatMessage]:
template = self.load_template(self.path, llm_name)
template = self.load_template(llm_name)
full_prompt = template.render(self.template_vars)

regex = r'\[#\s*(user|assistant|system|tool_result:.*|)\s*#\]'
Expand Down Expand Up @@ -141,12 +141,15 @@ def format(self, llm_name: str = "") -> List[ChatMessage]:

return [m for m in messages if m]

def load_template(self, path: str, llm_name: str) -> Template:
default_prompt = f"{path}.prompt"
llm_specific_prompt = f"{path}.{llm_name}.prompt"
prompt = self.prompt_templates.get(llm_specific_prompt, self.prompt_templates.get(default_prompt))
if not prompt:
raise FileNotFoundError(f"No prompt file {path} found for llm {llm_name}")
def load_template(self, llm_name: str) -> Template:
if self.path:
default_prompt = f"{self.path}.prompt"
llm_specific_prompt = f"{self.path}.{llm_name}.prompt"
prompt = self.prompt_templates.get(llm_specific_prompt, self.prompt_templates.get(default_prompt))
if not prompt:
raise FileNotFoundError(f"No prompt file {self.path} found for llm {llm_name}")
else:
prompt = self.template_str
return Template(prompt)

def __repr__(self):
Expand Down
38 changes: 38 additions & 0 deletions bpm-ai-core/tests/test.openai.prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[# system #]
You are a smart assistant.
[# blob {{image_url}} #]
Go!

[# user #]
What is one plus one?

[# assistant #]
I will call some tools.
[# tool_call: foo (foo_id) #]
x
[# tool_call: bar #]
y

[# tool_result: foo_id #]
the result

[# assistant #]
Looks good, now another one:

[# tool_call: other (other_id) #]
z

[# tool_result: other_id #]
the result 2

[# assistant #]
[# tool_call: another #]
123

[# assistant #]
That's that.

[# user #]
Here is an image:
[# blob {{image_url}} #]
{{task}}

0 comments on commit 1b78111

Please sign in to comment.