langchain 0.3.22__py3-none-any.whl → 0.3.24__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/_api/module_import.py +3 -3
- langchain/agents/agent.py +104 -109
- langchain/agents/agent_iterator.py +11 -15
- langchain/agents/agent_toolkits/conversational_retrieval/openai_functions.py +2 -2
- langchain/agents/agent_toolkits/vectorstore/base.py +3 -3
- langchain/agents/agent_toolkits/vectorstore/toolkit.py +4 -6
- langchain/agents/chat/base.py +7 -6
- langchain/agents/chat/output_parser.py +2 -1
- langchain/agents/conversational/base.py +5 -4
- langchain/agents/conversational_chat/base.py +9 -8
- langchain/agents/format_scratchpad/log.py +1 -3
- langchain/agents/format_scratchpad/log_to_messages.py +3 -5
- langchain/agents/format_scratchpad/openai_functions.py +4 -4
- langchain/agents/format_scratchpad/tools.py +3 -3
- langchain/agents/format_scratchpad/xml.py +1 -3
- langchain/agents/initialize.py +2 -1
- langchain/agents/json_chat/base.py +3 -2
- langchain/agents/loading.py +5 -5
- langchain/agents/mrkl/base.py +6 -5
- langchain/agents/openai_assistant/base.py +13 -17
- langchain/agents/openai_functions_agent/agent_token_buffer_memory.py +6 -6
- langchain/agents/openai_functions_agent/base.py +13 -12
- langchain/agents/openai_functions_multi_agent/base.py +15 -14
- langchain/agents/openai_tools/base.py +2 -1
- langchain/agents/output_parsers/openai_functions.py +2 -2
- langchain/agents/output_parsers/openai_tools.py +6 -6
- langchain/agents/output_parsers/react_json_single_input.py +2 -1
- langchain/agents/output_parsers/self_ask.py +2 -1
- langchain/agents/output_parsers/tools.py +7 -7
- langchain/agents/react/agent.py +3 -2
- langchain/agents/react/base.py +4 -3
- langchain/agents/schema.py +3 -3
- langchain/agents/self_ask_with_search/base.py +2 -1
- langchain/agents/structured_chat/base.py +9 -8
- langchain/agents/structured_chat/output_parser.py +2 -1
- langchain/agents/tool_calling_agent/base.py +3 -2
- langchain/agents/tools.py +4 -4
- langchain/agents/types.py +3 -3
- langchain/agents/utils.py +1 -1
- langchain/agents/xml/base.py +7 -6
- langchain/callbacks/streaming_aiter.py +3 -2
- langchain/callbacks/streaming_aiter_final_only.py +3 -3
- langchain/callbacks/streaming_stdout_final_only.py +3 -3
- langchain/chains/api/base.py +11 -12
- langchain/chains/base.py +47 -50
- langchain/chains/combine_documents/base.py +23 -23
- langchain/chains/combine_documents/map_reduce.py +12 -12
- langchain/chains/combine_documents/map_rerank.py +16 -15
- langchain/chains/combine_documents/reduce.py +17 -17
- langchain/chains/combine_documents/refine.py +12 -12
- langchain/chains/combine_documents/stuff.py +10 -10
- langchain/chains/constitutional_ai/base.py +9 -9
- langchain/chains/conversation/base.py +2 -4
- langchain/chains/conversational_retrieval/base.py +30 -30
- langchain/chains/elasticsearch_database/base.py +13 -13
- langchain/chains/example_generator.py +1 -3
- langchain/chains/flare/base.py +13 -12
- langchain/chains/flare/prompts.py +2 -4
- langchain/chains/hyde/base.py +8 -8
- langchain/chains/llm.py +31 -30
- langchain/chains/llm_checker/base.py +6 -6
- langchain/chains/llm_math/base.py +10 -10
- langchain/chains/llm_summarization_checker/base.py +6 -6
- langchain/chains/loading.py +12 -14
- langchain/chains/mapreduce.py +7 -6
- langchain/chains/moderation.py +8 -8
- langchain/chains/natbot/base.py +6 -6
- langchain/chains/openai_functions/base.py +8 -10
- langchain/chains/openai_functions/citation_fuzzy_match.py +4 -4
- langchain/chains/openai_functions/extraction.py +3 -3
- langchain/chains/openai_functions/openapi.py +12 -12
- langchain/chains/openai_functions/qa_with_structure.py +4 -4
- langchain/chains/openai_functions/utils.py +2 -2
- langchain/chains/openai_tools/extraction.py +2 -2
- langchain/chains/prompt_selector.py +3 -3
- langchain/chains/qa_generation/base.py +5 -5
- langchain/chains/qa_with_sources/base.py +21 -21
- langchain/chains/qa_with_sources/loading.py +2 -1
- langchain/chains/qa_with_sources/retrieval.py +6 -6
- langchain/chains/qa_with_sources/vector_db.py +8 -8
- langchain/chains/query_constructor/base.py +4 -3
- langchain/chains/query_constructor/parser.py +5 -4
- langchain/chains/question_answering/chain.py +3 -2
- langchain/chains/retrieval.py +2 -2
- langchain/chains/retrieval_qa/base.py +16 -16
- langchain/chains/router/base.py +12 -11
- langchain/chains/router/embedding_router.py +12 -11
- langchain/chains/router/llm_router.py +12 -12
- langchain/chains/router/multi_prompt.py +3 -3
- langchain/chains/router/multi_retrieval_qa.py +5 -4
- langchain/chains/sequential.py +18 -18
- langchain/chains/sql_database/query.py +4 -4
- langchain/chains/structured_output/base.py +14 -13
- langchain/chains/summarize/chain.py +4 -3
- langchain/chains/transform.py +12 -11
- langchain/chat_models/base.py +34 -31
- langchain/embeddings/__init__.py +1 -1
- langchain/embeddings/base.py +4 -4
- langchain/embeddings/cache.py +19 -18
- langchain/evaluation/agents/trajectory_eval_chain.py +16 -19
- langchain/evaluation/comparison/eval_chain.py +10 -10
- langchain/evaluation/criteria/eval_chain.py +11 -10
- langchain/evaluation/embedding_distance/base.py +21 -21
- langchain/evaluation/exact_match/base.py +3 -3
- langchain/evaluation/loading.py +7 -8
- langchain/evaluation/qa/eval_chain.py +7 -6
- langchain/evaluation/regex_match/base.py +3 -3
- langchain/evaluation/schema.py +6 -5
- langchain/evaluation/scoring/eval_chain.py +9 -9
- langchain/evaluation/string_distance/base.py +23 -23
- langchain/hub.py +2 -1
- langchain/indexes/_sql_record_manager.py +8 -7
- langchain/indexes/vectorstore.py +11 -11
- langchain/llms/__init__.py +3 -3
- langchain/memory/buffer.py +13 -13
- langchain/memory/buffer_window.py +5 -5
- langchain/memory/chat_memory.py +5 -5
- langchain/memory/combined.py +10 -10
- langchain/memory/entity.py +8 -7
- langchain/memory/readonly.py +4 -4
- langchain/memory/simple.py +5 -5
- langchain/memory/summary.py +8 -8
- langchain/memory/summary_buffer.py +11 -11
- langchain/memory/token_buffer.py +5 -5
- langchain/memory/utils.py +2 -2
- langchain/memory/vectorstore.py +15 -14
- langchain/memory/vectorstore_token_buffer_memory.py +7 -7
- langchain/model_laboratory.py +4 -3
- langchain/output_parsers/combining.py +5 -5
- langchain/output_parsers/datetime.py +1 -2
- langchain/output_parsers/enum.py +4 -5
- langchain/output_parsers/pandas_dataframe.py +5 -5
- langchain/output_parsers/regex.py +4 -4
- langchain/output_parsers/regex_dict.py +4 -4
- langchain/output_parsers/retry.py +2 -2
- langchain/output_parsers/structured.py +5 -5
- langchain/output_parsers/yaml.py +3 -3
- langchain/pydantic_v1/__init__.py +1 -6
- langchain/pydantic_v1/dataclasses.py +1 -5
- langchain/pydantic_v1/main.py +1 -5
- langchain/retrievers/contextual_compression.py +3 -3
- langchain/retrievers/document_compressors/base.py +3 -2
- langchain/retrievers/document_compressors/chain_extract.py +4 -3
- langchain/retrievers/document_compressors/chain_filter.py +3 -2
- langchain/retrievers/document_compressors/cohere_rerank.py +4 -3
- langchain/retrievers/document_compressors/cross_encoder.py +1 -2
- langchain/retrievers/document_compressors/cross_encoder_rerank.py +2 -1
- langchain/retrievers/document_compressors/embeddings_filter.py +3 -2
- langchain/retrievers/document_compressors/listwise_rerank.py +6 -5
- langchain/retrievers/ensemble.py +15 -19
- langchain/retrievers/merger_retriever.py +7 -12
- langchain/retrievers/multi_query.py +14 -13
- langchain/retrievers/multi_vector.py +4 -4
- langchain/retrievers/parent_document_retriever.py +9 -8
- langchain/retrievers/re_phraser.py +2 -3
- langchain/retrievers/self_query/base.py +13 -12
- langchain/retrievers/time_weighted_retriever.py +14 -14
- langchain/runnables/openai_functions.py +4 -3
- langchain/smith/evaluation/config.py +7 -6
- langchain/smith/evaluation/progress.py +3 -2
- langchain/smith/evaluation/runner_utils.py +58 -61
- langchain/smith/evaluation/string_run_evaluator.py +29 -29
- langchain/storage/encoder_backed.py +7 -11
- langchain/storage/file_system.py +5 -4
- {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/METADATA +5 -3
- {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/RECORD +169 -169
- {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/WHEEL +1 -1
- langchain-0.3.24.dist-info/entry_points.txt +4 -0
- langchain-0.3.22.dist-info/entry_points.txt +0 -5
- {langchain-0.3.22.dist-info → langchain-0.3.24.dist-info}/licenses/LICENSE +0 -0
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import inspect
|
|
6
6
|
import re
|
|
7
7
|
from abc import ABC, abstractmethod
|
|
8
|
-
from typing import Any,
|
|
8
|
+
from typing import Any, Optional
|
|
9
9
|
|
|
10
10
|
from langchain_core._api import deprecated
|
|
11
11
|
from langchain_core.callbacks import (
|
|
@@ -69,7 +69,7 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
69
69
|
document_prompt=document_prompt,
|
|
70
70
|
document_variable_name="summaries",
|
|
71
71
|
)
|
|
72
|
-
reduce_documents_chain = ReduceDocumentsChain(
|
|
72
|
+
reduce_documents_chain = ReduceDocumentsChain(
|
|
73
73
|
combine_documents_chain=combine_results_chain
|
|
74
74
|
)
|
|
75
75
|
combine_documents_chain = MapReduceDocumentsChain(
|
|
@@ -103,7 +103,7 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
103
103
|
)
|
|
104
104
|
|
|
105
105
|
@property
|
|
106
|
-
def input_keys(self) ->
|
|
106
|
+
def input_keys(self) -> list[str]:
|
|
107
107
|
"""Expect input key.
|
|
108
108
|
|
|
109
109
|
:meta private:
|
|
@@ -111,7 +111,7 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
111
111
|
return [self.question_key]
|
|
112
112
|
|
|
113
113
|
@property
|
|
114
|
-
def output_keys(self) ->
|
|
114
|
+
def output_keys(self) -> list[str]:
|
|
115
115
|
"""Return output key.
|
|
116
116
|
|
|
117
117
|
:meta private:
|
|
@@ -123,13 +123,13 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
123
123
|
|
|
124
124
|
@model_validator(mode="before")
|
|
125
125
|
@classmethod
|
|
126
|
-
def validate_naming(cls, values:
|
|
126
|
+
def validate_naming(cls, values: dict) -> Any:
|
|
127
127
|
"""Fix backwards compatibility in naming."""
|
|
128
128
|
if "combine_document_chain" in values:
|
|
129
129
|
values["combine_documents_chain"] = values.pop("combine_document_chain")
|
|
130
130
|
return values
|
|
131
131
|
|
|
132
|
-
def _split_sources(self, answer: str) ->
|
|
132
|
+
def _split_sources(self, answer: str) -> tuple[str, str]:
|
|
133
133
|
"""Split sources from answer."""
|
|
134
134
|
if re.search(r"SOURCES?:", answer, re.IGNORECASE):
|
|
135
135
|
answer, sources = re.split(
|
|
@@ -143,17 +143,17 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
143
143
|
@abstractmethod
|
|
144
144
|
def _get_docs(
|
|
145
145
|
self,
|
|
146
|
-
inputs:
|
|
146
|
+
inputs: dict[str, Any],
|
|
147
147
|
*,
|
|
148
148
|
run_manager: CallbackManagerForChainRun,
|
|
149
|
-
) ->
|
|
149
|
+
) -> list[Document]:
|
|
150
150
|
"""Get docs to run questioning over."""
|
|
151
151
|
|
|
152
152
|
def _call(
|
|
153
153
|
self,
|
|
154
|
-
inputs:
|
|
154
|
+
inputs: dict[str, Any],
|
|
155
155
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
156
|
-
) ->
|
|
156
|
+
) -> dict[str, str]:
|
|
157
157
|
_run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
|
|
158
158
|
accepts_run_manager = (
|
|
159
159
|
"run_manager" in inspect.signature(self._get_docs).parameters
|
|
@@ -167,7 +167,7 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
167
167
|
input_documents=docs, callbacks=_run_manager.get_child(), **inputs
|
|
168
168
|
)
|
|
169
169
|
answer, sources = self._split_sources(answer)
|
|
170
|
-
result:
|
|
170
|
+
result: dict[str, Any] = {
|
|
171
171
|
self.answer_key: answer,
|
|
172
172
|
self.sources_answer_key: sources,
|
|
173
173
|
}
|
|
@@ -178,17 +178,17 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
178
178
|
@abstractmethod
|
|
179
179
|
async def _aget_docs(
|
|
180
180
|
self,
|
|
181
|
-
inputs:
|
|
181
|
+
inputs: dict[str, Any],
|
|
182
182
|
*,
|
|
183
183
|
run_manager: AsyncCallbackManagerForChainRun,
|
|
184
|
-
) ->
|
|
184
|
+
) -> list[Document]:
|
|
185
185
|
"""Get docs to run questioning over."""
|
|
186
186
|
|
|
187
187
|
async def _acall(
|
|
188
188
|
self,
|
|
189
|
-
inputs:
|
|
189
|
+
inputs: dict[str, Any],
|
|
190
190
|
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
|
|
191
|
-
) ->
|
|
191
|
+
) -> dict[str, Any]:
|
|
192
192
|
_run_manager = run_manager or AsyncCallbackManagerForChainRun.get_noop_manager()
|
|
193
193
|
accepts_run_manager = (
|
|
194
194
|
"run_manager" in inspect.signature(self._aget_docs).parameters
|
|
@@ -201,7 +201,7 @@ class BaseQAWithSourcesChain(Chain, ABC):
|
|
|
201
201
|
input_documents=docs, callbacks=_run_manager.get_child(), **inputs
|
|
202
202
|
)
|
|
203
203
|
answer, sources = self._split_sources(answer)
|
|
204
|
-
result:
|
|
204
|
+
result: dict[str, Any] = {
|
|
205
205
|
self.answer_key: answer,
|
|
206
206
|
self.sources_answer_key: sources,
|
|
207
207
|
}
|
|
@@ -225,7 +225,7 @@ class QAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
225
225
|
input_docs_key: str = "docs" #: :meta private:
|
|
226
226
|
|
|
227
227
|
@property
|
|
228
|
-
def input_keys(self) ->
|
|
228
|
+
def input_keys(self) -> list[str]:
|
|
229
229
|
"""Expect input key.
|
|
230
230
|
|
|
231
231
|
:meta private:
|
|
@@ -234,19 +234,19 @@ class QAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
234
234
|
|
|
235
235
|
def _get_docs(
|
|
236
236
|
self,
|
|
237
|
-
inputs:
|
|
237
|
+
inputs: dict[str, Any],
|
|
238
238
|
*,
|
|
239
239
|
run_manager: CallbackManagerForChainRun,
|
|
240
|
-
) ->
|
|
240
|
+
) -> list[Document]:
|
|
241
241
|
"""Get docs to run questioning over."""
|
|
242
242
|
return inputs.pop(self.input_docs_key)
|
|
243
243
|
|
|
244
244
|
async def _aget_docs(
|
|
245
245
|
self,
|
|
246
|
-
inputs:
|
|
246
|
+
inputs: dict[str, Any],
|
|
247
247
|
*,
|
|
248
248
|
run_manager: AsyncCallbackManagerForChainRun,
|
|
249
|
-
) ->
|
|
249
|
+
) -> list[Document]:
|
|
250
250
|
"""Get docs to run questioning over."""
|
|
251
251
|
return inputs.pop(self.input_docs_key)
|
|
252
252
|
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any, Optional, Protocol
|
|
6
7
|
|
|
7
8
|
from langchain_core._api import deprecated
|
|
8
9
|
from langchain_core.language_models import BaseLanguageModel
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""Question-answering with sources over an index."""
|
|
2
2
|
|
|
3
|
-
from typing import Any
|
|
3
|
+
from typing import Any
|
|
4
4
|
|
|
5
5
|
from langchain_core.callbacks import (
|
|
6
6
|
AsyncCallbackManagerForChainRun,
|
|
@@ -25,7 +25,7 @@ class RetrievalQAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
25
25
|
"""Restrict the docs to return from store based on tokens,
|
|
26
26
|
enforced only for StuffDocumentChain and if reduce_k_below_max_tokens is to true"""
|
|
27
27
|
|
|
28
|
-
def _reduce_tokens_below_limit(self, docs:
|
|
28
|
+
def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
|
|
29
29
|
num_docs = len(docs)
|
|
30
30
|
|
|
31
31
|
if self.reduce_k_below_max_tokens and isinstance(
|
|
@@ -43,8 +43,8 @@ class RetrievalQAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
43
43
|
return docs[:num_docs]
|
|
44
44
|
|
|
45
45
|
def _get_docs(
|
|
46
|
-
self, inputs:
|
|
47
|
-
) ->
|
|
46
|
+
self, inputs: dict[str, Any], *, run_manager: CallbackManagerForChainRun
|
|
47
|
+
) -> list[Document]:
|
|
48
48
|
question = inputs[self.question_key]
|
|
49
49
|
docs = self.retriever.invoke(
|
|
50
50
|
question, config={"callbacks": run_manager.get_child()}
|
|
@@ -52,8 +52,8 @@ class RetrievalQAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
52
52
|
return self._reduce_tokens_below_limit(docs)
|
|
53
53
|
|
|
54
54
|
async def _aget_docs(
|
|
55
|
-
self, inputs:
|
|
56
|
-
) ->
|
|
55
|
+
self, inputs: dict[str, Any], *, run_manager: AsyncCallbackManagerForChainRun
|
|
56
|
+
) -> list[Document]:
|
|
57
57
|
question = inputs[self.question_key]
|
|
58
58
|
docs = await self.retriever.ainvoke(
|
|
59
59
|
question, config={"callbacks": run_manager.get_child()}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Question-answering with sources over a vector database."""
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
|
-
from typing import Any
|
|
4
|
+
from typing import Any
|
|
5
5
|
|
|
6
6
|
from langchain_core.callbacks import (
|
|
7
7
|
AsyncCallbackManagerForChainRun,
|
|
@@ -27,10 +27,10 @@ class VectorDBQAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
27
27
|
max_tokens_limit: int = 3375
|
|
28
28
|
"""Restrict the docs to return from store based on tokens,
|
|
29
29
|
enforced only for StuffDocumentChain and if reduce_k_below_max_tokens is to true"""
|
|
30
|
-
search_kwargs:
|
|
30
|
+
search_kwargs: dict[str, Any] = Field(default_factory=dict)
|
|
31
31
|
"""Extra search args."""
|
|
32
32
|
|
|
33
|
-
def _reduce_tokens_below_limit(self, docs:
|
|
33
|
+
def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
|
|
34
34
|
num_docs = len(docs)
|
|
35
35
|
|
|
36
36
|
if self.reduce_k_below_max_tokens and isinstance(
|
|
@@ -48,8 +48,8 @@ class VectorDBQAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
48
48
|
return docs[:num_docs]
|
|
49
49
|
|
|
50
50
|
def _get_docs(
|
|
51
|
-
self, inputs:
|
|
52
|
-
) ->
|
|
51
|
+
self, inputs: dict[str, Any], *, run_manager: CallbackManagerForChainRun
|
|
52
|
+
) -> list[Document]:
|
|
53
53
|
question = inputs[self.question_key]
|
|
54
54
|
docs = self.vectorstore.similarity_search(
|
|
55
55
|
question, k=self.k, **self.search_kwargs
|
|
@@ -57,13 +57,13 @@ class VectorDBQAWithSourcesChain(BaseQAWithSourcesChain):
|
|
|
57
57
|
return self._reduce_tokens_below_limit(docs)
|
|
58
58
|
|
|
59
59
|
async def _aget_docs(
|
|
60
|
-
self, inputs:
|
|
61
|
-
) ->
|
|
60
|
+
self, inputs: dict[str, Any], *, run_manager: AsyncCallbackManagerForChainRun
|
|
61
|
+
) -> list[Document]:
|
|
62
62
|
raise NotImplementedError("VectorDBQAWithSourcesChain does not support async")
|
|
63
63
|
|
|
64
64
|
@model_validator(mode="before")
|
|
65
65
|
@classmethod
|
|
66
|
-
def raise_deprecation(cls, values:
|
|
66
|
+
def raise_deprecation(cls, values: dict) -> Any:
|
|
67
67
|
warnings.warn(
|
|
68
68
|
"`VectorDBQAWithSourcesChain` is deprecated - "
|
|
69
69
|
"please use `from langchain.chains import RetrievalQAWithSourcesChain`"
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import json
|
|
6
|
-
from
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from typing import Any, Callable, Optional, Union, cast
|
|
7
8
|
|
|
8
9
|
from langchain_core._api import deprecated
|
|
9
10
|
from langchain_core.exceptions import OutputParserException
|
|
@@ -172,7 +173,7 @@ def _format_attribute_info(info: Sequence[Union[AttributeInfo, dict]]) -> str:
|
|
|
172
173
|
return json.dumps(info_dicts, indent=4).replace("{", "{{").replace("}", "}}")
|
|
173
174
|
|
|
174
175
|
|
|
175
|
-
def construct_examples(input_output_pairs: Sequence[
|
|
176
|
+
def construct_examples(input_output_pairs: Sequence[tuple[str, dict]]) -> list[dict]:
|
|
176
177
|
"""Construct examples from input-output pairs.
|
|
177
178
|
|
|
178
179
|
Args:
|
|
@@ -267,7 +268,7 @@ def load_query_constructor_chain(
|
|
|
267
268
|
llm: BaseLanguageModel,
|
|
268
269
|
document_contents: str,
|
|
269
270
|
attribute_info: Sequence[Union[AttributeInfo, dict]],
|
|
270
|
-
examples: Optional[
|
|
271
|
+
examples: Optional[list] = None,
|
|
271
272
|
allowed_comparators: Sequence[Comparator] = tuple(Comparator),
|
|
272
273
|
allowed_operators: Sequence[Operator] = tuple(Operator),
|
|
273
274
|
enable_limit: bool = False,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
import warnings
|
|
3
|
-
from
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any, Literal, Optional, Union
|
|
4
5
|
|
|
5
6
|
from langchain_core.utils import check_package_version
|
|
6
7
|
from typing_extensions import TypedDict
|
|
@@ -10,12 +11,12 @@ try:
|
|
|
10
11
|
from lark import Lark, Transformer, v_args
|
|
11
12
|
except ImportError:
|
|
12
13
|
|
|
13
|
-
def v_args(*args: Any, **kwargs: Any) -> Any: # type: ignore
|
|
14
|
+
def v_args(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]
|
|
14
15
|
"""Dummy decorator for when lark is not installed."""
|
|
15
16
|
return lambda _: None
|
|
16
17
|
|
|
17
|
-
Transformer = object # type: ignore
|
|
18
|
-
Lark = object # type: ignore
|
|
18
|
+
Transformer = object # type: ignore[assignment,misc]
|
|
19
|
+
Lark = object # type: ignore[assignment,misc]
|
|
19
20
|
|
|
20
21
|
from langchain_core.structured_query import (
|
|
21
22
|
Comparator,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Load question answering chains."""
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from typing import Any, Optional, Protocol
|
|
4
5
|
|
|
5
6
|
from langchain_core._api import deprecated
|
|
6
7
|
from langchain_core.callbacks import BaseCallbackManager, Callbacks
|
|
@@ -155,7 +156,7 @@ def _load_map_reduce_chain(
|
|
|
155
156
|
verbose=verbose, # type: ignore[arg-type]
|
|
156
157
|
callback_manager=callback_manager,
|
|
157
158
|
)
|
|
158
|
-
reduce_documents_chain = ReduceDocumentsChain(
|
|
159
|
+
reduce_documents_chain = ReduceDocumentsChain(
|
|
159
160
|
combine_documents_chain=combine_documents_chain,
|
|
160
161
|
collapse_documents_chain=collapse_chain,
|
|
161
162
|
token_max=token_max,
|
langchain/chains/retrieval.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import Any,
|
|
3
|
+
from typing import Any, Union
|
|
4
4
|
|
|
5
5
|
from langchain_core.retrievers import (
|
|
6
6
|
BaseRetriever,
|
|
@@ -11,7 +11,7 @@ from langchain_core.runnables import Runnable, RunnablePassthrough
|
|
|
11
11
|
|
|
12
12
|
def create_retrieval_chain(
|
|
13
13
|
retriever: Union[BaseRetriever, Runnable[dict, RetrieverOutput]],
|
|
14
|
-
combine_docs_chain: Runnable[
|
|
14
|
+
combine_docs_chain: Runnable[dict[str, Any], str],
|
|
15
15
|
) -> Runnable:
|
|
16
16
|
"""Create retrieval chain that retrieves documents and then passes them on.
|
|
17
17
|
|
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import inspect
|
|
6
6
|
import warnings
|
|
7
7
|
from abc import abstractmethod
|
|
8
|
-
from typing import Any,
|
|
8
|
+
from typing import Any, Optional
|
|
9
9
|
|
|
10
10
|
from langchain_core._api import deprecated
|
|
11
11
|
from langchain_core.callbacks import (
|
|
@@ -54,7 +54,7 @@ class BaseRetrievalQA(Chain):
|
|
|
54
54
|
)
|
|
55
55
|
|
|
56
56
|
@property
|
|
57
|
-
def input_keys(self) ->
|
|
57
|
+
def input_keys(self) -> list[str]:
|
|
58
58
|
"""Input keys.
|
|
59
59
|
|
|
60
60
|
:meta private:
|
|
@@ -62,7 +62,7 @@ class BaseRetrievalQA(Chain):
|
|
|
62
62
|
return [self.input_key]
|
|
63
63
|
|
|
64
64
|
@property
|
|
65
|
-
def output_keys(self) ->
|
|
65
|
+
def output_keys(self) -> list[str]:
|
|
66
66
|
"""Output keys.
|
|
67
67
|
|
|
68
68
|
:meta private:
|
|
@@ -123,14 +123,14 @@ class BaseRetrievalQA(Chain):
|
|
|
123
123
|
question: str,
|
|
124
124
|
*,
|
|
125
125
|
run_manager: CallbackManagerForChainRun,
|
|
126
|
-
) ->
|
|
126
|
+
) -> list[Document]:
|
|
127
127
|
"""Get documents to do question answering over."""
|
|
128
128
|
|
|
129
129
|
def _call(
|
|
130
130
|
self,
|
|
131
|
-
inputs:
|
|
131
|
+
inputs: dict[str, Any],
|
|
132
132
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
133
|
-
) ->
|
|
133
|
+
) -> dict[str, Any]:
|
|
134
134
|
"""Run get_relevant_text and llm on input query.
|
|
135
135
|
|
|
136
136
|
If chain has 'return_source_documents' as 'True', returns
|
|
@@ -166,14 +166,14 @@ class BaseRetrievalQA(Chain):
|
|
|
166
166
|
question: str,
|
|
167
167
|
*,
|
|
168
168
|
run_manager: AsyncCallbackManagerForChainRun,
|
|
169
|
-
) ->
|
|
169
|
+
) -> list[Document]:
|
|
170
170
|
"""Get documents to do question answering over."""
|
|
171
171
|
|
|
172
172
|
async def _acall(
|
|
173
173
|
self,
|
|
174
|
-
inputs:
|
|
174
|
+
inputs: dict[str, Any],
|
|
175
175
|
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
|
|
176
|
-
) ->
|
|
176
|
+
) -> dict[str, Any]:
|
|
177
177
|
"""Run get_relevant_text and llm on input query.
|
|
178
178
|
|
|
179
179
|
If chain has 'return_source_documents' as 'True', returns
|
|
@@ -266,7 +266,7 @@ class RetrievalQA(BaseRetrievalQA):
|
|
|
266
266
|
question: str,
|
|
267
267
|
*,
|
|
268
268
|
run_manager: CallbackManagerForChainRun,
|
|
269
|
-
) ->
|
|
269
|
+
) -> list[Document]:
|
|
270
270
|
"""Get docs."""
|
|
271
271
|
return self.retriever.invoke(
|
|
272
272
|
question, config={"callbacks": run_manager.get_child()}
|
|
@@ -277,7 +277,7 @@ class RetrievalQA(BaseRetrievalQA):
|
|
|
277
277
|
question: str,
|
|
278
278
|
*,
|
|
279
279
|
run_manager: AsyncCallbackManagerForChainRun,
|
|
280
|
-
) ->
|
|
280
|
+
) -> list[Document]:
|
|
281
281
|
"""Get docs."""
|
|
282
282
|
return await self.retriever.ainvoke(
|
|
283
283
|
question, config={"callbacks": run_manager.get_child()}
|
|
@@ -307,12 +307,12 @@ class VectorDBQA(BaseRetrievalQA):
|
|
|
307
307
|
"""Number of documents to query for."""
|
|
308
308
|
search_type: str = "similarity"
|
|
309
309
|
"""Search type to use over vectorstore. `similarity` or `mmr`."""
|
|
310
|
-
search_kwargs:
|
|
310
|
+
search_kwargs: dict[str, Any] = Field(default_factory=dict)
|
|
311
311
|
"""Extra search args."""
|
|
312
312
|
|
|
313
313
|
@model_validator(mode="before")
|
|
314
314
|
@classmethod
|
|
315
|
-
def raise_deprecation(cls, values:
|
|
315
|
+
def raise_deprecation(cls, values: dict) -> Any:
|
|
316
316
|
warnings.warn(
|
|
317
317
|
"`VectorDBQA` is deprecated - "
|
|
318
318
|
"please use `from langchain.chains import RetrievalQA`"
|
|
@@ -321,7 +321,7 @@ class VectorDBQA(BaseRetrievalQA):
|
|
|
321
321
|
|
|
322
322
|
@model_validator(mode="before")
|
|
323
323
|
@classmethod
|
|
324
|
-
def validate_search_type(cls, values:
|
|
324
|
+
def validate_search_type(cls, values: dict) -> Any:
|
|
325
325
|
"""Validate search type."""
|
|
326
326
|
if "search_type" in values:
|
|
327
327
|
search_type = values["search_type"]
|
|
@@ -334,7 +334,7 @@ class VectorDBQA(BaseRetrievalQA):
|
|
|
334
334
|
question: str,
|
|
335
335
|
*,
|
|
336
336
|
run_manager: CallbackManagerForChainRun,
|
|
337
|
-
) ->
|
|
337
|
+
) -> list[Document]:
|
|
338
338
|
"""Get docs."""
|
|
339
339
|
if self.search_type == "similarity":
|
|
340
340
|
docs = self.vectorstore.similarity_search(
|
|
@@ -353,7 +353,7 @@ class VectorDBQA(BaseRetrievalQA):
|
|
|
353
353
|
question: str,
|
|
354
354
|
*,
|
|
355
355
|
run_manager: AsyncCallbackManagerForChainRun,
|
|
356
|
-
) ->
|
|
356
|
+
) -> list[Document]:
|
|
357
357
|
"""Get docs."""
|
|
358
358
|
raise NotImplementedError("VectorDBQA does not support async")
|
|
359
359
|
|
langchain/chains/router/base.py
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from abc import ABC
|
|
6
|
-
from
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from typing import Any, NamedTuple, Optional
|
|
7
8
|
|
|
8
9
|
from langchain_core.callbacks import (
|
|
9
10
|
AsyncCallbackManagerForChainRun,
|
|
@@ -17,17 +18,17 @@ from langchain.chains.base import Chain
|
|
|
17
18
|
|
|
18
19
|
class Route(NamedTuple):
|
|
19
20
|
destination: Optional[str]
|
|
20
|
-
next_inputs:
|
|
21
|
+
next_inputs: dict[str, Any]
|
|
21
22
|
|
|
22
23
|
|
|
23
24
|
class RouterChain(Chain, ABC):
|
|
24
25
|
"""Chain that outputs the name of a destination chain and the inputs to it."""
|
|
25
26
|
|
|
26
27
|
@property
|
|
27
|
-
def output_keys(self) ->
|
|
28
|
+
def output_keys(self) -> list[str]:
|
|
28
29
|
return ["destination", "next_inputs"]
|
|
29
30
|
|
|
30
|
-
def route(self, inputs:
|
|
31
|
+
def route(self, inputs: dict[str, Any], callbacks: Callbacks = None) -> Route:
|
|
31
32
|
"""
|
|
32
33
|
Route inputs to a destination chain.
|
|
33
34
|
|
|
@@ -42,7 +43,7 @@ class RouterChain(Chain, ABC):
|
|
|
42
43
|
return Route(result["destination"], result["next_inputs"])
|
|
43
44
|
|
|
44
45
|
async def aroute(
|
|
45
|
-
self, inputs:
|
|
46
|
+
self, inputs: dict[str, Any], callbacks: Callbacks = None
|
|
46
47
|
) -> Route:
|
|
47
48
|
result = await self.acall(inputs, callbacks=callbacks)
|
|
48
49
|
return Route(result["destination"], result["next_inputs"])
|
|
@@ -67,7 +68,7 @@ class MultiRouteChain(Chain):
|
|
|
67
68
|
)
|
|
68
69
|
|
|
69
70
|
@property
|
|
70
|
-
def input_keys(self) ->
|
|
71
|
+
def input_keys(self) -> list[str]:
|
|
71
72
|
"""Will be whatever keys the router chain prompt expects.
|
|
72
73
|
|
|
73
74
|
:meta private:
|
|
@@ -75,7 +76,7 @@ class MultiRouteChain(Chain):
|
|
|
75
76
|
return self.router_chain.input_keys
|
|
76
77
|
|
|
77
78
|
@property
|
|
78
|
-
def output_keys(self) ->
|
|
79
|
+
def output_keys(self) -> list[str]:
|
|
79
80
|
"""Will always return text key.
|
|
80
81
|
|
|
81
82
|
:meta private:
|
|
@@ -84,9 +85,9 @@ class MultiRouteChain(Chain):
|
|
|
84
85
|
|
|
85
86
|
def _call(
|
|
86
87
|
self,
|
|
87
|
-
inputs:
|
|
88
|
+
inputs: dict[str, Any],
|
|
88
89
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
89
|
-
) ->
|
|
90
|
+
) -> dict[str, Any]:
|
|
90
91
|
_run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
|
|
91
92
|
callbacks = _run_manager.get_child()
|
|
92
93
|
route = self.router_chain.route(inputs, callbacks=callbacks)
|
|
@@ -109,9 +110,9 @@ class MultiRouteChain(Chain):
|
|
|
109
110
|
|
|
110
111
|
async def _acall(
|
|
111
112
|
self,
|
|
112
|
-
inputs:
|
|
113
|
+
inputs: dict[str, Any],
|
|
113
114
|
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
|
|
114
|
-
) ->
|
|
115
|
+
) -> dict[str, Any]:
|
|
115
116
|
_run_manager = run_manager or AsyncCallbackManagerForChainRun.get_noop_manager()
|
|
116
117
|
callbacks = _run_manager.get_child()
|
|
117
118
|
route = await self.router_chain.aroute(inputs, callbacks=callbacks)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any, Optional
|
|
4
5
|
|
|
5
6
|
from langchain_core.callbacks import (
|
|
6
7
|
AsyncCallbackManagerForChainRun,
|
|
@@ -18,7 +19,7 @@ class EmbeddingRouterChain(RouterChain):
|
|
|
18
19
|
"""Chain that uses embeddings to route between options."""
|
|
19
20
|
|
|
20
21
|
vectorstore: VectorStore
|
|
21
|
-
routing_keys:
|
|
22
|
+
routing_keys: list[str] = ["query"]
|
|
22
23
|
|
|
23
24
|
model_config = ConfigDict(
|
|
24
25
|
arbitrary_types_allowed=True,
|
|
@@ -26,7 +27,7 @@ class EmbeddingRouterChain(RouterChain):
|
|
|
26
27
|
)
|
|
27
28
|
|
|
28
29
|
@property
|
|
29
|
-
def input_keys(self) ->
|
|
30
|
+
def input_keys(self) -> list[str]:
|
|
30
31
|
"""Will be whatever keys the LLM chain prompt expects.
|
|
31
32
|
|
|
32
33
|
:meta private:
|
|
@@ -35,18 +36,18 @@ class EmbeddingRouterChain(RouterChain):
|
|
|
35
36
|
|
|
36
37
|
def _call(
|
|
37
38
|
self,
|
|
38
|
-
inputs:
|
|
39
|
+
inputs: dict[str, Any],
|
|
39
40
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
40
|
-
) ->
|
|
41
|
+
) -> dict[str, Any]:
|
|
41
42
|
_input = ", ".join([inputs[k] for k in self.routing_keys])
|
|
42
43
|
results = self.vectorstore.similarity_search(_input, k=1)
|
|
43
44
|
return {"next_inputs": inputs, "destination": results[0].metadata["name"]}
|
|
44
45
|
|
|
45
46
|
async def _acall(
|
|
46
47
|
self,
|
|
47
|
-
inputs:
|
|
48
|
+
inputs: dict[str, Any],
|
|
48
49
|
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
|
|
49
|
-
) ->
|
|
50
|
+
) -> dict[str, Any]:
|
|
50
51
|
_input = ", ".join([inputs[k] for k in self.routing_keys])
|
|
51
52
|
results = await self.vectorstore.asimilarity_search(_input, k=1)
|
|
52
53
|
return {"next_inputs": inputs, "destination": results[0].metadata["name"]}
|
|
@@ -54,8 +55,8 @@ class EmbeddingRouterChain(RouterChain):
|
|
|
54
55
|
@classmethod
|
|
55
56
|
def from_names_and_descriptions(
|
|
56
57
|
cls,
|
|
57
|
-
names_and_descriptions: Sequence[
|
|
58
|
-
vectorstore_cls:
|
|
58
|
+
names_and_descriptions: Sequence[tuple[str, Sequence[str]]],
|
|
59
|
+
vectorstore_cls: type[VectorStore],
|
|
59
60
|
embeddings: Embeddings,
|
|
60
61
|
**kwargs: Any,
|
|
61
62
|
) -> EmbeddingRouterChain:
|
|
@@ -72,8 +73,8 @@ class EmbeddingRouterChain(RouterChain):
|
|
|
72
73
|
@classmethod
|
|
73
74
|
async def afrom_names_and_descriptions(
|
|
74
75
|
cls,
|
|
75
|
-
names_and_descriptions: Sequence[
|
|
76
|
-
vectorstore_cls:
|
|
76
|
+
names_and_descriptions: Sequence[tuple[str, Sequence[str]]],
|
|
77
|
+
vectorstore_cls: type[VectorStore],
|
|
77
78
|
embeddings: Embeddings,
|
|
78
79
|
**kwargs: Any,
|
|
79
80
|
) -> EmbeddingRouterChain:
|