Improve RAG on TUI
This commit is contained in:
parent
9ded4e7b9e
commit
685e97d4ef
@ -7,6 +7,8 @@ from .render import init_colors, draw
|
||||
from .input import handle_key
|
||||
from .agent import log, WELCOME_ART
|
||||
from services.session_manager_neo import NeoSessionManager, NeoSession
|
||||
from lib import ragroleplay
|
||||
from lib import personality as personality_mod
|
||||
|
||||
|
||||
class HendrikTUI:
|
||||
@ -51,59 +53,61 @@ class HendrikTUI:
|
||||
"model": self.llm.model,
|
||||
}
|
||||
|
||||
def _inject_user_context(self) -> str | None:
|
||||
user_id = config.TUI_USER_ID
|
||||
if not user_id:
|
||||
return None
|
||||
|
||||
try:
|
||||
results = ragroleplay.user_load(
|
||||
config.ragroleplay_db_path,
|
||||
user_id=user_id,
|
||||
character=personality_mod.PERSONALITY.name
|
||||
)
|
||||
if results:
|
||||
u = results[0]
|
||||
return (
|
||||
f'[User Context]\n'
|
||||
f'ID: {u["id"]}\n'
|
||||
f'Nama: {u["fullname"]} ({u["nickname"]})\n'
|
||||
f'Family: {u.get("familyname", "-") or "-"}\n'
|
||||
f'Character: {u.get("character", "-") or "-"}\n'
|
||||
f'Alias: {u.get("alias", "-") or "-"}\n'
|
||||
f'Salutation: {u.get("salutation", "-") or "-"}\n'
|
||||
f'Persona: {u.get("persona", "-") or "-"}\n'
|
||||
f'Telegram: {u.get("telegram_id", "-") or "-"} / @{u.get("telegram_username", "-") or "-"}\n'
|
||||
f'XMPP: {u.get("xampp_username", "-") or "-"}\n'
|
||||
f'[/User Context]\n'
|
||||
f'[PENTING: Kamu SUDAH mengenal user ini. WAJIB panggil memories_latest(character="{personality_mod.PERSONALITY.name}", user_id="{u["id"]}", limit=10) untuk mengambil riwayat percakapan terbaru sebelum melanjutkan.]'
|
||||
)
|
||||
else:
|
||||
return (
|
||||
f'[User Context: Pengguna baru — belum ada di database]\n'
|
||||
f'[Platform: TUI]\n'
|
||||
f'[PENTING: Kamu BELUM mengenal user ini. WAJIB tanya nama sebagai pembuka. '
|
||||
f'Setelah nama diketahui, simpan via users_store. '
|
||||
f'Lanjutkan percakapan natural dan proaktif melengkapi data user lainnya di pesan berikutnya.]'
|
||||
)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def new_session(self):
|
||||
self.current_session = None
|
||||
|
||||
# --- Context Injection for TUI ---
|
||||
import config
|
||||
from lib import ragroleplay
|
||||
|
||||
user_id = config.TUI_USER_ID
|
||||
context_text = None
|
||||
|
||||
if user_id:
|
||||
try:
|
||||
# Meniru logika telegram_client.py untuk load user context
|
||||
results = ragroleplay.user_load(config.ragroleplay_db_path, unique_id=user_id, character=config.AGENT_CHARACTER or "Lily")
|
||||
if results:
|
||||
u = results[0]
|
||||
context_text = (
|
||||
f'[User Context]\n'
|
||||
f'ID: {u["id"]}\n'
|
||||
f'Nama: {u["fullname"]} ({u["nickname"]})\n'
|
||||
f'Family: {u.get("familyname", "-") or "-"}\n'
|
||||
f'Character: {u.get("character", "-") or "-"}\n'
|
||||
f'Alias: {u.get("alias", "-") or "-"}\n'
|
||||
f'Salutation: {u.get("salutation", "-") or "-"}\n'
|
||||
f'Persona: {u.get("persona", "-") or "-"}\n'
|
||||
f'Telegram: {u.get("telegram_id", "-") or "-"} / @{u.get("telegram_username", "-") or "-"}\n'
|
||||
f'XMPP: {u.get("xampp_username", "-") or "-"}\n'
|
||||
f'[/User Context]'
|
||||
)
|
||||
else:
|
||||
context_text = (
|
||||
f'[User Context: Pengguna baru — belum ada di database]\n'
|
||||
f'[Platform: TUI]\n'
|
||||
f'[PENTING: Kamu BELUM mengenal user ini. WAJIB tanya nama sebagai pembuka.]'
|
||||
)
|
||||
except Exception as e:
|
||||
# Log error silently like in telegram_client
|
||||
pass
|
||||
|
||||
system_prompt = self.build_system_prompt(
|
||||
tools_definition=self.tools_def,
|
||||
character=config.AGENT_CHARACTER or None,
|
||||
skills=config.AGENT_SKILLS.split(",") if config.AGENT_SKILLS else None,
|
||||
)
|
||||
|
||||
# Inject context if found
|
||||
messages = [{"role": "system", "content": system_prompt}]
|
||||
if context_text:
|
||||
messages.append({"role": "system", "content": context_text})
|
||||
|
||||
|
||||
context = self._inject_user_context()
|
||||
if context:
|
||||
messages.append({"role": "system", "content": context})
|
||||
|
||||
self.messages = messages
|
||||
# ---------------------------------
|
||||
|
||||
|
||||
self.log.clear()
|
||||
self.scroll = 0
|
||||
self.input_buffer = [""]
|
||||
@ -165,6 +169,9 @@ class HendrikTUI:
|
||||
character=config.AGENT_CHARACTER or None,
|
||||
skills=config.AGENT_SKILLS.split(",") if config.AGENT_SKILLS else None,
|
||||
)}]
|
||||
context = self._inject_user_context()
|
||||
if context:
|
||||
self.messages.append({"role": "system", "content": context})
|
||||
log(self, "welcome", WELCOME_ART)
|
||||
|
||||
while self.running:
|
||||
|
||||
@ -26,7 +26,7 @@ schema_users_store = {
|
||||
"telegram_username" : {"type": "string", "description": "Telegram username"},
|
||||
"xampp_username" : {"type": "string", "description": "XAMPP username"}
|
||||
},
|
||||
"required": ["fullname", "nickname", "character", "telegram_id"]
|
||||
"required": ["fullname", "nickname", "character"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user