Refactoring
This commit is contained in:
parent
e26a026e0c
commit
7aa1f56124
@ -1,11 +1,9 @@
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def _ts():
|
def _ts():
|
||||||
return datetime.now().strftime('%H:%M:%S')
|
return datetime.now().strftime('%H:%M:%S')
|
||||||
|
|
||||||
|
|
||||||
def execute_tool(tool_call, TOOL_HANDLERS):
|
def execute_tool(tool_call, TOOL_HANDLERS):
|
||||||
tname = tool_call['function']['name']
|
tname = tool_call['function']['name']
|
||||||
targs = json.loads(tool_call['function']['arguments'])
|
targs = json.loads(tool_call['function']['arguments'])
|
||||||
@ -26,7 +24,6 @@ def execute_tool(tool_call, TOOL_HANDLERS):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f'Error executing tool: {str(e)}'
|
return f'Error executing tool: {str(e)}'
|
||||||
|
|
||||||
|
|
||||||
def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on_tool_calls=None):
|
def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on_tool_calls=None):
|
||||||
for step in range(max_iterations):
|
for step in range(max_iterations):
|
||||||
print(f'[{_ts()}] Step {step + 1} — calling LLM...', flush=True)
|
print(f'[{_ts()}] Step {step + 1} — calling LLM...', flush=True)
|
||||||
@ -66,3 +63,4 @@ def run_agent_loop(session, llm_client, TOOLS, TOOL_HANDLERS, max_iterations, on
|
|||||||
'content': 'Max iterations reached without final answer.',
|
'content': 'Max iterations reached without final answer.',
|
||||||
})
|
})
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
from .personality import build_system_prompt
|
import re
|
||||||
|
|
||||||
def tools_mapping(schema, handler, name=None):
|
def tools_mapping(schema, handler, name=None):
|
||||||
tool_name = name or schema["function"]["name"]
|
tool_name = name or schema["function"]["name"]
|
||||||
@ -10,3 +10,25 @@ def tool_schemas(tools_definition):
|
|||||||
def tool_handlers(tools_definition):
|
def tool_handlers(tools_definition):
|
||||||
return {t["name"]: t["handler"] for t in tools_definition}
|
return {t["name"]: t["handler"] for t in tools_definition}
|
||||||
|
|
||||||
|
def strip_thinking(text: str) -> str:
|
||||||
|
if not text:
|
||||||
|
return text
|
||||||
|
# Strip XML-style thinking blocks (case-insensitive, DOTALL for multiline)
|
||||||
|
text = re.sub(r'<think[^>]*>.*?</think>', '', text, flags=re.DOTALL | re.IGNORECASE)
|
||||||
|
text = re.sub(r'<reasoning[^>]*>.*?</reasoning>', '', text, flags=re.DOTALL | re.IGNORECASE)
|
||||||
|
# Strip lines starting with Thinking: / Reasoning: / Let me think...
|
||||||
|
lines = text.splitlines()
|
||||||
|
cleaned = []
|
||||||
|
skip_block = False
|
||||||
|
for line in lines:
|
||||||
|
stripped = line.strip().lower()
|
||||||
|
if stripped.startswith(('thinking:', 'reasoning:', 'let me thought', 'let me think')):
|
||||||
|
skip_block = True
|
||||||
|
continue
|
||||||
|
if skip_block and not stripped:
|
||||||
|
skip_block = False
|
||||||
|
continue
|
||||||
|
if not skip_block:
|
||||||
|
cleaned.append(line)
|
||||||
|
result = '\n'.join(cleaned).strip()
|
||||||
|
return result
|
||||||
|
|||||||
@ -1,49 +1,14 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
from scripts import gadget
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import urllib.error
|
import urllib.error
|
||||||
|
|
||||||
|
|
||||||
def _strip_thinking(text: str) -> str:
|
|
||||||
"""
|
|
||||||
Hapus semua bentuk thinking/reasoning dari response text.
|
|
||||||
Handles:
|
|
||||||
- <think>...</think> blocks (any case)
|
|
||||||
- <reasoning>...</reasoning> blocks
|
|
||||||
- "Thinking:" / "Reasoning:" inline prefixes
|
|
||||||
"""
|
|
||||||
if not text:
|
|
||||||
return text
|
|
||||||
|
|
||||||
# Strip XML-style thinking blocks (case-insensitive, DOTALL for multiline)
|
|
||||||
text = re.sub(r'<think[^>]*>.*?</think>', '', text, flags=re.DOTALL | re.IGNORECASE)
|
|
||||||
text = re.sub(r'<reasoning[^>]*>.*?</reasoning>', '', text, flags=re.DOTALL | re.IGNORECASE)
|
|
||||||
|
|
||||||
# Strip lines starting with Thinking: / Reasoning: / Let me think...
|
|
||||||
lines = text.splitlines()
|
|
||||||
cleaned = []
|
|
||||||
skip_block = False
|
|
||||||
for line in lines:
|
|
||||||
stripped = line.strip().lower()
|
|
||||||
if stripped.startswith(('thinking:', 'reasoning:', 'let me thought', 'let me think')):
|
|
||||||
skip_block = True
|
|
||||||
continue
|
|
||||||
if skip_block and not stripped:
|
|
||||||
skip_block = False
|
|
||||||
continue
|
|
||||||
if not skip_block:
|
|
||||||
cleaned.append(line)
|
|
||||||
|
|
||||||
result = '\n'.join(cleaned).strip()
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
class LLMClient:
|
class LLMClient:
|
||||||
class Message:
|
class Message:
|
||||||
def __init__(self, msg):
|
def __init__(self, msg):
|
||||||
raw_content = msg.get('content', '')
|
raw_content = msg.get('content', '')
|
||||||
# Auto-strip thinking dari content
|
# Auto-strip thinking dari content
|
||||||
self.content = _strip_thinking(raw_content) if isinstance(raw_content, str) else raw_content
|
self.content = gadget.strip_thinking(raw_content) if isinstance(raw_content, str) else raw_content
|
||||||
self.tool_calls = msg.get('tool_calls', None)
|
self.tool_calls = msg.get('tool_calls', None)
|
||||||
self.warning = None
|
self.warning = None
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
import config
|
import config
|
||||||
from services.session_manager import SessionManager
|
from services.session_manager import SessionManager
|
||||||
from services.agent_loop import run_agent_loop
|
from scripts.agent_loop import run_agent_loop
|
||||||
from scripts.personality import PERSONALITY
|
from scripts.personality import PERSONALITY
|
||||||
from tools.roleplayer import _name_mentioned
|
from tools.roleplayer import _name_mentioned
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
from slixmpp import ClientXMPP
|
from slixmpp import ClientXMPP
|
||||||
from services.session_manager import SessionManager
|
from services.session_manager import SessionManager
|
||||||
from services.agent_loop import run_agent_loop
|
from scripts.agent_loop import run_agent_loop
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from tools.roleplayer import should_respond
|
from tools.roleplayer import should_respond
|
||||||
@ -436,7 +436,7 @@ class XMPPClient(ClientXMPP):
|
|||||||
session.start_timer(300, self._timeout_session, room, 'groupchat')
|
session.start_timer(300, self._timeout_session, room, 'groupchat')
|
||||||
|
|
||||||
def _execute_tool(self, tool_call):
|
def _execute_tool(self, tool_call):
|
||||||
from services.agent_loop import execute_tool
|
from scripts.agent_loop import execute_tool
|
||||||
return execute_tool(tool_call, self._TOOL_HANDLERS)
|
return execute_tool(tool_call, self._TOOL_HANDLERS)
|
||||||
|
|
||||||
def _schedule_send(self, to, body, mtype='chat'):
|
def _schedule_send(self, to, body, mtype='chat'):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user