langchain 0.3.23__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 +27 -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.23.dist-info → langchain-0.3.24.dist-info}/METADATA +2 -2
- {langchain-0.3.23.dist-info → langchain-0.3.24.dist-info}/RECORD +169 -169
- {langchain-0.3.23.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.23.dist-info/entry_points.txt +0 -5
- {langchain-0.3.23.dist-info → langchain-0.3.24.dist-info}/licenses/LICENSE +0 -0
langchain/chat_models/base.py
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
|
+
from collections.abc import AsyncIterator, Iterator, Sequence
|
|
4
5
|
from importlib import util
|
|
5
6
|
from typing import (
|
|
6
7
|
Any,
|
|
7
|
-
AsyncIterator,
|
|
8
8
|
Callable,
|
|
9
|
-
Dict,
|
|
10
|
-
Iterator,
|
|
11
|
-
List,
|
|
12
9
|
Literal,
|
|
13
10
|
Optional,
|
|
14
|
-
Sequence,
|
|
15
|
-
Tuple,
|
|
16
|
-
Type,
|
|
17
11
|
Union,
|
|
18
12
|
cast,
|
|
19
13
|
overload,
|
|
@@ -47,7 +41,7 @@ __all__ = [
|
|
|
47
41
|
|
|
48
42
|
|
|
49
43
|
@overload
|
|
50
|
-
def init_chat_model(
|
|
44
|
+
def init_chat_model(
|
|
51
45
|
model: str,
|
|
52
46
|
*,
|
|
53
47
|
model_provider: Optional[str] = None,
|
|
@@ -73,7 +67,7 @@ def init_chat_model(
|
|
|
73
67
|
model: Optional[str] = None,
|
|
74
68
|
*,
|
|
75
69
|
model_provider: Optional[str] = None,
|
|
76
|
-
configurable_fields: Union[Literal["any"],
|
|
70
|
+
configurable_fields: Union[Literal["any"], list[str], tuple[str, ...]] = ...,
|
|
77
71
|
config_prefix: Optional[str] = None,
|
|
78
72
|
**kwargs: Any,
|
|
79
73
|
) -> _ConfigurableModel: ...
|
|
@@ -87,7 +81,7 @@ def init_chat_model(
|
|
|
87
81
|
*,
|
|
88
82
|
model_provider: Optional[str] = None,
|
|
89
83
|
configurable_fields: Optional[
|
|
90
|
-
Union[Literal["any"],
|
|
84
|
+
Union[Literal["any"], list[str], tuple[str, ...]]
|
|
91
85
|
] = None,
|
|
92
86
|
config_prefix: Optional[str] = None,
|
|
93
87
|
**kwargs: Any,
|
|
@@ -139,6 +133,7 @@ def init_chat_model(
|
|
|
139
133
|
- 'mistral...' -> 'mistralai'
|
|
140
134
|
- 'deepseek...' -> 'deepseek'
|
|
141
135
|
- 'grok...' -> 'xai'
|
|
136
|
+
- 'sonar...' -> 'perplexity'
|
|
142
137
|
configurable_fields: Which model parameters are
|
|
143
138
|
configurable:
|
|
144
139
|
|
|
@@ -352,7 +347,7 @@ def _init_chat_model_helper(
|
|
|
352
347
|
_check_pkg("langchain_anthropic")
|
|
353
348
|
from langchain_anthropic import ChatAnthropic
|
|
354
349
|
|
|
355
|
-
return ChatAnthropic(model=model, **kwargs) # type: ignore[call-arg]
|
|
350
|
+
return ChatAnthropic(model=model, **kwargs) # type: ignore[call-arg,unused-ignore]
|
|
356
351
|
elif model_provider == "azure_openai":
|
|
357
352
|
_check_pkg("langchain_openai")
|
|
358
353
|
from langchain_openai import AzureChatOpenAI
|
|
@@ -407,7 +402,7 @@ def _init_chat_model_helper(
|
|
|
407
402
|
_check_pkg("langchain_mistralai")
|
|
408
403
|
from langchain_mistralai import ChatMistralAI
|
|
409
404
|
|
|
410
|
-
return ChatMistralAI(model=model, **kwargs) # type: ignore[call-arg]
|
|
405
|
+
return ChatMistralAI(model=model, **kwargs) # type: ignore[call-arg,unused-ignore]
|
|
411
406
|
elif model_provider == "huggingface":
|
|
412
407
|
_check_pkg("langchain_huggingface")
|
|
413
408
|
from langchain_huggingface import ChatHuggingFace
|
|
@@ -510,11 +505,13 @@ def _attempt_infer_model_provider(model_name: str) -> Optional[str]:
|
|
|
510
505
|
return "deepseek"
|
|
511
506
|
elif model_name.startswith("grok"):
|
|
512
507
|
return "xai"
|
|
508
|
+
elif model_name.startswith("sonar"):
|
|
509
|
+
return "perplexity"
|
|
513
510
|
else:
|
|
514
511
|
return None
|
|
515
512
|
|
|
516
513
|
|
|
517
|
-
def _parse_model(model: str, model_provider: Optional[str]) ->
|
|
514
|
+
def _parse_model(model: str, model_provider: Optional[str]) -> tuple[str, str]:
|
|
518
515
|
if (
|
|
519
516
|
not model_provider
|
|
520
517
|
and ":" in model
|
|
@@ -554,12 +551,12 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
554
551
|
self,
|
|
555
552
|
*,
|
|
556
553
|
default_config: Optional[dict] = None,
|
|
557
|
-
configurable_fields: Union[Literal["any"],
|
|
554
|
+
configurable_fields: Union[Literal["any"], list[str], tuple[str, ...]] = "any",
|
|
558
555
|
config_prefix: str = "",
|
|
559
|
-
queued_declarative_operations: Sequence[
|
|
556
|
+
queued_declarative_operations: Sequence[tuple[str, tuple, dict]] = (),
|
|
560
557
|
) -> None:
|
|
561
558
|
self._default_config: dict = default_config or {}
|
|
562
|
-
self._configurable_fields: Union[Literal["any"],
|
|
559
|
+
self._configurable_fields: Union[Literal["any"], list[str]] = (
|
|
563
560
|
configurable_fields
|
|
564
561
|
if configurable_fields == "any"
|
|
565
562
|
else list(configurable_fields)
|
|
@@ -569,7 +566,7 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
569
566
|
if config_prefix and not config_prefix.endswith("_")
|
|
570
567
|
else config_prefix
|
|
571
568
|
)
|
|
572
|
-
self._queued_declarative_operations:
|
|
569
|
+
self._queued_declarative_operations: list[tuple[str, tuple, dict]] = list(
|
|
573
570
|
queued_declarative_operations
|
|
574
571
|
)
|
|
575
572
|
|
|
@@ -670,7 +667,7 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
670
667
|
return Union[
|
|
671
668
|
str,
|
|
672
669
|
Union[StringPromptValue, ChatPromptValueConcrete],
|
|
673
|
-
|
|
670
|
+
list[AnyMessage],
|
|
674
671
|
]
|
|
675
672
|
|
|
676
673
|
def invoke(
|
|
@@ -708,12 +705,12 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
708
705
|
|
|
709
706
|
def batch(
|
|
710
707
|
self,
|
|
711
|
-
inputs:
|
|
712
|
-
config: Optional[Union[RunnableConfig,
|
|
708
|
+
inputs: list[LanguageModelInput],
|
|
709
|
+
config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
|
|
713
710
|
*,
|
|
714
711
|
return_exceptions: bool = False,
|
|
715
712
|
**kwargs: Optional[Any],
|
|
716
|
-
) ->
|
|
713
|
+
) -> list[Any]:
|
|
717
714
|
config = config or None
|
|
718
715
|
# If <= 1 config use the underlying models batch implementation.
|
|
719
716
|
if config is None or isinstance(config, dict) or len(config) <= 1:
|
|
@@ -731,12 +728,12 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
731
728
|
|
|
732
729
|
async def abatch(
|
|
733
730
|
self,
|
|
734
|
-
inputs:
|
|
735
|
-
config: Optional[Union[RunnableConfig,
|
|
731
|
+
inputs: list[LanguageModelInput],
|
|
732
|
+
config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
|
|
736
733
|
*,
|
|
737
734
|
return_exceptions: bool = False,
|
|
738
735
|
**kwargs: Optional[Any],
|
|
739
|
-
) ->
|
|
736
|
+
) -> list[Any]:
|
|
740
737
|
config = config or None
|
|
741
738
|
# If <= 1 config use the underlying models batch implementation.
|
|
742
739
|
if config is None or isinstance(config, dict) or len(config) <= 1:
|
|
@@ -759,7 +756,7 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
759
756
|
*,
|
|
760
757
|
return_exceptions: bool = False,
|
|
761
758
|
**kwargs: Any,
|
|
762
|
-
) -> Iterator[
|
|
759
|
+
) -> Iterator[tuple[int, Union[Any, Exception]]]:
|
|
763
760
|
config = config or None
|
|
764
761
|
# If <= 1 config use the underlying models batch implementation.
|
|
765
762
|
if config is None or isinstance(config, dict) or len(config) <= 1:
|
|
@@ -782,7 +779,7 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
782
779
|
*,
|
|
783
780
|
return_exceptions: bool = False,
|
|
784
781
|
**kwargs: Any,
|
|
785
|
-
) -> AsyncIterator[
|
|
782
|
+
) -> AsyncIterator[tuple[int, Any]]:
|
|
786
783
|
config = config or None
|
|
787
784
|
# If <= 1 config use the underlying models batch implementation.
|
|
788
785
|
if config is None or isinstance(config, dict) or len(config) <= 1:
|
|
@@ -808,8 +805,7 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
808
805
|
config: Optional[RunnableConfig] = None,
|
|
809
806
|
**kwargs: Optional[Any],
|
|
810
807
|
) -> Iterator[Any]:
|
|
811
|
-
|
|
812
|
-
yield x
|
|
808
|
+
yield from self._model(config).transform(input, config=config, **kwargs)
|
|
813
809
|
|
|
814
810
|
async def atransform(
|
|
815
811
|
self,
|
|
@@ -915,13 +911,13 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):
|
|
|
915
911
|
# Explicitly added to satisfy downstream linters.
|
|
916
912
|
def bind_tools(
|
|
917
913
|
self,
|
|
918
|
-
tools: Sequence[Union[
|
|
914
|
+
tools: Sequence[Union[dict[str, Any], type[BaseModel], Callable, BaseTool]],
|
|
919
915
|
**kwargs: Any,
|
|
920
916
|
) -> Runnable[LanguageModelInput, BaseMessage]:
|
|
921
917
|
return self.__getattr__("bind_tools")(tools, **kwargs)
|
|
922
918
|
|
|
923
919
|
# Explicitly added to satisfy downstream linters.
|
|
924
920
|
def with_structured_output(
|
|
925
|
-
self, schema: Union[
|
|
926
|
-
) -> Runnable[LanguageModelInput, Union[
|
|
921
|
+
self, schema: Union[dict, type[BaseModel]], **kwargs: Any
|
|
922
|
+
) -> Runnable[LanguageModelInput, Union[dict, BaseModel]]:
|
|
927
923
|
return self.__getattr__("with_structured_output")(schema, **kwargs)
|
langchain/embeddings/__init__.py
CHANGED
|
@@ -87,7 +87,7 @@ class HypotheticalDocumentEmbedder:
|
|
|
87
87
|
)
|
|
88
88
|
from langchain.chains.hyde.base import HypotheticalDocumentEmbedder as H
|
|
89
89
|
|
|
90
|
-
return H(*args, **kwargs) # type: ignore
|
|
90
|
+
return H(*args, **kwargs) # type: ignore[return-value]
|
|
91
91
|
|
|
92
92
|
@classmethod
|
|
93
93
|
def from_llm(cls, *args: Any, **kwargs: Any) -> Any:
|
langchain/embeddings/base.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import functools
|
|
2
2
|
from importlib import util
|
|
3
|
-
from typing import Any,
|
|
3
|
+
from typing import Any, Optional, Union
|
|
4
4
|
|
|
5
5
|
from langchain_core._api import beta
|
|
6
6
|
from langchain_core.embeddings import Embeddings
|
|
@@ -25,7 +25,7 @@ def _get_provider_list() -> str:
|
|
|
25
25
|
)
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def _parse_model_string(model_name: str) ->
|
|
28
|
+
def _parse_model_string(model_name: str) -> tuple[str, str]:
|
|
29
29
|
"""Parse a model string into provider and model name components.
|
|
30
30
|
|
|
31
31
|
The model string should be in the format 'provider:model-name', where provider
|
|
@@ -78,7 +78,7 @@ def _parse_model_string(model_name: str) -> Tuple[str, str]:
|
|
|
78
78
|
|
|
79
79
|
def _infer_model_and_provider(
|
|
80
80
|
model: str, *, provider: Optional[str] = None
|
|
81
|
-
) ->
|
|
81
|
+
) -> tuple[str, str]:
|
|
82
82
|
if not model.strip():
|
|
83
83
|
raise ValueError("Model name cannot be empty")
|
|
84
84
|
if provider is None and ":" in model:
|
|
@@ -122,7 +122,7 @@ def init_embeddings(
|
|
|
122
122
|
*,
|
|
123
123
|
provider: Optional[str] = None,
|
|
124
124
|
**kwargs: Any,
|
|
125
|
-
) -> Union[Embeddings, Runnable[Any,
|
|
125
|
+
) -> Union[Embeddings, Runnable[Any, list[float]]]:
|
|
126
126
|
"""Initialize an embeddings model from a model name and optional provider.
|
|
127
127
|
|
|
128
128
|
**Note:** Must have the integration package corresponding to the model provider
|
langchain/embeddings/cache.py
CHANGED
|
@@ -12,8 +12,9 @@ from __future__ import annotations
|
|
|
12
12
|
import hashlib
|
|
13
13
|
import json
|
|
14
14
|
import uuid
|
|
15
|
+
from collections.abc import Sequence
|
|
15
16
|
from functools import partial
|
|
16
|
-
from typing import Callable,
|
|
17
|
+
from typing import Callable, Optional, Union, cast
|
|
17
18
|
|
|
18
19
|
from langchain_core.embeddings import Embeddings
|
|
19
20
|
from langchain_core.stores import BaseStore, ByteStore
|
|
@@ -45,9 +46,9 @@ def _value_serializer(value: Sequence[float]) -> bytes:
|
|
|
45
46
|
return json.dumps(value).encode()
|
|
46
47
|
|
|
47
48
|
|
|
48
|
-
def _value_deserializer(serialized_value: bytes) ->
|
|
49
|
+
def _value_deserializer(serialized_value: bytes) -> list[float]:
|
|
49
50
|
"""Deserialize a value."""
|
|
50
|
-
return cast(
|
|
51
|
+
return cast(list[float], json.loads(serialized_value.decode()))
|
|
51
52
|
|
|
52
53
|
|
|
53
54
|
class CacheBackedEmbeddings(Embeddings):
|
|
@@ -88,10 +89,10 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
88
89
|
def __init__(
|
|
89
90
|
self,
|
|
90
91
|
underlying_embeddings: Embeddings,
|
|
91
|
-
document_embedding_store: BaseStore[str,
|
|
92
|
+
document_embedding_store: BaseStore[str, list[float]],
|
|
92
93
|
*,
|
|
93
94
|
batch_size: Optional[int] = None,
|
|
94
|
-
query_embedding_store: Optional[BaseStore[str,
|
|
95
|
+
query_embedding_store: Optional[BaseStore[str, list[float]]] = None,
|
|
95
96
|
) -> None:
|
|
96
97
|
"""Initialize the embedder.
|
|
97
98
|
|
|
@@ -108,7 +109,7 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
108
109
|
self.underlying_embeddings = underlying_embeddings
|
|
109
110
|
self.batch_size = batch_size
|
|
110
111
|
|
|
111
|
-
def embed_documents(self, texts:
|
|
112
|
+
def embed_documents(self, texts: list[str]) -> list[list[float]]:
|
|
112
113
|
"""Embed a list of texts.
|
|
113
114
|
|
|
114
115
|
The method first checks the cache for the embeddings.
|
|
@@ -121,10 +122,10 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
121
122
|
Returns:
|
|
122
123
|
A list of embeddings for the given texts.
|
|
123
124
|
"""
|
|
124
|
-
vectors:
|
|
125
|
+
vectors: list[Union[list[float], None]] = self.document_embedding_store.mget(
|
|
125
126
|
texts
|
|
126
127
|
)
|
|
127
|
-
all_missing_indices:
|
|
128
|
+
all_missing_indices: list[int] = [
|
|
128
129
|
i for i, vector in enumerate(vectors) if vector is None
|
|
129
130
|
]
|
|
130
131
|
|
|
@@ -138,10 +139,10 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
138
139
|
vectors[index] = updated_vector
|
|
139
140
|
|
|
140
141
|
return cast(
|
|
141
|
-
|
|
142
|
+
list[list[float]], vectors
|
|
142
143
|
) # Nones should have been resolved by now
|
|
143
144
|
|
|
144
|
-
async def aembed_documents(self, texts:
|
|
145
|
+
async def aembed_documents(self, texts: list[str]) -> list[list[float]]:
|
|
145
146
|
"""Embed a list of texts.
|
|
146
147
|
|
|
147
148
|
The method first checks the cache for the embeddings.
|
|
@@ -154,10 +155,10 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
154
155
|
Returns:
|
|
155
156
|
A list of embeddings for the given texts.
|
|
156
157
|
"""
|
|
157
|
-
vectors:
|
|
158
|
-
Union[
|
|
158
|
+
vectors: list[
|
|
159
|
+
Union[list[float], None]
|
|
159
160
|
] = await self.document_embedding_store.amget(texts)
|
|
160
|
-
all_missing_indices:
|
|
161
|
+
all_missing_indices: list[int] = [
|
|
161
162
|
i for i, vector in enumerate(vectors) if vector is None
|
|
162
163
|
]
|
|
163
164
|
|
|
@@ -175,10 +176,10 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
175
176
|
vectors[index] = updated_vector
|
|
176
177
|
|
|
177
178
|
return cast(
|
|
178
|
-
|
|
179
|
+
list[list[float]], vectors
|
|
179
180
|
) # Nones should have been resolved by now
|
|
180
181
|
|
|
181
|
-
def embed_query(self, text: str) ->
|
|
182
|
+
def embed_query(self, text: str) -> list[float]:
|
|
182
183
|
"""Embed query text.
|
|
183
184
|
|
|
184
185
|
By default, this method does not cache queries. To enable caching, set the
|
|
@@ -201,7 +202,7 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
201
202
|
self.query_embedding_store.mset([(text, vector)])
|
|
202
203
|
return vector
|
|
203
204
|
|
|
204
|
-
async def aembed_query(self, text: str) ->
|
|
205
|
+
async def aembed_query(self, text: str) -> list[float]:
|
|
205
206
|
"""Embed query text.
|
|
206
207
|
|
|
207
208
|
By default, this method does not cache queries. To enable caching, set the
|
|
@@ -250,7 +251,7 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
250
251
|
"""
|
|
251
252
|
namespace = namespace
|
|
252
253
|
key_encoder = _create_key_encoder(namespace)
|
|
253
|
-
document_embedding_store = EncoderBackedStore[str,
|
|
254
|
+
document_embedding_store = EncoderBackedStore[str, list[float]](
|
|
254
255
|
document_embedding_cache,
|
|
255
256
|
key_encoder,
|
|
256
257
|
_value_serializer,
|
|
@@ -261,7 +262,7 @@ class CacheBackedEmbeddings(Embeddings):
|
|
|
261
262
|
elif query_embedding_cache is False:
|
|
262
263
|
query_embedding_store = None
|
|
263
264
|
else:
|
|
264
|
-
query_embedding_store = EncoderBackedStore[str,
|
|
265
|
+
query_embedding_store = EncoderBackedStore[str, list[float]](
|
|
265
266
|
query_embedding_cache,
|
|
266
267
|
key_encoder,
|
|
267
268
|
_value_serializer,
|
|
@@ -6,13 +6,10 @@ chain (LLMChain) to generate the reasoning and scores.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
import re
|
|
9
|
+
from collections.abc import Sequence
|
|
9
10
|
from typing import (
|
|
10
11
|
Any,
|
|
11
|
-
Dict,
|
|
12
|
-
List,
|
|
13
12
|
Optional,
|
|
14
|
-
Sequence,
|
|
15
|
-
Tuple,
|
|
16
13
|
TypedDict,
|
|
17
14
|
Union,
|
|
18
15
|
cast,
|
|
@@ -145,7 +142,7 @@ class TrajectoryEvalChain(AgentTrajectoryEvaluator, LLMEvalChain):
|
|
|
145
142
|
# 0
|
|
146
143
|
"""
|
|
147
144
|
|
|
148
|
-
agent_tools: Optional[
|
|
145
|
+
agent_tools: Optional[list[BaseTool]] = None
|
|
149
146
|
"""A list of tools available to the agent."""
|
|
150
147
|
eval_chain: LLMChain
|
|
151
148
|
"""The language model chain used for evaluation."""
|
|
@@ -184,7 +181,7 @@ Description: {tool.description}"""
|
|
|
184
181
|
|
|
185
182
|
@staticmethod
|
|
186
183
|
def get_agent_trajectory(
|
|
187
|
-
steps: Union[str, Sequence[
|
|
184
|
+
steps: Union[str, Sequence[tuple[AgentAction, str]]],
|
|
188
185
|
) -> str:
|
|
189
186
|
"""Get the agent trajectory as a formatted string.
|
|
190
187
|
|
|
@@ -263,7 +260,7 @@ The following is the expected answer. Use this to measure correctness:
|
|
|
263
260
|
)
|
|
264
261
|
|
|
265
262
|
@property
|
|
266
|
-
def input_keys(self) ->
|
|
263
|
+
def input_keys(self) -> list[str]:
|
|
267
264
|
"""Get the input keys for the chain.
|
|
268
265
|
|
|
269
266
|
Returns:
|
|
@@ -272,7 +269,7 @@ The following is the expected answer. Use this to measure correctness:
|
|
|
272
269
|
return ["question", "agent_trajectory", "answer", "reference"]
|
|
273
270
|
|
|
274
271
|
@property
|
|
275
|
-
def output_keys(self) ->
|
|
272
|
+
def output_keys(self) -> list[str]:
|
|
276
273
|
"""Get the output keys for the chain.
|
|
277
274
|
|
|
278
275
|
Returns:
|
|
@@ -280,16 +277,16 @@ The following is the expected answer. Use this to measure correctness:
|
|
|
280
277
|
"""
|
|
281
278
|
return ["score", "reasoning"]
|
|
282
279
|
|
|
283
|
-
def prep_inputs(self, inputs: Union[
|
|
280
|
+
def prep_inputs(self, inputs: Union[dict[str, Any], Any]) -> dict[str, str]:
|
|
284
281
|
"""Validate and prep inputs."""
|
|
285
282
|
inputs["reference"] = self._format_reference(inputs.get("reference"))
|
|
286
283
|
return super().prep_inputs(inputs)
|
|
287
284
|
|
|
288
285
|
def _call(
|
|
289
286
|
self,
|
|
290
|
-
inputs:
|
|
287
|
+
inputs: dict[str, str],
|
|
291
288
|
run_manager: Optional[CallbackManagerForChainRun] = None,
|
|
292
|
-
) ->
|
|
289
|
+
) -> dict[str, Any]:
|
|
293
290
|
"""Run the chain and generate the output.
|
|
294
291
|
|
|
295
292
|
Args:
|
|
@@ -311,9 +308,9 @@ The following is the expected answer. Use this to measure correctness:
|
|
|
311
308
|
|
|
312
309
|
async def _acall(
|
|
313
310
|
self,
|
|
314
|
-
inputs:
|
|
311
|
+
inputs: dict[str, str],
|
|
315
312
|
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
|
|
316
|
-
) ->
|
|
313
|
+
) -> dict[str, Any]:
|
|
317
314
|
"""Run the chain and generate the output.
|
|
318
315
|
|
|
319
316
|
Args:
|
|
@@ -338,11 +335,11 @@ The following is the expected answer. Use this to measure correctness:
|
|
|
338
335
|
*,
|
|
339
336
|
prediction: str,
|
|
340
337
|
input: str,
|
|
341
|
-
agent_trajectory: Sequence[
|
|
338
|
+
agent_trajectory: Sequence[tuple[AgentAction, str]],
|
|
342
339
|
reference: Optional[str] = None,
|
|
343
340
|
callbacks: Callbacks = None,
|
|
344
|
-
tags: Optional[
|
|
345
|
-
metadata: Optional[
|
|
341
|
+
tags: Optional[list[str]] = None,
|
|
342
|
+
metadata: Optional[dict[str, Any]] = None,
|
|
346
343
|
include_run_info: bool = False,
|
|
347
344
|
**kwargs: Any,
|
|
348
345
|
) -> dict:
|
|
@@ -380,11 +377,11 @@ The following is the expected answer. Use this to measure correctness:
|
|
|
380
377
|
*,
|
|
381
378
|
prediction: str,
|
|
382
379
|
input: str,
|
|
383
|
-
agent_trajectory: Sequence[
|
|
380
|
+
agent_trajectory: Sequence[tuple[AgentAction, str]],
|
|
384
381
|
reference: Optional[str] = None,
|
|
385
382
|
callbacks: Callbacks = None,
|
|
386
|
-
tags: Optional[
|
|
387
|
-
metadata: Optional[
|
|
383
|
+
tags: Optional[list[str]] = None,
|
|
384
|
+
metadata: Optional[dict[str, Any]] = None,
|
|
388
385
|
include_run_info: bool = False,
|
|
389
386
|
**kwargs: Any,
|
|
390
387
|
) -> dict:
|
|
@@ -4,7 +4,7 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
6
|
import re
|
|
7
|
-
from typing import Any,
|
|
7
|
+
from typing import Any, Optional, Union
|
|
8
8
|
|
|
9
9
|
from langchain_core.callbacks.manager import Callbacks
|
|
10
10
|
from langchain_core.language_models import BaseLanguageModel
|
|
@@ -49,7 +49,7 @@ _SUPPORTED_CRITERIA = {
|
|
|
49
49
|
|
|
50
50
|
|
|
51
51
|
def resolve_pairwise_criteria(
|
|
52
|
-
criteria: Optional[Union[CRITERIA_TYPE, str,
|
|
52
|
+
criteria: Optional[Union[CRITERIA_TYPE, str, list[CRITERIA_TYPE]]],
|
|
53
53
|
) -> dict:
|
|
54
54
|
"""Resolve the criteria for the pairwise evaluator.
|
|
55
55
|
|
|
@@ -95,7 +95,7 @@ def resolve_pairwise_criteria(
|
|
|
95
95
|
return criteria_
|
|
96
96
|
|
|
97
97
|
|
|
98
|
-
class PairwiseStringResultOutputParser(BaseOutputParser[dict]):
|
|
98
|
+
class PairwiseStringResultOutputParser(BaseOutputParser[dict]):
|
|
99
99
|
"""A parser for the output of the PairwiseStringEvalChain.
|
|
100
100
|
|
|
101
101
|
Attributes:
|
|
@@ -113,7 +113,7 @@ class PairwiseStringResultOutputParser(BaseOutputParser[dict]): # type: ignore[
|
|
|
113
113
|
"""
|
|
114
114
|
return "pairwise_string_result"
|
|
115
115
|
|
|
116
|
-
def parse(self, text: str) ->
|
|
116
|
+
def parse(self, text: str) -> dict[str, Any]:
|
|
117
117
|
"""Parse the output text.
|
|
118
118
|
|
|
119
119
|
Args:
|
|
@@ -151,7 +151,7 @@ class PairwiseStringResultOutputParser(BaseOutputParser[dict]): # type: ignore[
|
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
|
|
154
|
-
class PairwiseStringEvalChain(PairwiseStringEvaluator, LLMEvalChain, LLMChain):
|
|
154
|
+
class PairwiseStringEvalChain(PairwiseStringEvaluator, LLMEvalChain, LLMChain):
|
|
155
155
|
"""A chain for comparing two outputs, such as the outputs
|
|
156
156
|
of two models, prompts, or outputs of a single model on similar inputs.
|
|
157
157
|
|
|
@@ -314,8 +314,8 @@ Performance may be significantly worse with other models."
|
|
|
314
314
|
input: Optional[str] = None,
|
|
315
315
|
reference: Optional[str] = None,
|
|
316
316
|
callbacks: Callbacks = None,
|
|
317
|
-
tags: Optional[
|
|
318
|
-
metadata: Optional[
|
|
317
|
+
tags: Optional[list[str]] = None,
|
|
318
|
+
metadata: Optional[dict[str, Any]] = None,
|
|
319
319
|
include_run_info: bool = False,
|
|
320
320
|
**kwargs: Any,
|
|
321
321
|
) -> dict:
|
|
@@ -356,8 +356,8 @@ Performance may be significantly worse with other models."
|
|
|
356
356
|
reference: Optional[str] = None,
|
|
357
357
|
input: Optional[str] = None,
|
|
358
358
|
callbacks: Callbacks = None,
|
|
359
|
-
tags: Optional[
|
|
360
|
-
metadata: Optional[
|
|
359
|
+
tags: Optional[list[str]] = None,
|
|
360
|
+
metadata: Optional[dict[str, Any]] = None,
|
|
361
361
|
include_run_info: bool = False,
|
|
362
362
|
**kwargs: Any,
|
|
363
363
|
) -> dict:
|
|
@@ -391,7 +391,7 @@ Performance may be significantly worse with other models."
|
|
|
391
391
|
return self._prepare_output(result)
|
|
392
392
|
|
|
393
393
|
|
|
394
|
-
class LabeledPairwiseStringEvalChain(PairwiseStringEvalChain):
|
|
394
|
+
class LabeledPairwiseStringEvalChain(PairwiseStringEvalChain):
|
|
395
395
|
"""A chain for comparing two outputs, such as the outputs
|
|
396
396
|
of two models, prompts, or outputs of a single model on similar inputs,
|
|
397
397
|
with labeled preferences.
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import re
|
|
4
|
+
from collections.abc import Mapping
|
|
4
5
|
from enum import Enum
|
|
5
|
-
from typing import Any,
|
|
6
|
+
from typing import Any, Optional, Union
|
|
6
7
|
|
|
7
8
|
from langchain_core.callbacks.manager import Callbacks
|
|
8
9
|
from langchain_core.language_models import BaseLanguageModel
|
|
@@ -68,7 +69,7 @@ class CriteriaResultOutputParser(BaseOutputParser[dict]):
|
|
|
68
69
|
def _type(self) -> str:
|
|
69
70
|
return "criteria_result"
|
|
70
71
|
|
|
71
|
-
def parse(self, text: str) ->
|
|
72
|
+
def parse(self, text: str) -> dict[str, Any]:
|
|
72
73
|
"""Parse the output text.
|
|
73
74
|
|
|
74
75
|
Args:
|
|
@@ -121,7 +122,7 @@ CRITERIA_TYPE = Union[
|
|
|
121
122
|
|
|
122
123
|
def resolve_criteria(
|
|
123
124
|
criteria: Optional[Union[CRITERIA_TYPE, str]],
|
|
124
|
-
) ->
|
|
125
|
+
) -> dict[str, str]:
|
|
125
126
|
"""Resolve the criteria to evaluate.
|
|
126
127
|
|
|
127
128
|
Parameters
|
|
@@ -164,7 +165,7 @@ def resolve_criteria(
|
|
|
164
165
|
return criteria_
|
|
165
166
|
|
|
166
167
|
|
|
167
|
-
class CriteriaEvalChain(StringEvaluator, LLMEvalChain, LLMChain):
|
|
168
|
+
class CriteriaEvalChain(StringEvaluator, LLMEvalChain, LLMChain):
|
|
168
169
|
"""LLM Chain for evaluating runs against criteria.
|
|
169
170
|
|
|
170
171
|
Parameters
|
|
@@ -285,7 +286,7 @@ class CriteriaEvalChain(StringEvaluator, LLMEvalChain, LLMChain): # type: ignor
|
|
|
285
286
|
def resolve_criteria(
|
|
286
287
|
cls,
|
|
287
288
|
criteria: Optional[Union[CRITERIA_TYPE, str]],
|
|
288
|
-
) ->
|
|
289
|
+
) -> dict[str, str]:
|
|
289
290
|
"""Resolve the criteria to evaluate.
|
|
290
291
|
|
|
291
292
|
Parameters
|
|
@@ -404,8 +405,8 @@ class CriteriaEvalChain(StringEvaluator, LLMEvalChain, LLMChain): # type: ignor
|
|
|
404
405
|
reference: Optional[str] = None,
|
|
405
406
|
input: Optional[str] = None,
|
|
406
407
|
callbacks: Callbacks = None,
|
|
407
|
-
tags: Optional[
|
|
408
|
-
metadata: Optional[
|
|
408
|
+
tags: Optional[list[str]] = None,
|
|
409
|
+
metadata: Optional[dict[str, Any]] = None,
|
|
409
410
|
include_run_info: bool = False,
|
|
410
411
|
**kwargs: Any,
|
|
411
412
|
) -> dict:
|
|
@@ -459,8 +460,8 @@ class CriteriaEvalChain(StringEvaluator, LLMEvalChain, LLMChain): # type: ignor
|
|
|
459
460
|
reference: Optional[str] = None,
|
|
460
461
|
input: Optional[str] = None,
|
|
461
462
|
callbacks: Callbacks = None,
|
|
462
|
-
tags: Optional[
|
|
463
|
-
metadata: Optional[
|
|
463
|
+
tags: Optional[list[str]] = None,
|
|
464
|
+
metadata: Optional[dict[str, Any]] = None,
|
|
464
465
|
include_run_info: bool = False,
|
|
465
466
|
**kwargs: Any,
|
|
466
467
|
) -> dict:
|
|
@@ -508,7 +509,7 @@ class CriteriaEvalChain(StringEvaluator, LLMEvalChain, LLMChain): # type: ignor
|
|
|
508
509
|
return self._prepare_output(result)
|
|
509
510
|
|
|
510
511
|
|
|
511
|
-
class LabeledCriteriaEvalChain(CriteriaEvalChain):
|
|
512
|
+
class LabeledCriteriaEvalChain(CriteriaEvalChain):
|
|
512
513
|
"""Criteria evaluation chain that requires references."""
|
|
513
514
|
|
|
514
515
|
@classmethod
|