minor: tweak prompt wording, improve OAI history compression

This commit is contained in:
Liang Jiaqing
2026-03-25 10:00:16 +08:00
parent 85250c2694
commit 1b60d2c773
2 changed files with 9 additions and 6 deletions

2
ga.py
View File

@@ -278,7 +278,7 @@ class GenericAgentHandler(BaseHandler):
summary = f"调用工具{tool_name}, args: {args}" summary = f"调用工具{tool_name}, args: {args}"
if tool_name == 'no_tool': summary = "直接回答了用户问题" if tool_name == 'no_tool': summary = "直接回答了用户问题"
if type(ret.next_prompt) is str: if type(ret.next_prompt) is str:
ret.next_prompt += "\nPROTOCOL_VIOLATION: 上一轮遗漏了<summary>。 已根据物理动作自动补全。请务必在下次回复中记得<summary>协议。" ret.next_prompt += "\nPROTOCOL_VIOLATION: 上一轮遗漏了<summary>。 已根据物理动作自动补全。请务必在下次回复中记得<summary>协议。"
self.history_info.append('[Agent] ' + smart_format(summary, max_str_len=100)) self.history_info.append('[Agent] ' + smart_format(summary, max_str_len=100))
def do_code_run(self, args, response): def do_code_run(self, args, response):

View File

@@ -419,7 +419,7 @@ class NativeOAISession:
def __init__(self, cfg): def __init__(self, cfg):
self.api_key = cfg['apikey']; self.api_base = cfg['apibase'].rstrip('/') self.api_key = cfg['apikey']; self.api_base = cfg['apibase'].rstrip('/')
self.default_model = cfg.get('model', 'gpt-4o') self.default_model = cfg.get('model', 'gpt-4o')
self.context_win = cfg.get('context_win', 24000) self.context_win = cfg.get('context_win', 28000)
proxy = cfg.get('proxy') proxy = cfg.get('proxy')
self.proxies = {"http": proxy, "https": proxy} if proxy else None self.proxies = {"http": proxy, "https": proxy} if proxy else None
self.history = []; self.system = None; self.lock = threading.Lock() self.history = []; self.system = None; self.lock = threading.Lock()
@@ -470,10 +470,13 @@ class NativeOAISession:
elif isinstance(msg, list): msg = {"role": "user", "content": msg} elif isinstance(msg, list): msg = {"role": "user", "content": msg}
with self.lock: with self.lock:
self.history.append(msg) self.history.append(msg)
while len(self.history) > 2: compress_history_tags(self.history)
cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history) + len(self.system or '') cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history)
if cost <= self.context_win * 4: break if cost > self.context_win * 3:
self.history.pop(0); self.history.pop(0) target = self.context_win * 3 * 0.6
while len(self.history) > 2 and cost > target:
self.history.pop(0); self.history.pop(0)
cost = sum(len(json.dumps(m, ensure_ascii=False)) for m in self.history)
messages = list(self.history) messages = list(self.history)
content_blocks = None content_blocks = None
gen = self.raw_ask(messages, tools, self.system, model) gen = self.raw_ask(messages, tools, self.system, model)