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
|
@@ -11,7 +11,7 @@ from langchain_core.documents import Document
|
|
|
11
11
|
from langchain_core.runnables.config import RunnableConfig
|
|
12
12
|
from langchain_core.utils.pydantic import create_model
|
|
13
13
|
from pydantic import BaseModel, ConfigDict, model_validator
|
|
14
|
-
from typing_extensions import Self
|
|
14
|
+
from typing_extensions import Self, override
|
|
15
15
|
|
|
16
16
|
from langchain.chains.combine_documents.base import BaseCombineDocumentsChain
|
|
17
17
|
from langchain.chains.llm import LLMChain
|
|
@@ -31,8 +31,8 @@ class MapRerankDocumentsChain(BaseCombineDocumentsChain):
|
|
|
31
31
|
"""Combining documents by mapping a chain over them, then reranking results.
|
|
32
32
|
|
|
33
33
|
This algorithm calls an LLMChain on each input document. The LLMChain is expected
|
|
34
|
-
to have an OutputParser that parses the result into both an answer (
|
|
35
|
-
and a score (
|
|
34
|
+
to have an OutputParser that parses the result into both an answer (``answer_key``)
|
|
35
|
+
and a score (``rank_key``). The answer with the highest score is then returned.
|
|
36
36
|
|
|
37
37
|
Example:
|
|
38
38
|
.. code-block:: python
|
|
@@ -69,6 +69,7 @@ class MapRerankDocumentsChain(BaseCombineDocumentsChain):
|
|
|
69
69
|
rank_key="score",
|
|
70
70
|
answer_key="answer",
|
|
71
71
|
)
|
|
72
|
+
|
|
72
73
|
"""
|
|
73
74
|
|
|
74
75
|
llm_chain: LLMChain
|
|
@@ -91,6 +92,7 @@ class MapRerankDocumentsChain(BaseCombineDocumentsChain):
|
|
|
91
92
|
extra="forbid",
|
|
92
93
|
)
|
|
93
94
|
|
|
95
|
+
@override
|
|
94
96
|
def get_output_schema(
|
|
95
97
|
self,
|
|
96
98
|
config: Optional[RunnableConfig] = None,
|
|
@@ -228,7 +230,7 @@ class MapRerankDocumentsChain(BaseCombineDocumentsChain):
|
|
|
228
230
|
docs: list[Document],
|
|
229
231
|
results: Sequence[Union[str, list[str], dict[str, str]]],
|
|
230
232
|
) -> tuple[str, dict]:
|
|
231
|
-
typed_results = cast(list[dict], results)
|
|
233
|
+
typed_results = cast("list[dict]", results)
|
|
232
234
|
sorted_res = sorted(
|
|
233
235
|
zip(typed_results, docs),
|
|
234
236
|
key=lambda x: -int(x[0][self.rank_key]),
|
|
@@ -10,6 +10,7 @@ from langchain_core.output_parsers import BaseOutputParser, StrOutputParser
|
|
|
10
10
|
from langchain_core.prompts import BasePromptTemplate, format_document
|
|
11
11
|
from langchain_core.runnables import Runnable, RunnablePassthrough
|
|
12
12
|
from pydantic import ConfigDict, Field, model_validator
|
|
13
|
+
from typing_extensions import override
|
|
13
14
|
|
|
14
15
|
from langchain.chains.combine_documents.base import (
|
|
15
16
|
DEFAULT_DOCUMENT_PROMPT,
|
|
@@ -74,6 +75,7 @@ def create_stuff_documents_chain(
|
|
|
74
75
|
]
|
|
75
76
|
|
|
76
77
|
chain.invoke({"context": docs})
|
|
78
|
+
|
|
77
79
|
""" # noqa: E501
|
|
78
80
|
|
|
79
81
|
_validate_prompt(prompt, document_variable_name)
|
|
@@ -141,6 +143,7 @@ class StuffDocumentsChain(BaseCombineDocumentsChain):
|
|
|
141
143
|
document_prompt=document_prompt,
|
|
142
144
|
document_variable_name=document_variable_name
|
|
143
145
|
)
|
|
146
|
+
|
|
144
147
|
"""
|
|
145
148
|
|
|
146
149
|
llm_chain: LLMChain
|
|
@@ -189,6 +192,7 @@ class StuffDocumentsChain(BaseCombineDocumentsChain):
|
|
|
189
192
|
return values
|
|
190
193
|
|
|
191
194
|
@property
|
|
195
|
+
@override
|
|
192
196
|
def input_keys(self) -> list[str]:
|
|
193
197
|
extra_keys = [
|
|
194
198
|
k for k in self.llm_chain.input_keys if k != self.document_variable_name
|
|
@@ -239,7 +243,7 @@ class StuffDocumentsChain(BaseCombineDocumentsChain):
|
|
|
239
243
|
"""
|
|
240
244
|
inputs = self._get_inputs(docs, **kwargs)
|
|
241
245
|
prompt = self.llm_chain.prompt.format(**inputs)
|
|
242
|
-
return self.llm_chain._get_num_tokens(prompt)
|
|
246
|
+
return self.llm_chain._get_num_tokens(prompt) # noqa: SLF001
|
|
243
247
|
|
|
244
248
|
def combine_docs(
|
|
245
249
|
self,
|
|
@@ -187,6 +187,7 @@ class ConstitutionalChain(Chain):
|
|
|
187
187
|
)
|
|
188
188
|
|
|
189
189
|
constitutional_chain.run(question="What is the meaning of life?")
|
|
190
|
+
|
|
190
191
|
""" # noqa: E501
|
|
191
192
|
|
|
192
193
|
chain: LLMChain
|
|
@@ -200,6 +201,12 @@ class ConstitutionalChain(Chain):
|
|
|
200
201
|
cls,
|
|
201
202
|
names: Optional[list[str]] = None,
|
|
202
203
|
) -> list[ConstitutionalPrinciple]:
|
|
204
|
+
"""Get constitutional principles by name.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
names: List of names of constitutional principles to retrieve.
|
|
208
|
+
If None (default), all principles are returned.
|
|
209
|
+
"""
|
|
203
210
|
if names is None:
|
|
204
211
|
return list(PRINCIPLES.values())
|
|
205
212
|
return [PRINCIPLES[name] for name in names]
|
|
@@ -4,7 +4,7 @@ from langchain_core._api import deprecated
|
|
|
4
4
|
from langchain_core.memory import BaseMemory
|
|
5
5
|
from langchain_core.prompts import BasePromptTemplate
|
|
6
6
|
from pydantic import ConfigDict, Field, model_validator
|
|
7
|
-
from typing_extensions import Self
|
|
7
|
+
from typing_extensions import Self, override
|
|
8
8
|
|
|
9
9
|
from langchain.chains.conversation.prompt import PROMPT
|
|
10
10
|
from langchain.chains.llm import LLMChain
|
|
@@ -53,6 +53,7 @@ class ConversationChain(LLMChain):
|
|
|
53
53
|
"Hi I'm Bob.",
|
|
54
54
|
config={"configurable": {"session_id": "1"}},
|
|
55
55
|
) # session_id determines thread
|
|
56
|
+
|
|
56
57
|
Memory objects can also be incorporated into the ``get_session_history`` callable:
|
|
57
58
|
|
|
58
59
|
.. code-block:: python
|
|
@@ -96,6 +97,7 @@ class ConversationChain(LLMChain):
|
|
|
96
97
|
from langchain_community.llms import OpenAI
|
|
97
98
|
|
|
98
99
|
conversation = ConversationChain(llm=OpenAI())
|
|
100
|
+
|
|
99
101
|
"""
|
|
100
102
|
|
|
101
103
|
memory: BaseMemory = Field(default_factory=ConversationBufferMemory)
|
|
@@ -112,6 +114,7 @@ class ConversationChain(LLMChain):
|
|
|
112
114
|
)
|
|
113
115
|
|
|
114
116
|
@classmethod
|
|
117
|
+
@override
|
|
115
118
|
def is_lc_serializable(cls) -> bool:
|
|
116
119
|
return False
|
|
117
120
|
|
|
@@ -22,6 +22,7 @@ from langchain_core.retrievers import BaseRetriever
|
|
|
22
22
|
from langchain_core.runnables import RunnableConfig
|
|
23
23
|
from langchain_core.vectorstores import VectorStore
|
|
24
24
|
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
25
|
+
from typing_extensions import override
|
|
25
26
|
|
|
26
27
|
from langchain.chains.base import Chain
|
|
27
28
|
from langchain.chains.combine_documents.base import BaseCombineDocumentsChain
|
|
@@ -109,6 +110,7 @@ class BaseConversationalRetrievalChain(Chain):
|
|
|
109
110
|
"""Input keys."""
|
|
110
111
|
return ["question", "chat_history"]
|
|
111
112
|
|
|
113
|
+
@override
|
|
112
114
|
def get_input_schema(
|
|
113
115
|
self,
|
|
114
116
|
config: Optional[RunnableConfig] = None,
|
|
@@ -242,6 +244,7 @@ class BaseConversationalRetrievalChain(Chain):
|
|
|
242
244
|
output["generated_question"] = new_question
|
|
243
245
|
return output
|
|
244
246
|
|
|
247
|
+
@override
|
|
245
248
|
def save(self, file_path: Union[Path, str]) -> None:
|
|
246
249
|
if self.get_chat_history:
|
|
247
250
|
msg = "Chain not saveable when `get_chat_history` is not None."
|
|
@@ -264,68 +267,67 @@ class ConversationalRetrievalChain(BaseConversationalRetrievalChain):
|
|
|
264
267
|
`create_retrieval_chain`. Additional walkthroughs can be found at
|
|
265
268
|
https://python.langchain.com/docs/use_cases/question_answering/chat_history
|
|
266
269
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
from langchain.chains import (
|
|
270
|
-
create_history_aware_retriever,
|
|
271
|
-
create_retrieval_chain,
|
|
272
|
-
)
|
|
273
|
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
|
274
|
-
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
|
275
|
-
from langchain_openai import ChatOpenAI
|
|
270
|
+
.. code-block:: python
|
|
276
271
|
|
|
272
|
+
from langchain.chains import (
|
|
273
|
+
create_history_aware_retriever,
|
|
274
|
+
create_retrieval_chain,
|
|
275
|
+
)
|
|
276
|
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
|
277
|
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
|
278
|
+
from langchain_openai import ChatOpenAI
|
|
277
279
|
|
|
278
|
-
|
|
280
|
+
retriever = ... # Your retriever
|
|
279
281
|
|
|
280
|
-
|
|
282
|
+
llm = ChatOpenAI()
|
|
281
283
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
284
|
+
# Contextualize question
|
|
285
|
+
contextualize_q_system_prompt = (
|
|
286
|
+
"Given a chat history and the latest user question "
|
|
287
|
+
"which might reference context in the chat history, "
|
|
288
|
+
"formulate a standalone question which can be understood "
|
|
289
|
+
"without the chat history. Do NOT answer the question, just "
|
|
290
|
+
"reformulate it if needed and otherwise return it as is."
|
|
291
|
+
)
|
|
292
|
+
contextualize_q_prompt = ChatPromptTemplate.from_messages(
|
|
293
|
+
[
|
|
294
|
+
("system", contextualize_q_system_prompt),
|
|
295
|
+
MessagesPlaceholder("chat_history"),
|
|
296
|
+
("human", "{input}"),
|
|
297
|
+
]
|
|
298
|
+
)
|
|
299
|
+
history_aware_retriever = create_history_aware_retriever(
|
|
300
|
+
llm, retriever, contextualize_q_prompt
|
|
301
|
+
)
|
|
300
302
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
303
|
+
# Answer question
|
|
304
|
+
qa_system_prompt = (
|
|
305
|
+
"You are an assistant for question-answering tasks. Use "
|
|
306
|
+
"the following pieces of retrieved context to answer the "
|
|
307
|
+
"question. If you don't know the answer, just say that you "
|
|
308
|
+
"don't know. Use three sentences maximum and keep the answer "
|
|
309
|
+
"concise."
|
|
310
|
+
"\n\n"
|
|
311
|
+
"{context}"
|
|
312
|
+
)
|
|
313
|
+
qa_prompt = ChatPromptTemplate.from_messages(
|
|
314
|
+
[
|
|
315
|
+
("system", qa_system_prompt),
|
|
316
|
+
MessagesPlaceholder("chat_history"),
|
|
317
|
+
("human", "{input}"),
|
|
318
|
+
]
|
|
319
|
+
)
|
|
320
|
+
# Below we use create_stuff_documents_chain to feed all retrieved context
|
|
321
|
+
# into the LLM. Note that we can also use StuffDocumentsChain and other
|
|
322
|
+
# instances of BaseCombineDocumentsChain.
|
|
323
|
+
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
|
|
324
|
+
rag_chain = create_retrieval_chain(
|
|
325
|
+
history_aware_retriever, question_answer_chain
|
|
326
|
+
)
|
|
325
327
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
328
|
+
# Usage:
|
|
329
|
+
chat_history = [] # Collect chat history here (a sequence of messages)
|
|
330
|
+
rag_chain.invoke({"input": query, "chat_history": chat_history})
|
|
329
331
|
|
|
330
332
|
This chain takes in chat history (a list of messages) and new questions,
|
|
331
333
|
and then returns an answer to that question.
|
|
@@ -372,13 +374,16 @@ class ConversationalRetrievalChain(BaseConversationalRetrievalChain):
|
|
|
372
374
|
retriever=retriever,
|
|
373
375
|
question_generator=question_generator_chain,
|
|
374
376
|
)
|
|
377
|
+
|
|
375
378
|
"""
|
|
376
379
|
|
|
377
380
|
retriever: BaseRetriever
|
|
378
381
|
"""Retriever to use to fetch documents."""
|
|
379
382
|
max_tokens_limit: Optional[int] = None
|
|
380
383
|
"""If set, enforces that the documents returned are less than this limit.
|
|
381
|
-
|
|
384
|
+
|
|
385
|
+
This is only enforced if ``combine_docs_chain`` is of type StuffDocumentsChain.
|
|
386
|
+
"""
|
|
382
387
|
|
|
383
388
|
def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
|
|
384
389
|
num_docs = len(docs)
|
|
@@ -388,7 +393,7 @@ class ConversationalRetrievalChain(BaseConversationalRetrievalChain):
|
|
|
388
393
|
StuffDocumentsChain,
|
|
389
394
|
):
|
|
390
395
|
tokens = [
|
|
391
|
-
self.combine_docs_chain.llm_chain._get_num_tokens(doc.page_content)
|
|
396
|
+
self.combine_docs_chain.llm_chain._get_num_tokens(doc.page_content) # noqa: SLF001
|
|
392
397
|
for doc in docs
|
|
393
398
|
]
|
|
394
399
|
token_count = sum(tokens[:num_docs])
|
|
@@ -398,6 +403,7 @@ class ConversationalRetrievalChain(BaseConversationalRetrievalChain):
|
|
|
398
403
|
|
|
399
404
|
return docs[:num_docs]
|
|
400
405
|
|
|
406
|
+
@override
|
|
401
407
|
def _get_docs(
|
|
402
408
|
self,
|
|
403
409
|
question: str,
|
|
@@ -412,6 +418,7 @@ class ConversationalRetrievalChain(BaseConversationalRetrievalChain):
|
|
|
412
418
|
)
|
|
413
419
|
return self._reduce_tokens_below_limit(docs)
|
|
414
420
|
|
|
421
|
+
@override
|
|
415
422
|
async def _aget_docs(
|
|
416
423
|
self,
|
|
417
424
|
question: str,
|
|
@@ -500,7 +507,7 @@ class ChatVectorDBChain(BaseConversationalRetrievalChain):
|
|
|
500
507
|
|
|
501
508
|
@model_validator(mode="before")
|
|
502
509
|
@classmethod
|
|
503
|
-
def
|
|
510
|
+
def _raise_deprecation(cls, values: dict) -> Any:
|
|
504
511
|
warnings.warn(
|
|
505
512
|
"`ChatVectorDBChain` is deprecated - "
|
|
506
513
|
"please use `from langchain.chains import ConversationalRetrievalChain`",
|
|
@@ -508,6 +515,7 @@ class ChatVectorDBChain(BaseConversationalRetrievalChain):
|
|
|
508
515
|
)
|
|
509
516
|
return values
|
|
510
517
|
|
|
518
|
+
@override
|
|
511
519
|
def _get_docs(
|
|
512
520
|
self,
|
|
513
521
|
question: str,
|
|
@@ -34,6 +34,7 @@ class ElasticsearchDatabaseChain(Chain):
|
|
|
34
34
|
|
|
35
35
|
database = Elasticsearch("http://localhost:9200")
|
|
36
36
|
db_chain = ElasticsearchDatabaseChain.from_llm(OpenAI(), database)
|
|
37
|
+
|
|
37
38
|
"""
|
|
38
39
|
|
|
39
40
|
query_chain: Runnable
|
|
@@ -58,7 +59,7 @@ class ElasticsearchDatabaseChain(Chain):
|
|
|
58
59
|
)
|
|
59
60
|
|
|
60
61
|
@model_validator(mode="after")
|
|
61
|
-
def
|
|
62
|
+
def _validate_indices(self) -> Self:
|
|
62
63
|
if self.include_indices and self.ignore_indices:
|
|
63
64
|
msg = "Cannot specify both 'include_indices' and 'ignore_indices'."
|
|
64
65
|
raise ValueError(msg)
|
langchain/chains/flare/base.py
CHANGED
|
@@ -15,6 +15,7 @@ from langchain_core.prompts import BasePromptTemplate
|
|
|
15
15
|
from langchain_core.retrievers import BaseRetriever
|
|
16
16
|
from langchain_core.runnables import Runnable
|
|
17
17
|
from pydantic import Field
|
|
18
|
+
from typing_extensions import override
|
|
18
19
|
|
|
19
20
|
from langchain.chains.base import Chain
|
|
20
21
|
from langchain.chains.flare.prompts import (
|
|
@@ -44,6 +45,7 @@ class QuestionGeneratorChain(LLMChain):
|
|
|
44
45
|
"""Prompt template for the chain."""
|
|
45
46
|
|
|
46
47
|
@classmethod
|
|
48
|
+
@override
|
|
47
49
|
def is_lc_serializable(cls) -> bool:
|
|
48
50
|
return False
|
|
49
51
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from langchain_core.output_parsers import BaseOutputParser
|
|
2
2
|
from langchain_core.prompts import PromptTemplate
|
|
3
|
+
from typing_extensions import override
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
class FinishedOutputParser(BaseOutputParser[tuple[str, bool]]):
|
|
@@ -8,6 +9,7 @@ class FinishedOutputParser(BaseOutputParser[tuple[str, bool]]):
|
|
|
8
9
|
finished_value: str = "FINISHED"
|
|
9
10
|
"""Value that indicates the output is finished."""
|
|
10
11
|
|
|
12
|
+
@override
|
|
11
13
|
def parse(self, text: str) -> tuple[str, bool]:
|
|
12
14
|
cleaned = text.strip()
|
|
13
15
|
finished = self.finished_value in cleaned
|
langchain/chains/llm.py
CHANGED
|
@@ -32,6 +32,7 @@ from langchain_core.runnables import (
|
|
|
32
32
|
from langchain_core.runnables.configurable import DynamicRunnable
|
|
33
33
|
from langchain_core.utils.input import get_colored_text
|
|
34
34
|
from pydantic import ConfigDict, Field
|
|
35
|
+
from typing_extensions import override
|
|
35
36
|
|
|
36
37
|
from langchain.chains.base import Chain
|
|
37
38
|
|
|
@@ -73,9 +74,11 @@ class LLMChain(Chain):
|
|
|
73
74
|
input_variables=["adjective"], template=prompt_template
|
|
74
75
|
)
|
|
75
76
|
llm = LLMChain(llm=OpenAI(), prompt=prompt)
|
|
77
|
+
|
|
76
78
|
"""
|
|
77
79
|
|
|
78
80
|
@classmethod
|
|
81
|
+
@override
|
|
79
82
|
def is_lc_serializable(self) -> bool:
|
|
80
83
|
return True
|
|
81
84
|
|
|
@@ -143,7 +146,7 @@ class LLMChain(Chain):
|
|
|
143
146
|
**self.llm_kwargs,
|
|
144
147
|
)
|
|
145
148
|
results = self.llm.bind(stop=stop, **self.llm_kwargs).batch(
|
|
146
|
-
cast(list, prompts),
|
|
149
|
+
cast("list", prompts),
|
|
147
150
|
{"callbacks": callbacks},
|
|
148
151
|
)
|
|
149
152
|
generations: list[list[Generation]] = []
|
|
@@ -170,7 +173,7 @@ class LLMChain(Chain):
|
|
|
170
173
|
**self.llm_kwargs,
|
|
171
174
|
)
|
|
172
175
|
results = await self.llm.bind(stop=stop, **self.llm_kwargs).abatch(
|
|
173
|
-
cast(list, prompts),
|
|
176
|
+
cast("list", prompts),
|
|
174
177
|
{"callbacks": callbacks},
|
|
175
178
|
)
|
|
176
179
|
generations: list[list[Generation]] = []
|
|
@@ -321,6 +324,7 @@ class LLMChain(Chain):
|
|
|
321
324
|
.. code-block:: python
|
|
322
325
|
|
|
323
326
|
completion = llm.predict(adjective="funny")
|
|
327
|
+
|
|
324
328
|
"""
|
|
325
329
|
return self(kwargs, callbacks=callbacks)[self.output_key]
|
|
326
330
|
|
|
@@ -338,6 +342,7 @@ class LLMChain(Chain):
|
|
|
338
342
|
.. code-block:: python
|
|
339
343
|
|
|
340
344
|
completion = llm.predict(adjective="funny")
|
|
345
|
+
|
|
341
346
|
"""
|
|
342
347
|
return (await self.acall(kwargs, callbacks=callbacks))[self.output_key]
|
|
343
348
|
|
|
@@ -82,6 +82,7 @@ class LLMCheckerChain(Chain):
|
|
|
82
82
|
from langchain.chains import LLMCheckerChain
|
|
83
83
|
llm = OpenAI(temperature=0.7)
|
|
84
84
|
checker_chain = LLMCheckerChain.from_llm(llm)
|
|
85
|
+
|
|
85
86
|
"""
|
|
86
87
|
|
|
87
88
|
question_to_checked_assertions_chain: SequentialChain
|
|
@@ -106,7 +107,7 @@ class LLMCheckerChain(Chain):
|
|
|
106
107
|
|
|
107
108
|
@model_validator(mode="before")
|
|
108
109
|
@classmethod
|
|
109
|
-
def
|
|
110
|
+
def _raise_deprecation(cls, values: dict) -> Any:
|
|
110
111
|
if "llm" in values:
|
|
111
112
|
warnings.warn(
|
|
112
113
|
"Directly instantiating an LLMCheckerChain with an llm is deprecated. "
|
|
@@ -179,6 +180,16 @@ class LLMCheckerChain(Chain):
|
|
|
179
180
|
revised_answer_prompt: PromptTemplate = REVISED_ANSWER_PROMPT,
|
|
180
181
|
**kwargs: Any,
|
|
181
182
|
) -> LLMCheckerChain:
|
|
183
|
+
"""Create an LLMCheckerChain from a language model.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
llm: a language model
|
|
187
|
+
create_draft_answer_prompt: prompt to create a draft answer
|
|
188
|
+
list_assertions_prompt: prompt to list assertions
|
|
189
|
+
check_assertions_prompt: prompt to check assertions
|
|
190
|
+
revised_answer_prompt: prompt to revise the answer
|
|
191
|
+
**kwargs: additional arguments
|
|
192
|
+
"""
|
|
182
193
|
question_to_checked_assertions_chain = (
|
|
183
194
|
_load_question_to_checked_assertions_chain(
|
|
184
195
|
llm,
|
|
@@ -146,6 +146,7 @@ class LLMMathChain(Chain):
|
|
|
146
146
|
from langchain.chains import LLMMathChain
|
|
147
147
|
from langchain_community.llms import OpenAI
|
|
148
148
|
llm_math = LLMMathChain.from_llm(OpenAI())
|
|
149
|
+
|
|
149
150
|
""" # noqa: E501
|
|
150
151
|
|
|
151
152
|
llm_chain: LLMChain
|
|
@@ -163,7 +164,7 @@ class LLMMathChain(Chain):
|
|
|
163
164
|
|
|
164
165
|
@model_validator(mode="before")
|
|
165
166
|
@classmethod
|
|
166
|
-
def
|
|
167
|
+
def _raise_deprecation(cls, values: dict) -> Any:
|
|
167
168
|
try:
|
|
168
169
|
import numexpr # noqa: F401
|
|
169
170
|
except ImportError as e:
|
|
@@ -307,5 +308,12 @@ class LLMMathChain(Chain):
|
|
|
307
308
|
prompt: BasePromptTemplate = PROMPT,
|
|
308
309
|
**kwargs: Any,
|
|
309
310
|
) -> LLMMathChain:
|
|
311
|
+
"""Create a LLMMathChain from a language model.
|
|
312
|
+
|
|
313
|
+
Args:
|
|
314
|
+
llm: a language model
|
|
315
|
+
prompt: a prompt template
|
|
316
|
+
**kwargs: additional arguments
|
|
317
|
+
"""
|
|
310
318
|
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
|
311
319
|
return cls(llm_chain=llm_chain, **kwargs)
|
|
@@ -85,6 +85,7 @@ class LLMSummarizationCheckerChain(Chain):
|
|
|
85
85
|
from langchain.chains import LLMSummarizationCheckerChain
|
|
86
86
|
llm = OpenAI(temperature=0.0)
|
|
87
87
|
checker_chain = LLMSummarizationCheckerChain.from_llm(llm)
|
|
88
|
+
|
|
88
89
|
"""
|
|
89
90
|
|
|
90
91
|
sequential_chain: SequentialChain
|
|
@@ -112,7 +113,7 @@ class LLMSummarizationCheckerChain(Chain):
|
|
|
112
113
|
|
|
113
114
|
@model_validator(mode="before")
|
|
114
115
|
@classmethod
|
|
115
|
-
def
|
|
116
|
+
def _raise_deprecation(cls, values: dict) -> Any:
|
|
116
117
|
if "llm" in values:
|
|
117
118
|
warnings.warn(
|
|
118
119
|
"Directly instantiating an LLMSummarizationCheckerChain with an llm is "
|
|
@@ -194,6 +195,17 @@ class LLMSummarizationCheckerChain(Chain):
|
|
|
194
195
|
verbose: bool = False, # noqa: FBT001,FBT002
|
|
195
196
|
**kwargs: Any,
|
|
196
197
|
) -> LLMSummarizationCheckerChain:
|
|
198
|
+
"""Create a LLMSummarizationCheckerChain from a language model.
|
|
199
|
+
|
|
200
|
+
Args:
|
|
201
|
+
llm: a language model
|
|
202
|
+
create_assertions_prompt: prompt to create assertions
|
|
203
|
+
check_assertions_prompt: prompt to check assertions
|
|
204
|
+
revised_summary_prompt: prompt to revise summary
|
|
205
|
+
are_all_true_prompt: prompt to check if all assertions are true
|
|
206
|
+
verbose: whether to print verbose output
|
|
207
|
+
**kwargs: additional arguments
|
|
208
|
+
"""
|
|
197
209
|
chain = _load_sequential_chain(
|
|
198
210
|
llm,
|
|
199
211
|
create_assertions_prompt,
|
langchain/chains/loading.py
CHANGED
|
@@ -39,7 +39,8 @@ try:
|
|
|
39
39
|
from langchain_community.llms.loading import load_llm, load_llm_from_config
|
|
40
40
|
except ImportError:
|
|
41
41
|
|
|
42
|
-
def load_llm(*
|
|
42
|
+
def load_llm(*_: Any, **__: Any) -> None:
|
|
43
|
+
"""Import error for load_llm."""
|
|
43
44
|
msg = (
|
|
44
45
|
"To use this load_llm functionality you must install the "
|
|
45
46
|
"langchain_community package. "
|
|
@@ -47,7 +48,8 @@ except ImportError:
|
|
|
47
48
|
)
|
|
48
49
|
raise ImportError(msg)
|
|
49
50
|
|
|
50
|
-
def load_llm_from_config(*
|
|
51
|
+
def load_llm_from_config(*_: Any, **__: Any) -> None:
|
|
52
|
+
"""Import error for load_llm_from_config."""
|
|
51
53
|
msg = (
|
|
52
54
|
"To use this load_llm_from_config functionality you must install the "
|
|
53
55
|
"langchain_community package. "
|
langchain/chains/moderation.py
CHANGED
|
@@ -8,6 +8,7 @@ from langchain_core.callbacks import (
|
|
|
8
8
|
)
|
|
9
9
|
from langchain_core.utils import check_package_version, get_from_dict_or_env
|
|
10
10
|
from pydantic import Field, model_validator
|
|
11
|
+
from typing_extensions import override
|
|
11
12
|
|
|
12
13
|
from langchain.chains.base import Chain
|
|
13
14
|
|
|
@@ -26,6 +27,7 @@ class OpenAIModerationChain(Chain):
|
|
|
26
27
|
|
|
27
28
|
from langchain.chains import OpenAIModerationChain
|
|
28
29
|
moderation = OpenAIModerationChain()
|
|
30
|
+
|
|
29
31
|
"""
|
|
30
32
|
|
|
31
33
|
client: Any = None #: :meta private:
|
|
@@ -105,6 +107,7 @@ class OpenAIModerationChain(Chain):
|
|
|
105
107
|
return error_str
|
|
106
108
|
return text
|
|
107
109
|
|
|
110
|
+
@override
|
|
108
111
|
def _call(
|
|
109
112
|
self,
|
|
110
113
|
inputs: dict[str, Any],
|
langchain/chains/natbot/base.py
CHANGED
|
@@ -47,6 +47,7 @@ class NatBotChain(Chain):
|
|
|
47
47
|
|
|
48
48
|
from langchain.chains import NatBotChain
|
|
49
49
|
natbot = NatBotChain.from_default("Buy me a new hat.")
|
|
50
|
+
|
|
50
51
|
"""
|
|
51
52
|
|
|
52
53
|
llm_chain: Runnable
|
|
@@ -66,7 +67,7 @@ class NatBotChain(Chain):
|
|
|
66
67
|
|
|
67
68
|
@model_validator(mode="before")
|
|
68
69
|
@classmethod
|
|
69
|
-
def
|
|
70
|
+
def _raise_deprecation(cls, values: dict) -> Any:
|
|
70
71
|
if "llm" in values:
|
|
71
72
|
warnings.warn(
|
|
72
73
|
"Directly instantiating an NatBotChain with an llm is deprecated. "
|
|
@@ -151,6 +152,7 @@ class NatBotChain(Chain):
|
|
|
151
152
|
|
|
152
153
|
browser_content = "...."
|
|
153
154
|
llm_command = natbot.run("www.google.com", browser_content)
|
|
155
|
+
|
|
154
156
|
"""
|
|
155
157
|
_inputs = {
|
|
156
158
|
self.input_url_key: url,
|