feat: add verbose mode and telegram bot integration
- Add exhaust() helper for generator handling - Add verbose parameter to control output verbosity - Extend next_llm() to support direct index switching - Add list_llms() and get_llm_name() query methods - New tgapp.py: Telegram bot with streaming updates - Update stapp.py to use new LLM query API
This commit is contained in:
@@ -34,13 +34,18 @@ def json_default(o):
|
||||
if isinstance(o, set): return list(o)
|
||||
return str(o)
|
||||
|
||||
def exhaust(g):
|
||||
try:
|
||||
while True: next(g)
|
||||
except StopIteration as e: return e.value
|
||||
|
||||
def get_pretty_json(data):
|
||||
if isinstance(data, dict) and "script" in data:
|
||||
data = data.copy()
|
||||
data["script"] = data["script"].replace("; ", ";\n ")
|
||||
return json.dumps(data, indent=2, ensure_ascii=False).replace('\\n', '\n')
|
||||
|
||||
def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, max_turns=15):
|
||||
def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, max_turns=15, verbose=True):
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_input}
|
||||
@@ -50,7 +55,7 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
||||
if (turn+1) % 10 == 0: client.last_tools = '' # 每10轮重置一次工具描述,避免上下文过大导致的模型性能下降
|
||||
response_gen = client.chat(messages=messages, tools=tools_schema)
|
||||
response = yield from response_gen
|
||||
yield '\n\n'
|
||||
if verbose: yield '\n\n'
|
||||
|
||||
if not response.tool_calls:
|
||||
tool_name, args = 'no_tool', {}
|
||||
@@ -61,11 +66,16 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
|
||||
|
||||
if tool_name == 'no_tool': pass
|
||||
else:
|
||||
yield f"🛠️ **正在调用工具:** `{tool_name}` 📥**参数:**\n"
|
||||
yield f"````text\n{get_pretty_json(args)}\n````\n"
|
||||
yield f"🛠️ **正在调用工具:** `{tool_name}`"
|
||||
if verbose: yield f"📥**参数:**\n````text\n{get_pretty_json(args)}\n````\n"
|
||||
else: yield '\n\n\n'
|
||||
gen = handler.dispatch(tool_name, args, response)
|
||||
if verbose:
|
||||
yield '`````\n'
|
||||
outcome = yield from handler.dispatch(tool_name, args, response)
|
||||
outcome = yield from gen
|
||||
yield '`````\n'
|
||||
else:
|
||||
outcome = exhaust(gen)
|
||||
|
||||
if outcome.next_prompt is None: return {'result': 'CURRENT_TASK_DONE', 'data': outcome.data}
|
||||
if outcome.should_exit: return {'result': 'EXITED', 'data': outcome.data}
|
||||
|
||||
13
agentmain.py
13
agentmain.py
@@ -43,17 +43,19 @@ class GeneraticAgent:
|
||||
self.lock = threading.Lock()
|
||||
self.history = []
|
||||
self.task_queue = queue.Queue()
|
||||
self.display_queue = queue.Queue()
|
||||
self.last_active_time = time.time()
|
||||
self.is_running = False
|
||||
self.llm_no = 0
|
||||
self.stop_sig = False
|
||||
self.current_source = 'none'
|
||||
self.handler = None
|
||||
self.verbose = True
|
||||
|
||||
def next_llm(self):
|
||||
self.llm_no = (self.llm_no + 1) % len(self.llmclient.backends)
|
||||
def next_llm(self, n=-1):
|
||||
self.llm_no = ((self.llm_no + 1) if n < 0 else n) % len(self.llmclient.backends)
|
||||
self.llmclient.last_tools = ''
|
||||
def list_llms(self): return [(i, b.default_model, i == self.llm_no) for i, b in enumerate(self.llmclient.backends)]
|
||||
def get_llm_name(self): return self.llmclient.backends[self.llm_no].default_model
|
||||
|
||||
def abort(self):
|
||||
print('Abort current task...')
|
||||
@@ -82,9 +84,8 @@ class GeneraticAgent:
|
||||
handler = GenericAgentHandler(None, self.history, './temp')
|
||||
self.handler = handler
|
||||
self.llmclient.backend = self.llmclient.backends[self.llm_no]
|
||||
gen = agent_runner_loop(self.llmclient, sys_prompt,
|
||||
raw_query, handler, TOOLS_SCHEMA, max_turns=40)
|
||||
|
||||
gen = agent_runner_loop(self.llmclient, sys_prompt, raw_query,
|
||||
handler, TOOLS_SCHEMA, max_turns=40, verbose=self.verbose)
|
||||
try:
|
||||
full_response = ""; last_pos = 0
|
||||
for chunk in gen:
|
||||
|
||||
2
stapp.py
2
stapp.py
@@ -29,7 +29,7 @@ if 'autonomous_enabled' not in st.session_state:
|
||||
@st.fragment
|
||||
def render_sidebar():
|
||||
current_idx = agent.llm_no
|
||||
st.caption(f"LLM Core: {current_idx}: {agent.llmclient.backends[current_idx].default_model}", help="点击切换备用链路")
|
||||
st.caption(f"LLM Core: {current_idx}: {agent.get_llm_name()}", help="点击切换备用链路")
|
||||
last_reply_time = st.session_state.get('last_reply_time', 0)
|
||||
if last_reply_time > 0:
|
||||
st.caption(f"空闲时间:{int(time.time()) - last_reply_time}秒", help="当超过30分钟未收到回复时,系统会自动任务")
|
||||
|
||||
79
tgapp.py
Normal file
79
tgapp.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import os, sys, re, threading, asyncio, queue as Q
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from agentmain import GeneraticAgent
|
||||
from telegram import Update
|
||||
from telegram.ext import ApplicationBuilder, MessageHandler, CommandHandler, filters, ContextTypes
|
||||
import mykey
|
||||
|
||||
agent = GeneraticAgent()
|
||||
agent.verbose = False
|
||||
ALLOWED = set(getattr(mykey, 'tg_allowed_users', []))
|
||||
|
||||
_TAG_PATS = [r'<' + t + r'>.*?</' + t + r'>' for t in ('thinking', 'summary', 'tool_use')]
|
||||
_TAG_PATS.append(r'<file_content>.*?</file_content>')
|
||||
|
||||
def _clean(t):
|
||||
for p in _TAG_PATS:
|
||||
t = re.sub(p, '', t, flags=re.DOTALL)
|
||||
return re.sub(r'\n{3,}', '\n\n', t).strip() or '...'
|
||||
|
||||
async def _stream(dq, msg):
|
||||
last_text = ""
|
||||
while True:
|
||||
await asyncio.sleep(3)
|
||||
item = None
|
||||
try:
|
||||
while True: item = dq.get_nowait()
|
||||
except Q.Empty: pass
|
||||
if item is None: continue
|
||||
raw = item.get("done") or item.get("next", "")
|
||||
done = "done" in item
|
||||
show = _clean(raw)
|
||||
if len(show) > 4000:
|
||||
# freeze current msg, start a new one
|
||||
try: msg = await msg.reply_text("(continued...)")
|
||||
except Exception: pass
|
||||
last_text = ""
|
||||
show = show[-3900:]
|
||||
if show != last_text:
|
||||
try: await msg.edit_text(show)
|
||||
except Exception: pass
|
||||
last_text = show
|
||||
if done: break
|
||||
|
||||
async def handle_msg(update, ctx):
|
||||
uid = update.effective_user.id
|
||||
if ALLOWED and uid not in ALLOWED:
|
||||
return await update.message.reply_text("no")
|
||||
msg = await update.message.reply_text("thinking...")
|
||||
dq = agent.put_task(update.message.text, source="telegram")
|
||||
await _stream(dq, msg)
|
||||
|
||||
async def cmd_abort(update, ctx):
|
||||
agent.abort()
|
||||
await update.message.reply_text("Aborted")
|
||||
|
||||
async def cmd_llm(update, ctx):
|
||||
args = (update.message.text or '').split()
|
||||
if len(args) > 1:
|
||||
try:
|
||||
n = int(args[1])
|
||||
agent.next_llm(n)
|
||||
await update.message.reply_text(f"Switched to [{agent.llm_no}] {agent.get_llm_name()}")
|
||||
except (ValueError, IndexError):
|
||||
await update.message.reply_text(f"Usage: /llm <0-{len(agent.list_llms())-1}>")
|
||||
else:
|
||||
lines = [f"{'→' if cur else ' '} [{i}] {name}" for i, name, cur in agent.list_llms()]
|
||||
await update.message.reply_text("LLMs:\n" + "\n".join(lines))
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not ALLOWED:
|
||||
sys.exit('ERROR: tg_allowed_users in mykey.py is empty or missing. Set it to avoid unauthorized access.')
|
||||
threading.Thread(target=agent.run, daemon=True).start()
|
||||
proxy = os.environ.get('HTTPS_PROXY') or 'http://127.0.0.1:2082'
|
||||
app = ApplicationBuilder().token(mykey.tg_bot_token).proxy(proxy).get_updates_proxy(proxy).build()
|
||||
app.add_handler(CommandHandler("stop", cmd_abort))
|
||||
app.add_handler(CommandHandler("llm", cmd_llm))
|
||||
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_msg))
|
||||
print("TG bot running...")
|
||||
app.run_polling()
|
||||
Reference in New Issue
Block a user