Fixing uuid issues

This commit is contained in:
Dita Aji Pratama 2026-07-07 16:57:46 +07:00
parent 3d2587168a
commit 5779c9bcdf

View File

@ -14,7 +14,7 @@ def create_table(db_path, vector_size):
pyarrow.field('telegram_id', pyarrow.string(), nullable=True, metadata={'description': 'Telegram ID'}), pyarrow.field('telegram_id', pyarrow.string(), nullable=True, metadata={'description': 'Telegram ID'}),
pyarrow.field('telegram_username', pyarrow.string(), nullable=True, metadata={'description': 'Telegram username'}), pyarrow.field('telegram_username', pyarrow.string(), nullable=True, metadata={'description': 'Telegram username'}),
pyarrow.field('xampp_username', pyarrow.string(), nullable=True, metadata={'description': 'XAMPP username'}), pyarrow.field('xampp_username', pyarrow.string(), nullable=True, metadata={'description': 'XAMPP username'}),
pyarrow.field('vector_fullname', pyarrow.list_(pyarrow.float32(), vector_size) metadata={'description': 'Vector data of fullname'}), pyarrow.field('vector_fullname', pyarrow.list_(pyarrow.float32(), vector_size), metadata={'description': 'Vector data of fullname'}),
]) ])
db.create_table("knowledge_user", schema=schema_user) db.create_table("knowledge_user", schema=schema_user)
@ -29,7 +29,7 @@ def create_table(db_path, vector_size):
pyarrow.field('detail', pyarrow.string(), metadata={'description': 'Event detail'}), pyarrow.field('detail', pyarrow.string(), metadata={'description': 'Event detail'}),
pyarrow.field('physical', pyarrow.string(), metadata={'description': 'Character physical state'}), pyarrow.field('physical', pyarrow.string(), metadata={'description': 'Character physical state'}),
pyarrow.field('emotional', pyarrow.string(), metadata={'description': 'Character emotional state'}), pyarrow.field('emotional', pyarrow.string(), metadata={'description': 'Character emotional state'}),
pyarrow.field('vector_context', pyarrow.list_(pyarrow.float32(), vector_size) metadata={'description': 'Vector data of combined data'}), pyarrow.field('vector_context', pyarrow.list_(pyarrow.float32(), vector_size), metadata={'description': 'Vector data of combined data'}),
]) ])
db.create_table("knowledge_memories", schema=schema_memories) db.create_table("knowledge_memories", schema=schema_memories)
@ -94,6 +94,11 @@ def create_table(db_path, vector_size):
gc.collect() gc.collect()
def _uuid(val):
if isinstance(val, str):
return uuid.UUID(val)
return val
def embed_text(url, model, text): def embed_text(url, model, text):
response = requests.post(url=url, json={"model": model, "input": text} ) response = requests.post(url=url, json={"model": model, "input": text} )
data = response.json() data = response.json()
@ -105,7 +110,7 @@ def users_store(db_path, model_url, model_name, fullname, nickname, alias, perso
table = db.open_table("knowledge_user") table = db.open_table("knowledge_user")
vector = embed_text(model_url, model_name, fullname) vector = embed_text(model_url, model_name, fullname)
record = { record = {
"id": str(uuid.uuid4()), "id": uuid.uuid4(),
"timestamp": datetime.now(), "timestamp": datetime.now(),
"fullname": fullname, "fullname": fullname,
"nickname": nickname, "nickname": nickname,
@ -132,7 +137,7 @@ def users_filter(db_path, query_name=None, unique_id=None, persona_keyword=None,
table = db.open_table("knowledge_user") table = db.open_table("knowledge_user")
filters = [] filters = []
if user_id: if user_id:
filters.append(f"id = '{user_id}'") filters.append(f"id = X'{_uuid(user_id).hex}'")
if unique_id: if unique_id:
filters.append(f"(telegram_id = '{unique_id}' OR telegram_username = '{unique_id}' OR xampp_username = '{unique_id}')") filters.append(f"(telegram_id = '{unique_id}' OR telegram_username = '{unique_id}' OR xampp_username = '{unique_id}')")
if query_name: if query_name:
@ -156,7 +161,7 @@ def users_delete(db_path, user_id):
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_user") table = db.open_table("knowledge_user")
table.delete(f"id = '{user_id}'") table.delete(f"id = X'{_uuid(user_id).hex}'")
del table del table
del db del db
@ -171,7 +176,7 @@ def users_update(db_path, model_url, model_name, user_id, **updates):
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_user") table = db.open_table("knowledge_user")
current_record = table.search().where(f"id = '{user_id}'").to_list() current_record = table.search().where(f"id = X'{_uuid(user_id).hex}'").to_list()
if not current_record: if not current_record:
return False return False
@ -205,7 +210,7 @@ def memories_store(db_path, model_url, model_name, character, user, relative_tim
vector = embed_text(model_url, model_name, context_text) vector = embed_text(model_url, model_name, context_text)
record = { record = {
"id": str(uuid.uuid4()), "id": uuid.uuid4(),
"timestamp": datetime.now(), "timestamp": datetime.now(),
"character": character, "character": character,
"user": user, "user": user,
@ -280,7 +285,7 @@ def memories_delete(db_path, memory_id):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_memories") table = db.open_table("knowledge_memories")
table.delete(f"id = '{memory_id}'") table.delete(f"id = X'{_uuid(memory_id).hex}'")
del table del table
del db del db
gc.collect() gc.collect()
@ -293,7 +298,7 @@ def memories_update(db_path, model_url, model_name, memory_id, **updates):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_memories") table = db.open_table("knowledge_memories")
current_record = table.search().where(f"id = '{memory_id}'").to_list() current_record = table.search().where(f"id = X'{_uuid(memory_id).hex}'").to_list()
if not current_record: if not current_record:
return False return False
@ -325,7 +330,7 @@ def objects_store(db_path, model_url, model_name, keyword, when, where, name, de
context_text = f"Keyword: {keyword}\nWhen: {when}\nName: {name}" context_text = f"Keyword: {keyword}\nWhen: {when}\nName: {name}"
vector = embed_text(model_url, model_name, context_text) vector = embed_text(model_url, model_name, context_text)
record = { record = {
"id": str(uuid.uuid4()), "id": uuid.uuid4(),
"timestamp": datetime.now(), "timestamp": datetime.now(),
"keyword": keyword, "when": when, "where": where, "name": name, "description": description, "shape": shape, "usage": usage, "keyword": keyword, "when": when, "where": where, "name": name, "description": description, "shape": shape, "usage": usage,
"vector_context": vector "vector_context": vector
@ -340,7 +345,7 @@ def objects_filter(db_path, keyword=None, object_id=None):
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_object") table = db.open_table("knowledge_object")
query = "" query = ""
if object_id: query = f"id = '{object_id}'" if object_id: query = f"id = X'{_uuid(object_id).hex}'"
elif keyword: query = f"keyword = '{keyword}'" elif keyword: query = f"keyword = '{keyword}'"
results = table.search().where(query).to_list() if query else table.to_list() results = table.search().where(query).to_list() if query else table.to_list()
del table; del db; gc.collect() del table; del db; gc.collect()
@ -351,7 +356,7 @@ def objects_update(db_path, model_url, model_name, object_id, **updates):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_object") table = db.open_table("knowledge_object")
current = table.search().where(f"id = '{object_id}'").to_list() current = table.search().where(f"id = X'{_uuid(object_id).hex}'").to_list()
if not current: return False if not current: return False
record = current[0] record = current[0]
needs_reembed = any(f in updates for f in ['keyword', 'when', 'name']) needs_reembed = any(f in updates for f in ['keyword', 'when', 'name'])
@ -368,7 +373,7 @@ def objects_delete(db_path, object_id):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_object") table = db.open_table("knowledge_object")
table.delete(f"id = '{object_id}'") table.delete(f"id = X'{_uuid(object_id).hex}'")
del table; del db; gc.collect() del table; del db; gc.collect()
return True return True
except Exception as e: return False except Exception as e: return False
@ -380,7 +385,7 @@ def outfits_store(db_path, model_url, model_name, keyword, when, outfit):
context_text = f"Keyword: {keyword}\nWhen: {when}" context_text = f"Keyword: {keyword}\nWhen: {when}"
vector = embed_text(model_url, model_name, context_text) vector = embed_text(model_url, model_name, context_text)
record = { record = {
"id": str(uuid.uuid4()), "id": uuid.uuid4(),
"timestamp": datetime.now(), "timestamp": datetime.now(),
"keyword": keyword, "when": when, "outfit": outfit, "keyword": keyword, "when": when, "outfit": outfit,
"vector_context": vector "vector_context": vector
@ -395,7 +400,7 @@ def outfits_filter(db_path, keyword=None, outfit_id=None):
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_outfit_set") table = db.open_table("knowledge_outfit_set")
query = "" query = ""
if outfit_id: query = f"id = '{outfit_id}'" if outfit_id: query = f"id = X'{_uuid(outfit_id).hex}'"
elif keyword: query = f"keyword = '{keyword}'" elif keyword: query = f"keyword = '{keyword}'"
results = table.search().where(query).to_list() if query else table.to_list() results = table.search().where(query).to_list() if query else table.to_list()
del table; del db; gc.collect() del table; del db; gc.collect()
@ -406,7 +411,7 @@ def outfits_update(db_path, model_url, model_name, outfit_id, **updates):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_outfit_set") table = db.open_table("knowledge_outfit_set")
current = table.search().where(f"id = '{outfit_id}'").to_list() current = table.search().where(f"id = X'{_uuid(outfit_id).hex}'").to_list()
if not current: return False if not current: return False
record = current[0] record = current[0]
needs_reembed = any(f in updates for f in ['keyword', 'when']) needs_reembed = any(f in updates for f in ['keyword', 'when'])
@ -423,7 +428,7 @@ def outfits_delete(db_path, outfit_id):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_outfit_set") table = db.open_table("knowledge_outfit_set")
table.delete(f"id = '{outfit_id}'") table.delete(f"id = X'{_uuid(outfit_id).hex}'")
del table; del db; gc.collect() del table; del db; gc.collect()
return True return True
except Exception as e: return False except Exception as e: return False
@ -435,7 +440,7 @@ def todos_store(db_path, model_url, model_name, keyword, when, do):
context_text = f"Keyword: {keyword}\nWhen: {when}" context_text = f"Keyword: {keyword}\nWhen: {when}"
vector = embed_text(model_url, model_name, context_text) vector = embed_text(model_url, model_name, context_text)
record = { record = {
"id": str(uuid.uuid4()), "id": uuid.uuid4(),
"timestamp": datetime.now(), "timestamp": datetime.now(),
"keyword": keyword, "when": when, "do": do, "keyword": keyword, "when": when, "do": do,
"vector_context": vector "vector_context": vector
@ -450,7 +455,7 @@ def todos_filter(db_path, keyword=None, todo_id=None):
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_todo") table = db.open_table("knowledge_todo")
query = "" query = ""
if todo_id: query = f"id = '{todo_id}'" if todo_id: query = f"id = X'{_uuid(todo_id).hex}'"
elif keyword: query = f"keyword = '{keyword}'" elif keyword: query = f"keyword = '{keyword}'"
results = table.search().where(query).to_list() if query else table.to_list() results = table.search().where(query).to_list() if query else table.to_list()
del table; del db; gc.collect() del table; del db; gc.collect()
@ -461,7 +466,7 @@ def todos_update(db_path, model_url, model_name, todo_id, **updates):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_todo") table = db.open_table("knowledge_todo")
current = table.search().where(f"id = '{todo_id}'").to_list() current = table.search().where(f"id = X'{_uuid(todo_id).hex}'").to_list()
if not current: return False if not current: return False
record = current[0] record = current[0]
needs_reembed = any(f in updates for f in ['keyword', 'when']) needs_reembed = any(f in updates for f in ['keyword', 'when'])
@ -478,7 +483,7 @@ def todos_delete(db_path, todo_id):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_todo") table = db.open_table("knowledge_todo")
table.delete(f"id = '{todo_id}'") table.delete(f"id = X'{_uuid(todo_id).hex}'")
del table; del db; gc.collect() del table; del db; gc.collect()
return True return True
except Exception as e: return False except Exception as e: return False
@ -490,7 +495,7 @@ def worlds_store(db_path, model_url, model_name, category, location, description
context_text = f"Category: {category}\nLocation: {location}\nDescription: {description}" context_text = f"Category: {category}\nLocation: {location}\nDescription: {description}"
vector = embed_text(model_url, model_name, context_text) vector = embed_text(model_url, model_name, context_text)
record = { record = {
"id": str(uuid.uuid4()), "id": uuid.uuid4(),
"timestamp": datetime.now(), "timestamp": datetime.now(),
"category": category, "location": location, "description": description, "category": category, "location": location, "description": description,
"vector_context": vector "vector_context": vector
@ -505,7 +510,7 @@ def worlds_filter(db_path, category=None, location=None, world_id=None):
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_world") table = db.open_table("knowledge_world")
filters = [] filters = []
if world_id: filters.append(f"id = '{world_id}'") if world_id: filters.append(f"id = X'{_uuid(world_id).hex}'")
if category: filters.append(f"category = '{category}'") if category: filters.append(f"category = '{category}'")
if location: filters.append(f"location = '{location}'") if location: filters.append(f"location = '{location}'")
query = " AND ".join(filters) query = " AND ".join(filters)
@ -518,7 +523,7 @@ def worlds_update(db_path, model_url, model_name, world_id, **updates):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_world") table = db.open_table("knowledge_world")
current = table.search().where(f"id = '{world_id}'").to_list() current = table.search().where(f"id = X'{_uuid(world_id).hex}'").to_list()
if not current: return False if not current: return False
record = current[0] record = current[0]
needs_reembed = any(f in updates for f in ['category', 'location', 'description']) needs_reembed = any(f in updates for f in ['category', 'location', 'description'])
@ -535,7 +540,7 @@ def worlds_delete(db_path, world_id):
try: try:
db = lancedb.connect(db_path) db = lancedb.connect(db_path)
table = db.open_table("knowledge_world") table = db.open_table("knowledge_world")
table.delete(f"id = '{world_id}'") table.delete(f"id = X'{_uuid(world_id).hex}'")
del table; del db; gc.collect() del table; del db; gc.collect()
return True return True
except Exception as e: return False except Exception as e: return False