Skip to content

Commit

Permalink
✨ Enhance prompt creation function
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Nov 8, 2023
1 parent f35aff1 commit 3e84c10
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions funcchain/prompt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from string import Formatter

from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.schema import BaseMessage, SystemMessage

Expand All @@ -8,7 +10,7 @@ def create_prompt(
instruction: str,
system: str,
context: list[BaseMessage] = [],
**input_kwargs,
**input_kwargs: str,
) -> ChatPromptTemplate:
"""
Helper to create a prompt from an instruction and a system message.
Expand All @@ -22,16 +24,29 @@ def create_prompt(
if base_tokens + content_tokens > settings.MAX_TOKENS:
input_kwargs[k] = v[: (settings.MAX_TOKENS - base_tokens) * 2 // 3]
print("Truncated: ", len(input_kwargs[k]))
# check if instruction is a jinja2 template
template_format = "jinja2" if "{{" in instruction or "{%" in instruction else "f-string"
# Extract all the f-string template variables from instruction
required_f_str_vars = extract_fstring_vars(instruction) # TODO: jinja2
# get all input_kwargs that are not required by the template
inject_vars = [f"[{var}]:\n{value}\n" for var, value in input_kwargs.items() if var not in required_f_str_vars]
added_instruction = ("".join(inject_vars)).replace("{", "{{").replace("}", "}}")
instruction = added_instruction + instruction

return ChatPromptTemplate.from_messages(
[SystemMessage(content=system)]
+ context
+ [
HumanMessagePromptTemplate.from_template(
template=instruction,
template_format="jinja2" if "{{" in instruction else "f-string",
template_format=template_format,
)
if isinstance(instruction, str)
else instruction
]
)


def extract_fstring_vars(template: str) -> list[str]:
"""
Function to extract f-string variables from a string.
"""
return [field_name for _, field_name, _, _ in Formatter().parse(template) if field_name is not None]

0 comments on commit 3e84c10

Please sign in to comment.