langchain-core 1.0.0a6__py3-none-any.whl → 1.0.4__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_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +51 -64
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +55 -48
- langchain_core/caches.py +65 -66
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +321 -336
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +454 -514
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +53 -68
- langchain_core/document_loaders/base.py +27 -25
- langchain_core/document_loaders/blob_loaders.py +1 -1
- langchain_core/document_loaders/langsmith.py +44 -48
- langchain_core/documents/__init__.py +23 -3
- langchain_core/documents/base.py +102 -94
- langchain_core/documents/compressor.py +10 -10
- langchain_core/documents/transformers.py +34 -35
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +2 -2
- langchain_core/example_selectors/semantic_similarity.py +28 -32
- langchain_core/exceptions.py +21 -20
- langchain_core/globals.py +3 -151
- langchain_core/indexing/__init__.py +1 -1
- langchain_core/indexing/api.py +121 -126
- langchain_core/indexing/base.py +73 -75
- langchain_core/indexing/in_memory.py +4 -6
- langchain_core/language_models/__init__.py +14 -29
- langchain_core/language_models/_utils.py +58 -61
- langchain_core/language_models/base.py +82 -172
- langchain_core/language_models/chat_models.py +329 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +189 -269
- langchain_core/load/dump.py +9 -12
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +42 -40
- langchain_core/messages/__init__.py +10 -16
- langchain_core/messages/ai.py +148 -148
- langchain_core/messages/base.py +53 -51
- langchain_core/messages/block_translators/__init__.py +19 -22
- langchain_core/messages/block_translators/anthropic.py +6 -6
- langchain_core/messages/block_translators/bedrock_converse.py +5 -5
- langchain_core/messages/block_translators/google_genai.py +10 -7
- langchain_core/messages/block_translators/google_vertexai.py +4 -32
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +5 -5
- langchain_core/messages/block_translators/openai.py +11 -11
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +339 -330
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +24 -31
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +19 -29
- langchain_core/messages/tool.py +74 -90
- langchain_core/messages/utils.py +484 -510
- langchain_core/output_parsers/__init__.py +13 -10
- langchain_core/output_parsers/base.py +61 -61
- langchain_core/output_parsers/format_instructions.py +9 -4
- langchain_core/output_parsers/json.py +12 -10
- langchain_core/output_parsers/list.py +21 -23
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +30 -23
- langchain_core/output_parsers/pydantic.py +13 -14
- langchain_core/output_parsers/string.py +5 -5
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +35 -34
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +16 -16
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +13 -19
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +81 -86
- langchain_core/prompts/chat.py +308 -351
- langchain_core/prompts/dict.py +6 -6
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +4 -6
- langchain_core/prompts/message.py +7 -7
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +26 -10
- langchain_core/prompts/structured.py +49 -53
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +61 -198
- langchain_core/runnables/base.py +1551 -1656
- langchain_core/runnables/branch.py +68 -70
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +145 -161
- langchain_core/runnables/fallbacks.py +102 -96
- langchain_core/runnables/graph.py +91 -97
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +42 -51
- langchain_core/runnables/graph_png.py +43 -16
- langchain_core/runnables/history.py +175 -177
- langchain_core/runnables/passthrough.py +151 -167
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +30 -35
- langchain_core/runnables/schema.py +75 -80
- langchain_core/runnables/utils.py +60 -67
- langchain_core/stores.py +85 -121
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +29 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +306 -245
- langchain_core/tools/convert.py +160 -155
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +32 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +65 -64
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +45 -45
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +2 -2
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +94 -188
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +9 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +36 -40
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +9 -9
- langchain_core/utils/mustache.py +8 -10
- langchain_core/utils/pydantic.py +35 -37
- langchain_core/utils/strings.py +6 -9
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +66 -62
- langchain_core/vectorstores/base.py +182 -216
- langchain_core/vectorstores/in_memory.py +101 -176
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- langchain_core-1.0.4.dist-info/METADATA +69 -0
- langchain_core-1.0.4.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.4.dist-info}/WHEEL +1 -1
- langchain_core/memory.py +0 -120
- langchain_core/messages/block_translators/ollama.py +0 -47
- langchain_core/prompts/pipeline.py +0 -138
- 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 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-1.0.0a6.dist-info/METADATA +0 -67
- langchain_core-1.0.0a6.dist-info/RECORD +0 -181
- langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
|
@@ -10,7 +10,7 @@ import string
|
|
|
10
10
|
import time
|
|
11
11
|
from dataclasses import asdict
|
|
12
12
|
from pathlib import Path
|
|
13
|
-
from typing import TYPE_CHECKING, Any, Literal
|
|
13
|
+
from typing import TYPE_CHECKING, Any, Literal
|
|
14
14
|
|
|
15
15
|
import yaml
|
|
16
16
|
|
|
@@ -45,50 +45,44 @@ def draw_mermaid(
|
|
|
45
45
|
nodes: dict[str, Node],
|
|
46
46
|
edges: list[Edge],
|
|
47
47
|
*,
|
|
48
|
-
first_node:
|
|
49
|
-
last_node:
|
|
48
|
+
first_node: str | None = None,
|
|
49
|
+
last_node: str | None = None,
|
|
50
50
|
with_styles: bool = True,
|
|
51
51
|
curve_style: CurveStyle = CurveStyle.LINEAR,
|
|
52
|
-
node_styles:
|
|
52
|
+
node_styles: NodeStyles | None = None,
|
|
53
53
|
wrap_label_n_words: int = 9,
|
|
54
|
-
frontmatter_config:
|
|
54
|
+
frontmatter_config: dict[str, Any] | None = None,
|
|
55
55
|
) -> str:
|
|
56
56
|
"""Draws a Mermaid graph using the provided graph data.
|
|
57
57
|
|
|
58
58
|
Args:
|
|
59
|
-
nodes
|
|
60
|
-
edges
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
node_styles (NodeStyles, optional): Node colors for different types.
|
|
69
|
-
Defaults to NodeStyles().
|
|
70
|
-
wrap_label_n_words (int, optional): Words to wrap the edge labels.
|
|
71
|
-
Defaults to 9.
|
|
72
|
-
frontmatter_config (dict[str, Any], optional): Mermaid frontmatter config.
|
|
59
|
+
nodes: List of node ids.
|
|
60
|
+
edges: List of edges, object with a source, target and data.
|
|
61
|
+
first_node: Id of the first node.
|
|
62
|
+
last_node: Id of the last node.
|
|
63
|
+
with_styles: Whether to include styles in the graph.
|
|
64
|
+
curve_style: Curve style for the edges.
|
|
65
|
+
node_styles: Node colors for different types.
|
|
66
|
+
wrap_label_n_words: Words to wrap the edge labels.
|
|
67
|
+
frontmatter_config: Mermaid frontmatter config.
|
|
73
68
|
Can be used to customize theme and styles. Will be converted to YAML and
|
|
74
|
-
added to the beginning of the mermaid graph.
|
|
69
|
+
added to the beginning of the mermaid graph.
|
|
75
70
|
|
|
76
71
|
See more here: https://mermaid.js.org/config/configuration.html.
|
|
77
72
|
|
|
78
73
|
Example config:
|
|
79
74
|
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
```python
|
|
82
76
|
{
|
|
83
77
|
"config": {
|
|
84
78
|
"theme": "neutral",
|
|
85
79
|
"look": "handDrawn",
|
|
86
|
-
"themeVariables": {
|
|
80
|
+
"themeVariables": {"primaryColor": "#e2e2e2"},
|
|
87
81
|
}
|
|
88
82
|
}
|
|
89
|
-
|
|
83
|
+
```
|
|
90
84
|
Returns:
|
|
91
|
-
|
|
85
|
+
Mermaid graph syntax.
|
|
92
86
|
|
|
93
87
|
"""
|
|
94
88
|
# Initialize Mermaid graph configuration
|
|
@@ -164,7 +158,7 @@ def draw_mermaid(
|
|
|
164
158
|
src_parts = edge.source.split(":")
|
|
165
159
|
tgt_parts = edge.target.split(":")
|
|
166
160
|
common_prefix = ":".join(
|
|
167
|
-
src for src, tgt in zip(src_parts, tgt_parts) if src == tgt
|
|
161
|
+
src for src, tgt in zip(src_parts, tgt_parts, strict=False) if src == tgt
|
|
168
162
|
)
|
|
169
163
|
edge_groups.setdefault(common_prefix, []).append(edge)
|
|
170
164
|
|
|
@@ -280,34 +274,28 @@ def _generate_mermaid_graph_styles(node_colors: NodeStyles) -> str:
|
|
|
280
274
|
|
|
281
275
|
def draw_mermaid_png(
|
|
282
276
|
mermaid_syntax: str,
|
|
283
|
-
output_file_path:
|
|
277
|
+
output_file_path: str | None = None,
|
|
284
278
|
draw_method: MermaidDrawMethod = MermaidDrawMethod.API,
|
|
285
|
-
background_color:
|
|
279
|
+
background_color: str | None = "white",
|
|
286
280
|
padding: int = 10,
|
|
287
281
|
max_retries: int = 1,
|
|
288
282
|
retry_delay: float = 1.0,
|
|
289
|
-
base_url:
|
|
283
|
+
base_url: str | None = None,
|
|
290
284
|
) -> bytes:
|
|
291
285
|
"""Draws a Mermaid graph as PNG using provided syntax.
|
|
292
286
|
|
|
293
287
|
Args:
|
|
294
|
-
mermaid_syntax
|
|
295
|
-
output_file_path
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
max_retries (int, optional): Maximum number of retries (MermaidDrawMethod.API).
|
|
303
|
-
Defaults to 1.
|
|
304
|
-
retry_delay (float, optional): Delay between retries (MermaidDrawMethod.API).
|
|
305
|
-
Defaults to 1.0.
|
|
306
|
-
base_url (str, optional): Base URL for the Mermaid.ink API.
|
|
307
|
-
Defaults to None.
|
|
288
|
+
mermaid_syntax: Mermaid graph syntax.
|
|
289
|
+
output_file_path: Path to save the PNG image.
|
|
290
|
+
draw_method: Method to draw the graph.
|
|
291
|
+
background_color: Background color of the image.
|
|
292
|
+
padding: Padding around the image.
|
|
293
|
+
max_retries: Maximum number of retries (MermaidDrawMethod.API).
|
|
294
|
+
retry_delay: Delay between retries (MermaidDrawMethod.API).
|
|
295
|
+
base_url: Base URL for the Mermaid.ink API.
|
|
308
296
|
|
|
309
297
|
Returns:
|
|
310
|
-
|
|
298
|
+
PNG image bytes.
|
|
311
299
|
|
|
312
300
|
Raises:
|
|
313
301
|
ValueError: If an invalid draw method is provided.
|
|
@@ -340,8 +328,8 @@ def draw_mermaid_png(
|
|
|
340
328
|
|
|
341
329
|
async def _render_mermaid_using_pyppeteer(
|
|
342
330
|
mermaid_syntax: str,
|
|
343
|
-
output_file_path:
|
|
344
|
-
background_color:
|
|
331
|
+
output_file_path: str | None = None,
|
|
332
|
+
background_color: str | None = "white",
|
|
345
333
|
padding: int = 10,
|
|
346
334
|
device_scale_factor: int = 3,
|
|
347
335
|
) -> bytes:
|
|
@@ -412,12 +400,12 @@ async def _render_mermaid_using_pyppeteer(
|
|
|
412
400
|
def _render_mermaid_using_api(
|
|
413
401
|
mermaid_syntax: str,
|
|
414
402
|
*,
|
|
415
|
-
output_file_path:
|
|
416
|
-
background_color:
|
|
417
|
-
file_type:
|
|
403
|
+
output_file_path: str | None = None,
|
|
404
|
+
background_color: str | None = "white",
|
|
405
|
+
file_type: Literal["jpeg", "png", "webp"] | None = "png",
|
|
418
406
|
max_retries: int = 1,
|
|
419
407
|
retry_delay: float = 1.0,
|
|
420
|
-
base_url:
|
|
408
|
+
base_url: str | None = None,
|
|
421
409
|
) -> bytes:
|
|
422
410
|
"""Renders Mermaid graph using the Mermaid.INK API."""
|
|
423
411
|
# Defaults to using the public mermaid.ink server.
|
|
@@ -466,7 +454,10 @@ def _render_mermaid_using_api(
|
|
|
466
454
|
return img_bytes
|
|
467
455
|
|
|
468
456
|
# If we get a server error (5xx), retry
|
|
469
|
-
if
|
|
457
|
+
if (
|
|
458
|
+
requests.codes.internal_server_error <= response.status_code
|
|
459
|
+
and attempt < max_retries
|
|
460
|
+
):
|
|
470
461
|
# Exponential backoff with jitter
|
|
471
462
|
sleep_time = retry_delay * (2**attempt) * (0.5 + 0.5 * random.random()) # noqa: S311 not used for crypto
|
|
472
463
|
time.sleep(sleep_time)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Helper class to draw a state graph into a PNG file."""
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from itertools import groupby
|
|
4
|
+
from typing import Any
|
|
4
5
|
|
|
5
6
|
from langchain_core.runnables.graph import Graph, LabelsDict
|
|
6
7
|
|
|
@@ -15,18 +16,17 @@ except ImportError:
|
|
|
15
16
|
class PngDrawer:
|
|
16
17
|
"""Helper class to draw a state graph into a PNG file.
|
|
17
18
|
|
|
18
|
-
It requires
|
|
19
|
+
It requires `graphviz` and `pygraphviz` to be installed.
|
|
19
20
|
|
|
20
21
|
Example:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
drawer.draw(state_graph, "graph.png")
|
|
22
|
+
```python
|
|
23
|
+
drawer = PngDrawer()
|
|
24
|
+
drawer.draw(state_graph, "graph.png")
|
|
25
|
+
```
|
|
26
26
|
"""
|
|
27
27
|
|
|
28
28
|
def __init__(
|
|
29
|
-
self, fontname:
|
|
29
|
+
self, fontname: str | None = None, labels: LabelsDict | None = None
|
|
30
30
|
) -> None:
|
|
31
31
|
"""Initializes the PNG drawer.
|
|
32
32
|
|
|
@@ -46,7 +46,7 @@ class PngDrawer:
|
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
The keys are the original labels, and the values are the new labels.
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
"""
|
|
51
51
|
self.fontname = fontname or "arial"
|
|
52
52
|
self.labels = labels or LabelsDict(nodes={}, edges={})
|
|
@@ -96,7 +96,7 @@ class PngDrawer:
|
|
|
96
96
|
viz: Any,
|
|
97
97
|
source: str,
|
|
98
98
|
target: str,
|
|
99
|
-
label:
|
|
99
|
+
label: str | None = None,
|
|
100
100
|
conditional: bool = False, # noqa: FBT001,FBT002
|
|
101
101
|
) -> None:
|
|
102
102
|
"""Adds an edge to the graph.
|
|
@@ -105,8 +105,8 @@ class PngDrawer:
|
|
|
105
105
|
viz: The graphviz object.
|
|
106
106
|
source: The source node.
|
|
107
107
|
target: The target node.
|
|
108
|
-
label: The label for the edge.
|
|
109
|
-
conditional: Whether the edge is conditional.
|
|
108
|
+
label: The label for the edge.
|
|
109
|
+
conditional: Whether the edge is conditional.
|
|
110
110
|
"""
|
|
111
111
|
viz.add_edge(
|
|
112
112
|
source,
|
|
@@ -117,20 +117,20 @@ class PngDrawer:
|
|
|
117
117
|
style="dotted" if conditional else "solid",
|
|
118
118
|
)
|
|
119
119
|
|
|
120
|
-
def draw(self, graph: Graph, output_path:
|
|
120
|
+
def draw(self, graph: Graph, output_path: str | None = None) -> bytes | None:
|
|
121
121
|
"""Draw the given state graph into a PNG file.
|
|
122
122
|
|
|
123
123
|
Requires `graphviz` and `pygraphviz` to be installed.
|
|
124
124
|
|
|
125
125
|
Args:
|
|
126
126
|
graph: The graph to draw
|
|
127
|
-
output_path: The path to save the PNG. If None
|
|
127
|
+
output_path: The path to save the PNG. If `None`, PNG bytes are returned.
|
|
128
128
|
|
|
129
129
|
Raises:
|
|
130
|
-
ImportError: If
|
|
130
|
+
ImportError: If `pygraphviz` is not installed.
|
|
131
131
|
|
|
132
132
|
Returns:
|
|
133
|
-
The PNG bytes if
|
|
133
|
+
The PNG bytes if `output_path` is None, else None.
|
|
134
134
|
"""
|
|
135
135
|
if not _HAS_PYGRAPHVIZ:
|
|
136
136
|
msg = "Install pygraphviz to draw graphs: `pip install pygraphviz`."
|
|
@@ -142,6 +142,7 @@ class PngDrawer:
|
|
|
142
142
|
# Add nodes, conditional edges, and edges to the graph
|
|
143
143
|
self.add_nodes(viz, graph)
|
|
144
144
|
self.add_edges(viz, graph)
|
|
145
|
+
self.add_subgraph(viz, [node.split(":") for node in graph.nodes])
|
|
145
146
|
|
|
146
147
|
# Update entrypoint and END styles
|
|
147
148
|
self.update_styles(viz, graph)
|
|
@@ -162,6 +163,32 @@ class PngDrawer:
|
|
|
162
163
|
for node in graph.nodes:
|
|
163
164
|
self.add_node(viz, node)
|
|
164
165
|
|
|
166
|
+
def add_subgraph(
|
|
167
|
+
self,
|
|
168
|
+
viz: Any,
|
|
169
|
+
nodes: list[list[str]],
|
|
170
|
+
parent_prefix: list[str] | None = None,
|
|
171
|
+
) -> None:
|
|
172
|
+
"""Add subgraphs to the graph.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
viz: The graphviz object.
|
|
176
|
+
nodes: The nodes to add.
|
|
177
|
+
parent_prefix: The prefix of the parent subgraph.
|
|
178
|
+
"""
|
|
179
|
+
for prefix, grouped in groupby(
|
|
180
|
+
[node[:] for node in sorted(nodes)],
|
|
181
|
+
key=lambda x: x.pop(0),
|
|
182
|
+
):
|
|
183
|
+
current_prefix = (parent_prefix or []) + [prefix]
|
|
184
|
+
grouped_nodes = list(grouped)
|
|
185
|
+
if len(grouped_nodes) > 1:
|
|
186
|
+
subgraph = viz.add_subgraph(
|
|
187
|
+
[":".join(current_prefix + node) for node in grouped_nodes],
|
|
188
|
+
name="cluster_" + ":".join(current_prefix),
|
|
189
|
+
)
|
|
190
|
+
self.add_subgraph(subgraph, grouped_nodes, current_prefix)
|
|
191
|
+
|
|
165
192
|
def add_edges(self, viz: Any, graph: Graph) -> None:
|
|
166
193
|
"""Add edges to the graph.
|
|
167
194
|
|