Compare commits

...

4 Commits

Author SHA1 Message Date
90798c6a38 RAG Business in progress 2026-07-30 17:04:13 +07:00
040721dd98 Minor improve 2026-07-30 17:03:17 +07:00
ef7e62e24b Update default config 2026-07-30 17:02:38 +07:00
c8158ed747 Error message 2026-07-30 09:54:46 +07:00
4 changed files with 111 additions and 6 deletions

View File

@ -35,7 +35,7 @@ llm:
- name : "z-ai/glm-5"
ragroleplay:
db_path : "./.ragroleplay"
db_path : "~/.config/hendrik/ragroleplay"
vector_size : 768 # used on table create only
model_url : "http://localhost:11434/api/embed"
model_name : "nomic-embed-text" # Need to download model from local ollama
@ -64,4 +64,4 @@ delay: # Humanize Delay (anti-bot detection)
typing_max : 10.0 # max typing delay limit per second
user:
id: "YOUR_USER_ID_HERE"
id: # Fill your user UUID here

View File

@ -130,6 +130,30 @@ def _agent_loop(app):
if response.warning:
log(app, "system", f" {response.warning}")
# Cek apakah response adalah error dari LLM client
content = response.content
is_error = isinstance(content, str) and (content.startswith("Error:") or content.startswith("HTTP Error:"))
if is_error:
# Hapus "Thinking..." log dan placeholder
for i in range(len(app.log) - 1, -1, -1):
if app.log[i].get('role') == 'system' and 'Thinking' in app.log[i].get('text', ''):
app.log.pop(i)
break
if placeholder_marker is not None:
for i in range(len(app.log) - 1, -1, -1):
if app.log[i].get('role') == 'ai':
text = app.log[i].get('text', '')
if placeholder_marker in text or text == "" or text == "...":
app.log.pop(i)
break
log(app, "error", content)
_add_msg(app, "assistant", content)
log(app, "sep", "")
ntro.end(stamp)
app.agent_done.set()
return
# Cek apakah ada tool_calls
has_tool_calls = bool(response.tool_calls)

84
lib/ragbusiness.py Normal file
View File

@ -0,0 +1,84 @@
import requests, gc, lancedb, pyarrow, uuid
from datetime import datetime
def _schema_domainlogic(vector_size):
return pyarrow.schema([
pyarrow.field('timestamp', pyarrow.timestamp('ms'), metadata={'description': 'When record created'}),
pyarrow.field('content', pyarrow.string(), metadata={'description': 'Content'}),
pyarrow.field('category', pyarrow.string(), metadata={'description': 'Category. Comma separated.'}),
pyarrow.field('vector_context', pyarrow.list_(pyarrow.float32(), vector_size), metadata={'description': 'Vector data of combined data'}),
])
_TABLE_SCHEMAS = {
'knowledge_domainlogic': _schema_domainlogic,
}
def db_init(db_path, vector_size):
db = lancedb.connect(db_path)
existing = db.table_names()
for name, schema_fn in _TABLE_SCHEMAS.items():
if name not in existing:
db.create_table(name, schema=schema_fn(vector_size))
print(f"[ragroleplay] Created table: {name}", flush=True)
del db
gc.collect()
def table_ensure(db_path, table_name, vector_size):
db = lancedb.connect(db_path)
existing = db.table_names()
if table_name not in existing:
schema_fn = _TABLE_SCHEMAS.get(table_name)
if schema_fn:
db.create_table(table_name, schema=schema_fn(vector_size))
del db
gc.collect()
def _uuid(val):
return uuid.UUID(val) if isinstance(val, str) else val
def embed_text(url, model, text):
response = requests.post(url=url, json={"model": model, "input": text} )
data = response.json()
return data["embeddings"][0]
def domainlogic_store(db_path, model_url, model_name, payload):
try:
db = lancedb.connect(db_path)
table = db.open_table("knowledge_domainlogic")
vector = embed_text(model_url, model_name, f'Category: {payload["category"]}. \n{payload["content"]}')
record = {
"timestamp" : datetime.now(),
"content" : payload["content" ],
"category" : payload["category" ],
"vector_context" : vector
}
table.add([record])
del table
del db
gc.collect()
return "success"
except Exception as e:
return e
def domainlogic_load(db_path, payload):
try:
db = lancedb.connect(db_path)
table = db.open_table("knowledge_domainlogic")
results = table.search().to_list()
del table
del db
gc.collect()
return results
except Exception as e:
print(f"error: {e}")
return []

View File

@ -115,11 +115,8 @@ def ensure_table(db_path, table_name, vector_size): # table_ensure seharusnya
del db
gc.collect()
def _uuid(val):
if isinstance(val, str):
return uuid.UUID(val)
return val
return uuid.UUID(val) if isinstance(val, str) else val
def embed_text(url, model, text):
response = requests.post(url=url, json={"model": model, "input": text} )