-
Notifications
You must be signed in to change notification settings - Fork 19k
chore(langchain): cleanup langchain_v1
ruff config
#32810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,47 +16,47 @@ class AnthropicPromptCachingMiddleware(AgentMiddleware): | |
|
||
def __init__( | ||
self, | ||
type: Literal["ephemeral"] = "ephemeral", | ||
cache_type: Literal["ephemeral"] = "ephemeral", | ||
mdrxy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ttl: Literal["5m", "1h"] = "5m", | ||
min_messages_to_cache: int = 0, | ||
) -> None: | ||
"""Initialize the middleware with cache control settings. | ||
|
||
Args: | ||
type: The type of cache to use, only "ephemeral" is supported. | ||
cache_type: The type of cache to use, only "ephemeral" is supported. | ||
ttl: The time to live for the cache, only "5m" and "1h" are supported. | ||
min_messages_to_cache: The minimum number of messages until the cache is used, | ||
default is 0. | ||
""" | ||
self.type = type | ||
self.cache_type = cache_type | ||
self.ttl = ttl | ||
self.min_messages_to_cache = min_messages_to_cache | ||
|
||
def modify_model_request(self, request: ModelRequest, state: AgentState) -> ModelRequest: # noqa: ARG002 | ||
"""Modify the model request to add cache control blocks.""" | ||
try: | ||
from langchain_anthropic import ChatAnthropic | ||
except ImportError: | ||
except ImportError as e: | ||
msg = ( | ||
"AnthropicPromptCachingMiddleware caching middleware only supports " | ||
"Anthropic models." | ||
"Please install langchain-anthropic." | ||
) | ||
raise ValueError(msg) | ||
raise ValueError(msg) from e | ||
|
||
if not isinstance(request.model, ChatAnthropic): | ||
msg = ( | ||
"AnthropicPromptCachingMiddleware caching middleware only supports " | ||
f"Anthropic models, not instances of {type(request.model)}" | ||
) | ||
raise ValueError(msg) | ||
raise TypeError(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's still OK to do these breaking changes as it was not released yet ? |
||
|
||
messages_count = ( | ||
len(request.messages) + 1 if request.system_prompt else len(request.messages) | ||
) | ||
if messages_count < self.min_messages_to_cache: | ||
return request | ||
|
||
request.model_settings["cache_control"] = {"type": self.type, "ttl": self.ttl} | ||
request.model_settings["cache_control"] = {"type": self.cache_type, "ttl": self.ttl} | ||
|
||
return request |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,9 +122,9 @@ def _get_prompt_runnable(prompt: Prompt | None) -> Runnable: | |
lambda state: _get_state_value(state, "messages"), name=PROMPT_RUNNABLE_NAME | ||
) | ||
elif isinstance(prompt, str): | ||
_system_message: BaseMessage = SystemMessage(content=prompt) | ||
system_message: BaseMessage = SystemMessage(content=prompt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the motivation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ruff rule RUF052 |
||
prompt_runnable = RunnableCallable( | ||
lambda state: [_system_message, *_get_state_value(state, "messages")], | ||
lambda state: [system_message, *_get_state_value(state, "messages")], | ||
name=PROMPT_RUNNABLE_NAME, | ||
) | ||
elif isinstance(prompt, SystemMessage): | ||
|
@@ -220,7 +220,7 @@ def __init__( | |
"The `model` parameter should not have pre-bound tools, " | ||
"simply pass the model and tools separately." | ||
) | ||
raise ValueError(msg) | ||
raise TypeError(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's still OK to do these breaking changes as it was not released yet ? |
||
|
||
self._setup_tools() | ||
self._setup_state_schema() | ||
|
@@ -397,13 +397,13 @@ def _handle_single_structured_output( | |
"structured_response": structured_response, | ||
} | ||
) | ||
except Exception as exc: # noqa: BLE001 | ||
except Exception as exc: | ||
exception = StructuredOutputValidationError(tool_call["name"], exc) | ||
|
||
should_retry, error_message = self._handle_structured_output_error(exception) | ||
|
||
if not should_retry: | ||
raise exception | ||
raise exception from exc | ||
|
||
return Command( | ||
update={ | ||
|
@@ -583,7 +583,7 @@ def _are_more_steps_needed(state: StateT, response: BaseMessage) -> bool: | |
remaining_steps is not None # type: ignore[return-value] | ||
and ( | ||
(remaining_steps < 1 and all_tools_return_direct) | ||
or (remaining_steps < 2 and has_tool_calls) | ||
or (remaining_steps < 2 and has_tool_calls) # noqa: PLR2004 | ||
) | ||
) | ||
|
||
|
@@ -1188,7 +1188,7 @@ def check_weather(location: str) -> str: | |
response_format = ToolStrategy( | ||
schema=response_format, | ||
) | ||
elif isinstance(response_format, tuple) and len(response_format) == 2: | ||
elif isinstance(response_format, tuple) and len(response_format) == 2: # noqa: PLR2004 | ||
msg = "Passing a 2-tuple as response_format is no longer supported. " | ||
raise ValueError(msg) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's still OK to do these breaking changes as it was not released yet ?