KiroProxy User commited on
Commit ·
703f1ac
1
Parent(s): d3cadd5
Improve thinking prompt system with better constraints and history handling
Browse files
KiroProxy/kiro_proxy/core/thinking.py
CHANGED
|
@@ -318,28 +318,59 @@ def format_thinking_block(thinking_content: str) -> str:
|
|
| 318 |
return f"<thinking>\n{thinking_content}\n</thinking>"
|
| 319 |
|
| 320 |
|
| 321 |
-
def build_thinking_prompt(
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
-
|
|
|
|
| 325 |
"""
|
| 326 |
-
if user_content is None:
|
| 327 |
-
user_content = ""
|
| 328 |
-
|
| 329 |
budget_str = ""
|
| 330 |
if budget_tokens:
|
| 331 |
-
budget_str = f" Budget: {budget_tokens} tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
return (
|
| 334 |
-
f"
|
| 335 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
"1. Break down the problem into components\n"
|
| 337 |
-
"2. Consider multiple
|
| 338 |
"3. Evaluate trade-offs and edge cases\n"
|
| 339 |
-
"4.
|
| 340 |
-
|
|
|
|
| 341 |
)
|
| 342 |
|
|
|
|
| 343 |
def build_user_prompt_with_thinking(user_content: str, thinking_content: str) -> str:
|
| 344 |
"""Inject thinking into the main prompt.
|
| 345 |
|
|
@@ -402,8 +433,14 @@ async def fetch_thinking_text(
|
|
| 402 |
timeout_s: float = 600.0,
|
| 403 |
) -> str:
|
| 404 |
"""Non-streaming helper to get thinking content (best-effort)."""
|
| 405 |
-
thinking_prompt = build_thinking_prompt(user_content, budget_tokens=budget_tokens)
|
| 406 |
clean_history = strip_thinking_from_history(history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
thinking_request = build_kiro_request(
|
| 408 |
thinking_prompt,
|
| 409 |
model,
|
|
@@ -435,8 +472,14 @@ async def stream_thinking_text(
|
|
| 435 |
timeout_s: float = 600.0,
|
| 436 |
) -> AsyncIterator[str]:
|
| 437 |
"""Streaming helper to yield thinking content incrementally (best-effort)."""
|
| 438 |
-
thinking_prompt = build_thinking_prompt(user_content, budget_tokens=budget_tokens)
|
| 439 |
clean_history = strip_thinking_from_history(history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
thinking_request = build_kiro_request(
|
| 441 |
thinking_prompt,
|
| 442 |
model,
|
|
|
|
| 318 |
return f"<thinking>\n{thinking_content}\n</thinking>"
|
| 319 |
|
| 320 |
|
| 321 |
+
def build_thinking_prompt(
|
| 322 |
+
user_content: str,
|
| 323 |
+
*,
|
| 324 |
+
budget_tokens: Optional[int],
|
| 325 |
+
history: list = None,
|
| 326 |
+
has_tool_results: bool = False
|
| 327 |
+
) -> str:
|
| 328 |
+
"""Build a thinking prompt for internal reasoning phase.
|
| 329 |
|
| 330 |
+
Key: This is planning-only, no actual tool execution.
|
| 331 |
+
The model will receive full conversation history via the API.
|
| 332 |
"""
|
|
|
|
|
|
|
|
|
|
| 333 |
budget_str = ""
|
| 334 |
if budget_tokens:
|
| 335 |
+
budget_str = f" (Budget: {budget_tokens} tokens)"
|
| 336 |
+
|
| 337 |
+
# 根据是否有工具结果调整提示
|
| 338 |
+
if has_tool_results:
|
| 339 |
+
return (
|
| 340 |
+
f"[INTERNAL REASONING PHASE]{budget_str}\n\n"
|
| 341 |
+
"You have received tool execution results. Based on the COMPLETE conversation history above, "
|
| 342 |
+
"think through what these results mean and plan your response.\n\n"
|
| 343 |
+
"CRITICAL CONSTRAINTS:\n"
|
| 344 |
+
"- DO NOT call any tools in this thinking phase - analysis only\n"
|
| 345 |
+
"- DO NOT output JSON tool calls or function_call structures\n"
|
| 346 |
+
"- Output ONLY plain text reasoning\n"
|
| 347 |
+
"- IMPORTANT: Think in the SAME LANGUAGE as the user's request\n\n"
|
| 348 |
+
"Think about:\n"
|
| 349 |
+
"1. What was the user's original request (from conversation history)?\n"
|
| 350 |
+
"2. What do the tool results tell us?\n"
|
| 351 |
+
"3. Is the task complete, or are more steps needed?\n"
|
| 352 |
+
"4. How should I synthesize this into a helpful response?\n\n"
|
| 353 |
+
f"Current input: {user_content}"
|
| 354 |
+
)
|
| 355 |
|
| 356 |
return (
|
| 357 |
+
f"[INTERNAL REASONING PHASE]{budget_str}\n\n"
|
| 358 |
+
"Based on the conversation history above (if any), analyze the current request thoroughly.\n\n"
|
| 359 |
+
"CRITICAL CONSTRAINTS:\n"
|
| 360 |
+
"- DO NOT call any tools - planning and analysis only\n"
|
| 361 |
+
"- DO NOT output JSON tool calls, function_call, or tool_use structures\n"
|
| 362 |
+
"- Output ONLY plain text reasoning\n"
|
| 363 |
+
"- IMPORTANT: Think in the SAME LANGUAGE as the user's request\n\n"
|
| 364 |
+
"Think about:\n"
|
| 365 |
"1. Break down the problem into components\n"
|
| 366 |
+
"2. Consider multiple approaches and perspectives\n"
|
| 367 |
"3. Evaluate trade-offs and edge cases\n"
|
| 368 |
+
"4. Plan what actions/tools would be needed (without executing)\n"
|
| 369 |
+
"5. Synthesize into a coherent strategy\n\n"
|
| 370 |
+
f"Current request: {user_content}"
|
| 371 |
)
|
| 372 |
|
| 373 |
+
|
| 374 |
def build_user_prompt_with_thinking(user_content: str, thinking_content: str) -> str:
|
| 375 |
"""Inject thinking into the main prompt.
|
| 376 |
|
|
|
|
| 433 |
timeout_s: float = 600.0,
|
| 434 |
) -> str:
|
| 435 |
"""Non-streaming helper to get thinking content (best-effort)."""
|
|
|
|
| 436 |
clean_history = strip_thinking_from_history(history)
|
| 437 |
+
has_tool_results = bool(tool_results)
|
| 438 |
+
thinking_prompt = build_thinking_prompt(
|
| 439 |
+
user_content,
|
| 440 |
+
budget_tokens=budget_tokens,
|
| 441 |
+
history=clean_history,
|
| 442 |
+
has_tool_results=has_tool_results
|
| 443 |
+
)
|
| 444 |
thinking_request = build_kiro_request(
|
| 445 |
thinking_prompt,
|
| 446 |
model,
|
|
|
|
| 472 |
timeout_s: float = 600.0,
|
| 473 |
) -> AsyncIterator[str]:
|
| 474 |
"""Streaming helper to yield thinking content incrementally (best-effort)."""
|
|
|
|
| 475 |
clean_history = strip_thinking_from_history(history)
|
| 476 |
+
has_tool_results = bool(tool_results)
|
| 477 |
+
thinking_prompt = build_thinking_prompt(
|
| 478 |
+
user_content,
|
| 479 |
+
budget_tokens=budget_tokens,
|
| 480 |
+
history=clean_history,
|
| 481 |
+
has_tool_results=has_tool_results
|
| 482 |
+
)
|
| 483 |
thinking_request = build_kiro_request(
|
| 484 |
thinking_prompt,
|
| 485 |
model,
|