langchain 0.3.23__py3-none-any.whl → 0.3.25__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.
Potentially problematic release.
This version of langchain might be problematic. Click here for more details.
- 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 +17 -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 +21 -5
- 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 +27 -31
- langchain/embeddings/__init__.py +1 -1
- langchain/embeddings/base.py +4 -6
- 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 +66 -69
- langchain/smith/evaluation/string_run_evaluator.py +38 -31
- langchain/storage/encoder_backed.py +7 -11
- langchain/storage/file_system.py +5 -4
- {langchain-0.3.23.dist-info → langchain-0.3.25.dist-info}/METADATA +3 -3
- {langchain-0.3.23.dist-info → langchain-0.3.25.dist-info}/RECORD +169 -169
- {langchain-0.3.23.dist-info → langchain-0.3.25.dist-info}/WHEEL +1 -1
- langchain-0.3.25.dist-info/entry_points.txt +4 -0
- langchain-0.3.23.dist-info/entry_points.txt +0 -5
- {langchain-0.3.23.dist-info → langchain-0.3.25.dist-info}/licenses/LICENSE +0 -0
langchain/memory/vectorstore.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Class for a VectorStore-backed memory object."""
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any, Optional, Union
|
|
4
5
|
|
|
5
6
|
from langchain_core._api import deprecated
|
|
6
7
|
from langchain_core.documents import Document
|
|
@@ -40,20 +41,20 @@ class VectorStoreRetrieverMemory(BaseMemory):
|
|
|
40
41
|
"""Input keys to exclude in addition to memory key when constructing the document"""
|
|
41
42
|
|
|
42
43
|
@property
|
|
43
|
-
def memory_variables(self) ->
|
|
44
|
+
def memory_variables(self) -> list[str]:
|
|
44
45
|
"""The list of keys emitted from the load_memory_variables method."""
|
|
45
46
|
return [self.memory_key]
|
|
46
47
|
|
|
47
|
-
def _get_prompt_input_key(self, inputs:
|
|
48
|
+
def _get_prompt_input_key(self, inputs: dict[str, Any]) -> str:
|
|
48
49
|
"""Get the input key for the prompt."""
|
|
49
50
|
if self.input_key is None:
|
|
50
51
|
return get_prompt_input_key(inputs, self.memory_variables)
|
|
51
52
|
return self.input_key
|
|
52
53
|
|
|
53
54
|
def _documents_to_memory_variables(
|
|
54
|
-
self, docs:
|
|
55
|
-
) ->
|
|
56
|
-
result: Union[
|
|
55
|
+
self, docs: list[Document]
|
|
56
|
+
) -> dict[str, Union[list[Document], str]]:
|
|
57
|
+
result: Union[list[Document], str]
|
|
57
58
|
if not self.return_docs:
|
|
58
59
|
result = "\n".join([doc.page_content for doc in docs])
|
|
59
60
|
else:
|
|
@@ -61,8 +62,8 @@ class VectorStoreRetrieverMemory(BaseMemory):
|
|
|
61
62
|
return {self.memory_key: result}
|
|
62
63
|
|
|
63
64
|
def load_memory_variables(
|
|
64
|
-
self, inputs:
|
|
65
|
-
) ->
|
|
65
|
+
self, inputs: dict[str, Any]
|
|
66
|
+
) -> dict[str, Union[list[Document], str]]:
|
|
66
67
|
"""Return history buffer."""
|
|
67
68
|
input_key = self._get_prompt_input_key(inputs)
|
|
68
69
|
query = inputs[input_key]
|
|
@@ -70,8 +71,8 @@ class VectorStoreRetrieverMemory(BaseMemory):
|
|
|
70
71
|
return self._documents_to_memory_variables(docs)
|
|
71
72
|
|
|
72
73
|
async def aload_memory_variables(
|
|
73
|
-
self, inputs:
|
|
74
|
-
) ->
|
|
74
|
+
self, inputs: dict[str, Any]
|
|
75
|
+
) -> dict[str, Union[list[Document], str]]:
|
|
75
76
|
"""Return history buffer."""
|
|
76
77
|
input_key = self._get_prompt_input_key(inputs)
|
|
77
78
|
query = inputs[input_key]
|
|
@@ -79,8 +80,8 @@ class VectorStoreRetrieverMemory(BaseMemory):
|
|
|
79
80
|
return self._documents_to_memory_variables(docs)
|
|
80
81
|
|
|
81
82
|
def _form_documents(
|
|
82
|
-
self, inputs:
|
|
83
|
-
) ->
|
|
83
|
+
self, inputs: dict[str, Any], outputs: dict[str, str]
|
|
84
|
+
) -> list[Document]:
|
|
84
85
|
"""Format context from this conversation to buffer."""
|
|
85
86
|
# Each document should only include the current turn, not the chat history
|
|
86
87
|
exclude = set(self.exclude_input_keys)
|
|
@@ -93,13 +94,13 @@ class VectorStoreRetrieverMemory(BaseMemory):
|
|
|
93
94
|
page_content = "\n".join(texts)
|
|
94
95
|
return [Document(page_content=page_content)]
|
|
95
96
|
|
|
96
|
-
def save_context(self, inputs:
|
|
97
|
+
def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
|
|
97
98
|
"""Save context from this conversation to buffer."""
|
|
98
99
|
documents = self._form_documents(inputs, outputs)
|
|
99
100
|
self.retriever.add_documents(documents)
|
|
100
101
|
|
|
101
102
|
async def asave_context(
|
|
102
|
-
self, inputs:
|
|
103
|
+
self, inputs: dict[str, Any], outputs: dict[str, str]
|
|
103
104
|
) -> None:
|
|
104
105
|
"""Save context from this conversation to buffer."""
|
|
105
106
|
documents = self._form_documents(inputs, outputs)
|
|
@@ -9,7 +9,7 @@ sessions.
|
|
|
9
9
|
|
|
10
10
|
import warnings
|
|
11
11
|
from datetime import datetime
|
|
12
|
-
from typing import Any
|
|
12
|
+
from typing import Any
|
|
13
13
|
|
|
14
14
|
from langchain_core.messages import BaseMessage
|
|
15
15
|
from langchain_core.prompts.chat import SystemMessagePromptTemplate
|
|
@@ -109,8 +109,8 @@ class ConversationVectorStoreTokenBufferMemory(ConversationTokenBufferMemory):
|
|
|
109
109
|
previous_history_template: str = DEFAULT_HISTORY_TEMPLATE
|
|
110
110
|
split_chunk_size: int = 1000
|
|
111
111
|
|
|
112
|
-
_memory_retriever: VectorStoreRetrieverMemory = PrivateAttr(default=None) # type: ignore
|
|
113
|
-
_timestamps:
|
|
112
|
+
_memory_retriever: VectorStoreRetrieverMemory = PrivateAttr(default=None) # type: ignore[assignment]
|
|
113
|
+
_timestamps: list[datetime] = PrivateAttr(default_factory=list)
|
|
114
114
|
|
|
115
115
|
@property
|
|
116
116
|
def memory_retriever(self) -> VectorStoreRetrieverMemory:
|
|
@@ -120,7 +120,7 @@ class ConversationVectorStoreTokenBufferMemory(ConversationTokenBufferMemory):
|
|
|
120
120
|
self._memory_retriever = VectorStoreRetrieverMemory(retriever=self.retriever)
|
|
121
121
|
return self._memory_retriever
|
|
122
122
|
|
|
123
|
-
def load_memory_variables(self, inputs:
|
|
123
|
+
def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]:
|
|
124
124
|
"""Return history and memory buffer."""
|
|
125
125
|
try:
|
|
126
126
|
with warnings.catch_warnings():
|
|
@@ -142,7 +142,7 @@ class ConversationVectorStoreTokenBufferMemory(ConversationTokenBufferMemory):
|
|
|
142
142
|
messages.extend(current_history[self.memory_key])
|
|
143
143
|
return {self.memory_key: messages}
|
|
144
144
|
|
|
145
|
-
def save_context(self, inputs:
|
|
145
|
+
def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
|
|
146
146
|
"""Save context from this conversation to buffer. Pruned."""
|
|
147
147
|
BaseChatMemory.save_context(self, inputs, outputs)
|
|
148
148
|
self._timestamps.append(datetime.now().astimezone())
|
|
@@ -166,7 +166,7 @@ class ConversationVectorStoreTokenBufferMemory(ConversationTokenBufferMemory):
|
|
|
166
166
|
while len(buffer) > 0:
|
|
167
167
|
self._pop_and_store_interaction(buffer)
|
|
168
168
|
|
|
169
|
-
def _pop_and_store_interaction(self, buffer:
|
|
169
|
+
def _pop_and_store_interaction(self, buffer: list[BaseMessage]) -> None:
|
|
170
170
|
input = buffer.pop(0)
|
|
171
171
|
output = buffer.pop(0)
|
|
172
172
|
timestamp = self._timestamps.pop(0).strftime(TIMESTAMP_FORMAT)
|
|
@@ -179,6 +179,6 @@ class ConversationVectorStoreTokenBufferMemory(ConversationTokenBufferMemory):
|
|
|
179
179
|
{"AI": f"<{timestamp}/{index:02}> {chunk}"},
|
|
180
180
|
)
|
|
181
181
|
|
|
182
|
-
def _split_long_ai_text(self, text: str) ->
|
|
182
|
+
def _split_long_ai_text(self, text: str) -> list[str]:
|
|
183
183
|
splitter = RecursiveCharacterTextSplitter(chunk_size=self.split_chunk_size)
|
|
184
184
|
return [chunk.page_content for chunk in splitter.create_documents([text])]
|
langchain/model_laboratory.py
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import Optional
|
|
6
7
|
|
|
7
8
|
from langchain_core.language_models.llms import BaseLLM
|
|
8
9
|
from langchain_core.prompts.prompt import PromptTemplate
|
|
@@ -15,7 +16,7 @@ from langchain.chains.llm import LLMChain
|
|
|
15
16
|
class ModelLaboratory:
|
|
16
17
|
"""A utility to experiment with and compare the performance of different models."""
|
|
17
18
|
|
|
18
|
-
def __init__(self, chains: Sequence[Chain], names: Optional[
|
|
19
|
+
def __init__(self, chains: Sequence[Chain], names: Optional[list[str]] = None):
|
|
19
20
|
"""Initialize the ModelLaboratory with chains to experiment with.
|
|
20
21
|
|
|
21
22
|
Args:
|
|
@@ -58,7 +59,7 @@ class ModelLaboratory:
|
|
|
58
59
|
|
|
59
60
|
@classmethod
|
|
60
61
|
def from_llms(
|
|
61
|
-
cls, llms:
|
|
62
|
+
cls, llms: list[BaseLLM], prompt: Optional[PromptTemplate] = None
|
|
62
63
|
) -> ModelLaboratory:
|
|
63
64
|
"""Initialize the ModelLaboratory with LLMs and an optional prompt.
|
|
64
65
|
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import Any
|
|
3
|
+
from typing import Any
|
|
4
4
|
|
|
5
5
|
from langchain_core.output_parsers import BaseOutputParser
|
|
6
6
|
from langchain_core.utils import pre_init
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
class CombiningOutputParser(BaseOutputParser[
|
|
9
|
+
class CombiningOutputParser(BaseOutputParser[dict[str, Any]]):
|
|
10
10
|
"""Combine multiple output parsers into one."""
|
|
11
11
|
|
|
12
|
-
parsers:
|
|
12
|
+
parsers: list[BaseOutputParser]
|
|
13
13
|
|
|
14
14
|
@classmethod
|
|
15
15
|
def is_lc_serializable(cls) -> bool:
|
|
16
16
|
return True
|
|
17
17
|
|
|
18
18
|
@pre_init
|
|
19
|
-
def validate_parsers(cls, values:
|
|
19
|
+
def validate_parsers(cls, values: dict[str, Any]) -> dict[str, Any]:
|
|
20
20
|
"""Validate the parsers."""
|
|
21
21
|
parsers = values["parsers"]
|
|
22
22
|
if len(parsers) < 2:
|
|
@@ -43,7 +43,7 @@ class CombiningOutputParser(BaseOutputParser[Dict[str, Any]]):
|
|
|
43
43
|
)
|
|
44
44
|
return f"{initial}\n{subsequent}"
|
|
45
45
|
|
|
46
|
-
def parse(self, text: str) ->
|
|
46
|
+
def parse(self, text: str) -> dict[str, Any]:
|
|
47
47
|
"""Parse the output of an LLM call."""
|
|
48
48
|
texts = text.split("\n\n")
|
|
49
49
|
output = dict()
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import random
|
|
2
2
|
from datetime import datetime, timedelta
|
|
3
|
-
from typing import List
|
|
4
3
|
|
|
5
4
|
from langchain_core.exceptions import OutputParserException
|
|
6
5
|
from langchain_core.output_parsers import BaseOutputParser
|
|
@@ -12,7 +11,7 @@ def _generate_random_datetime_strings(
|
|
|
12
11
|
n: int = 3,
|
|
13
12
|
start_date: datetime = datetime(1, 1, 1),
|
|
14
13
|
end_date: datetime = datetime.now() + timedelta(days=3650),
|
|
15
|
-
) ->
|
|
14
|
+
) -> list[str]:
|
|
16
15
|
"""Generates n random datetime strings conforming to the
|
|
17
16
|
given pattern within the specified date range.
|
|
18
17
|
|
langchain/output_parsers/enum.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
|
-
from typing import Dict, List, Type
|
|
3
2
|
|
|
4
3
|
from langchain_core.exceptions import OutputParserException
|
|
5
4
|
from langchain_core.output_parsers import BaseOutputParser
|
|
@@ -9,18 +8,18 @@ from langchain_core.utils import pre_init
|
|
|
9
8
|
class EnumOutputParser(BaseOutputParser[Enum]):
|
|
10
9
|
"""Parse an output that is one of a set of values."""
|
|
11
10
|
|
|
12
|
-
enum:
|
|
11
|
+
enum: type[Enum]
|
|
13
12
|
"""The enum to parse. Its values must be strings."""
|
|
14
13
|
|
|
15
14
|
@pre_init
|
|
16
|
-
def raise_deprecation(cls, values:
|
|
15
|
+
def raise_deprecation(cls, values: dict) -> dict:
|
|
17
16
|
enum = values["enum"]
|
|
18
17
|
if not all(isinstance(e.value, str) for e in enum):
|
|
19
18
|
raise ValueError("Enum values must be strings")
|
|
20
19
|
return values
|
|
21
20
|
|
|
22
21
|
@property
|
|
23
|
-
def _valid_values(self) ->
|
|
22
|
+
def _valid_values(self) -> list[str]:
|
|
24
23
|
return [e.value for e in self.enum]
|
|
25
24
|
|
|
26
25
|
def parse(self, response: str) -> Enum:
|
|
@@ -36,5 +35,5 @@ class EnumOutputParser(BaseOutputParser[Enum]):
|
|
|
36
35
|
return f"Select one of the following options: {', '.join(self._valid_values)}"
|
|
37
36
|
|
|
38
37
|
@property
|
|
39
|
-
def OutputType(self) ->
|
|
38
|
+
def OutputType(self) -> type[Enum]:
|
|
40
39
|
return self.enum
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import re
|
|
2
|
-
from typing import Any,
|
|
2
|
+
from typing import Any, Union
|
|
3
3
|
|
|
4
4
|
from langchain_core.exceptions import OutputParserException
|
|
5
5
|
from langchain_core.output_parsers.base import BaseOutputParser
|
|
@@ -10,7 +10,7 @@ from langchain.output_parsers.format_instructions import (
|
|
|
10
10
|
)
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
class PandasDataFrameOutputParser(BaseOutputParser[
|
|
13
|
+
class PandasDataFrameOutputParser(BaseOutputParser[dict[str, Any]]):
|
|
14
14
|
"""Parse an output using Pandas DataFrame format."""
|
|
15
15
|
|
|
16
16
|
"""The Pandas DataFrame to parse."""
|
|
@@ -33,8 +33,8 @@ class PandasDataFrameOutputParser(BaseOutputParser[Dict[str, Any]]):
|
|
|
33
33
|
|
|
34
34
|
def parse_array(
|
|
35
35
|
self, array: str, original_request_params: str
|
|
36
|
-
) ->
|
|
37
|
-
parsed_array:
|
|
36
|
+
) -> tuple[list[Union[int, str]], str]:
|
|
37
|
+
parsed_array: list[Union[int, str]] = []
|
|
38
38
|
|
|
39
39
|
# Check if the format is [1,3,5]
|
|
40
40
|
if re.match(r"\[\d+(,\s*\d+)*\]", array):
|
|
@@ -78,7 +78,7 @@ class PandasDataFrameOutputParser(BaseOutputParser[Dict[str, Any]]):
|
|
|
78
78
|
|
|
79
79
|
return parsed_array, original_request_params.split("[")[0]
|
|
80
80
|
|
|
81
|
-
def parse(self, request: str) ->
|
|
81
|
+
def parse(self, request: str) -> dict[str, Any]:
|
|
82
82
|
stripped_request_params = None
|
|
83
83
|
splitted_request = request.strip().split(":")
|
|
84
84
|
if len(splitted_request) != 2:
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import re
|
|
4
|
-
from typing import
|
|
4
|
+
from typing import Optional
|
|
5
5
|
|
|
6
6
|
from langchain_core.output_parsers import BaseOutputParser
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
class RegexParser(BaseOutputParser[
|
|
9
|
+
class RegexParser(BaseOutputParser[dict[str, str]]):
|
|
10
10
|
"""Parse the output of an LLM call using a regex."""
|
|
11
11
|
|
|
12
12
|
@classmethod
|
|
@@ -15,7 +15,7 @@ class RegexParser(BaseOutputParser[Dict[str, str]]):
|
|
|
15
15
|
|
|
16
16
|
regex: str
|
|
17
17
|
"""The regex to use to parse the output."""
|
|
18
|
-
output_keys:
|
|
18
|
+
output_keys: list[str]
|
|
19
19
|
"""The keys to use for the output."""
|
|
20
20
|
default_output_key: Optional[str] = None
|
|
21
21
|
"""The default key to use for the output."""
|
|
@@ -25,7 +25,7 @@ class RegexParser(BaseOutputParser[Dict[str, str]]):
|
|
|
25
25
|
"""Return the type key."""
|
|
26
26
|
return "regex_parser"
|
|
27
27
|
|
|
28
|
-
def parse(self, text: str) ->
|
|
28
|
+
def parse(self, text: str) -> dict[str, str]:
|
|
29
29
|
"""Parse the output of an LLM call."""
|
|
30
30
|
match = re.search(self.regex, text)
|
|
31
31
|
if match:
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import re
|
|
4
|
-
from typing import
|
|
4
|
+
from typing import Optional
|
|
5
5
|
|
|
6
6
|
from langchain_core.output_parsers import BaseOutputParser
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
class RegexDictParser(BaseOutputParser[
|
|
9
|
+
class RegexDictParser(BaseOutputParser[dict[str, str]]):
|
|
10
10
|
"""Parse the output of an LLM call into a Dictionary using a regex."""
|
|
11
11
|
|
|
12
12
|
regex_pattern: str = r"{}:\s?([^.'\n']*)\.?" # : :meta private:
|
|
13
13
|
"""The regex pattern to use to parse the output."""
|
|
14
|
-
output_key_to_format:
|
|
14
|
+
output_key_to_format: dict[str, str]
|
|
15
15
|
"""The keys to use for the output."""
|
|
16
16
|
no_update_value: Optional[str] = None
|
|
17
17
|
"""The default key to use for the output."""
|
|
@@ -21,7 +21,7 @@ class RegexDictParser(BaseOutputParser[Dict[str, str]]):
|
|
|
21
21
|
"""Return the type key."""
|
|
22
22
|
return "regex_dict_parser"
|
|
23
23
|
|
|
24
|
-
def parse(self, text: str) ->
|
|
24
|
+
def parse(self, text: str) -> dict[str, str]:
|
|
25
25
|
"""Parse the output of an LLM call."""
|
|
26
26
|
result = {}
|
|
27
27
|
for output_key, expected_format in self.output_key_to_format.items():
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import Any, TypeVar, Union
|
|
3
|
+
from typing import Annotated, Any, TypeVar, Union
|
|
4
4
|
|
|
5
5
|
from langchain_core.exceptions import OutputParserException
|
|
6
6
|
from langchain_core.language_models import BaseLanguageModel
|
|
@@ -9,7 +9,7 @@ from langchain_core.prompt_values import PromptValue
|
|
|
9
9
|
from langchain_core.prompts import BasePromptTemplate, PromptTemplate
|
|
10
10
|
from langchain_core.runnables import RunnableSerializable
|
|
11
11
|
from pydantic import SkipValidation
|
|
12
|
-
from typing_extensions import
|
|
12
|
+
from typing_extensions import TypedDict
|
|
13
13
|
|
|
14
14
|
NAIVE_COMPLETION_RETRY = """Prompt:
|
|
15
15
|
{prompt}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import Any
|
|
3
|
+
from typing import Any
|
|
4
4
|
|
|
5
5
|
from langchain_core.output_parsers import BaseOutputParser
|
|
6
6
|
from langchain_core.output_parsers.json import parse_and_check_json_markdown
|
|
@@ -31,15 +31,15 @@ def _get_sub_string(schema: ResponseSchema) -> str:
|
|
|
31
31
|
)
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
class StructuredOutputParser(BaseOutputParser[
|
|
34
|
+
class StructuredOutputParser(BaseOutputParser[dict[str, Any]]):
|
|
35
35
|
"""Parse the output of an LLM call to a structured output."""
|
|
36
36
|
|
|
37
|
-
response_schemas:
|
|
37
|
+
response_schemas: list[ResponseSchema]
|
|
38
38
|
"""The schemas for the response."""
|
|
39
39
|
|
|
40
40
|
@classmethod
|
|
41
41
|
def from_response_schemas(
|
|
42
|
-
cls, response_schemas:
|
|
42
|
+
cls, response_schemas: list[ResponseSchema]
|
|
43
43
|
) -> StructuredOutputParser:
|
|
44
44
|
return cls(response_schemas=response_schemas)
|
|
45
45
|
|
|
@@ -92,7 +92,7 @@ class StructuredOutputParser(BaseOutputParser[Dict[str, Any]]):
|
|
|
92
92
|
else:
|
|
93
93
|
return STRUCTURED_FORMAT_INSTRUCTIONS.format(format=schema_str)
|
|
94
94
|
|
|
95
|
-
def parse(self, text: str) ->
|
|
95
|
+
def parse(self, text: str) -> dict[str, Any]:
|
|
96
96
|
expected_keys = [rs.name for rs in self.response_schemas]
|
|
97
97
|
return parse_and_check_json_markdown(text, expected_keys)
|
|
98
98
|
|
langchain/output_parsers/yaml.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import re
|
|
3
|
-
from typing import
|
|
3
|
+
from typing import TypeVar
|
|
4
4
|
|
|
5
5
|
import yaml
|
|
6
6
|
from langchain_core.exceptions import OutputParserException
|
|
@@ -15,7 +15,7 @@ T = TypeVar("T", bound=BaseModel)
|
|
|
15
15
|
class YamlOutputParser(BaseOutputParser[T]):
|
|
16
16
|
"""Parse YAML output using a pydantic model."""
|
|
17
17
|
|
|
18
|
-
pydantic_object:
|
|
18
|
+
pydantic_object: type[T]
|
|
19
19
|
"""The pydantic model to parse."""
|
|
20
20
|
pattern: re.Pattern = re.compile(
|
|
21
21
|
r"^```(?:ya?ml)?(?P<yaml>[^`]*)", re.MULTILINE | re.DOTALL
|
|
@@ -65,5 +65,5 @@ class YamlOutputParser(BaseOutputParser[T]):
|
|
|
65
65
|
return "yaml"
|
|
66
66
|
|
|
67
67
|
@property
|
|
68
|
-
def OutputType(self) ->
|
|
68
|
+
def OutputType(self) -> type[T]:
|
|
69
69
|
return self.pydantic_object
|
|
@@ -12,12 +12,7 @@ from langchain_core._api import warn_deprecated
|
|
|
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.
|
|
14
14
|
# * This change is easier to roll out and roll back.
|
|
15
|
-
|
|
16
|
-
try:
|
|
17
|
-
from pydantic.v1 import * # noqa: F403
|
|
18
|
-
except ImportError:
|
|
19
|
-
from pydantic import * # type: ignore # noqa: F403
|
|
20
|
-
|
|
15
|
+
from pydantic.v1 import * # noqa: F403
|
|
21
16
|
|
|
22
17
|
try:
|
|
23
18
|
_PYDANTIC_MAJOR_VERSION: int = int(metadata.version("pydantic").split(".")[0])
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
from langchain_core._api import warn_deprecated
|
|
2
|
-
|
|
3
|
-
try:
|
|
4
|
-
from pydantic.v1.dataclasses import * # noqa: F403
|
|
5
|
-
except ImportError:
|
|
6
|
-
from pydantic.dataclasses import * # type: ignore # noqa: F403
|
|
2
|
+
from pydantic.v1.dataclasses import * # noqa: F403
|
|
7
3
|
|
|
8
4
|
warn_deprecated(
|
|
9
5
|
"0.3.0",
|
langchain/pydantic_v1/main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Any
|
|
1
|
+
from typing import Any
|
|
2
2
|
|
|
3
3
|
from langchain_core.callbacks import (
|
|
4
4
|
AsyncCallbackManagerForRetrieverRun,
|
|
@@ -32,7 +32,7 @@ class ContextualCompressionRetriever(BaseRetriever):
|
|
|
32
32
|
*,
|
|
33
33
|
run_manager: CallbackManagerForRetrieverRun,
|
|
34
34
|
**kwargs: Any,
|
|
35
|
-
) ->
|
|
35
|
+
) -> list[Document]:
|
|
36
36
|
"""Get documents relevant for a query.
|
|
37
37
|
|
|
38
38
|
Args:
|
|
@@ -58,7 +58,7 @@ class ContextualCompressionRetriever(BaseRetriever):
|
|
|
58
58
|
*,
|
|
59
59
|
run_manager: AsyncCallbackManagerForRetrieverRun,
|
|
60
60
|
**kwargs: Any,
|
|
61
|
-
) ->
|
|
61
|
+
) -> list[Document]:
|
|
62
62
|
"""Get documents relevant for a query.
|
|
63
63
|
|
|
64
64
|
Args:
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
from collections.abc import Sequence
|
|
1
2
|
from inspect import signature
|
|
2
|
-
from typing import
|
|
3
|
+
from typing import Optional, Union
|
|
3
4
|
|
|
4
5
|
from langchain_core.callbacks.manager import Callbacks
|
|
5
6
|
from langchain_core.documents import (
|
|
@@ -13,7 +14,7 @@ from pydantic import ConfigDict
|
|
|
13
14
|
class DocumentCompressorPipeline(BaseDocumentCompressor):
|
|
14
15
|
"""Document compressor that uses a pipeline of Transformers."""
|
|
15
16
|
|
|
16
|
-
transformers:
|
|
17
|
+
transformers: list[Union[BaseDocumentTransformer, BaseDocumentCompressor]]
|
|
17
18
|
"""List of document filters that are chained together and run in sequence."""
|
|
18
19
|
|
|
19
20
|
model_config = ConfigDict(
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import Any, Callable, Optional, cast
|
|
6
7
|
|
|
7
8
|
from langchain_core.callbacks.manager import Callbacks
|
|
8
9
|
from langchain_core.documents import Document
|
|
@@ -19,7 +20,7 @@ from langchain.retrievers.document_compressors.chain_extract_prompt import (
|
|
|
19
20
|
)
|
|
20
21
|
|
|
21
22
|
|
|
22
|
-
def default_get_input(query: str, doc: Document) ->
|
|
23
|
+
def default_get_input(query: str, doc: Document) -> dict[str, Any]:
|
|
23
24
|
"""Return the compression chain input."""
|
|
24
25
|
return {"question": query, "context": doc.page_content}
|
|
25
26
|
|
|
@@ -98,7 +99,7 @@ class LLMChainExtractor(BaseDocumentCompressor):
|
|
|
98
99
|
if len(outputs[i]) == 0:
|
|
99
100
|
continue
|
|
100
101
|
compressed_docs.append(
|
|
101
|
-
Document(page_content=outputs[i], metadata=doc.metadata)
|
|
102
|
+
Document(page_content=outputs[i], metadata=doc.metadata)
|
|
102
103
|
)
|
|
103
104
|
return compressed_docs
|
|
104
105
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Filter that uses an LLM to drop documents that aren't relevant to the query."""
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any, Callable, Optional
|
|
4
5
|
|
|
5
6
|
from langchain_core.callbacks.manager import Callbacks
|
|
6
7
|
from langchain_core.documents import Document
|
|
@@ -27,7 +28,7 @@ def _get_default_chain_prompt() -> PromptTemplate:
|
|
|
27
28
|
)
|
|
28
29
|
|
|
29
30
|
|
|
30
|
-
def default_get_input(query: str, doc: Document) ->
|
|
31
|
+
def default_get_input(query: str, doc: Document) -> dict[str, Any]:
|
|
31
32
|
"""Return the compression chain input."""
|
|
32
33
|
return {"question": query, "context": doc.page_content}
|
|
33
34
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from collections.abc import Sequence
|
|
3
4
|
from copy import deepcopy
|
|
4
|
-
from typing import Any,
|
|
5
|
+
from typing import Any, Optional, Union
|
|
5
6
|
|
|
6
7
|
from langchain_core._api.deprecation import deprecated
|
|
7
8
|
from langchain_core.callbacks.manager import Callbacks
|
|
@@ -37,7 +38,7 @@ class CohereRerank(BaseDocumentCompressor):
|
|
|
37
38
|
|
|
38
39
|
@model_validator(mode="before")
|
|
39
40
|
@classmethod
|
|
40
|
-
def validate_environment(cls, values:
|
|
41
|
+
def validate_environment(cls, values: dict) -> Any:
|
|
41
42
|
"""Validate that api key and python package exists in environment."""
|
|
42
43
|
if not values.get("client"):
|
|
43
44
|
try:
|
|
@@ -62,7 +63,7 @@ class CohereRerank(BaseDocumentCompressor):
|
|
|
62
63
|
model: Optional[str] = None,
|
|
63
64
|
top_n: Optional[int] = -1,
|
|
64
65
|
max_chunks_per_doc: Optional[int] = None,
|
|
65
|
-
) ->
|
|
66
|
+
) -> list[dict[str, Any]]:
|
|
66
67
|
"""Returns an ordered list of documents ordered by their relevance to the provided query.
|
|
67
68
|
|
|
68
69
|
Args:
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
|
-
from typing import List, Tuple
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
class BaseCrossEncoder(ABC):
|
|
6
5
|
"""Interface for cross encoder models."""
|
|
7
6
|
|
|
8
7
|
@abstractmethod
|
|
9
|
-
def score(self, text_pairs:
|
|
8
|
+
def score(self, text_pairs: list[tuple[str, str]]) -> list[float]:
|
|
10
9
|
"""Score pairs' similarity.
|
|
11
10
|
|
|
12
11
|
Args:
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import operator
|
|
4
|
-
from
|
|
4
|
+
from collections.abc import Sequence
|
|
5
|
+
from typing import Optional
|
|
5
6
|
|
|
6
7
|
from langchain_core.callbacks import Callbacks
|
|
7
8
|
from langchain_core.documents import BaseDocumentCompressor, Document
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
from
|
|
1
|
+
from collections.abc import Sequence
|
|
2
|
+
from typing import Callable, Optional
|
|
2
3
|
|
|
3
4
|
from langchain_core.callbacks.manager import Callbacks
|
|
4
5
|
from langchain_core.documents import Document
|
|
@@ -45,7 +46,7 @@ class EmbeddingsFilter(BaseDocumentCompressor):
|
|
|
45
46
|
)
|
|
46
47
|
|
|
47
48
|
@pre_init
|
|
48
|
-
def validate_params(cls, values:
|
|
49
|
+
def validate_params(cls, values: dict) -> dict:
|
|
49
50
|
"""Validate similarity parameters."""
|
|
50
51
|
if values["k"] is None and values["similarity_threshold"] is None:
|
|
51
52
|
raise ValueError("Must specify one of `k` or `similarity_threshold`.")
|