langchain-core 0.3.74__py3-none-any.whl → 0.3.76__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-core might be problematic. Click here for more details.
- langchain_core/_api/beta_decorator.py +18 -41
- langchain_core/_api/deprecation.py +20 -7
- langchain_core/_api/path.py +19 -2
- langchain_core/_import_utils.py +7 -0
- langchain_core/agents.py +10 -6
- langchain_core/beta/runnables/context.py +2 -3
- langchain_core/callbacks/base.py +11 -4
- langchain_core/callbacks/file.py +13 -2
- langchain_core/callbacks/manager.py +129 -78
- langchain_core/callbacks/usage.py +4 -2
- langchain_core/chat_history.py +10 -12
- langchain_core/document_loaders/base.py +34 -9
- langchain_core/document_loaders/langsmith.py +3 -0
- langchain_core/documents/base.py +36 -11
- langchain_core/documents/compressor.py +9 -6
- langchain_core/documents/transformers.py +4 -2
- langchain_core/embeddings/fake.py +8 -5
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/exceptions.py +7 -0
- langchain_core/globals.py +17 -28
- langchain_core/indexing/api.py +56 -44
- langchain_core/indexing/base.py +7 -10
- langchain_core/indexing/in_memory.py +23 -3
- langchain_core/language_models/__init__.py +3 -2
- langchain_core/language_models/base.py +64 -39
- langchain_core/language_models/chat_models.py +130 -42
- langchain_core/language_models/fake_chat_models.py +10 -11
- langchain_core/language_models/llms.py +49 -17
- langchain_core/load/dump.py +5 -7
- langchain_core/load/load.py +15 -1
- langchain_core/load/serializable.py +38 -43
- langchain_core/memory.py +7 -3
- langchain_core/messages/ai.py +36 -16
- langchain_core/messages/base.py +13 -6
- langchain_core/messages/content_blocks.py +23 -2
- langchain_core/messages/human.py +2 -6
- langchain_core/messages/modifier.py +1 -1
- langchain_core/messages/system.py +2 -6
- langchain_core/messages/tool.py +36 -16
- langchain_core/messages/utils.py +198 -87
- langchain_core/output_parsers/base.py +5 -2
- langchain_core/output_parsers/json.py +4 -4
- langchain_core/output_parsers/list.py +7 -22
- langchain_core/output_parsers/openai_functions.py +3 -0
- langchain_core/output_parsers/openai_tools.py +8 -1
- langchain_core/output_parsers/pydantic.py +4 -0
- langchain_core/output_parsers/string.py +5 -1
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +23 -22
- langchain_core/outputs/chat_generation.py +18 -7
- langchain_core/outputs/generation.py +14 -3
- langchain_core/outputs/llm_result.py +8 -1
- langchain_core/prompt_values.py +10 -4
- langchain_core/prompts/base.py +4 -9
- langchain_core/prompts/chat.py +88 -61
- langchain_core/prompts/dict.py +16 -8
- langchain_core/prompts/few_shot.py +9 -11
- langchain_core/prompts/few_shot_with_templates.py +5 -1
- langchain_core/prompts/image.py +12 -5
- langchain_core/prompts/message.py +5 -6
- langchain_core/prompts/pipeline.py +13 -8
- langchain_core/prompts/prompt.py +22 -8
- langchain_core/prompts/string.py +18 -10
- langchain_core/prompts/structured.py +7 -2
- langchain_core/rate_limiters.py +2 -2
- langchain_core/retrievers.py +7 -6
- langchain_core/runnables/base.py +842 -567
- langchain_core/runnables/branch.py +15 -20
- langchain_core/runnables/config.py +11 -17
- langchain_core/runnables/configurable.py +34 -19
- langchain_core/runnables/fallbacks.py +24 -17
- langchain_core/runnables/graph.py +47 -40
- langchain_core/runnables/graph_ascii.py +40 -17
- langchain_core/runnables/graph_mermaid.py +27 -15
- langchain_core/runnables/graph_png.py +27 -31
- langchain_core/runnables/history.py +56 -59
- langchain_core/runnables/passthrough.py +47 -24
- langchain_core/runnables/retry.py +10 -6
- langchain_core/runnables/router.py +10 -9
- langchain_core/runnables/schema.py +2 -0
- langchain_core/runnables/utils.py +51 -89
- langchain_core/stores.py +13 -25
- langchain_core/structured_query.py +3 -7
- langchain_core/sys_info.py +9 -8
- langchain_core/tools/base.py +30 -23
- langchain_core/tools/convert.py +24 -13
- langchain_core/tools/simple.py +35 -3
- langchain_core/tools/structured.py +26 -3
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +2 -2
- langchain_core/tracers/context.py +5 -1
- langchain_core/tracers/core.py +109 -39
- langchain_core/tracers/evaluation.py +22 -26
- langchain_core/tracers/event_stream.py +41 -28
- langchain_core/tracers/langchain.py +12 -3
- langchain_core/tracers/langchain_v1.py +10 -2
- langchain_core/tracers/log_stream.py +57 -18
- langchain_core/tracers/root_listeners.py +4 -20
- langchain_core/tracers/run_collector.py +6 -16
- langchain_core/tracers/schemas.py +5 -1
- langchain_core/utils/aiter.py +14 -6
- langchain_core/utils/env.py +3 -0
- langchain_core/utils/function_calling.py +49 -30
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +11 -3
- langchain_core/utils/json.py +5 -2
- langchain_core/utils/json_schema.py +15 -5
- langchain_core/utils/loading.py +5 -1
- langchain_core/utils/mustache.py +24 -15
- langchain_core/utils/pydantic.py +32 -4
- langchain_core/utils/utils.py +24 -8
- langchain_core/vectorstores/base.py +7 -20
- langchain_core/vectorstores/in_memory.py +18 -12
- langchain_core/vectorstores/utils.py +18 -12
- langchain_core/version.py +1 -1
- langchain_core-0.3.76.dist-info/METADATA +77 -0
- langchain_core-0.3.76.dist-info/RECORD +174 -0
- langchain_core-0.3.74.dist-info/METADATA +0 -108
- langchain_core-0.3.74.dist-info/RECORD +0 -174
- {langchain_core-0.3.74.dist-info → langchain_core-0.3.76.dist-info}/WHEEL +0 -0
- {langchain_core-0.3.74.dist-info → langchain_core-0.3.76.dist-info}/entry_points.txt +0 -0
|
@@ -46,6 +46,10 @@ from langchain_core.messages import (
|
|
|
46
46
|
message_chunk_to_message,
|
|
47
47
|
)
|
|
48
48
|
from langchain_core.messages.ai import _LC_ID_PREFIX
|
|
49
|
+
from langchain_core.output_parsers.openai_tools import (
|
|
50
|
+
JsonOutputKeyToolsParser,
|
|
51
|
+
PydanticToolsParser,
|
|
52
|
+
)
|
|
49
53
|
from langchain_core.outputs import (
|
|
50
54
|
ChatGeneration,
|
|
51
55
|
ChatGenerationChunk,
|
|
@@ -78,6 +82,11 @@ def _generate_response_from_error(error: BaseException) -> list[ChatGeneration]:
|
|
|
78
82
|
if hasattr(error, "response"):
|
|
79
83
|
response = error.response
|
|
80
84
|
metadata: dict = {}
|
|
85
|
+
if hasattr(response, "json"):
|
|
86
|
+
try:
|
|
87
|
+
metadata["body"] = response.json()
|
|
88
|
+
except Exception:
|
|
89
|
+
metadata["body"] = getattr(response, "text", None)
|
|
81
90
|
if hasattr(response, "headers"):
|
|
82
91
|
try:
|
|
83
92
|
metadata["headers"] = dict(response.headers)
|
|
@@ -97,17 +106,18 @@ def _generate_response_from_error(error: BaseException) -> list[ChatGeneration]:
|
|
|
97
106
|
|
|
98
107
|
|
|
99
108
|
def _format_for_tracing(messages: list[BaseMessage]) -> list[BaseMessage]:
|
|
100
|
-
"""Format messages for tracing in on_chat_model_start
|
|
109
|
+
"""Format messages for tracing in ``on_chat_model_start``.
|
|
101
110
|
|
|
102
111
|
- Update image content blocks to OpenAI Chat Completions format (backward
|
|
103
112
|
compatibility).
|
|
104
|
-
- Add
|
|
113
|
+
- Add ``type`` key to content blocks that have a single key.
|
|
105
114
|
|
|
106
115
|
Args:
|
|
107
116
|
messages: List of messages to format.
|
|
108
117
|
|
|
109
118
|
Returns:
|
|
110
119
|
List of messages formatted for tracing.
|
|
120
|
+
|
|
111
121
|
"""
|
|
112
122
|
messages_to_trace = []
|
|
113
123
|
for message in messages:
|
|
@@ -142,8 +152,6 @@ def _format_for_tracing(messages: list[BaseMessage]) -> list[BaseMessage]:
|
|
|
142
152
|
"type": key,
|
|
143
153
|
key: block[key],
|
|
144
154
|
}
|
|
145
|
-
else:
|
|
146
|
-
pass
|
|
147
155
|
messages_to_trace.append(message_to_trace)
|
|
148
156
|
|
|
149
157
|
return messages_to_trace
|
|
@@ -153,10 +161,14 @@ def generate_from_stream(stream: Iterator[ChatGenerationChunk]) -> ChatResult:
|
|
|
153
161
|
"""Generate from a stream.
|
|
154
162
|
|
|
155
163
|
Args:
|
|
156
|
-
stream: Iterator of ChatGenerationChunk
|
|
164
|
+
stream: Iterator of ``ChatGenerationChunk``.
|
|
165
|
+
|
|
166
|
+
Raises:
|
|
167
|
+
ValueError: If no generations are found in the stream.
|
|
157
168
|
|
|
158
169
|
Returns:
|
|
159
170
|
ChatResult: Chat result.
|
|
171
|
+
|
|
160
172
|
"""
|
|
161
173
|
generation = next(stream, None)
|
|
162
174
|
if generation:
|
|
@@ -180,10 +192,11 @@ async def agenerate_from_stream(
|
|
|
180
192
|
"""Async generate from a stream.
|
|
181
193
|
|
|
182
194
|
Args:
|
|
183
|
-
stream: Iterator of ChatGenerationChunk
|
|
195
|
+
stream: Iterator of ``ChatGenerationChunk``.
|
|
184
196
|
|
|
185
197
|
Returns:
|
|
186
198
|
ChatResult: Chat result.
|
|
199
|
+
|
|
187
200
|
"""
|
|
188
201
|
chunks = [chunk async for chunk in stream]
|
|
189
202
|
return await run_in_executor(None, generate_from_stream, iter(chunks))
|
|
@@ -311,15 +324,16 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
311
324
|
provided. This offers the best of both worlds.
|
|
312
325
|
- If False (default), will always use streaming case if available.
|
|
313
326
|
|
|
314
|
-
The main reason for this flag is that code might be written using
|
|
327
|
+
The main reason for this flag is that code might be written using ``stream()`` and
|
|
315
328
|
a user may want to swap out a given model for another model whose the implementation
|
|
316
329
|
does not properly support streaming.
|
|
330
|
+
|
|
317
331
|
"""
|
|
318
332
|
|
|
319
333
|
@model_validator(mode="before")
|
|
320
334
|
@classmethod
|
|
321
335
|
def raise_deprecation(cls, values: dict) -> Any:
|
|
322
|
-
"""
|
|
336
|
+
"""Emit deprecation warning if ``callback_manager`` is used.
|
|
323
337
|
|
|
324
338
|
Args:
|
|
325
339
|
values (Dict): Values to validate.
|
|
@@ -327,8 +341,6 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
327
341
|
Returns:
|
|
328
342
|
Dict: Validated values.
|
|
329
343
|
|
|
330
|
-
Raises:
|
|
331
|
-
DeprecationWarning: If callback_manager is used.
|
|
332
344
|
"""
|
|
333
345
|
if values.get("callback_manager") is not None:
|
|
334
346
|
warnings.warn(
|
|
@@ -528,7 +540,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
528
540
|
generations = [generations_with_error_metadata]
|
|
529
541
|
run_manager.on_llm_error(
|
|
530
542
|
e,
|
|
531
|
-
response=LLMResult(generations=generations),
|
|
543
|
+
response=LLMResult(generations=generations),
|
|
532
544
|
)
|
|
533
545
|
raise
|
|
534
546
|
|
|
@@ -622,7 +634,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
622
634
|
generations = [generations_with_error_metadata]
|
|
623
635
|
await run_manager.on_llm_error(
|
|
624
636
|
e,
|
|
625
|
-
response=LLMResult(generations=generations),
|
|
637
|
+
response=LLMResult(generations=generations),
|
|
626
638
|
)
|
|
627
639
|
raise
|
|
628
640
|
|
|
@@ -653,6 +665,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
653
665
|
|
|
654
666
|
Returns:
|
|
655
667
|
List of ChatGeneration objects.
|
|
668
|
+
|
|
656
669
|
"""
|
|
657
670
|
converted_generations = []
|
|
658
671
|
for gen in cache_val:
|
|
@@ -758,10 +771,11 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
758
771
|
API.
|
|
759
772
|
|
|
760
773
|
Use this method when you want to:
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
774
|
+
|
|
775
|
+
1. Take advantage of batched calls,
|
|
776
|
+
2. Need more output from the model than just the top generated value,
|
|
777
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
778
|
+
type (e.g., pure text completion models vs chat models).
|
|
765
779
|
|
|
766
780
|
Args:
|
|
767
781
|
messages: List of list of messages.
|
|
@@ -778,7 +792,8 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
778
792
|
|
|
779
793
|
Returns:
|
|
780
794
|
An LLMResult, which contains a list of candidate Generations for each input
|
|
781
|
-
|
|
795
|
+
prompt and additional model provider-specific output.
|
|
796
|
+
|
|
782
797
|
"""
|
|
783
798
|
ls_structured_output_format = kwargs.pop(
|
|
784
799
|
"ls_structured_output_format", None
|
|
@@ -835,17 +850,17 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
835
850
|
run_managers[i].on_llm_error(
|
|
836
851
|
e,
|
|
837
852
|
response=LLMResult(
|
|
838
|
-
generations=[generations_with_error_metadata]
|
|
853
|
+
generations=[generations_with_error_metadata]
|
|
839
854
|
),
|
|
840
855
|
)
|
|
841
856
|
raise
|
|
842
857
|
flattened_outputs = [
|
|
843
|
-
LLMResult(generations=[res.generations], llm_output=res.llm_output)
|
|
858
|
+
LLMResult(generations=[res.generations], llm_output=res.llm_output)
|
|
844
859
|
for res in results
|
|
845
860
|
]
|
|
846
861
|
llm_output = self._combine_llm_outputs([res.llm_output for res in results])
|
|
847
862
|
generations = [res.generations for res in results]
|
|
848
|
-
output = LLMResult(generations=generations, llm_output=llm_output)
|
|
863
|
+
output = LLMResult(generations=generations, llm_output=llm_output)
|
|
849
864
|
if run_managers:
|
|
850
865
|
run_infos = []
|
|
851
866
|
for manager, flattened_output in zip(run_managers, flattened_outputs):
|
|
@@ -872,10 +887,11 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
872
887
|
API.
|
|
873
888
|
|
|
874
889
|
Use this method when you want to:
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
890
|
+
|
|
891
|
+
1. Take advantage of batched calls,
|
|
892
|
+
2. Need more output from the model than just the top generated value,
|
|
893
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
894
|
+
type (e.g., pure text completion models vs chat models).
|
|
879
895
|
|
|
880
896
|
Args:
|
|
881
897
|
messages: List of list of messages.
|
|
@@ -892,7 +908,8 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
892
908
|
|
|
893
909
|
Returns:
|
|
894
910
|
An LLMResult, which contains a list of candidate Generations for each input
|
|
895
|
-
|
|
911
|
+
prompt and additional model provider-specific output.
|
|
912
|
+
|
|
896
913
|
"""
|
|
897
914
|
ls_structured_output_format = kwargs.pop(
|
|
898
915
|
"ls_structured_output_format", None
|
|
@@ -954,7 +971,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
954
971
|
await run_managers[i].on_llm_error(
|
|
955
972
|
res,
|
|
956
973
|
response=LLMResult(
|
|
957
|
-
generations=[generations_with_error_metadata]
|
|
974
|
+
generations=[generations_with_error_metadata]
|
|
958
975
|
),
|
|
959
976
|
)
|
|
960
977
|
exceptions.append(res)
|
|
@@ -964,7 +981,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
964
981
|
*[
|
|
965
982
|
run_manager.on_llm_end(
|
|
966
983
|
LLMResult(
|
|
967
|
-
generations=[res.generations], # type: ignore[
|
|
984
|
+
generations=[res.generations], # type: ignore[union-attr]
|
|
968
985
|
llm_output=res.llm_output, # type: ignore[union-attr]
|
|
969
986
|
)
|
|
970
987
|
)
|
|
@@ -974,12 +991,12 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
974
991
|
)
|
|
975
992
|
raise exceptions[0]
|
|
976
993
|
flattened_outputs = [
|
|
977
|
-
LLMResult(generations=[res.generations], llm_output=res.llm_output) # type: ignore[
|
|
994
|
+
LLMResult(generations=[res.generations], llm_output=res.llm_output) # type: ignore[union-attr]
|
|
978
995
|
for res in results
|
|
979
996
|
]
|
|
980
997
|
llm_output = self._combine_llm_outputs([res.llm_output for res in results]) # type: ignore[union-attr]
|
|
981
998
|
generations = [res.generations for res in results] # type: ignore[union-attr]
|
|
982
|
-
output = LLMResult(generations=generations, llm_output=llm_output)
|
|
999
|
+
output = LLMResult(generations=generations, llm_output=llm_output)
|
|
983
1000
|
await asyncio.gather(
|
|
984
1001
|
*[
|
|
985
1002
|
run_manager.on_llm_end(flattened_output)
|
|
@@ -1172,7 +1189,17 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1172
1189
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
1173
1190
|
**kwargs: Any,
|
|
1174
1191
|
) -> ChatResult:
|
|
1175
|
-
"""
|
|
1192
|
+
"""Generate the result.
|
|
1193
|
+
|
|
1194
|
+
Args:
|
|
1195
|
+
messages: The messages to generate from.
|
|
1196
|
+
stop: Optional list of stop words to use when generating.
|
|
1197
|
+
run_manager: Optional callback manager to use for this call.
|
|
1198
|
+
**kwargs: Additional keyword arguments to pass to the model.
|
|
1199
|
+
|
|
1200
|
+
Returns:
|
|
1201
|
+
The chat result.
|
|
1202
|
+
"""
|
|
1176
1203
|
|
|
1177
1204
|
async def _agenerate(
|
|
1178
1205
|
self,
|
|
@@ -1181,7 +1208,17 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1181
1208
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
1182
1209
|
**kwargs: Any,
|
|
1183
1210
|
) -> ChatResult:
|
|
1184
|
-
"""
|
|
1211
|
+
"""Generate the result.
|
|
1212
|
+
|
|
1213
|
+
Args:
|
|
1214
|
+
messages: The messages to generate from.
|
|
1215
|
+
stop: Optional list of stop words to use when generating.
|
|
1216
|
+
run_manager: Optional callback manager to use for this call.
|
|
1217
|
+
**kwargs: Additional keyword arguments to pass to the model.
|
|
1218
|
+
|
|
1219
|
+
Returns:
|
|
1220
|
+
The chat result.
|
|
1221
|
+
"""
|
|
1185
1222
|
return await run_in_executor(
|
|
1186
1223
|
None,
|
|
1187
1224
|
self._generate,
|
|
@@ -1198,6 +1235,17 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1198
1235
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
1199
1236
|
**kwargs: Any,
|
|
1200
1237
|
) -> Iterator[ChatGenerationChunk]:
|
|
1238
|
+
"""Stream the output of the model.
|
|
1239
|
+
|
|
1240
|
+
Args:
|
|
1241
|
+
messages: The messages to generate from.
|
|
1242
|
+
stop: Optional list of stop words to use when generating.
|
|
1243
|
+
run_manager: Optional callback manager to use for this call.
|
|
1244
|
+
**kwargs: Additional keyword arguments to pass to the model.
|
|
1245
|
+
|
|
1246
|
+
Yields:
|
|
1247
|
+
The chat generation chunks.
|
|
1248
|
+
"""
|
|
1201
1249
|
raise NotImplementedError
|
|
1202
1250
|
|
|
1203
1251
|
async def _astream(
|
|
@@ -1207,6 +1255,17 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1207
1255
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
1208
1256
|
**kwargs: Any,
|
|
1209
1257
|
) -> AsyncIterator[ChatGenerationChunk]:
|
|
1258
|
+
"""Stream the output of the model.
|
|
1259
|
+
|
|
1260
|
+
Args:
|
|
1261
|
+
messages: The messages to generate from.
|
|
1262
|
+
stop: Optional list of stop words to use when generating.
|
|
1263
|
+
run_manager: Optional callback manager to use for this call.
|
|
1264
|
+
**kwargs: Additional keyword arguments to pass to the model.
|
|
1265
|
+
|
|
1266
|
+
Yields:
|
|
1267
|
+
The chat generation chunks.
|
|
1268
|
+
"""
|
|
1210
1269
|
iterator = await run_in_executor(
|
|
1211
1270
|
None,
|
|
1212
1271
|
self._stream,
|
|
@@ -1246,8 +1305,12 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1246
1305
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
1247
1306
|
to the model provider API call.
|
|
1248
1307
|
|
|
1308
|
+
Raises:
|
|
1309
|
+
ValueError: If the generation is not a chat generation.
|
|
1310
|
+
|
|
1249
1311
|
Returns:
|
|
1250
1312
|
The model output message.
|
|
1313
|
+
|
|
1251
1314
|
"""
|
|
1252
1315
|
generation = self.generate(
|
|
1253
1316
|
[messages], stop=stop, callbacks=callbacks, **kwargs
|
|
@@ -1288,6 +1351,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1288
1351
|
|
|
1289
1352
|
Returns:
|
|
1290
1353
|
The model output string.
|
|
1354
|
+
|
|
1291
1355
|
"""
|
|
1292
1356
|
return self.predict(message, stop=stop, **kwargs)
|
|
1293
1357
|
|
|
@@ -1305,8 +1369,12 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1305
1369
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
1306
1370
|
to the model provider API call.
|
|
1307
1371
|
|
|
1372
|
+
Raises:
|
|
1373
|
+
ValueError: If the output is not a string.
|
|
1374
|
+
|
|
1308
1375
|
Returns:
|
|
1309
1376
|
The predicted output string.
|
|
1377
|
+
|
|
1310
1378
|
"""
|
|
1311
1379
|
stop_ = None if stop is None else list(stop)
|
|
1312
1380
|
result = self([HumanMessage(content=text)], stop=stop_, **kwargs)
|
|
@@ -1382,6 +1450,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1382
1450
|
|
|
1383
1451
|
Returns:
|
|
1384
1452
|
A Runnable that returns a message.
|
|
1453
|
+
|
|
1385
1454
|
"""
|
|
1386
1455
|
raise NotImplementedError
|
|
1387
1456
|
|
|
@@ -1417,6 +1486,11 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1417
1486
|
will be caught and returned as well. The final output is always a dict
|
|
1418
1487
|
with keys ``'raw'``, ``'parsed'``, and ``'parsing_error'``.
|
|
1419
1488
|
|
|
1489
|
+
Raises:
|
|
1490
|
+
ValueError: If there are any unsupported ``kwargs``.
|
|
1491
|
+
NotImplementedError: If the model does not implement
|
|
1492
|
+
``with_structured_output()``.
|
|
1493
|
+
|
|
1420
1494
|
Returns:
|
|
1421
1495
|
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
|
1422
1496
|
|
|
@@ -1436,15 +1510,20 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1436
1510
|
|
|
1437
1511
|
from pydantic import BaseModel
|
|
1438
1512
|
|
|
1513
|
+
|
|
1439
1514
|
class AnswerWithJustification(BaseModel):
|
|
1440
1515
|
'''An answer to the user question along with justification for the answer.'''
|
|
1516
|
+
|
|
1441
1517
|
answer: str
|
|
1442
1518
|
justification: str
|
|
1443
1519
|
|
|
1520
|
+
|
|
1444
1521
|
llm = ChatModel(model="model-name", temperature=0)
|
|
1445
1522
|
structured_llm = llm.with_structured_output(AnswerWithJustification)
|
|
1446
1523
|
|
|
1447
|
-
structured_llm.invoke(
|
|
1524
|
+
structured_llm.invoke(
|
|
1525
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
|
1526
|
+
)
|
|
1448
1527
|
|
|
1449
1528
|
# -> AnswerWithJustification(
|
|
1450
1529
|
# answer='They weigh the same',
|
|
@@ -1456,15 +1535,22 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1456
1535
|
|
|
1457
1536
|
from pydantic import BaseModel
|
|
1458
1537
|
|
|
1538
|
+
|
|
1459
1539
|
class AnswerWithJustification(BaseModel):
|
|
1460
1540
|
'''An answer to the user question along with justification for the answer.'''
|
|
1541
|
+
|
|
1461
1542
|
answer: str
|
|
1462
1543
|
justification: str
|
|
1463
1544
|
|
|
1545
|
+
|
|
1464
1546
|
llm = ChatModel(model="model-name", temperature=0)
|
|
1465
|
-
structured_llm = llm.with_structured_output(
|
|
1547
|
+
structured_llm = llm.with_structured_output(
|
|
1548
|
+
AnswerWithJustification, include_raw=True
|
|
1549
|
+
)
|
|
1466
1550
|
|
|
1467
|
-
structured_llm.invoke(
|
|
1551
|
+
structured_llm.invoke(
|
|
1552
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
|
1553
|
+
)
|
|
1468
1554
|
# -> {
|
|
1469
1555
|
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
|
|
1470
1556
|
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
|
|
@@ -1477,16 +1563,21 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1477
1563
|
from pydantic import BaseModel
|
|
1478
1564
|
from langchain_core.utils.function_calling import convert_to_openai_tool
|
|
1479
1565
|
|
|
1566
|
+
|
|
1480
1567
|
class AnswerWithJustification(BaseModel):
|
|
1481
1568
|
'''An answer to the user question along with justification for the answer.'''
|
|
1569
|
+
|
|
1482
1570
|
answer: str
|
|
1483
1571
|
justification: str
|
|
1484
1572
|
|
|
1573
|
+
|
|
1485
1574
|
dict_schema = convert_to_openai_tool(AnswerWithJustification)
|
|
1486
1575
|
llm = ChatModel(model="model-name", temperature=0)
|
|
1487
1576
|
structured_llm = llm.with_structured_output(dict_schema)
|
|
1488
1577
|
|
|
1489
|
-
structured_llm.invoke(
|
|
1578
|
+
structured_llm.invoke(
|
|
1579
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
|
1580
|
+
)
|
|
1490
1581
|
# -> {
|
|
1491
1582
|
# 'answer': 'They weigh the same',
|
|
1492
1583
|
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
|
|
@@ -1503,11 +1594,6 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1503
1594
|
msg = f"Received unsupported arguments {kwargs}"
|
|
1504
1595
|
raise ValueError(msg)
|
|
1505
1596
|
|
|
1506
|
-
from langchain_core.output_parsers.openai_tools import (
|
|
1507
|
-
JsonOutputKeyToolsParser,
|
|
1508
|
-
PydanticToolsParser,
|
|
1509
|
-
)
|
|
1510
|
-
|
|
1511
1597
|
if type(self).bind_tools is BaseChatModel.bind_tools:
|
|
1512
1598
|
msg = "with_structured_output is not implemented for this model."
|
|
1513
1599
|
raise NotImplementedError(msg)
|
|
@@ -1544,8 +1630,10 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
|
|
|
1544
1630
|
class SimpleChatModel(BaseChatModel):
|
|
1545
1631
|
"""Simplified implementation for a chat model to inherit from.
|
|
1546
1632
|
|
|
1547
|
-
|
|
1548
|
-
|
|
1633
|
+
.. note::
|
|
1634
|
+
This implementation is primarily here for backwards compatibility. For new
|
|
1635
|
+
implementations, please use ``BaseChatModel`` directly.
|
|
1636
|
+
|
|
1549
1637
|
"""
|
|
1550
1638
|
|
|
1551
1639
|
def _generate(
|
|
@@ -75,12 +75,13 @@ class FakeListChatModel(SimpleChatModel):
|
|
|
75
75
|
@override
|
|
76
76
|
def _call(
|
|
77
77
|
self,
|
|
78
|
-
|
|
79
|
-
stop: Optional[list[str]] = None,
|
|
80
|
-
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
78
|
+
*args: Any,
|
|
81
79
|
**kwargs: Any,
|
|
82
80
|
) -> str:
|
|
83
|
-
"""
|
|
81
|
+
"""Return the next response in the list.
|
|
82
|
+
|
|
83
|
+
Cycle back to the start if at the end.
|
|
84
|
+
"""
|
|
84
85
|
if self.sleep is not None:
|
|
85
86
|
time.sleep(self.sleep)
|
|
86
87
|
response = self.responses[self.i]
|
|
@@ -223,11 +224,12 @@ class GenericFakeChatModel(BaseChatModel):
|
|
|
223
224
|
This can be expanded to accept other types like Callables / dicts / strings
|
|
224
225
|
to make the interface more generic if needed.
|
|
225
226
|
|
|
226
|
-
|
|
227
|
+
.. note::
|
|
228
|
+
if you want to pass a list, you can use ``iter`` to convert it to an iterator.
|
|
227
229
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
230
|
+
.. warning::
|
|
231
|
+
Streaming is not implemented yet. We should try to implement it in the future by
|
|
232
|
+
delegating to invoke and then breaking the resulting output into message chunks.
|
|
231
233
|
"""
|
|
232
234
|
|
|
233
235
|
@override
|
|
@@ -238,7 +240,6 @@ class GenericFakeChatModel(BaseChatModel):
|
|
|
238
240
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
239
241
|
**kwargs: Any,
|
|
240
242
|
) -> ChatResult:
|
|
241
|
-
"""Top Level call."""
|
|
242
243
|
message = next(self.messages)
|
|
243
244
|
message_ = AIMessage(content=message) if isinstance(message, str) else message
|
|
244
245
|
generation = ChatGeneration(message=message_)
|
|
@@ -251,7 +252,6 @@ class GenericFakeChatModel(BaseChatModel):
|
|
|
251
252
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
252
253
|
**kwargs: Any,
|
|
253
254
|
) -> Iterator[ChatGenerationChunk]:
|
|
254
|
-
"""Stream the output of the model."""
|
|
255
255
|
chat_result = self._generate(
|
|
256
256
|
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
257
257
|
)
|
|
@@ -361,7 +361,6 @@ class ParrotFakeChatModel(BaseChatModel):
|
|
|
361
361
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
362
362
|
**kwargs: Any,
|
|
363
363
|
) -> ChatResult:
|
|
364
|
-
"""Top Level call."""
|
|
365
364
|
return ChatResult(generations=[ChatGeneration(message=messages[-1])])
|
|
366
365
|
|
|
367
366
|
@property
|
|
@@ -131,6 +131,7 @@ def create_base_retry_decorator(
|
|
|
131
131
|
|
|
132
132
|
def _resolve_cache(*, cache: Union[BaseCache, bool, None]) -> Optional[BaseCache]:
|
|
133
133
|
"""Resolve the cache."""
|
|
134
|
+
llm_cache: Optional[BaseCache]
|
|
134
135
|
if isinstance(cache, BaseCache):
|
|
135
136
|
llm_cache = cache
|
|
136
137
|
elif cache is None:
|
|
@@ -663,7 +664,18 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
663
664
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
664
665
|
**kwargs: Any,
|
|
665
666
|
) -> LLMResult:
|
|
666
|
-
"""Run the LLM on the given prompts.
|
|
667
|
+
"""Run the LLM on the given prompts.
|
|
668
|
+
|
|
669
|
+
Args:
|
|
670
|
+
prompts: The prompts to generate from.
|
|
671
|
+
stop: Stop words to use when generating. Model output is cut off at the
|
|
672
|
+
first occurrence of any of the stop substrings.
|
|
673
|
+
If stop tokens are not supported consider raising NotImplementedError.
|
|
674
|
+
run_manager: Callback manager for the run.
|
|
675
|
+
|
|
676
|
+
Returns:
|
|
677
|
+
The LLM result.
|
|
678
|
+
"""
|
|
667
679
|
|
|
668
680
|
async def _agenerate(
|
|
669
681
|
self,
|
|
@@ -672,7 +684,18 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
672
684
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
673
685
|
**kwargs: Any,
|
|
674
686
|
) -> LLMResult:
|
|
675
|
-
"""Run the LLM on the given prompts.
|
|
687
|
+
"""Run the LLM on the given prompts.
|
|
688
|
+
|
|
689
|
+
Args:
|
|
690
|
+
prompts: The prompts to generate from.
|
|
691
|
+
stop: Stop words to use when generating. Model output is cut off at the
|
|
692
|
+
first occurrence of any of the stop substrings.
|
|
693
|
+
If stop tokens are not supported consider raising NotImplementedError.
|
|
694
|
+
run_manager: Callback manager for the run.
|
|
695
|
+
|
|
696
|
+
Returns:
|
|
697
|
+
The LLM result.
|
|
698
|
+
"""
|
|
676
699
|
return await run_in_executor(
|
|
677
700
|
None,
|
|
678
701
|
self._generate,
|
|
@@ -705,8 +728,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
705
728
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
706
729
|
to the model provider API call.
|
|
707
730
|
|
|
708
|
-
|
|
709
|
-
|
|
731
|
+
Yields:
|
|
732
|
+
Generation chunks.
|
|
710
733
|
"""
|
|
711
734
|
raise NotImplementedError
|
|
712
735
|
|
|
@@ -731,8 +754,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
731
754
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
732
755
|
to the model provider API call.
|
|
733
756
|
|
|
734
|
-
|
|
735
|
-
|
|
757
|
+
Yields:
|
|
758
|
+
Generation chunks.
|
|
736
759
|
"""
|
|
737
760
|
iterator = await run_in_executor(
|
|
738
761
|
None,
|
|
@@ -830,10 +853,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
830
853
|
API.
|
|
831
854
|
|
|
832
855
|
Use this method when you want to:
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
856
|
+
|
|
857
|
+
1. Take advantage of batched calls,
|
|
858
|
+
2. Need more output from the model than just the top generated value,
|
|
859
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
860
|
+
type (e.g., pure text completion models vs chat models).
|
|
837
861
|
|
|
838
862
|
Args:
|
|
839
863
|
prompts: List of string prompts.
|
|
@@ -853,6 +877,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
853
877
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
854
878
|
to the model provider API call.
|
|
855
879
|
|
|
880
|
+
Raises:
|
|
881
|
+
ValueError: If prompts is not a list.
|
|
882
|
+
ValueError: If the length of ``callbacks``, ``tags``, ``metadata``, or
|
|
883
|
+
``run_name`` (if provided) does not match the length of prompts.
|
|
884
|
+
|
|
856
885
|
Returns:
|
|
857
886
|
An LLMResult, which contains a list of candidate Generations for each input
|
|
858
887
|
prompt and additional model provider-specific output.
|
|
@@ -1090,10 +1119,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1090
1119
|
API.
|
|
1091
1120
|
|
|
1092
1121
|
Use this method when you want to:
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1122
|
+
|
|
1123
|
+
1. Take advantage of batched calls,
|
|
1124
|
+
2. Need more output from the model than just the top generated value,
|
|
1125
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
1126
|
+
type (e.g., pure text completion models vs chat models).
|
|
1097
1127
|
|
|
1098
1128
|
Args:
|
|
1099
1129
|
prompts: List of string prompts.
|
|
@@ -1113,6 +1143,10 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1113
1143
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
1114
1144
|
to the model provider API call.
|
|
1115
1145
|
|
|
1146
|
+
Raises:
|
|
1147
|
+
ValueError: If the length of ``callbacks``, ``tags``, ``metadata``, or
|
|
1148
|
+
``run_name`` (if provided) does not match the length of prompts.
|
|
1149
|
+
|
|
1116
1150
|
Returns:
|
|
1117
1151
|
An LLMResult, which contains a list of candidate Generations for each input
|
|
1118
1152
|
prompt and additional model provider-specific output.
|
|
@@ -1388,7 +1422,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1388
1422
|
return AIMessage(content=content)
|
|
1389
1423
|
|
|
1390
1424
|
def __str__(self) -> str:
|
|
1391
|
-
"""
|
|
1425
|
+
"""Return a string representation of the object for printing."""
|
|
1392
1426
|
cls_name = f"\033[1m{self.__class__.__name__}\033[0m"
|
|
1393
1427
|
return f"{cls_name}\nParams: {self._identifying_params}"
|
|
1394
1428
|
|
|
@@ -1536,7 +1570,6 @@ class LLM(BaseLLM):
|
|
|
1536
1570
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
1537
1571
|
**kwargs: Any,
|
|
1538
1572
|
) -> LLMResult:
|
|
1539
|
-
"""Run the LLM on the given prompt and input."""
|
|
1540
1573
|
# TODO: add caching here.
|
|
1541
1574
|
generations = []
|
|
1542
1575
|
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
|
|
@@ -1556,7 +1589,6 @@ class LLM(BaseLLM):
|
|
|
1556
1589
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
1557
1590
|
**kwargs: Any,
|
|
1558
1591
|
) -> LLMResult:
|
|
1559
|
-
"""Async run the LLM on the given prompt and input."""
|
|
1560
1592
|
generations = []
|
|
1561
1593
|
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
|
|
1562
1594
|
for prompt in prompts:
|