mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-24 03:52:10 +00:00
core: Add ruff rules RUF (#29353)
See https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf Mostly: * [RUF022](https://docs.astral.sh/ruff/rules/unsorted-dunder-all/) (unsorted `__all__`) * [RUF100](https://docs.astral.sh/ruff/rules/unused-noqa/) (unused noqa) * [RUF021](https://docs.astral.sh/ruff/rules/parenthesize-chained-operators/) (parenthesize-chained-operators) * [RUF015](https://docs.astral.sh/ruff/rules/unnecessary-iterable-allocation-for-first-element/) (unnecessary-iterable-allocation-for-first-element) * [RUF005](https://docs.astral.sh/ruff/rules/collection-literal-concatenation/) (collection-literal-concatenation) * [RUF046](https://docs.astral.sh/ruff/rules/unnecessary-cast-to-int/) (unnecessary-cast-to-int) --------- Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
committed by
GitHub
parent
6cd1aadf60
commit
a8f2ddee31
@@ -70,21 +70,21 @@ __all__ = (
|
||||
"ChatMessagePromptTemplate",
|
||||
"ChatPromptTemplate",
|
||||
"DictPromptTemplate",
|
||||
"FewShotChatMessagePromptTemplate",
|
||||
"FewShotPromptTemplate",
|
||||
"FewShotPromptWithTemplates",
|
||||
"FewShotChatMessagePromptTemplate",
|
||||
"HumanMessagePromptTemplate",
|
||||
"MessagesPlaceholder",
|
||||
"PipelinePromptTemplate",
|
||||
"PromptTemplate",
|
||||
"StringPromptTemplate",
|
||||
"SystemMessagePromptTemplate",
|
||||
"load_prompt",
|
||||
"format_document",
|
||||
"aformat_document",
|
||||
"check_valid_template",
|
||||
"format_document",
|
||||
"get_template_variables",
|
||||
"jinja2_formatter",
|
||||
"load_prompt",
|
||||
"validate_jinja2",
|
||||
)
|
||||
|
||||
|
@@ -445,9 +445,8 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):
|
||||
raise ValueError(msg)
|
||||
prompt = []
|
||||
for tmpl in template:
|
||||
if (
|
||||
isinstance(tmpl, str)
|
||||
or isinstance(tmpl, dict)
|
||||
if isinstance(tmpl, str) or (
|
||||
isinstance(tmpl, dict)
|
||||
and "text" in tmpl
|
||||
and set(tmpl.keys()) <= {"type", "text"}
|
||||
):
|
||||
@@ -524,7 +523,7 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):
|
||||
raise ValueError(msg)
|
||||
return cls(prompt=prompt, **kwargs)
|
||||
msg = f"Invalid template: {template}"
|
||||
raise ValueError(msg) # noqa: TRY004
|
||||
raise ValueError(msg)
|
||||
|
||||
@classmethod
|
||||
def from_template_file(
|
||||
@@ -1000,7 +999,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate):
|
||||
if isinstance(
|
||||
other, (BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate)
|
||||
):
|
||||
return ChatPromptTemplate(messages=self.messages + [other]).partial(
|
||||
return ChatPromptTemplate(messages=[*self.messages, other]).partial(
|
||||
**partials
|
||||
)
|
||||
if isinstance(other, (list, tuple)):
|
||||
@@ -1010,7 +1009,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate):
|
||||
)
|
||||
if isinstance(other, str):
|
||||
prompt = HumanMessagePromptTemplate.from_template(other)
|
||||
return ChatPromptTemplate(messages=self.messages + [prompt]).partial(
|
||||
return ChatPromptTemplate(messages=[*self.messages, prompt]).partial(
|
||||
**partials
|
||||
)
|
||||
msg = f"Unsupported operand type for +: {type(other)}"
|
||||
|
@@ -84,15 +84,11 @@ def _load_examples(config: dict) -> dict:
|
||||
|
||||
def _load_output_parser(config: dict) -> dict:
|
||||
"""Load output parser."""
|
||||
if "output_parser" in config and config["output_parser"]:
|
||||
_config = config.pop("output_parser")
|
||||
output_parser_type = _config.pop("_type")
|
||||
if output_parser_type == "default":
|
||||
output_parser = StrOutputParser(**_config)
|
||||
else:
|
||||
if _config := config.get("output_parser"):
|
||||
if output_parser_type := _config.get("_type") != "default":
|
||||
msg = f"Unsupported output parser {output_parser_type}"
|
||||
raise ValueError(msg)
|
||||
config["output_parser"] = output_parser
|
||||
config["output_parser"] = StrOutputParser(**_config)
|
||||
return config
|
||||
|
||||
|
||||
|
@@ -153,10 +153,8 @@ class StructuredPrompt(ChatPromptTemplate):
|
||||
NotImplementedError: If the first element of `others`
|
||||
is not a language model.
|
||||
"""
|
||||
if (
|
||||
others
|
||||
and isinstance(others[0], BaseLanguageModel)
|
||||
or hasattr(others[0], "with_structured_output")
|
||||
if (others and isinstance(others[0], BaseLanguageModel)) or hasattr(
|
||||
others[0], "with_structured_output"
|
||||
):
|
||||
return RunnableSequence(
|
||||
self,
|
||||
|
Reference in New Issue
Block a user