langchain-core 0.4.0.dev0__py3-none-any.whl → 1.0.0__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/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +45 -70
- langchain_core/_api/deprecation.py +80 -80
- langchain_core/_api/path.py +22 -8
- langchain_core/_import_utils.py +10 -4
- langchain_core/agents.py +25 -21
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +341 -348
- langchain_core/callbacks/file.py +55 -44
- langchain_core/callbacks/manager.py +546 -683
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +35 -36
- langchain_core/callbacks/usage.py +65 -70
- langchain_core/chat_history.py +48 -55
- langchain_core/document_loaders/base.py +46 -21
- langchain_core/document_loaders/langsmith.py +39 -36
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +96 -74
- langchain_core/documents/compressor.py +12 -9
- langchain_core/documents/transformers.py +29 -28
- langchain_core/embeddings/fake.py +56 -57
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +15 -9
- langchain_core/globals.py +4 -163
- langchain_core/indexing/api.py +132 -125
- langchain_core/indexing/base.py +64 -67
- langchain_core/indexing/in_memory.py +26 -6
- langchain_core/language_models/__init__.py +15 -27
- langchain_core/language_models/_utils.py +267 -117
- langchain_core/language_models/base.py +92 -177
- langchain_core/language_models/chat_models.py +547 -407
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +72 -118
- langchain_core/language_models/llms.py +168 -242
- langchain_core/load/dump.py +8 -11
- langchain_core/load/load.py +32 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +50 -56
- langchain_core/messages/__init__.py +36 -51
- langchain_core/messages/ai.py +377 -150
- langchain_core/messages/base.py +239 -47
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -3
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +7 -7
- langchain_core/messages/human.py +44 -38
- langchain_core/messages/modifier.py +3 -2
- langchain_core/messages/system.py +40 -27
- langchain_core/messages/tool.py +160 -58
- langchain_core/messages/utils.py +527 -638
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +68 -104
- langchain_core/output_parsers/json.py +13 -17
- langchain_core/output_parsers/list.py +11 -33
- langchain_core/output_parsers/openai_functions.py +56 -74
- langchain_core/output_parsers/openai_tools.py +68 -109
- langchain_core/output_parsers/pydantic.py +15 -13
- langchain_core/output_parsers/string.py +6 -2
- langchain_core/output_parsers/transform.py +17 -60
- langchain_core/output_parsers/xml.py +34 -44
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +26 -11
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +17 -6
- langchain_core/outputs/llm_result.py +15 -8
- langchain_core/prompt_values.py +29 -123
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -63
- langchain_core/prompts/chat.py +259 -288
- langchain_core/prompts/dict.py +19 -11
- langchain_core/prompts/few_shot.py +84 -90
- langchain_core/prompts/few_shot_with_templates.py +14 -12
- langchain_core/prompts/image.py +19 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +7 -8
- langchain_core/prompts/prompt.py +42 -43
- langchain_core/prompts/string.py +37 -16
- langchain_core/prompts/structured.py +43 -46
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +52 -192
- langchain_core/runnables/base.py +1727 -1683
- langchain_core/runnables/branch.py +52 -73
- langchain_core/runnables/config.py +89 -103
- langchain_core/runnables/configurable.py +128 -130
- langchain_core/runnables/fallbacks.py +93 -82
- langchain_core/runnables/graph.py +127 -127
- langchain_core/runnables/graph_ascii.py +63 -41
- langchain_core/runnables/graph_mermaid.py +87 -70
- langchain_core/runnables/graph_png.py +31 -36
- langchain_core/runnables/history.py +145 -161
- langchain_core/runnables/passthrough.py +141 -144
- langchain_core/runnables/retry.py +84 -68
- langchain_core/runnables/router.py +33 -37
- langchain_core/runnables/schema.py +79 -72
- langchain_core/runnables/utils.py +95 -139
- langchain_core/stores.py +85 -131
- langchain_core/structured_query.py +11 -15
- langchain_core/sys_info.py +31 -32
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +221 -247
- langchain_core/tools/convert.py +144 -161
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -19
- langchain_core/tools/simple.py +52 -29
- langchain_core/tools/structured.py +56 -60
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +103 -112
- langchain_core/tracers/context.py +29 -48
- langchain_core/tracers/core.py +142 -105
- langchain_core/tracers/evaluation.py +30 -34
- langchain_core/tracers/event_stream.py +162 -117
- langchain_core/tracers/langchain.py +34 -36
- langchain_core/tracers/log_stream.py +87 -49
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +18 -34
- langchain_core/tracers/run_collector.py +8 -20
- langchain_core/tracers/schemas.py +0 -125
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +47 -9
- langchain_core/utils/aiter.py +70 -66
- langchain_core/utils/env.py +12 -9
- langchain_core/utils/function_calling.py +139 -206
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +48 -45
- langchain_core/utils/json.py +14 -4
- langchain_core/utils/json_schema.py +159 -43
- langchain_core/utils/mustache.py +32 -25
- langchain_core/utils/pydantic.py +67 -40
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +104 -62
- langchain_core/vectorstores/base.py +131 -179
- langchain_core/vectorstores/in_memory.py +113 -182
- langchain_core/vectorstores/utils.py +23 -17
- langchain_core/version.py +1 -1
- langchain_core-1.0.0.dist-info/METADATA +68 -0
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -448
- langchain_core/memory.py +0 -116
- langchain_core/messages/content_blocks.py +0 -1435
- langchain_core/prompts/pipeline.py +0 -133
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -23
- langchain_core/utils/loading.py +0 -31
- langchain_core/v1/__init__.py +0 -1
- langchain_core/v1/chat_models.py +0 -1047
- langchain_core/v1/messages.py +0 -755
- langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
- langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
- langchain_core-0.4.0.dev0.dist-info/entry_points.txt +0 -4
langchain_core/callbacks/file.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from typing import TYPE_CHECKING, Any,
|
|
6
|
+
from typing import TYPE_CHECKING, Any, TextIO, cast
|
|
7
7
|
|
|
8
8
|
from typing_extensions import Self, override
|
|
9
9
|
|
|
@@ -27,44 +27,45 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
27
27
|
Examples:
|
|
28
28
|
Using as a context manager (recommended):
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
with
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
```python
|
|
31
|
+
with FileCallbackHandler("output.txt") as handler:
|
|
32
|
+
# Use handler with your chain/agent
|
|
33
|
+
chain.invoke(inputs, config={"callbacks": [handler]})
|
|
34
|
+
```
|
|
35
35
|
|
|
36
36
|
Direct instantiation (deprecated):
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
```python
|
|
39
|
+
handler = FileCallbackHandler("output.txt")
|
|
40
|
+
# File remains open until handler is garbage collected
|
|
41
|
+
try:
|
|
42
|
+
chain.invoke(inputs, config={"callbacks": [handler]})
|
|
43
|
+
finally:
|
|
44
|
+
handler.close() # Explicit cleanup recommended
|
|
45
|
+
```
|
|
46
46
|
|
|
47
47
|
Args:
|
|
48
48
|
filename: The file path to write to.
|
|
49
|
-
mode: The file open mode. Defaults to
|
|
50
|
-
color: Default color for text output.
|
|
49
|
+
mode: The file open mode. Defaults to `'a'` (append).
|
|
50
|
+
color: Default color for text output.
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
!!! note
|
|
53
53
|
When not used as a context manager, a deprecation warning will be issued
|
|
54
|
-
on first use. The file will be opened immediately in
|
|
55
|
-
in
|
|
54
|
+
on first use. The file will be opened immediately in `__init__` and closed
|
|
55
|
+
in `__del__` or when `close()` is called explicitly.
|
|
56
56
|
|
|
57
57
|
"""
|
|
58
58
|
|
|
59
59
|
def __init__(
|
|
60
|
-
self, filename: str, mode: str = "a", color:
|
|
60
|
+
self, filename: str, mode: str = "a", color: str | None = None
|
|
61
61
|
) -> None:
|
|
62
62
|
"""Initialize the file callback handler.
|
|
63
63
|
|
|
64
64
|
Args:
|
|
65
65
|
filename: Path to the output file.
|
|
66
|
-
mode: File open mode (e.g.,
|
|
67
|
-
color: Default text color for output.
|
|
66
|
+
mode: File open mode (e.g., `'w'`, `'a'`, `'x'`). Defaults to `'a'`.
|
|
67
|
+
color: Default text color for output.
|
|
68
|
+
|
|
68
69
|
"""
|
|
69
70
|
self.filename = filename
|
|
70
71
|
self.mode = mode
|
|
@@ -82,9 +83,10 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
82
83
|
Returns:
|
|
83
84
|
The FileCallbackHandler instance.
|
|
84
85
|
|
|
85
|
-
|
|
86
|
-
The file is already opened in
|
|
86
|
+
!!! note
|
|
87
|
+
The file is already opened in `__init__`, so this just marks that
|
|
87
88
|
the handler is being used as a context manager.
|
|
89
|
+
|
|
88
90
|
"""
|
|
89
91
|
self._file_opened_in_context = True
|
|
90
92
|
return self
|
|
@@ -101,6 +103,7 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
101
103
|
exc_type: Exception type if an exception occurred.
|
|
102
104
|
exc_val: Exception value if an exception occurred.
|
|
103
105
|
exc_tb: Exception traceback if an exception occurred.
|
|
106
|
+
|
|
104
107
|
"""
|
|
105
108
|
self.close()
|
|
106
109
|
|
|
@@ -113,6 +116,7 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
113
116
|
|
|
114
117
|
This method is safe to call multiple times and will only close
|
|
115
118
|
the file if it's currently open.
|
|
119
|
+
|
|
116
120
|
"""
|
|
117
121
|
if hasattr(self, "file") and self.file and not self.file.closed:
|
|
118
122
|
self.file.close()
|
|
@@ -120,19 +124,20 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
120
124
|
def _write(
|
|
121
125
|
self,
|
|
122
126
|
text: str,
|
|
123
|
-
color:
|
|
127
|
+
color: str | None = None,
|
|
124
128
|
end: str = "",
|
|
125
129
|
) -> None:
|
|
126
130
|
"""Write text to the file with deprecation warning if needed.
|
|
127
131
|
|
|
128
132
|
Args:
|
|
129
133
|
text: The text to write to the file.
|
|
130
|
-
color: Optional color for the text. Defaults to
|
|
131
|
-
end: String appended after the text.
|
|
132
|
-
file: Optional file to write to. Defaults to
|
|
134
|
+
color: Optional color for the text. Defaults to `self.color`.
|
|
135
|
+
end: String appended after the text.
|
|
136
|
+
file: Optional file to write to. Defaults to `self.file`.
|
|
133
137
|
|
|
134
138
|
Raises:
|
|
135
139
|
RuntimeError: If the file is closed or not available.
|
|
140
|
+
|
|
136
141
|
"""
|
|
137
142
|
global _GLOBAL_DEPRECATION_WARNED # noqa: PLW0603
|
|
138
143
|
if not self._file_opened_in_context and not _GLOBAL_DEPRECATION_WARNED:
|
|
@@ -162,7 +167,8 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
162
167
|
Args:
|
|
163
168
|
serialized: The serialized chain information.
|
|
164
169
|
inputs: The inputs to the chain.
|
|
165
|
-
**kwargs: Additional keyword arguments that may contain
|
|
170
|
+
**kwargs: Additional keyword arguments that may contain `'name'`.
|
|
171
|
+
|
|
166
172
|
"""
|
|
167
173
|
name = (
|
|
168
174
|
kwargs.get("name")
|
|
@@ -178,20 +184,22 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
178
184
|
Args:
|
|
179
185
|
outputs: The outputs of the chain.
|
|
180
186
|
**kwargs: Additional keyword arguments.
|
|
187
|
+
|
|
181
188
|
"""
|
|
182
189
|
self._write("\n> Finished chain.", end="\n")
|
|
183
190
|
|
|
184
191
|
@override
|
|
185
192
|
def on_agent_action(
|
|
186
|
-
self, action: AgentAction, color:
|
|
193
|
+
self, action: AgentAction, color: str | None = None, **kwargs: Any
|
|
187
194
|
) -> Any:
|
|
188
195
|
"""Handle agent action by writing the action log.
|
|
189
196
|
|
|
190
197
|
Args:
|
|
191
198
|
action: The agent action containing the log to write.
|
|
192
|
-
color: Color override for this specific output. If
|
|
193
|
-
|
|
199
|
+
color: Color override for this specific output. If `None`, uses
|
|
200
|
+
`self.color`.
|
|
194
201
|
**kwargs: Additional keyword arguments.
|
|
202
|
+
|
|
195
203
|
"""
|
|
196
204
|
self._write(action.log, color=color or self.color)
|
|
197
205
|
|
|
@@ -199,20 +207,21 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
199
207
|
def on_tool_end(
|
|
200
208
|
self,
|
|
201
209
|
output: str,
|
|
202
|
-
color:
|
|
203
|
-
observation_prefix:
|
|
204
|
-
llm_prefix:
|
|
210
|
+
color: str | None = None,
|
|
211
|
+
observation_prefix: str | None = None,
|
|
212
|
+
llm_prefix: str | None = None,
|
|
205
213
|
**kwargs: Any,
|
|
206
214
|
) -> None:
|
|
207
215
|
"""Handle tool end by writing the output with optional prefixes.
|
|
208
216
|
|
|
209
217
|
Args:
|
|
210
218
|
output: The tool output to write.
|
|
211
|
-
color: Color override for this specific output. If
|
|
212
|
-
|
|
219
|
+
color: Color override for this specific output. If `None`, uses
|
|
220
|
+
`self.color`.
|
|
213
221
|
observation_prefix: Optional prefix to write before the output.
|
|
214
222
|
llm_prefix: Optional prefix to write after the output.
|
|
215
223
|
**kwargs: Additional keyword arguments.
|
|
224
|
+
|
|
216
225
|
"""
|
|
217
226
|
if observation_prefix is not None:
|
|
218
227
|
self._write(f"\n{observation_prefix}")
|
|
@@ -222,29 +231,31 @@ class FileCallbackHandler(BaseCallbackHandler):
|
|
|
222
231
|
|
|
223
232
|
@override
|
|
224
233
|
def on_text(
|
|
225
|
-
self, text: str, color:
|
|
234
|
+
self, text: str, color: str | None = None, end: str = "", **kwargs: Any
|
|
226
235
|
) -> None:
|
|
227
236
|
"""Handle text output.
|
|
228
237
|
|
|
229
238
|
Args:
|
|
230
239
|
text: The text to write.
|
|
231
|
-
color: Color override for this specific output. If
|
|
232
|
-
|
|
233
|
-
end: String appended after the text.
|
|
240
|
+
color: Color override for this specific output. If `None`, uses
|
|
241
|
+
`self.color`.
|
|
242
|
+
end: String appended after the text.
|
|
234
243
|
**kwargs: Additional keyword arguments.
|
|
244
|
+
|
|
235
245
|
"""
|
|
236
246
|
self._write(text, color=color or self.color, end=end)
|
|
237
247
|
|
|
238
248
|
@override
|
|
239
249
|
def on_agent_finish(
|
|
240
|
-
self, finish: AgentFinish, color:
|
|
250
|
+
self, finish: AgentFinish, color: str | None = None, **kwargs: Any
|
|
241
251
|
) -> None:
|
|
242
252
|
"""Handle agent finish by writing the finish log.
|
|
243
253
|
|
|
244
254
|
Args:
|
|
245
255
|
finish: The agent finish object containing the log to write.
|
|
246
|
-
color: Color override for this specific output. If
|
|
247
|
-
|
|
256
|
+
color: Color override for this specific output. If `None`, uses
|
|
257
|
+
`self.color`.
|
|
248
258
|
**kwargs: Additional keyword arguments.
|
|
259
|
+
|
|
249
260
|
"""
|
|
250
261
|
self._write(finish.log, color=color or self.color, end="\n")
|