You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
4.3 KiB
Python
95 lines
4.3 KiB
Python
import os
|
|
import json
|
|
import asyncio
|
|
import sys
|
|
import uuid
|
|
from datetime import datetime
|
|
from unittest.mock import MagicMock
|
|
|
|
# Set PYTHONPATH to include the current directory
|
|
sys.path.append(os.getcwd())
|
|
|
|
# Ensure we're in "Frontend Simulation" mode
|
|
os.environ["ENABLE_DEBUG_EXPORT"] = "True"
|
|
|
|
from src.ircfa_interaction.schema import ChatRequest
|
|
from src.auth.model import UserBase
|
|
from src.ircfa_interaction.stream2 import stream_create
|
|
from src.ircfa_interaction.model import Conversation as ConversationModel
|
|
from src.database.core import async_session, collector_async_session
|
|
|
|
# Token provided by USER
|
|
TOKEN = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc3NTExNTA5Ny4yMDc2OTgsImp0aSI6Im4xZTQ0SjBtYUpaMk9NR19mU2FjYlEiLCJ0eXBlIjoiYWNjZXNzIiwic3ViIjoiMTUzZjdlMmUtZjBlYS00NDBjLTk5Y2QtN2E3ODJhZThhNjMxIiwibmJmIjoxNzc1MTE1MDk3LCJjc3JmIjoiZjRmYjMzYmMtNWVlYy00MjQ5LTkzZjEtYzhiNTQwZmI5OGVlIiwiZXhwIjoyMDkwNDc1MDk3LCJub25jZSI6ImM1ZTlmODdiM2I1NDIyYjFlYzdjZGM0NDU1NDhlNWRhIiwidXNlcm5hbWUiOiJkZXZhZG1pbiJ9.QU8iC5_1gjne3YtrMM7IyKQ0P7K9a1zM8zv6LV7mw-PQx000ndj9oP1-sneqF8lPGUgXuPh2i3WeS-Oy1cqAe5OzrZNV4KxW2tCc6-uOJZSfJNrBOUyLUB9hTMR-EpLUy6_BSNghxxqMX1i4cGp23MCC1025Fi2xhuo48SrusBSHSJVELVVtx-o_97E7CqDJdTnuSmUk_oa5YthT-2ZS8N2svHf1u98lEjbBUVKXe99QQ8leZeFiaLebgETVS9ImHd0EMA4xGeIVNP6ocUmb0k8fbWZ4FvPTR2aHbLnzCOm-Bs-zyCer5Og363VfvJGXjWPXggSwj1FocktjojgKRQ"
|
|
|
|
async def simulate_frontend_chat(message: str, location_tag: str = ""):
|
|
print(f"\n{'='*50}")
|
|
print(f"SIMULATING FRONTEND REQUEST")
|
|
print(f"Message: {message}")
|
|
print(f"Location Tag Payload: '{location_tag}'")
|
|
print(f"{'='*50}\n")
|
|
|
|
current_user = UserBase(user_id=str(uuid.uuid4()), name="Frontend Sim User", role="admin")
|
|
conv_uuid = uuid.uuid4()
|
|
|
|
async with async_session() as db_session:
|
|
async with collector_async_session() as collector_db_session:
|
|
# Create Dummy Conv
|
|
new_conv = ConversationModel(
|
|
id=conv_uuid,
|
|
conversation_id=conv_uuid,
|
|
user_id=uuid.UUID(current_user.user_id),
|
|
conversation_title=f"Sim: {message[:20]}",
|
|
created_by=current_user.name,
|
|
updated_by=current_user.name
|
|
)
|
|
db_session.add(new_conv)
|
|
await db_session.commit()
|
|
|
|
request = MagicMock()
|
|
request.message = message
|
|
request.conversation_id = conv_uuid
|
|
request.attachments = []
|
|
|
|
common = {"db_session": db_session, "page": 1, "items_per_page": 5}
|
|
|
|
generator = stream_create(
|
|
db_session=db_session,
|
|
collector_db_session=collector_db_session,
|
|
request=request,
|
|
upload_type=None,
|
|
location_tag=location_tag,
|
|
current_user=current_user,
|
|
token=TOKEN,
|
|
common=common
|
|
)
|
|
|
|
print("--- STREAM START ---")
|
|
tool_calls = []
|
|
final_tag = None
|
|
async for chunk in generator:
|
|
if chunk.startswith("event: tool_result"):
|
|
# Extract tool name from chunk
|
|
# data: {"name": "...", "content": "..."}
|
|
try:
|
|
data_part = chunk.split("data: ")[1].strip()
|
|
tool_info = json.loads(data_part)
|
|
print(f"🛠️ [Tool Used]: {tool_info['name']}")
|
|
tool_calls.append(tool_info['name'])
|
|
except: pass
|
|
elif chunk.startswith("event: location_tag"):
|
|
final_tag = chunk.split("data: ")[1].strip()
|
|
print(f"📍 [Captured Tag]: {final_tag}")
|
|
elif chunk.startswith("data: "):
|
|
content = chunk[6:].strip()
|
|
if content and not content.startswith("{"):
|
|
print(f" {content}")
|
|
|
|
print("\n--- STREAM END ---")
|
|
print(f"Summary:")
|
|
print(f"- Search Tool Called: {'YES' if 'get_location_tag_equipment_name' in tool_calls else 'NO'}")
|
|
print(f"- Location Tag Found: {final_tag or 'None'}")
|
|
|
|
if __name__ == "__main__":
|
|
# Test case 1: General name "Boiler" with empty tag
|
|
asyncio.run(simulate_frontend_chat("bagaimana kondisi boiler saat ini?", location_tag=""))
|