-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.py
More file actions
78 lines (63 loc) · 2.45 KB
/
Copy pathbootstrap.py
File metadata and controls
78 lines (63 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
bootstrap.py
App initialization module for LoreBuilder CLI.
Sets up config, env, embedding, vector DB, chat DB, and modules.
"""
import yaml
import chromadb
from chromadb.utils import embedding_functions
from dotenv import load_dotenv
import os
from rich import print as rprint
import chat_history
import lore
import lore_entities
from lore_entities import extract_character_names_from_lore
def initialize_app():
"""
Bootstraps configuration, environment variables, DBs, and module dependencies.
Returns a context dict for use by the CLI.
"""
os.environ["TOKENIZERS_PARALLELISM"] = "false"
load_dotenv()
rprint("[dim]Loading configuration and environment...[/dim]")
# Load config
with open("app.yaml") as f:
config = yaml.safe_load(f)
if "lore_llm_model" not in config:
config["lore_llm_model"] = config.get("llm_provider", "openai/gpt-4o-mini")
if "lore_llm_system_prompt" not in config:
config["lore_llm_system_prompt"] = config.get("prompt_template", "You are a creative worldbuilder.")
# Environmental config
if not os.getenv("LLM_API_KEY"):
raise EnvironmentError("LLM_API_KEY not set in .env or environment.")
llm_api_key = os.getenv("LLM_API_KEY")
# Embedding init
embedder = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name=config["embedding_model"]
)
# Chroma DB init
client = chromadb.PersistentClient(path=config["chromadb_path"])
collection = client.get_or_create_collection("lore", embedding_function=embedder)
# Chat DB
chat_history.initialize_db()
# Module dependency injection
rprint("[dim]Initializing modules...[/dim]")
lore.init_lore(config=config, chroma_collection=collection, embedder=embedder,
llm_api_key=llm_api_key, chat_history=chat_history)
# npcs module is stateless
# Extract lore documents and character names
try:
lore_docs = collection.get().get('documents', [])
characters_in_lore = extract_character_names_from_lore(lore_docs)
except Exception as e:
rprint(f"[yellow]Warning: Could not extract character names from lore. {e}[/yellow]")
characters_in_lore = set()
context = {
'config': config, # pass to CLI if handlers wish to use
'last_generated_lore': '',
'last_look_result': None,
'characters_in_lore': characters_in_lore,
'chroma_collection': collection,
}
return context