Spaces:
Running
Running
Pulastya B
commited on
Commit
Β·
b54c2e5
1
Parent(s):
b7bc364
Fix Mistral message ordering: remove orphaned tool messages during pruning
Browse files- src/orchestrator.py +35 -5
src/orchestrator.py
CHANGED
|
@@ -2028,12 +2028,29 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2028 |
iteration += 1
|
| 2029 |
|
| 2030 |
try:
|
| 2031 |
-
# π
|
| 2032 |
# Keep only: system + user + last 4 exchanges (8 messages)
|
| 2033 |
-
#
|
| 2034 |
if len(messages) > 10:
|
| 2035 |
-
# Keep: system prompt [0], user query [1], last
|
| 2036 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2037 |
print(f"βοΈ Pruned conversation (keeping last 4 exchanges, ~4K tokens saved)")
|
| 2038 |
|
| 2039 |
# π Token estimation and warning
|
|
@@ -2043,7 +2060,20 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2043 |
)
|
| 2044 |
if estimated_tokens > 8000:
|
| 2045 |
# Emergency pruning - keep only last 2 exchanges
|
| 2046 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2047 |
print(f"β οΈ Emergency pruning (conversation > 8K tokens)")
|
| 2048 |
|
| 2049 |
# π° Token budget management (TPM limit)
|
|
|
|
| 2028 |
iteration += 1
|
| 2029 |
|
| 2030 |
try:
|
| 2031 |
+
# π SMART CONVERSATION PRUNING (Mistral-compatible)
|
| 2032 |
# Keep only: system + user + last 4 exchanges (8 messages)
|
| 2033 |
+
# CRITICAL: Maintain valid message ordering for Mistral API
|
| 2034 |
if len(messages) > 10:
|
| 2035 |
+
# Keep: system prompt [0], user query [1], last valid exchanges
|
| 2036 |
+
system_msg = messages[0]
|
| 2037 |
+
user_msg = messages[1]
|
| 2038 |
+
recent_msgs = messages[-8:]
|
| 2039 |
+
|
| 2040 |
+
# Ensure no orphaned tool messages after pruning
|
| 2041 |
+
# Mistral requires: assistant β tool β assistant β user (never tool after user)
|
| 2042 |
+
cleaned_recent = []
|
| 2043 |
+
for i, msg in enumerate(recent_msgs):
|
| 2044 |
+
# Skip tool messages that aren't preceded by assistant
|
| 2045 |
+
if msg.get('role') == 'tool':
|
| 2046 |
+
# Check if previous message is assistant
|
| 2047 |
+
if i > 0 and recent_msgs[i-1].get('role') == 'assistant':
|
| 2048 |
+
cleaned_recent.append(msg)
|
| 2049 |
+
# Otherwise skip this orphaned tool message
|
| 2050 |
+
else:
|
| 2051 |
+
cleaned_recent.append(msg)
|
| 2052 |
+
|
| 2053 |
+
messages = [system_msg, user_msg] + cleaned_recent
|
| 2054 |
print(f"βοΈ Pruned conversation (keeping last 4 exchanges, ~4K tokens saved)")
|
| 2055 |
|
| 2056 |
# π Token estimation and warning
|
|
|
|
| 2060 |
)
|
| 2061 |
if estimated_tokens > 8000:
|
| 2062 |
# Emergency pruning - keep only last 2 exchanges
|
| 2063 |
+
system_msg = messages[0]
|
| 2064 |
+
user_msg = messages[1]
|
| 2065 |
+
recent_msgs = messages[-4:]
|
| 2066 |
+
|
| 2067 |
+
# Clean orphaned tool messages
|
| 2068 |
+
cleaned_recent = []
|
| 2069 |
+
for i, msg in enumerate(recent_msgs):
|
| 2070 |
+
if msg.get('role') == 'tool':
|
| 2071 |
+
if i > 0 and recent_msgs[i-1].get('role') == 'assistant':
|
| 2072 |
+
cleaned_recent.append(msg)
|
| 2073 |
+
else:
|
| 2074 |
+
cleaned_recent.append(msg)
|
| 2075 |
+
|
| 2076 |
+
messages = [system_msg, user_msg] + cleaned_recent
|
| 2077 |
print(f"β οΈ Emergency pruning (conversation > 8K tokens)")
|
| 2078 |
|
| 2079 |
# π° Token budget management (TPM limit)
|