langchain 0.3.27__py3-none-any.whl → 0.4.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain/agents/agent.py +16 -20
- langchain/agents/agent_iterator.py +19 -12
- langchain/agents/agent_toolkits/vectorstore/base.py +2 -0
- langchain/agents/chat/base.py +2 -0
- langchain/agents/conversational/base.py +2 -0
- langchain/agents/conversational_chat/base.py +2 -0
- langchain/agents/initialize.py +1 -1
- langchain/agents/json_chat/base.py +1 -0
- langchain/agents/mrkl/base.py +2 -0
- langchain/agents/openai_assistant/base.py +1 -1
- langchain/agents/openai_functions_agent/agent_token_buffer_memory.py +2 -0
- langchain/agents/openai_functions_agent/base.py +3 -2
- langchain/agents/openai_functions_multi_agent/base.py +1 -1
- langchain/agents/openai_tools/base.py +1 -0
- langchain/agents/output_parsers/json.py +2 -0
- langchain/agents/output_parsers/openai_functions.py +10 -3
- langchain/agents/output_parsers/openai_tools.py +8 -1
- langchain/agents/output_parsers/react_json_single_input.py +3 -0
- langchain/agents/output_parsers/react_single_input.py +3 -0
- langchain/agents/output_parsers/self_ask.py +2 -0
- langchain/agents/output_parsers/tools.py +16 -2
- langchain/agents/output_parsers/xml.py +3 -0
- langchain/agents/react/agent.py +1 -0
- langchain/agents/react/base.py +4 -0
- langchain/agents/react/output_parser.py +2 -0
- langchain/agents/schema.py +2 -0
- langchain/agents/self_ask_with_search/base.py +4 -0
- langchain/agents/structured_chat/base.py +5 -0
- langchain/agents/structured_chat/output_parser.py +13 -0
- langchain/agents/tool_calling_agent/base.py +1 -0
- langchain/agents/tools.py +3 -0
- langchain/agents/xml/base.py +7 -1
- langchain/callbacks/streaming_aiter.py +13 -2
- langchain/callbacks/streaming_aiter_final_only.py +11 -2
- langchain/callbacks/streaming_stdout_final_only.py +5 -0
- langchain/callbacks/tracers/logging.py +11 -0
- langchain/chains/api/base.py +5 -1
- langchain/chains/base.py +8 -2
- langchain/chains/combine_documents/base.py +7 -1
- langchain/chains/combine_documents/map_reduce.py +3 -0
- langchain/chains/combine_documents/map_rerank.py +6 -4
- langchain/chains/combine_documents/reduce.py +1 -0
- langchain/chains/combine_documents/refine.py +1 -0
- langchain/chains/combine_documents/stuff.py +5 -1
- langchain/chains/constitutional_ai/base.py +7 -0
- langchain/chains/conversation/base.py +4 -1
- langchain/chains/conversational_retrieval/base.py +67 -59
- langchain/chains/elasticsearch_database/base.py +2 -1
- langchain/chains/flare/base.py +2 -0
- langchain/chains/flare/prompts.py +2 -0
- langchain/chains/llm.py +7 -2
- langchain/chains/llm_bash/__init__.py +1 -1
- langchain/chains/llm_checker/base.py +12 -1
- langchain/chains/llm_math/base.py +9 -1
- langchain/chains/llm_summarization_checker/base.py +13 -1
- langchain/chains/llm_symbolic_math/__init__.py +1 -1
- langchain/chains/loading.py +4 -2
- langchain/chains/moderation.py +3 -0
- langchain/chains/natbot/base.py +3 -1
- langchain/chains/natbot/crawler.py +29 -0
- langchain/chains/openai_functions/base.py +2 -0
- langchain/chains/openai_functions/citation_fuzzy_match.py +9 -0
- langchain/chains/openai_functions/openapi.py +4 -0
- langchain/chains/openai_functions/qa_with_structure.py +3 -3
- langchain/chains/openai_functions/tagging.py +2 -0
- langchain/chains/qa_generation/base.py +4 -0
- langchain/chains/qa_with_sources/base.py +3 -0
- langchain/chains/qa_with_sources/retrieval.py +1 -1
- langchain/chains/qa_with_sources/vector_db.py +4 -2
- langchain/chains/query_constructor/base.py +4 -2
- langchain/chains/query_constructor/parser.py +64 -2
- langchain/chains/retrieval_qa/base.py +4 -0
- langchain/chains/router/base.py +14 -2
- langchain/chains/router/embedding_router.py +3 -0
- langchain/chains/router/llm_router.py +6 -4
- langchain/chains/router/multi_prompt.py +3 -0
- langchain/chains/router/multi_retrieval_qa.py +18 -0
- langchain/chains/sql_database/query.py +1 -0
- langchain/chains/structured_output/base.py +2 -0
- langchain/chains/transform.py +4 -0
- langchain/chat_models/base.py +55 -18
- langchain/document_loaders/blob_loaders/schema.py +1 -4
- langchain/embeddings/base.py +2 -0
- langchain/embeddings/cache.py +3 -3
- langchain/evaluation/agents/trajectory_eval_chain.py +3 -2
- langchain/evaluation/comparison/eval_chain.py +1 -0
- langchain/evaluation/criteria/eval_chain.py +3 -0
- langchain/evaluation/embedding_distance/base.py +11 -0
- langchain/evaluation/exact_match/base.py +14 -1
- langchain/evaluation/loading.py +1 -0
- langchain/evaluation/parsing/base.py +16 -3
- langchain/evaluation/parsing/json_distance.py +19 -8
- langchain/evaluation/parsing/json_schema.py +1 -4
- langchain/evaluation/qa/eval_chain.py +8 -0
- langchain/evaluation/qa/generate_chain.py +2 -0
- langchain/evaluation/regex_match/base.py +9 -1
- langchain/evaluation/scoring/eval_chain.py +1 -0
- langchain/evaluation/string_distance/base.py +6 -0
- langchain/memory/buffer.py +5 -0
- langchain/memory/buffer_window.py +2 -0
- langchain/memory/combined.py +1 -1
- langchain/memory/entity.py +47 -0
- langchain/memory/simple.py +3 -0
- langchain/memory/summary.py +30 -0
- langchain/memory/summary_buffer.py +3 -0
- langchain/memory/token_buffer.py +2 -0
- langchain/output_parsers/combining.py +4 -2
- langchain/output_parsers/enum.py +5 -1
- langchain/output_parsers/fix.py +8 -1
- langchain/output_parsers/pandas_dataframe.py +16 -1
- langchain/output_parsers/regex.py +2 -0
- langchain/output_parsers/retry.py +21 -1
- langchain/output_parsers/structured.py +10 -0
- langchain/output_parsers/yaml.py +4 -0
- langchain/pydantic_v1/__init__.py +1 -1
- langchain/retrievers/document_compressors/chain_extract.py +4 -2
- langchain/retrievers/document_compressors/cohere_rerank.py +2 -0
- langchain/retrievers/document_compressors/cross_encoder_rerank.py +2 -0
- langchain/retrievers/document_compressors/embeddings_filter.py +3 -0
- langchain/retrievers/document_compressors/listwise_rerank.py +1 -0
- langchain/retrievers/ensemble.py +2 -2
- langchain/retrievers/multi_query.py +3 -1
- langchain/retrievers/multi_vector.py +4 -1
- langchain/retrievers/parent_document_retriever.py +15 -0
- langchain/retrievers/self_query/base.py +19 -0
- langchain/retrievers/time_weighted_retriever.py +3 -0
- langchain/runnables/hub.py +12 -0
- langchain/runnables/openai_functions.py +6 -0
- langchain/smith/__init__.py +1 -0
- langchain/smith/evaluation/config.py +5 -22
- langchain/smith/evaluation/progress.py +12 -3
- langchain/smith/evaluation/runner_utils.py +240 -123
- langchain/smith/evaluation/string_run_evaluator.py +27 -0
- langchain/storage/encoder_backed.py +1 -0
- langchain/tools/python/__init__.py +1 -1
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/METADATA +2 -12
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/RECORD +140 -141
- langchain/smith/evaluation/utils.py +0 -0
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/WHEEL +0 -0
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/entry_points.txt +0 -0
- {langchain-0.3.27.dist-info → langchain-0.4.0.dev0.dist-info}/licenses/LICENSE +0 -0
|
@@ -5,6 +5,7 @@ from typing import Any
|
|
|
5
5
|
from langchain_core.output_parsers import BaseOutputParser
|
|
6
6
|
from langchain_core.output_parsers.json import parse_and_check_json_markdown
|
|
7
7
|
from pydantic import BaseModel
|
|
8
|
+
from typing_extensions import override
|
|
8
9
|
|
|
9
10
|
from langchain.output_parsers.format_instructions import (
|
|
10
11
|
STRUCTURED_FORMAT_INSTRUCTIONS,
|
|
@@ -44,6 +45,14 @@ class StructuredOutputParser(BaseOutputParser[dict[str, Any]]):
|
|
|
44
45
|
cls,
|
|
45
46
|
response_schemas: list[ResponseSchema],
|
|
46
47
|
) -> StructuredOutputParser:
|
|
48
|
+
"""Create a StructuredOutputParser from a list of ResponseSchema.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
response_schemas: The schemas for the response.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
An instance of StructuredOutputParser.
|
|
55
|
+
"""
|
|
47
56
|
return cls(response_schemas=response_schemas)
|
|
48
57
|
|
|
49
58
|
def get_format_instructions(
|
|
@@ -97,6 +106,7 @@ class StructuredOutputParser(BaseOutputParser[dict[str, Any]]):
|
|
|
97
106
|
return STRUCTURED_FORMAT_SIMPLE_INSTRUCTIONS.format(format=schema_str)
|
|
98
107
|
return STRUCTURED_FORMAT_INSTRUCTIONS.format(format=schema_str)
|
|
99
108
|
|
|
109
|
+
@override
|
|
100
110
|
def parse(self, text: str) -> dict[str, Any]:
|
|
101
111
|
expected_keys = [rs.name for rs in self.response_schemas]
|
|
102
112
|
return parse_and_check_json_markdown(text, expected_keys)
|
langchain/output_parsers/yaml.py
CHANGED
|
@@ -6,6 +6,7 @@ import yaml
|
|
|
6
6
|
from langchain_core.exceptions import OutputParserException
|
|
7
7
|
from langchain_core.output_parsers import BaseOutputParser
|
|
8
8
|
from pydantic import BaseModel, ValidationError
|
|
9
|
+
from typing_extensions import override
|
|
9
10
|
|
|
10
11
|
from langchain.output_parsers.format_instructions import YAML_FORMAT_INSTRUCTIONS
|
|
11
12
|
|
|
@@ -24,6 +25,7 @@ class YamlOutputParser(BaseOutputParser[T]):
|
|
|
24
25
|
"""Regex pattern to match yaml code blocks
|
|
25
26
|
within triple backticks with optional yaml or yml prefix."""
|
|
26
27
|
|
|
28
|
+
@override
|
|
27
29
|
def parse(self, text: str) -> T:
|
|
28
30
|
try:
|
|
29
31
|
# Greedy search for 1st yaml candidate.
|
|
@@ -41,6 +43,7 @@ class YamlOutputParser(BaseOutputParser[T]):
|
|
|
41
43
|
msg = f"Failed to parse {name} from completion {text}. Got: {e}"
|
|
42
44
|
raise OutputParserException(msg, llm_output=text) from e
|
|
43
45
|
|
|
46
|
+
@override
|
|
44
47
|
def get_format_instructions(self) -> str:
|
|
45
48
|
# Copy schema to avoid altering original Pydantic schema.
|
|
46
49
|
if hasattr(self.pydantic_object, "model_json_schema"):
|
|
@@ -69,5 +72,6 @@ class YamlOutputParser(BaseOutputParser[T]):
|
|
|
69
72
|
return "yaml"
|
|
70
73
|
|
|
71
74
|
@property
|
|
75
|
+
@override
|
|
72
76
|
def OutputType(self) -> type[T]:
|
|
73
77
|
return self.pydantic_object
|
|
@@ -7,7 +7,7 @@ from langchain_core._api import warn_deprecated
|
|
|
7
7
|
# attempt to import pydantic since it adds pydantic_v1 and pydantic_v2 to sys.modules.
|
|
8
8
|
#
|
|
9
9
|
# This hack is done for the following reasons:
|
|
10
|
-
# *
|
|
10
|
+
# * LangChain will attempt to remain compatible with both pydantic v1 and v2 since
|
|
11
11
|
# both dependencies and dependents may be stuck on either version of v1 or v2.
|
|
12
12
|
# * Creating namespaces for pydantic v1 and v2 should allow us to write code that
|
|
13
13
|
# unambiguously uses either v1 or v2 API.
|
|
@@ -12,6 +12,7 @@ from langchain_core.output_parsers import BaseOutputParser, StrOutputParser
|
|
|
12
12
|
from langchain_core.prompts import PromptTemplate
|
|
13
13
|
from langchain_core.runnables import Runnable
|
|
14
14
|
from pydantic import ConfigDict
|
|
15
|
+
from typing_extensions import override
|
|
15
16
|
|
|
16
17
|
from langchain.chains.llm import LLMChain
|
|
17
18
|
from langchain.retrievers.document_compressors.chain_extract_prompt import (
|
|
@@ -29,6 +30,7 @@ class NoOutputParser(BaseOutputParser[str]):
|
|
|
29
30
|
|
|
30
31
|
no_output_str: str = "NO_OUTPUT"
|
|
31
32
|
|
|
33
|
+
@override
|
|
32
34
|
def parse(self, text: str) -> str:
|
|
33
35
|
cleaned_text = text.strip()
|
|
34
36
|
if cleaned_text == self.no_output_str:
|
|
@@ -80,7 +82,7 @@ class LLMChainExtractor(BaseDocumentCompressor):
|
|
|
80
82
|
if len(output) == 0:
|
|
81
83
|
continue
|
|
82
84
|
compressed_docs.append(
|
|
83
|
-
Document(page_content=cast(str, output), metadata=doc.metadata),
|
|
85
|
+
Document(page_content=cast("str", output), metadata=doc.metadata),
|
|
84
86
|
)
|
|
85
87
|
return compressed_docs
|
|
86
88
|
|
|
@@ -108,7 +110,7 @@ class LLMChainExtractor(BaseDocumentCompressor):
|
|
|
108
110
|
llm: BaseLanguageModel,
|
|
109
111
|
prompt: Optional[PromptTemplate] = None,
|
|
110
112
|
get_input: Optional[Callable[[str, Document], str]] = None,
|
|
111
|
-
llm_chain_kwargs: Optional[dict] = None,
|
|
113
|
+
llm_chain_kwargs: Optional[dict] = None, # noqa: ARG003
|
|
112
114
|
) -> LLMChainExtractor:
|
|
113
115
|
"""Initialize from LLM."""
|
|
114
116
|
_prompt = prompt if prompt is not None else _get_default_chain_prompt()
|
|
@@ -9,6 +9,7 @@ from langchain_core.callbacks import Callbacks
|
|
|
9
9
|
from langchain_core.documents import BaseDocumentCompressor, Document
|
|
10
10
|
from langchain_core.utils import get_from_dict_or_env
|
|
11
11
|
from pydantic import ConfigDict, model_validator
|
|
12
|
+
from typing_extensions import override
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
@deprecated(
|
|
@@ -98,6 +99,7 @@ class CohereRerank(BaseDocumentCompressor):
|
|
|
98
99
|
for res in results
|
|
99
100
|
]
|
|
100
101
|
|
|
102
|
+
@override
|
|
101
103
|
def compress_documents(
|
|
102
104
|
self,
|
|
103
105
|
documents: Sequence[Document],
|
|
@@ -7,6 +7,7 @@ from typing import Optional
|
|
|
7
7
|
from langchain_core.callbacks import Callbacks
|
|
8
8
|
from langchain_core.documents import BaseDocumentCompressor, Document
|
|
9
9
|
from pydantic import ConfigDict
|
|
10
|
+
from typing_extensions import override
|
|
10
11
|
|
|
11
12
|
from langchain.retrievers.document_compressors.cross_encoder import BaseCrossEncoder
|
|
12
13
|
|
|
@@ -25,6 +26,7 @@ class CrossEncoderReranker(BaseDocumentCompressor):
|
|
|
25
26
|
extra="forbid",
|
|
26
27
|
)
|
|
27
28
|
|
|
29
|
+
@override
|
|
28
30
|
def compress_documents(
|
|
29
31
|
self,
|
|
30
32
|
documents: Sequence[Document],
|
|
@@ -6,6 +6,7 @@ from langchain_core.documents import BaseDocumentCompressor, Document
|
|
|
6
6
|
from langchain_core.embeddings import Embeddings
|
|
7
7
|
from langchain_core.utils import pre_init
|
|
8
8
|
from pydantic import ConfigDict, Field
|
|
9
|
+
from typing_extensions import override
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
def _get_similarity_function() -> Callable:
|
|
@@ -50,6 +51,7 @@ class EmbeddingsFilter(BaseDocumentCompressor):
|
|
|
50
51
|
raise ValueError(msg)
|
|
51
52
|
return values
|
|
52
53
|
|
|
54
|
+
@override
|
|
53
55
|
def compress_documents(
|
|
54
56
|
self,
|
|
55
57
|
documents: Sequence[Document],
|
|
@@ -93,6 +95,7 @@ class EmbeddingsFilter(BaseDocumentCompressor):
|
|
|
93
95
|
stateful_documents[i].state["query_similarity_score"] = similarity[i]
|
|
94
96
|
return [stateful_documents[i] for i in included_idxs]
|
|
95
97
|
|
|
98
|
+
@override
|
|
96
99
|
async def acompress_documents(
|
|
97
100
|
self,
|
|
98
101
|
documents: Sequence[Document],
|
|
@@ -70,6 +70,7 @@ class LLMListwiseRerank(BaseDocumentCompressor):
|
|
|
70
70
|
compressed_docs = reranker.compress_documents(documents, "Who is steve")
|
|
71
71
|
assert len(compressed_docs) == 3
|
|
72
72
|
assert "Steve" in compressed_docs[0].page_content
|
|
73
|
+
|
|
73
74
|
"""
|
|
74
75
|
|
|
75
76
|
reranker: Runnable[dict, list[Document]]
|
langchain/retrievers/ensemble.py
CHANGED
|
@@ -81,7 +81,7 @@ class EnsembleRetriever(BaseRetriever):
|
|
|
81
81
|
|
|
82
82
|
@model_validator(mode="before")
|
|
83
83
|
@classmethod
|
|
84
|
-
def
|
|
84
|
+
def _set_weights(cls, values: dict[str, Any]) -> Any:
|
|
85
85
|
if not values.get("weights"):
|
|
86
86
|
n_retrievers = len(values["retrievers"])
|
|
87
87
|
values["weights"] = [1 / n_retrievers] * n_retrievers
|
|
@@ -236,7 +236,7 @@ class EnsembleRetriever(BaseRetriever):
|
|
|
236
236
|
# Enforce that retrieved docs are Documents for each list in retriever_docs
|
|
237
237
|
for i in range(len(retriever_docs)):
|
|
238
238
|
retriever_docs[i] = [
|
|
239
|
-
Document(page_content=cast(str, doc)) if isinstance(doc, str) else doc
|
|
239
|
+
Document(page_content=cast("str", doc)) if isinstance(doc, str) else doc
|
|
240
240
|
for doc in retriever_docs[i]
|
|
241
241
|
]
|
|
242
242
|
|
|
@@ -14,6 +14,7 @@ from langchain_core.prompts import BasePromptTemplate
|
|
|
14
14
|
from langchain_core.prompts.prompt import PromptTemplate
|
|
15
15
|
from langchain_core.retrievers import BaseRetriever
|
|
16
16
|
from langchain_core.runnables import Runnable
|
|
17
|
+
from typing_extensions import override
|
|
17
18
|
|
|
18
19
|
from langchain.chains.llm import LLMChain
|
|
19
20
|
|
|
@@ -23,6 +24,7 @@ logger = logging.getLogger(__name__)
|
|
|
23
24
|
class LineListOutputParser(BaseOutputParser[list[str]]):
|
|
24
25
|
"""Output parser for a list of lines."""
|
|
25
26
|
|
|
27
|
+
@override
|
|
26
28
|
def parse(self, text: str) -> list[str]:
|
|
27
29
|
lines = text.strip().split("\n")
|
|
28
30
|
return list(filter(None, lines)) # Remove empty lines
|
|
@@ -65,7 +67,7 @@ class MultiQueryRetriever(BaseRetriever):
|
|
|
65
67
|
retriever: BaseRetriever,
|
|
66
68
|
llm: BaseLanguageModel,
|
|
67
69
|
prompt: BasePromptTemplate = DEFAULT_QUERY_PROMPT,
|
|
68
|
-
parser_key: Optional[str] = None,
|
|
70
|
+
parser_key: Optional[str] = None, # noqa: ARG003
|
|
69
71
|
include_original: bool = False, # noqa: FBT001,FBT002
|
|
70
72
|
) -> "MultiQueryRetriever":
|
|
71
73
|
"""Initialize from llm using default template.
|
|
@@ -10,6 +10,7 @@ from langchain_core.retrievers import BaseRetriever
|
|
|
10
10
|
from langchain_core.stores import BaseStore, ByteStore
|
|
11
11
|
from langchain_core.vectorstores import VectorStore
|
|
12
12
|
from pydantic import Field, model_validator
|
|
13
|
+
from typing_extensions import override
|
|
13
14
|
|
|
14
15
|
from langchain.storage._lc_store import create_kv_docstore
|
|
15
16
|
|
|
@@ -43,7 +44,7 @@ class MultiVectorRetriever(BaseRetriever):
|
|
|
43
44
|
|
|
44
45
|
@model_validator(mode="before")
|
|
45
46
|
@classmethod
|
|
46
|
-
def
|
|
47
|
+
def _shim_docstore(cls, values: dict) -> Any:
|
|
47
48
|
byte_store = values.get("byte_store")
|
|
48
49
|
docstore = values.get("docstore")
|
|
49
50
|
if byte_store is not None:
|
|
@@ -54,6 +55,7 @@ class MultiVectorRetriever(BaseRetriever):
|
|
|
54
55
|
values["docstore"] = docstore
|
|
55
56
|
return values
|
|
56
57
|
|
|
58
|
+
@override
|
|
57
59
|
def _get_relevant_documents(
|
|
58
60
|
self,
|
|
59
61
|
query: str,
|
|
@@ -91,6 +93,7 @@ class MultiVectorRetriever(BaseRetriever):
|
|
|
91
93
|
docs = self.docstore.mget(ids)
|
|
92
94
|
return [d for d in docs if d is not None]
|
|
93
95
|
|
|
96
|
+
@override
|
|
94
97
|
async def _aget_relevant_documents(
|
|
95
98
|
self,
|
|
96
99
|
query: str,
|
|
@@ -54,6 +54,7 @@ class ParentDocumentRetriever(MultiVectorRetriever):
|
|
|
54
54
|
child_splitter=child_splitter,
|
|
55
55
|
parent_splitter=parent_splitter,
|
|
56
56
|
)
|
|
57
|
+
|
|
57
58
|
""" # noqa: E501
|
|
58
59
|
|
|
59
60
|
child_splitter: TextSplitter
|
|
@@ -147,6 +148,20 @@ class ParentDocumentRetriever(MultiVectorRetriever):
|
|
|
147
148
|
add_to_docstore: bool = True, # noqa: FBT001,FBT002
|
|
148
149
|
**kwargs: Any,
|
|
149
150
|
) -> None:
|
|
151
|
+
"""Adds documents to the docstore and vectorstores.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
documents: List of documents to add
|
|
155
|
+
ids: Optional list of ids for documents. If provided should be the same
|
|
156
|
+
length as the list of documents. Can be provided if parent documents
|
|
157
|
+
are already in the document store and you don't want to re-add
|
|
158
|
+
to the docstore. If not provided, random UUIDs will be used as
|
|
159
|
+
ids.
|
|
160
|
+
add_to_docstore: Boolean of whether to add documents to docstore.
|
|
161
|
+
This can be false if and only if `ids` are provided. You may want
|
|
162
|
+
to set this to False if the documents are already in the docstore
|
|
163
|
+
and you don't want to re-add them.
|
|
164
|
+
"""
|
|
150
165
|
docs, full_docs = self._split_docs_for_adding(
|
|
151
166
|
documents,
|
|
152
167
|
ids,
|
|
@@ -360,6 +360,25 @@ class SelfQueryRetriever(BaseRetriever):
|
|
|
360
360
|
use_original_query: bool = False, # noqa: FBT001,FBT002
|
|
361
361
|
**kwargs: Any,
|
|
362
362
|
) -> "SelfQueryRetriever":
|
|
363
|
+
"""Create a SelfQueryRetriever from an LLM and a vector store.
|
|
364
|
+
|
|
365
|
+
Args:
|
|
366
|
+
llm: The language model to use for generating queries.
|
|
367
|
+
vectorstore: The vector store to use for retrieving documents.
|
|
368
|
+
document_contents: Description of the page contents of the document to be
|
|
369
|
+
queried.
|
|
370
|
+
metadata_field_info: Metadata field information for the documents.
|
|
371
|
+
structured_query_translator: Optional translator for turning internal query
|
|
372
|
+
language into vectorstore search params.
|
|
373
|
+
chain_kwargs: Additional keyword arguments for the query constructor.
|
|
374
|
+
enable_limit: Whether to enable the limit operator.
|
|
375
|
+
use_original_query: Whether to use the original query instead of the revised
|
|
376
|
+
query from the LLM.
|
|
377
|
+
**kwargs: Additional keyword arguments for the SelfQueryRetriever.
|
|
378
|
+
|
|
379
|
+
Returns:
|
|
380
|
+
An instance of SelfQueryRetriever.
|
|
381
|
+
"""
|
|
363
382
|
if structured_query_translator is None:
|
|
364
383
|
structured_query_translator = _get_builtin_translator(vectorstore)
|
|
365
384
|
chain_kwargs = chain_kwargs or {}
|
|
@@ -10,6 +10,7 @@ from langchain_core.documents import Document
|
|
|
10
10
|
from langchain_core.retrievers import BaseRetriever
|
|
11
11
|
from langchain_core.vectorstores import VectorStore
|
|
12
12
|
from pydantic import ConfigDict, Field
|
|
13
|
+
from typing_extensions import override
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
def _get_hours_passed(time: datetime.datetime, ref_time: datetime.datetime) -> float:
|
|
@@ -128,6 +129,7 @@ class TimeWeightedVectorStoreRetriever(BaseRetriever):
|
|
|
128
129
|
result.append(buffered_doc)
|
|
129
130
|
return result
|
|
130
131
|
|
|
132
|
+
@override
|
|
131
133
|
def _get_relevant_documents(
|
|
132
134
|
self,
|
|
133
135
|
query: str,
|
|
@@ -142,6 +144,7 @@ class TimeWeightedVectorStoreRetriever(BaseRetriever):
|
|
|
142
144
|
docs_and_scores.update(self.get_salient_docs(query))
|
|
143
145
|
return self._get_rescored_docs(docs_and_scores)
|
|
144
146
|
|
|
147
|
+
@override
|
|
145
148
|
async def _aget_relevant_documents(
|
|
146
149
|
self,
|
|
147
150
|
query: str,
|
langchain/runnables/hub.py
CHANGED
|
@@ -19,6 +19,18 @@ class HubRunnable(RunnableBindingBase[Input, Output]):
|
|
|
19
19
|
api_key: Optional[str] = None,
|
|
20
20
|
**kwargs: Any,
|
|
21
21
|
) -> None:
|
|
22
|
+
"""Initialize the HubRunnable.
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
owner_repo_commit: The full name of the prompt to pull from in the format of
|
|
26
|
+
`owner/prompt_name:commit_hash` or `owner/prompt_name`
|
|
27
|
+
or just `prompt_name` if it's your own prompt.
|
|
28
|
+
api_url: The URL of the LangChain Hub API.
|
|
29
|
+
Defaults to the hosted API service if you have an api key set,
|
|
30
|
+
or a localhost instance if not.
|
|
31
|
+
api_key: The API key to use to authenticate with the LangChain Hub API.
|
|
32
|
+
**kwargs: Additional keyword arguments to pass to the parent class.
|
|
33
|
+
"""
|
|
22
34
|
from langchain.hub import pull
|
|
23
35
|
|
|
24
36
|
pulled = pull(owner_repo_commit, api_url=api_url, api_key=api_key)
|
|
@@ -36,6 +36,12 @@ class OpenAIFunctionsRouter(RunnableBindingBase[BaseMessage, Any]):
|
|
|
36
36
|
],
|
|
37
37
|
functions: Optional[list[OpenAIFunction]] = None,
|
|
38
38
|
):
|
|
39
|
+
"""Initialize the OpenAIFunctionsRouter.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
runnables: A mapping of function names to runnables.
|
|
43
|
+
functions: Optional list of functions to check against the runnables.
|
|
44
|
+
"""
|
|
39
45
|
if functions is not None:
|
|
40
46
|
if len(functions) != len(runnables):
|
|
41
47
|
msg = "The number of functions does not match the number of runnables."
|
langchain/smith/__init__.py
CHANGED
|
@@ -87,6 +87,7 @@ or LangSmith's `RunEvaluator` classes.
|
|
|
87
87
|
- :func:`arun_on_dataset <langchain.smith.evaluation.runner_utils.arun_on_dataset>`: Asynchronous function to evaluate a chain, agent, or other LangChain component over a dataset.
|
|
88
88
|
- :func:`run_on_dataset <langchain.smith.evaluation.runner_utils.run_on_dataset>`: Function to evaluate a chain, agent, or other LangChain component over a dataset.
|
|
89
89
|
- :class:`RunEvalConfig <langchain.smith.evaluation.config.RunEvalConfig>`: Class representing the configuration for running evaluation. You can select evaluators by :class:`EvaluatorType <langchain.evaluation.schema.EvaluatorType>` or config, or you can pass in `custom_evaluators`
|
|
90
|
+
|
|
90
91
|
""" # noqa: E501
|
|
91
92
|
|
|
92
93
|
from langchain.smith.evaluation import (
|
|
@@ -10,6 +10,7 @@ from langsmith import RunEvaluator
|
|
|
10
10
|
from langsmith.evaluation.evaluator import EvaluationResult, EvaluationResults
|
|
11
11
|
from langsmith.schemas import Example, Run
|
|
12
12
|
from pydantic import BaseModel, ConfigDict, Field
|
|
13
|
+
from typing_extensions import override
|
|
13
14
|
|
|
14
15
|
from langchain.evaluation.criteria.eval_chain import CRITERIA_TYPE
|
|
15
16
|
from langchain.evaluation.embedding_distance.base import (
|
|
@@ -78,6 +79,7 @@ class SingleKeyEvalConfig(EvalConfig):
|
|
|
78
79
|
"""The key from the traced run's inputs dictionary to use to represent the
|
|
79
80
|
input. If not provided, it will be inferred automatically."""
|
|
80
81
|
|
|
82
|
+
@override
|
|
81
83
|
def get_kwargs(self) -> dict[str, Any]:
|
|
82
84
|
kwargs = super().get_kwargs()
|
|
83
85
|
# Filer out the keys that are not needed for the evaluator.
|
|
@@ -176,13 +178,6 @@ class RunEvalConfig(BaseModel):
|
|
|
176
178
|
llm: Optional[BaseLanguageModel] = None
|
|
177
179
|
evaluator_type: EvaluatorType = EvaluatorType.CRITERIA
|
|
178
180
|
|
|
179
|
-
def __init__(
|
|
180
|
-
self,
|
|
181
|
-
criteria: Optional[CRITERIA_TYPE] = None,
|
|
182
|
-
**kwargs: Any,
|
|
183
|
-
) -> None:
|
|
184
|
-
super().__init__(criteria=criteria, **kwargs) # type: ignore[call-arg]
|
|
185
|
-
|
|
186
181
|
class LabeledCriteria(SingleKeyEvalConfig):
|
|
187
182
|
"""Configuration for a labeled (with references) criteria evaluator.
|
|
188
183
|
|
|
@@ -198,13 +193,6 @@ class RunEvalConfig(BaseModel):
|
|
|
198
193
|
llm: Optional[BaseLanguageModel] = None
|
|
199
194
|
evaluator_type: EvaluatorType = EvaluatorType.LABELED_CRITERIA
|
|
200
195
|
|
|
201
|
-
def __init__(
|
|
202
|
-
self,
|
|
203
|
-
criteria: Optional[CRITERIA_TYPE] = None,
|
|
204
|
-
**kwargs: Any,
|
|
205
|
-
) -> None:
|
|
206
|
-
super().__init__(criteria=criteria, **kwargs) # type: ignore[call-arg]
|
|
207
|
-
|
|
208
196
|
class EmbeddingDistance(SingleKeyEvalConfig):
|
|
209
197
|
"""Configuration for an embedding distance evaluator.
|
|
210
198
|
|
|
@@ -345,6 +333,7 @@ class RunEvalConfig(BaseModel):
|
|
|
345
333
|
|
|
346
334
|
class ScoreString(SingleKeyEvalConfig):
|
|
347
335
|
"""Configuration for a score string evaluator.
|
|
336
|
+
|
|
348
337
|
This is like the criteria evaluator but it is configured by
|
|
349
338
|
default to return a score on the scale from 1-10.
|
|
350
339
|
|
|
@@ -370,13 +359,7 @@ class RunEvalConfig(BaseModel):
|
|
|
370
359
|
normalize_by: Optional[float] = None
|
|
371
360
|
prompt: Optional[BasePromptTemplate] = None
|
|
372
361
|
|
|
373
|
-
def __init__(
|
|
374
|
-
self,
|
|
375
|
-
criteria: Optional[CRITERIA_TYPE] = None,
|
|
376
|
-
normalize_by: Optional[float] = None,
|
|
377
|
-
**kwargs: Any,
|
|
378
|
-
) -> None:
|
|
379
|
-
super().__init__(criteria=criteria, normalize_by=normalize_by, **kwargs) # type: ignore[call-arg]
|
|
380
|
-
|
|
381
362
|
class LabeledScoreString(ScoreString):
|
|
363
|
+
""" "Configuration for a labeled score string evaluator."""
|
|
364
|
+
|
|
382
365
|
evaluator_type: EvaluatorType = EvaluatorType.LABELED_SCORE_STRING
|
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import threading
|
|
4
4
|
from collections.abc import Sequence
|
|
5
|
-
from typing import Any, Optional
|
|
5
|
+
from typing import Any, Optional, Union
|
|
6
6
|
from uuid import UUID
|
|
7
7
|
|
|
8
8
|
from langchain_core.callbacks import base as base_callbacks
|
|
9
9
|
from langchain_core.documents import Document
|
|
10
10
|
from langchain_core.outputs import LLMResult
|
|
11
|
+
from langchain_core.v1.messages import AIMessage
|
|
12
|
+
from typing_extensions import override
|
|
11
13
|
|
|
12
14
|
|
|
13
15
|
class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
@@ -18,7 +20,6 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
18
20
|
total: int,
|
|
19
21
|
ncols: int = 50,
|
|
20
22
|
end_with: str = "\n",
|
|
21
|
-
**kwargs: Any,
|
|
22
23
|
):
|
|
23
24
|
"""Initialize the progress bar.
|
|
24
25
|
|
|
@@ -48,6 +49,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
48
49
|
end = "" if self.counter < self.total else self.end_with
|
|
49
50
|
print(f"\r[{arrow + spaces}] {self.counter}/{self.total}", end=end) # noqa: T201
|
|
50
51
|
|
|
52
|
+
@override
|
|
51
53
|
def on_chain_error(
|
|
52
54
|
self,
|
|
53
55
|
error: BaseException,
|
|
@@ -59,6 +61,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
59
61
|
if parent_run_id is None:
|
|
60
62
|
self.increment()
|
|
61
63
|
|
|
64
|
+
@override
|
|
62
65
|
def on_chain_end(
|
|
63
66
|
self,
|
|
64
67
|
outputs: dict[str, Any],
|
|
@@ -70,6 +73,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
70
73
|
if parent_run_id is None:
|
|
71
74
|
self.increment()
|
|
72
75
|
|
|
76
|
+
@override
|
|
73
77
|
def on_retriever_error(
|
|
74
78
|
self,
|
|
75
79
|
error: BaseException,
|
|
@@ -81,6 +85,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
81
85
|
if parent_run_id is None:
|
|
82
86
|
self.increment()
|
|
83
87
|
|
|
88
|
+
@override
|
|
84
89
|
def on_retriever_end(
|
|
85
90
|
self,
|
|
86
91
|
documents: Sequence[Document],
|
|
@@ -92,6 +97,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
92
97
|
if parent_run_id is None:
|
|
93
98
|
self.increment()
|
|
94
99
|
|
|
100
|
+
@override
|
|
95
101
|
def on_llm_error(
|
|
96
102
|
self,
|
|
97
103
|
error: BaseException,
|
|
@@ -103,9 +109,10 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
103
109
|
if parent_run_id is None:
|
|
104
110
|
self.increment()
|
|
105
111
|
|
|
112
|
+
@override
|
|
106
113
|
def on_llm_end(
|
|
107
114
|
self,
|
|
108
|
-
response: LLMResult,
|
|
115
|
+
response: Union[LLMResult, AIMessage],
|
|
109
116
|
*,
|
|
110
117
|
run_id: UUID,
|
|
111
118
|
parent_run_id: Optional[UUID] = None,
|
|
@@ -114,6 +121,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
114
121
|
if parent_run_id is None:
|
|
115
122
|
self.increment()
|
|
116
123
|
|
|
124
|
+
@override
|
|
117
125
|
def on_tool_error(
|
|
118
126
|
self,
|
|
119
127
|
error: BaseException,
|
|
@@ -125,6 +133,7 @@ class ProgressBarCallback(base_callbacks.BaseCallbackHandler):
|
|
|
125
133
|
if parent_run_id is None:
|
|
126
134
|
self.increment()
|
|
127
135
|
|
|
136
|
+
@override
|
|
128
137
|
def on_tool_end(
|
|
129
138
|
self,
|
|
130
139
|
output: str,
|