2Btainment commited on
Commit
5324a16
·
verified ·
1 Parent(s): a08dba6

security: load system prompt from FUUGI_SYSTEM_PROMPT secret

Browse files
Files changed (1) hide show
  1. app/knowledge.py +24 -1
app/knowledge.py CHANGED
@@ -1,6 +1,7 @@
1
  from __future__ import annotations
2
 
3
  import json
 
4
  from functools import lru_cache
5
  from pathlib import Path
6
  from typing import Any
@@ -8,6 +9,16 @@ from typing import Any
8
  KNOWLEDGE_PATH = Path(__file__).resolve().parent.parent / "data" / "website_knowledge.json"
9
  SYSTEM_PROMPT_PATH = Path(__file__).resolve().parent.parent / "data" / "fuugi_system_prompt.md"
10
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  @lru_cache(maxsize=1)
13
  def load_knowledge() -> dict[str, Any]:
@@ -18,10 +29,22 @@ def load_knowledge() -> dict[str, Any]:
18
  return data
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @lru_cache(maxsize=1)
22
  def system_prompt() -> str:
23
  serialized = json.dumps(load_knowledge(), ensure_ascii=True, separators=(",", ":"))
24
- template = SYSTEM_PROMPT_PATH.read_text(encoding="utf-8")
25
  if template.count("{knowledge}") != 1:
26
  raise RuntimeError("Fuugi system prompt must contain exactly one knowledge placeholder.")
27
  return template.replace("{knowledge}", serialized)
 
1
  from __future__ import annotations
2
 
3
  import json
4
+ import os
5
  from functools import lru_cache
6
  from pathlib import Path
7
  from typing import Any
 
9
  KNOWLEDGE_PATH = Path(__file__).resolve().parent.parent / "data" / "website_knowledge.json"
10
  SYSTEM_PROMPT_PATH = Path(__file__).resolve().parent.parent / "data" / "fuugi_system_prompt.md"
11
 
12
+ # Minimal public fallback if FUUGI_SYSTEM_PROMPT secret / file missing.
13
+ _FALLBACK_PROMPT = """You are Fuugi, the public AI companion for 2B Entertainment.
14
+ Be natural, direct, calm, precise, and warm. Match the user language.
15
+ Use the website knowledge JSON below when answering about 2B Entertainment.
16
+ Do not invent private credentials or internal systems.
17
+
18
+ Website knowledge (JSON):
19
+ {knowledge}
20
+ """
21
+
22
 
23
  @lru_cache(maxsize=1)
24
  def load_knowledge() -> dict[str, Any]:
 
29
  return data
30
 
31
 
32
+ def _load_template() -> str:
33
+ env = (os.environ.get("FUUGI_SYSTEM_PROMPT") or "").strip()
34
+ if env and "{knowledge}" in env:
35
+ return env
36
+ if SYSTEM_PROMPT_PATH.is_file():
37
+ text = SYSTEM_PROMPT_PATH.read_text(encoding="utf-8").strip()
38
+ # Ignore stub files that lack the placeholder
39
+ if text and "{knowledge}" in text and not text.startswith("# STUB"):
40
+ return text
41
+ return _FALLBACK_PROMPT
42
+
43
+
44
  @lru_cache(maxsize=1)
45
  def system_prompt() -> str:
46
  serialized = json.dumps(load_knowledge(), ensure_ascii=True, separators=(",", ":"))
47
+ template = _load_template()
48
  if template.count("{knowledge}") != 1:
49
  raise RuntimeError("Fuugi system prompt must contain exactly one knowledge placeholder.")
50
  return template.replace("{knowledge}", serialized)