flare / prompy_engine.py
ciyidogan's picture
Update prompy_engine.py
827c88f verified
raw
history blame
2.51 kB
from utils import log
class PromptEngine:
def __init__(self, service_config):
self.service_config = service_config
def build_intent_prompt(self, project_name):
project = self.service_config.get_project(project_name)
if not project:
raise Exception(f"Project not found: {project_name}")
system_prompt = self.service_config.system_prompt
intent_examples = []
for intent in project["intents"]:
examples = intent.get("examples", [])
intent_examples.append(f"Intent: {intent['name']}\nExamples:\n" + "\n".join(f"- {ex}" for ex in examples))
full_prompt = f"{system_prompt}\n\nIntent Definitions:\n" + "\n\n".join(intent_examples)
log("πŸ›  Built intent detection prompt.")
return full_prompt
def build_parameter_prompt(self, project_name, intent_name, missing_parameters):
project = self.service_config.get_project(project_name)
if not project:
raise Exception(f"Project not found: {project_name}")
intent = next((i for i in project["intents"] if i["name"] == intent_name), None)
if not intent:
raise Exception(f"Intent not found: {intent_name}")
system_prompt = self.service_config.system_prompt
prompts = []
for param in intent.get("parameters", []):
if param["name"] in missing_parameters:
prompts.append(f"Extract the value for parameter '{param['name']}': {param['extraction_prompt']}")
if not prompts:
raise Exception("No extraction prompts found for missing parameters.")
full_prompt = f"{system_prompt}\n\nParameter Extraction:\n" + "\n".join(prompts)
log("πŸ›  Built parameter extraction prompt.")
return full_prompt
def build_humanization_prompt(self, project_name, intent_name):
project = self.service_config.get_project(project_name)
if not project:
raise Exception(f"Project not found: {project_name}")
intent = next((i for i in project["intents"] if i["name"] == intent_name), None)
if not intent:
raise Exception(f"Intent not found: {intent_name}")
system_prompt = self.service_config.system_prompt
humanization_instruction = intent.get("humanization_prompt", "")
full_prompt = f"{system_prompt}\n\nHumanization Instruction:\n{humanization_instruction}"
log("πŸ›  Built humanization prompt.")
return full_prompt