chore: initial import
This commit is contained in:
51
PromptLibrary.py
Normal file
51
PromptLibrary.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from AppConfig import promptsFolderPath, schemaTemplateName
|
||||
|
||||
|
||||
class PromptLibrary:
|
||||
"""Simple prompt storage; swap with DB/API later without touching callers."""
|
||||
|
||||
promptCatalog = {}
|
||||
|
||||
@staticmethod
|
||||
def reloadCatalog():
|
||||
catalog = {}
|
||||
if os.path.isdir(promptsFolderPath):
|
||||
for fileName in os.listdir(promptsFolderPath):
|
||||
if not fileName.endswith(".json"):
|
||||
continue
|
||||
if fileName == schemaTemplateName:
|
||||
continue
|
||||
filePath = os.path.join(promptsFolderPath, fileName)
|
||||
try:
|
||||
with open(filePath, "r", encoding="utf-8") as promptFile:
|
||||
records = json.load(promptFile)
|
||||
except (json.JSONDecodeError, OSError):
|
||||
continue
|
||||
if not isinstance(records, list):
|
||||
records = [records]
|
||||
for record in records:
|
||||
category = record.get("category")
|
||||
name = record.get("name")
|
||||
template = record.get("template")
|
||||
if not category or not name or not template:
|
||||
continue
|
||||
categoryPrompts = catalog.setdefault(category, {})
|
||||
categoryPrompts[name] = template
|
||||
generalPrompts = catalog.setdefault("general", {})
|
||||
if "fallback" not in generalPrompts:
|
||||
generalPrompts["fallback"] = "Still working on that, {user}. Here's what I can do next: {context}"
|
||||
PromptLibrary.promptCatalog = catalog
|
||||
|
||||
@staticmethod
|
||||
def fetch(category, promptName):
|
||||
return PromptLibrary.promptCatalog.get(category, {}).get(promptName)
|
||||
|
||||
@staticmethod
|
||||
def fallback():
|
||||
return PromptLibrary.promptCatalog["general"]["fallback"]
|
||||
|
||||
|
||||
PromptLibrary.reloadCatalog()
|
||||
Reference in New Issue
Block a user