feat: add orig-based tag compression to LLMSession.make_messages
This commit is contained in:
29
sidercall.py
29
sidercall.py
@@ -82,8 +82,7 @@ class ClaudeSession:
|
|||||||
messages = self.make_messages(self.raw_msgs)
|
messages = self.make_messages(self.raw_msgs)
|
||||||
for chunk in self.raw_ask(messages, model):
|
for chunk in self.raw_ask(messages, model):
|
||||||
content += chunk; yield chunk
|
content += chunk; yield chunk
|
||||||
if not content.startswith("Error:"):
|
if not content.startswith("Error:"): self.raw_msgs.append({"role": "assistant", "prompt": content})
|
||||||
self.raw_msgs.append({"role": "assistant", "prompt": content})
|
|
||||||
return _ask_gen() if stream else ''.join(list(_ask_gen()))
|
return _ask_gen() if stream else ''.join(list(_ask_gen()))
|
||||||
|
|
||||||
class LLMSession:
|
class LLMSession:
|
||||||
@@ -110,13 +109,11 @@ class LLMSession:
|
|||||||
if not line or not line.startswith("data:"): continue
|
if not line or not line.startswith("data:"): continue
|
||||||
data = line[5:].lstrip()
|
data = line[5:].lstrip()
|
||||||
if data == "[DONE]": break
|
if data == "[DONE]": break
|
||||||
obj = json.loads(data)
|
obj = json.loads(data); ch = (obj.get("choices") or [{}])[0]
|
||||||
ch = (obj.get("choices") or [{}])[0]
|
|
||||||
finish_reason = ch.get("finish_reason")
|
finish_reason = ch.get("finish_reason")
|
||||||
delta = (ch.get("delta") or {}).get("content")
|
delta = (ch.get("delta") or {}).get("content")
|
||||||
if delta:
|
if delta:
|
||||||
yield delta
|
yield delta; buffer += delta
|
||||||
buffer += delta
|
|
||||||
if '</tool_use>' in buffer[-30:]: break
|
if '</tool_use>' in buffer[-30:]: break
|
||||||
if finish_reason: break
|
if finish_reason: break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -124,15 +121,23 @@ class LLMSession:
|
|||||||
|
|
||||||
def make_messages(self, raw_list, omit_images=True):
|
def make_messages(self, raw_list, omit_images=True):
|
||||||
messages = []
|
messages = []
|
||||||
for msg in raw_list:
|
for i, msg in enumerate(raw_list):
|
||||||
if omit_images and msg['image']:
|
if i < len(raw_list) - 4 and 'orig' not in msg:
|
||||||
messages.append({"role": msg['role'], "content": "[Image omitted, if you needed it, ask me]\n" + msg['prompt']})
|
msg['orig'] = msg['prompt']
|
||||||
|
for tag in ('thinking', 'tool_use', 'tool_result'):
|
||||||
|
msg['prompt'] = re.sub(
|
||||||
|
rf'(<{tag}>)([\s\S]*?)(</{tag}>)',
|
||||||
|
lambda m: m.group(1) + (m.group(2)[:200] + '...') + m.group(3) if len(m.group(2)) > 200 else m.group(0),
|
||||||
|
msg['prompt']
|
||||||
|
)
|
||||||
|
prompt = msg['prompt']
|
||||||
|
if omit_images and msg['image']: messages.append({"role": msg['role'], "content": "[Image omitted, if you needed it, ask me]\n" + prompt})
|
||||||
elif not omit_images and msg['image']:
|
elif not omit_images and msg['image']:
|
||||||
messages.append({"role": msg['role'], "content": [
|
messages.append({"role": msg['role'], "content": [
|
||||||
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{msg['image']}"}},
|
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{msg['image']}"}},
|
||||||
{"type": "text", "text": msg['prompt']} ]})
|
{"type": "text", "text": prompt} ]})
|
||||||
else:
|
else:
|
||||||
messages.append({"role": msg['role'], "content": msg['prompt']})
|
messages.append({"role": msg['role'], "content": prompt})
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
def summary_history(self, model=None):
|
def summary_history(self, model=None):
|
||||||
@@ -210,7 +215,6 @@ class GeminiSession:
|
|||||||
class MockFunction:
|
class MockFunction:
|
||||||
def __init__(self, name, arguments): self.name, self.arguments = name, arguments
|
def __init__(self, name, arguments): self.name, self.arguments = name, arguments
|
||||||
|
|
||||||
|
|
||||||
class MockToolCall:
|
class MockToolCall:
|
||||||
def __init__(self, name, args):
|
def __init__(self, name, args):
|
||||||
arg_str = json.dumps(args, ensure_ascii=False) if isinstance(args, dict) else args
|
arg_str = json.dumps(args, ensure_ascii=False) if isinstance(args, dict) else args
|
||||||
@@ -254,7 +258,6 @@ class ToolClient:
|
|||||||
def _build_protocol_prompt(self, messages, tools):
|
def _build_protocol_prompt(self, messages, tools):
|
||||||
system_content = next((m['content'] for m in messages if m['role'].lower() == 'system'), "")
|
system_content = next((m['content'] for m in messages if m['role'].lower() == 'system'), "")
|
||||||
history_msgs = [m for m in messages if m['role'].lower() != 'system']
|
history_msgs = [m for m in messages if m['role'].lower() != 'system']
|
||||||
|
|
||||||
# 构造工具描述
|
# 构造工具描述
|
||||||
tool_instruction = ""
|
tool_instruction = ""
|
||||||
if tools:
|
if tools:
|
||||||
|
|||||||
Reference in New Issue
Block a user