-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlore.py
More file actions
400 lines (332 loc) · 13.3 KB
/
Copy pathlore.py
File metadata and controls
400 lines (332 loc) · 13.3 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
lore.py
Lore-related logic, ChromaDB and embedding initializations relevant for lore functions.
Exports: add_lore, generate_lore, store_generated_lore, and other lore commands.
Required: Must call init_lore(config, chroma_collection, embedder, llm_api_key, chat_history) before use.
"""
import json
import random
import traceback
from typing import Dict, List, Optional, Any, Union
from rich import print as rprint
import litellm
from datetime import datetime
from constants import (
MIN_LORE_ID,
MAX_LORE_ID,
DEFAULT_MAX_TOKENS,
DEFAULT_QUERY_RESULTS,
DISPLAY_TEXT_LIMIT,
MIN_QUERY_LENGTH,
ERROR_LORE_SERVICE_NOT_INITIALIZED,
SUCCESS_LORE_ADDED,
SUCCESS_LORE_STORED,
SUCCESS_LORE_DELETED,
ERROR_ADDING_LORE,
ERROR_GENERATING_LORE,
ERROR_STORING_LORE,
ERROR_QUERYING_LORE,
ERROR_LISTING_LORE,
ERROR_DELETING_LORE,
WARNING_NO_LORE_TO_STORE,
WARNING_NO_LORE_FOUND,
INFO_NO_EXISTING_LORE,
USAGE_ADD_LORE,
USAGE_GENERATE_LORE,
USAGE_QUERY_LORE,
USAGE_DELETE_LORE,
DEFAULT_LORE_SYSTEM_PROMPT,
DEFAULT_LORE_FALLBACK_CONTEXT,
LORE_MODULE_INITIALIZED
)
class LoreService:
"""Service class for managing lore entries with ChromaDB and LLM integration."""
def __init__(
self,
config: Dict[str, Any],
chroma_collection: Any,
embedder: Any,
llm_api_key: str,
chat_history: Any
) -> None:
"""Initialize the LoreService with required dependencies.
Args:
config: Application configuration dictionary
chroma_collection: ChromaDB collection for storing lore
embedder: Document embedding function
llm_api_key: API key for language model access
chat_history: Chat history module for character interactions
"""
self.config = config
self.chroma_collection = chroma_collection
self.embedder = embedder
self.llm_api_key = llm_api_key
self.chat_history = chat_history
rprint(LORE_MODULE_INITIALIZED)
def _generate_lore_id(self) -> str:
"""Generate a random lore ID.
Returns:
A string representation of a random integer ID
"""
return str(random.randint(MIN_LORE_ID, MAX_LORE_ID))
def _get_system_prompt(self, existing_lore: str = "") -> str:
"""Get the system prompt for lore generation with context replacement.
Args:
existing_lore: Existing lore context to include in prompt
Returns:
Formatted system prompt string
"""
system_prompt = self.config.get(
"lore_llm_system_prompt",
self.config.get("prompt_template", DEFAULT_LORE_SYSTEM_PROMPT)
)
if existing_lore:
system_prompt = system_prompt.replace("{context}", existing_lore)
else:
system_prompt = system_prompt.replace(
"{context}",
DEFAULT_LORE_FALLBACK_CONTEXT
)
return system_prompt
def add_lore(self, args: List[str], context: Dict[str, Any]) -> None:
"""Add a lore entry to ChromaDB.
Args:
args: Command arguments containing the lore text
context: Shared context dictionary
"""
if not args:
rprint(USAGE_ADD_LORE)
return
lore_text = " ".join(args)
lore_id = self._generate_lore_id()
try:
# Generate embedding for the lore text
embedding = self.embedder([lore_text])[0]
# Add to ChromaDB collection
self.chroma_collection.add(
documents=[lore_text],
embeddings=[embedding],
ids=[lore_id],
metadatas=[{"timestamp": datetime.now().isoformat(), "source": "user"}]
)
rprint(SUCCESS_LORE_ADDED.format(id=lore_id))
context['last_generated_lore'] = lore_text
except Exception as e:
rprint(ERROR_ADDING_LORE.format(error=e))
def generate_lore(self, args: List[str], context: Dict[str, Any]) -> None:
"""Generate new lore using LLM and config prompt.
Args:
args: Command arguments containing the prompt
context: Shared context dictionary
"""
if not args:
rprint(USAGE_GENERATE_LORE)
return
prompt = " ".join(args)
# Get relevant existing lore if available
existing_lore = ""
try:
if len(prompt) > MIN_QUERY_LENGTH:
results = self.query_lore_internal(prompt, 3)
if results:
existing_lore = "\n\n".join(results)
except Exception as e:
rprint(f"[dim]Note: Could not retrieve existing lore context. {e}[/dim]")
# Prepare system prompt with existing lore if available
system_prompt = self._get_system_prompt(existing_lore)
# Set up LLM request
llm_args = {
"model": self.config.get("lore_llm_model", self.config.get("llm_provider", "openai/gpt-4o-mini")),
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
"api_key": self.llm_api_key,
"max_tokens": DEFAULT_MAX_TOKENS
}
try:
response = litellm.completion(**llm_args)
generated = response["choices"][0]["message"]["content"]
context['last_generated_lore'] = generated
rprint(f"[green]Generated Lore:")
rprint(f"{generated}")
except Exception as e:
rprint(ERROR_GENERATING_LORE.format(error=e))
def store_generated_lore(self, args: List[str], context: Dict[str, Any]) -> None:
"""Store the last generated lore into ChromaDB.
Args:
args: Command arguments (unused)
context: Shared context dictionary containing last_generated_lore
"""
if not context.get("last_generated_lore"):
rprint(WARNING_NO_LORE_TO_STORE)
return
lore_text = context["last_generated_lore"]
lore_id = self._generate_lore_id()
try:
embedding = self.embedder([lore_text])[0]
self.chroma_collection.add(
documents=[lore_text],
embeddings=[embedding],
ids=[lore_id],
metadatas=[{"timestamp": datetime.now().isoformat(), "source": "generated"}]
)
rprint(SUCCESS_LORE_STORED.format(id=lore_id))
except Exception as e:
rprint(ERROR_STORING_LORE.format(error=e))
def query_lore_internal(self, query: str, n_results: int = DEFAULT_QUERY_RESULTS) -> List[str]:
"""Query relevant lore from the collection.
Args:
query: The search query text
n_results: Number of results to return
Returns:
List of relevant lore text entries
"""
# Let the ChromaDB collection handle the embedding internally
results = self.chroma_collection.query(
query_texts=[query],
n_results=n_results
)
docs = results.get("documents", [[]])[0]
return docs
def query_lore(self, args: List[str], context: Dict[str, Any]) -> None:
"""Query relevant lore entries.
Args:
args: Command arguments containing the query
context: Shared context dictionary
"""
if not args:
rprint(USAGE_QUERY_LORE)
return
query = " ".join(args)
try:
docs = self.query_lore_internal(query)
if docs:
rprint(f"[cyan]Relevant Lore Entries:")
for i, d in enumerate(docs, 1):
rprint(f"[bold]Entry {i}:[/bold] {d}")
else:
rprint(WARNING_NO_LORE_FOUND)
except Exception as e:
rprint(ERROR_QUERYING_LORE.format(error=e))
def list_all_lore(self, args: List[str], context: Dict[str, Any]) -> None:
"""List all lore entries in the system.
Args:
args: Command arguments (unused)
context: Shared context dictionary
"""
try:
# Get all documents from collection
results = self.chroma_collection.get()
docs = results.get("documents", [])
ids = results.get("ids", [])
if not docs:
rprint(INFO_NO_EXISTING_LORE)
return
rprint(f"[cyan]All Lore Entries ([bold]{len(docs)}[/bold] total):")
for i, (doc_id, doc) in enumerate(zip(ids, docs), 1):
# Truncate long entries for display
display_text = doc[:DISPLAY_TEXT_LIMIT] + "..." if len(doc) > DISPLAY_TEXT_LIMIT else doc
rprint(f"[bold]{i}. ID {doc_id}:[/bold] {display_text}")
rprint("[dim]Use query_lore to find specific entries.")
except Exception as e:
rprint(ERROR_LISTING_LORE.format(error=e))
def delete_lore(self, args: List[str], context: Dict[str, Any]) -> None:
"""Delete a specific lore entry by ID.
Args:
args: Command arguments containing the lore ID
context: Shared context dictionary
"""
if not args:
rprint(USAGE_DELETE_LORE)
return
lore_id = args[0]
try:
self.chroma_collection.delete(ids=[lore_id])
rprint(SUCCESS_LORE_DELETED.format(id=lore_id))
except Exception as e:
rprint(ERROR_DELETING_LORE.format(error=e))
# Global instance for backward compatibility
_lore_service: Optional[LoreService] = None
def init_lore(config: Dict[str, Any], chroma_collection: Any, embedder: Any, llm_api_key: str, chat_history: Any) -> None:
"""Initialize the global lore service with dependencies.
Args:
config: Application configuration dictionary
chroma_collection: ChromaDB collection for storing lore
embedder: Document embedding function
llm_api_key: API key for language model access
chat_history: Chat history module for character interactions
"""
global _lore_service
_lore_service = LoreService(config, chroma_collection, embedder, llm_api_key, chat_history)
def add_lore(args: List[str], context: Dict[str, Any]) -> None:
"""Command handler for adding lore entry to ChromaDB.
Args:
args: Command arguments containing the lore text
context: Shared context dictionary
"""
if _lore_service is None:
rprint(ERROR_LORE_SERVICE_NOT_INITIALIZED)
return
_lore_service.add_lore(args, context)
def generate_lore(args: List[str], context: Dict[str, Any]) -> None:
"""Command handler: Generate new lore using LLM and config prompt.
Args:
args: Command arguments containing the prompt
context: Shared context dictionary
"""
if _lore_service is None:
rprint(ERROR_LORE_SERVICE_NOT_INITIALIZED)
return
_lore_service.generate_lore(args, context)
def store_generated_lore(args: List[str], context: Dict[str, Any]) -> None:
"""Command handler: Store the last generated lore into ChromaDB.
Args:
args: Command arguments (unused)
context: Shared context dictionary containing last_generated_lore
"""
if _lore_service is None:
rprint(ERROR_LORE_SERVICE_NOT_INITIALIZED)
return
_lore_service.store_generated_lore(args, context)
def query_lore_internal(query: str, n_results: int = DEFAULT_QUERY_RESULTS) -> List[str]:
"""Internal helper function to query relevant lore.
Args:
query: The search query text
n_results: Number of results to return
Returns:
List of relevant lore text entries
"""
if _lore_service is None:
return []
return _lore_service.query_lore_internal(query, n_results)
def query_lore(args: List[str], context: Dict[str, Any]) -> None:
"""Command handler: Query relevant lore.
Args:
args: Command arguments containing the query
context: Shared context dictionary
"""
if _lore_service is None:
rprint(ERROR_LORE_SERVICE_NOT_INITIALIZED)
return
_lore_service.query_lore(args, context)
def list_all_lore(args: List[str], context: Dict[str, Any]) -> None:
"""Command handler: List all lore entries in the system.
Args:
args: Command arguments (unused)
context: Shared context dictionary
"""
if _lore_service is None:
rprint(ERROR_LORE_SERVICE_NOT_INITIALIZED)
return
_lore_service.list_all_lore(args, context)
def delete_lore(args: List[str], context: Dict[str, Any]) -> None:
"""Command handler: Delete a specific lore entry by ID.
Args:
args: Command arguments containing the lore ID
context: Shared context dictionary
"""
if _lore_service is None:
rprint(ERROR_LORE_SERVICE_NOT_INITIALIZED)
return
_lore_service.delete_lore(args, context)