-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_db.py
More file actions
43 lines (31 loc) · 1.29 KB
/
Copy pathbot_db.py
File metadata and controls
43 lines (31 loc) · 1.29 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
import chromadb
from sentence_transformers import SentenceTransformer
db_path = "memory/ai_dm_chronicles"
chroma_client = chromadb.PersistentClient(path=db_path)
embedding_model = SentenceTransformer("all-MiniLM-L12-v2")
collection = chroma_client.get_or_create_collection(name="ttrpg_world_memory")
def remember_event(event_text: str):
"""Encodes a text event and stores it in the vector database."""
if not event_text.strip():
return
embedding = embedding_model.encode(event_text).tolist()
event_id = event_text
collection.add(
embeddings=[embedding],
documents=[event_text],
ids=[event_id]
)
def recall_relevant_events(player_action: str, n_results: int = 3) -> list:
"""Retrieves the most relevant past events from memory based on the player's action."""
if not player_action.strip():
return []
query_embedding = embedding_model.encode(player_action).tolist()
results = collection.query(
query_embeddings=[query_embedding],
n_results=n_results
)
return results['documents'][0]
def chat_bot(dm_response,player_action):
print(f"\nDM: {dm_response}")
turn_summary = f"The player chose to '{player_action}', and as a result: {dm_response}"
remember_event(turn_summary)