Files
EvoScientist-Multi/EvoScientist/middleware/utils.py
T
Xi Zhang 897b444d27 Feat/context management middleware (#127)
* feat: Add context management middleware for improved error handling and context editing

* feat: Implement LLMToolSelectorMiddleware for enhanced tool selection and tracking

* feat(tests): update test functions to include mock timestamp parameter

* refactor: simplify tool selection state storage and update comments in middleware

* feat: Enhance tool selection handling and suppress structured output for improved event streaming

* refactor: simplify patching in test_create_tool_selector functions

* feat: add model parameter to create_tool_selector_middleware for enhanced flexibility

* feat: enhance tool selection suppression with JSON buffering for improved accuracy
2026-04-02 22:39:20 +02:00

45 lines
1.6 KiB
Python

"""Shared utilities for EvoScientist middleware.
Functions here are used by multiple middleware modules (memory, tool_selector)
and should not depend on any specific middleware class.
"""
from __future__ import annotations
from typing import Any
from langchain_core.language_models import BaseChatModel
def disable_thinking(model: BaseChatModel) -> BaseChatModel:
"""Return a copy of the model with thinking/reasoning disabled.
Anthropic's API does not allow extended thinking when ``tool_choice``
forces tool use (as ``with_structured_output`` does). Similarly,
OpenAI reasoning can conflict. Strip these settings so structured
output calls work reliably.
Uses ``model_copy()`` to produce a real new instance — ``bind()`` only
wraps the model in a ``RunnableBinding`` whose kwargs do NOT override
first-class Pydantic fields like ``thinking`` on ``ChatAnthropic``.
"""
updates: dict[str, Any] = {}
model_kwargs = getattr(model, "model_kwargs", {}) or {}
if getattr(model, "thinking", None) or "thinking" in model_kwargs:
updates["thinking"] = None
if getattr(model, "reasoning", None) or "reasoning" in model_kwargs:
updates["reasoning"] = None
if not updates:
return model
# Prefer Pydantic model_copy (creates a true new instance with the
# field cleared) over bind() which only adds invocation kwargs.
try:
return model.model_copy(update=updates)
except Exception:
# Fallback for non-Pydantic or unusual model classes
# Note: bind() may not effectively override first-class Pydantic fields
return model.bind(**updates)