langchain-core 1.0.0a3__py3-none-any.whl → 1.0.0a5__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/_api/beta_decorator.py +6 -5
- langchain_core/_api/deprecation.py +11 -11
- langchain_core/callbacks/manager.py +2 -2
- langchain_core/callbacks/usage.py +2 -2
- langchain_core/document_loaders/langsmith.py +1 -1
- langchain_core/indexing/api.py +30 -30
- langchain_core/language_models/chat_models.py +7 -6
- langchain_core/language_models/fake_chat_models.py +5 -2
- langchain_core/load/serializable.py +1 -1
- langchain_core/messages/__init__.py +9 -15
- langchain_core/messages/ai.py +75 -9
- langchain_core/messages/base.py +79 -37
- langchain_core/messages/block_translators/__init__.py +11 -1
- langchain_core/messages/block_translators/anthropic.py +151 -134
- langchain_core/messages/block_translators/bedrock.py +73 -26
- langchain_core/messages/block_translators/bedrock_converse.py +270 -22
- langchain_core/messages/block_translators/langchain_v0.py +180 -43
- langchain_core/messages/block_translators/openai.py +224 -42
- langchain_core/messages/chat.py +4 -1
- langchain_core/messages/content.py +56 -112
- langchain_core/messages/function.py +9 -5
- langchain_core/messages/human.py +6 -2
- langchain_core/messages/modifier.py +1 -0
- langchain_core/messages/system.py +9 -2
- langchain_core/messages/tool.py +31 -14
- langchain_core/messages/utils.py +89 -83
- langchain_core/outputs/chat_generation.py +10 -6
- langchain_core/prompt_values.py +6 -2
- langchain_core/prompts/chat.py +6 -3
- langchain_core/prompts/few_shot.py +4 -1
- langchain_core/runnables/base.py +4 -1
- langchain_core/runnables/graph_ascii.py +1 -1
- langchain_core/tools/base.py +1 -2
- langchain_core/tools/convert.py +1 -1
- langchain_core/utils/aiter.py +1 -1
- langchain_core/utils/function_calling.py +5 -6
- langchain_core/utils/iter.py +1 -1
- langchain_core/vectorstores/in_memory.py +5 -5
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a3.dist-info → langchain_core-1.0.0a5.dist-info}/METADATA +8 -8
- {langchain_core-1.0.0a3.dist-info → langchain_core-1.0.0a5.dist-info}/RECORD +43 -43
- {langchain_core-1.0.0a3.dist-info → langchain_core-1.0.0a5.dist-info}/WHEEL +0 -0
- {langchain_core-1.0.0a3.dist-info → langchain_core-1.0.0a5.dist-info}/entry_points.txt +0 -0
|
@@ -1,39 +1,86 @@
|
|
|
1
|
-
"""Derivations of standard content blocks from
|
|
2
|
-
|
|
3
|
-
import warnings
|
|
1
|
+
"""Derivations of standard content blocks from Bedrock content."""
|
|
4
2
|
|
|
5
3
|
from langchain_core.messages import AIMessage, AIMessageChunk
|
|
6
4
|
from langchain_core.messages import content as types
|
|
5
|
+
from langchain_core.messages.block_translators.anthropic import (
|
|
6
|
+
_convert_to_v1_from_anthropic,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _convert_to_v1_from_bedrock(message: AIMessage) -> list[types.ContentBlock]:
|
|
11
|
+
"""Convert bedrock message content to v1 format."""
|
|
12
|
+
out = _convert_to_v1_from_anthropic(message)
|
|
13
|
+
|
|
14
|
+
content_tool_call_ids = {
|
|
15
|
+
block.get("id")
|
|
16
|
+
for block in out
|
|
17
|
+
if isinstance(block, dict) and block.get("type") == "tool_call"
|
|
18
|
+
}
|
|
19
|
+
for tool_call in message.tool_calls:
|
|
20
|
+
if (id_ := tool_call.get("id")) and id_ not in content_tool_call_ids:
|
|
21
|
+
tool_call_block: types.ToolCall = {
|
|
22
|
+
"type": "tool_call",
|
|
23
|
+
"id": id_,
|
|
24
|
+
"name": tool_call["name"],
|
|
25
|
+
"args": tool_call["args"],
|
|
26
|
+
}
|
|
27
|
+
if "index" in tool_call:
|
|
28
|
+
tool_call_block["index"] = tool_call["index"] # type: ignore[typeddict-item]
|
|
29
|
+
if "extras" in tool_call:
|
|
30
|
+
tool_call_block["extras"] = tool_call["extras"] # type: ignore[typeddict-item]
|
|
31
|
+
out.append(tool_call_block)
|
|
32
|
+
return out
|
|
33
|
+
|
|
7
34
|
|
|
8
|
-
|
|
35
|
+
def _convert_to_v1_from_bedrock_chunk(
|
|
36
|
+
message: AIMessageChunk,
|
|
37
|
+
) -> list[types.ContentBlock]:
|
|
38
|
+
"""Convert bedrock message chunk content to v1 format."""
|
|
39
|
+
if (
|
|
40
|
+
message.content == ""
|
|
41
|
+
and not message.additional_kwargs
|
|
42
|
+
and not message.tool_calls
|
|
43
|
+
):
|
|
44
|
+
# Bedrock outputs multiple chunks containing response metadata
|
|
45
|
+
return []
|
|
9
46
|
|
|
47
|
+
out = _convert_to_v1_from_anthropic(message)
|
|
10
48
|
|
|
11
|
-
|
|
49
|
+
if (
|
|
50
|
+
message.tool_call_chunks
|
|
51
|
+
and not message.content
|
|
52
|
+
and message.chunk_position != "last" # keep tool_calls if aggregated
|
|
53
|
+
):
|
|
54
|
+
for tool_call_chunk in message.tool_call_chunks:
|
|
55
|
+
tc: types.ToolCallChunk = {
|
|
56
|
+
"type": "tool_call_chunk",
|
|
57
|
+
"id": tool_call_chunk.get("id"),
|
|
58
|
+
"name": tool_call_chunk.get("name"),
|
|
59
|
+
"args": tool_call_chunk.get("args"),
|
|
60
|
+
}
|
|
61
|
+
if (idx := tool_call_chunk.get("index")) is not None:
|
|
62
|
+
tc["index"] = idx
|
|
63
|
+
out.append(tc)
|
|
64
|
+
return out
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def translate_content(message: AIMessage) -> list[types.ContentBlock]:
|
|
12
68
|
"""Derive standard content blocks from a message with Bedrock content."""
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"""Derive standard content blocks from a chunk with Bedrock content."""
|
|
25
|
-
global WARNED # noqa: PLW0603
|
|
26
|
-
if not WARNED:
|
|
27
|
-
warning_message = (
|
|
28
|
-
"Content block standardization is not yet fully supported for Bedrock."
|
|
29
|
-
)
|
|
30
|
-
warnings.warn(warning_message, stacklevel=2)
|
|
31
|
-
WARNED = True
|
|
32
|
-
raise NotImplementedError
|
|
69
|
+
if "claude" not in message.response_metadata.get("model_name", "").lower():
|
|
70
|
+
raise NotImplementedError # fall back to best-effort parsing
|
|
71
|
+
return _convert_to_v1_from_bedrock(message)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def translate_content_chunk(message: AIMessageChunk) -> list[types.ContentBlock]:
|
|
75
|
+
"""Derive standard content blocks from a message chunk with Bedrock content."""
|
|
76
|
+
# TODO: add model_name to all Bedrock chunks and update core merging logic
|
|
77
|
+
# to not append during aggregation. Then raise NotImplementedError here if
|
|
78
|
+
# not an Anthropic model to fall back to best-effort parsing.
|
|
79
|
+
return _convert_to_v1_from_bedrock_chunk(message)
|
|
33
80
|
|
|
34
81
|
|
|
35
82
|
def _register_bedrock_translator() -> None:
|
|
36
|
-
"""Register the
|
|
83
|
+
"""Register the bedrock translator with the central registry.
|
|
37
84
|
|
|
38
85
|
Run automatically when the module is imported.
|
|
39
86
|
"""
|
|
@@ -1,37 +1,285 @@
|
|
|
1
1
|
"""Derivations of standard content blocks from Amazon (Bedrock Converse) content."""
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import base64
|
|
4
|
+
from collections.abc import Iterable
|
|
5
|
+
from typing import Any, Optional, cast
|
|
4
6
|
|
|
5
7
|
from langchain_core.messages import AIMessage, AIMessageChunk
|
|
6
8
|
from langchain_core.messages import content as types
|
|
7
9
|
|
|
8
|
-
WARNED = False
|
|
9
10
|
|
|
11
|
+
def _bytes_to_b64_str(bytes_: bytes) -> str:
|
|
12
|
+
return base64.b64encode(bytes_).decode("utf-8")
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
|
|
15
|
+
def _populate_extras(
|
|
16
|
+
standard_block: types.ContentBlock, block: dict[str, Any], known_fields: set[str]
|
|
17
|
+
) -> types.ContentBlock:
|
|
18
|
+
"""Mutate a block, populating extras."""
|
|
19
|
+
if standard_block.get("type") == "non_standard":
|
|
20
|
+
return standard_block
|
|
21
|
+
|
|
22
|
+
for key, value in block.items():
|
|
23
|
+
if key not in known_fields:
|
|
24
|
+
if "extras" not in standard_block:
|
|
25
|
+
# Below type-ignores are because mypy thinks a non-standard block can
|
|
26
|
+
# get here, although we exclude them above.
|
|
27
|
+
standard_block["extras"] = {} # type: ignore[typeddict-unknown-key]
|
|
28
|
+
standard_block["extras"][key] = value # type: ignore[typeddict-item]
|
|
29
|
+
|
|
30
|
+
return standard_block
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _convert_to_v1_from_converse_input(
|
|
34
|
+
content: list[types.ContentBlock],
|
|
35
|
+
) -> list[types.ContentBlock]:
|
|
36
|
+
"""Convert Bedrock Converse format blocks to v1 format.
|
|
37
|
+
|
|
38
|
+
During the `.content_blocks` parsing process, we wrap blocks not recognized as a v1
|
|
39
|
+
block as a ``'non_standard'`` block with the original block stored in the ``value``
|
|
40
|
+
field. This function attempts to unpack those blocks and convert any blocks that
|
|
41
|
+
might be Converse format to v1 ContentBlocks.
|
|
42
|
+
|
|
43
|
+
If conversion fails, the block is left as a ``'non_standard'`` block.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
content: List of content blocks to process.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Updated list with Converse blocks converted to v1 format.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def _iter_blocks() -> Iterable[types.ContentBlock]:
|
|
53
|
+
blocks: list[dict[str, Any]] = [
|
|
54
|
+
cast("dict[str, Any]", block)
|
|
55
|
+
if block.get("type") != "non_standard"
|
|
56
|
+
else block["value"] # type: ignore[typeddict-item] # this is only non-standard blocks
|
|
57
|
+
for block in content
|
|
58
|
+
]
|
|
59
|
+
for block in blocks:
|
|
60
|
+
num_keys = len(block)
|
|
61
|
+
|
|
62
|
+
if num_keys == 1 and (text := block.get("text")):
|
|
63
|
+
yield {"type": "text", "text": text}
|
|
64
|
+
|
|
65
|
+
elif (
|
|
66
|
+
num_keys == 1
|
|
67
|
+
and (document := block.get("document"))
|
|
68
|
+
and isinstance(document, dict)
|
|
69
|
+
and "format" in document
|
|
70
|
+
):
|
|
71
|
+
if document.get("format") == "pdf":
|
|
72
|
+
if "bytes" in document.get("source", {}):
|
|
73
|
+
file_block: types.FileContentBlock = {
|
|
74
|
+
"type": "file",
|
|
75
|
+
"base64": _bytes_to_b64_str(document["source"]["bytes"]),
|
|
76
|
+
"mime_type": "application/pdf",
|
|
77
|
+
}
|
|
78
|
+
_populate_extras(file_block, document, {"format", "source"})
|
|
79
|
+
yield file_block
|
|
80
|
+
|
|
81
|
+
else:
|
|
82
|
+
yield {"type": "non_standard", "value": block}
|
|
83
|
+
|
|
84
|
+
elif document["format"] == "txt":
|
|
85
|
+
if "text" in document.get("source", {}):
|
|
86
|
+
plain_text_block: types.PlainTextContentBlock = {
|
|
87
|
+
"type": "text-plain",
|
|
88
|
+
"text": document["source"]["text"],
|
|
89
|
+
"mime_type": "text/plain",
|
|
90
|
+
}
|
|
91
|
+
_populate_extras(
|
|
92
|
+
plain_text_block, document, {"format", "source"}
|
|
93
|
+
)
|
|
94
|
+
yield plain_text_block
|
|
95
|
+
else:
|
|
96
|
+
yield {"type": "non_standard", "value": block}
|
|
97
|
+
|
|
98
|
+
else:
|
|
99
|
+
yield {"type": "non_standard", "value": block}
|
|
100
|
+
|
|
101
|
+
elif (
|
|
102
|
+
num_keys == 1
|
|
103
|
+
and (image := block.get("image"))
|
|
104
|
+
and isinstance(image, dict)
|
|
105
|
+
and "format" in image
|
|
106
|
+
):
|
|
107
|
+
if "bytes" in image.get("source", {}):
|
|
108
|
+
image_block: types.ImageContentBlock = {
|
|
109
|
+
"type": "image",
|
|
110
|
+
"base64": _bytes_to_b64_str(image["source"]["bytes"]),
|
|
111
|
+
"mime_type": f"image/{image['format']}",
|
|
112
|
+
}
|
|
113
|
+
_populate_extras(image_block, image, {"format", "source"})
|
|
114
|
+
yield image_block
|
|
115
|
+
|
|
116
|
+
else:
|
|
117
|
+
yield {"type": "non_standard", "value": block}
|
|
118
|
+
|
|
119
|
+
elif block.get("type") in types.KNOWN_BLOCK_TYPES:
|
|
120
|
+
yield cast("types.ContentBlock", block)
|
|
121
|
+
|
|
122
|
+
else:
|
|
123
|
+
yield {"type": "non_standard", "value": block}
|
|
124
|
+
|
|
125
|
+
return list(_iter_blocks())
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _convert_citation_to_v1(citation: dict[str, Any]) -> types.Annotation:
|
|
129
|
+
standard_citation: types.Citation = {"type": "citation"}
|
|
130
|
+
if "title" in citation:
|
|
131
|
+
standard_citation["title"] = citation["title"]
|
|
132
|
+
if (
|
|
133
|
+
(source_content := citation.get("source_content"))
|
|
134
|
+
and isinstance(source_content, list)
|
|
135
|
+
and all(isinstance(item, dict) for item in source_content)
|
|
136
|
+
):
|
|
137
|
+
standard_citation["cited_text"] = "".join(
|
|
138
|
+
item.get("text", "") for item in source_content
|
|
18
139
|
)
|
|
19
|
-
warnings.warn(warning_message, stacklevel=2)
|
|
20
|
-
WARNED = True
|
|
21
|
-
raise NotImplementedError
|
|
22
140
|
|
|
141
|
+
known_fields = {"type", "source_content", "title", "index", "extras"}
|
|
142
|
+
|
|
143
|
+
for key, value in citation.items():
|
|
144
|
+
if key not in known_fields:
|
|
145
|
+
if "extras" not in standard_citation:
|
|
146
|
+
standard_citation["extras"] = {}
|
|
147
|
+
standard_citation["extras"][key] = value
|
|
148
|
+
|
|
149
|
+
return standard_citation
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _convert_to_v1_from_converse(message: AIMessage) -> list[types.ContentBlock]:
|
|
153
|
+
"""Convert Bedrock Converse message content to v1 format."""
|
|
154
|
+
if (
|
|
155
|
+
message.content == ""
|
|
156
|
+
and not message.additional_kwargs
|
|
157
|
+
and not message.tool_calls
|
|
158
|
+
):
|
|
159
|
+
# Converse outputs multiple chunks containing response metadata
|
|
160
|
+
return []
|
|
161
|
+
|
|
162
|
+
if isinstance(message.content, str):
|
|
163
|
+
message.content = [{"type": "text", "text": message.content}]
|
|
164
|
+
|
|
165
|
+
def _iter_blocks() -> Iterable[types.ContentBlock]:
|
|
166
|
+
for block in message.content:
|
|
167
|
+
if not isinstance(block, dict):
|
|
168
|
+
continue
|
|
169
|
+
block_type = block.get("type")
|
|
170
|
+
|
|
171
|
+
if block_type == "text":
|
|
172
|
+
if citations := block.get("citations"):
|
|
173
|
+
text_block: types.TextContentBlock = {
|
|
174
|
+
"type": "text",
|
|
175
|
+
"text": block.get("text", ""),
|
|
176
|
+
"annotations": [_convert_citation_to_v1(a) for a in citations],
|
|
177
|
+
}
|
|
178
|
+
else:
|
|
179
|
+
text_block = {"type": "text", "text": block["text"]}
|
|
180
|
+
if "index" in block:
|
|
181
|
+
text_block["index"] = block["index"]
|
|
182
|
+
yield text_block
|
|
183
|
+
|
|
184
|
+
elif block_type == "reasoning_content":
|
|
185
|
+
reasoning_block: types.ReasoningContentBlock = {"type": "reasoning"}
|
|
186
|
+
if reasoning_content := block.get("reasoning_content"):
|
|
187
|
+
if reasoning := reasoning_content.get("text"):
|
|
188
|
+
reasoning_block["reasoning"] = reasoning
|
|
189
|
+
if signature := reasoning_content.get("signature"):
|
|
190
|
+
if "extras" not in reasoning_block:
|
|
191
|
+
reasoning_block["extras"] = {}
|
|
192
|
+
reasoning_block["extras"]["signature"] = signature
|
|
193
|
+
|
|
194
|
+
if "index" in block:
|
|
195
|
+
reasoning_block["index"] = block["index"]
|
|
23
196
|
|
|
24
|
-
|
|
197
|
+
known_fields = {"type", "reasoning_content", "index", "extras"}
|
|
198
|
+
for key in block:
|
|
199
|
+
if key not in known_fields:
|
|
200
|
+
if "extras" not in reasoning_block:
|
|
201
|
+
reasoning_block["extras"] = {}
|
|
202
|
+
reasoning_block["extras"][key] = block[key]
|
|
203
|
+
yield reasoning_block
|
|
204
|
+
|
|
205
|
+
elif block_type == "tool_use":
|
|
206
|
+
if (
|
|
207
|
+
isinstance(message, AIMessageChunk)
|
|
208
|
+
and len(message.tool_call_chunks) == 1
|
|
209
|
+
and message.chunk_position != "last"
|
|
210
|
+
):
|
|
211
|
+
# Isolated chunk
|
|
212
|
+
tool_call_chunk: types.ToolCallChunk = (
|
|
213
|
+
message.tool_call_chunks[0].copy() # type: ignore[assignment]
|
|
214
|
+
)
|
|
215
|
+
if "type" not in tool_call_chunk:
|
|
216
|
+
tool_call_chunk["type"] = "tool_call_chunk"
|
|
217
|
+
yield tool_call_chunk
|
|
218
|
+
else:
|
|
219
|
+
tool_call_block: Optional[types.ToolCall] = None
|
|
220
|
+
# Non-streaming or gathered chunk
|
|
221
|
+
if len(message.tool_calls) == 1:
|
|
222
|
+
tool_call_block = {
|
|
223
|
+
"type": "tool_call",
|
|
224
|
+
"name": message.tool_calls[0]["name"],
|
|
225
|
+
"args": message.tool_calls[0]["args"],
|
|
226
|
+
"id": message.tool_calls[0].get("id"),
|
|
227
|
+
}
|
|
228
|
+
elif call_id := block.get("id"):
|
|
229
|
+
for tc in message.tool_calls:
|
|
230
|
+
if tc.get("id") == call_id:
|
|
231
|
+
tool_call_block = {
|
|
232
|
+
"type": "tool_call",
|
|
233
|
+
"name": tc["name"],
|
|
234
|
+
"args": tc["args"],
|
|
235
|
+
"id": tc.get("id"),
|
|
236
|
+
}
|
|
237
|
+
break
|
|
238
|
+
else:
|
|
239
|
+
pass
|
|
240
|
+
if not tool_call_block:
|
|
241
|
+
tool_call_block = {
|
|
242
|
+
"type": "tool_call",
|
|
243
|
+
"name": block.get("name", ""),
|
|
244
|
+
"args": block.get("input", {}),
|
|
245
|
+
"id": block.get("id", ""),
|
|
246
|
+
}
|
|
247
|
+
if "index" in block:
|
|
248
|
+
tool_call_block["index"] = block["index"]
|
|
249
|
+
yield tool_call_block
|
|
250
|
+
|
|
251
|
+
elif (
|
|
252
|
+
block_type == "input_json_delta"
|
|
253
|
+
and isinstance(message, AIMessageChunk)
|
|
254
|
+
and len(message.tool_call_chunks) == 1
|
|
255
|
+
):
|
|
256
|
+
tool_call_chunk = (
|
|
257
|
+
message.tool_call_chunks[0].copy() # type: ignore[assignment]
|
|
258
|
+
)
|
|
259
|
+
if "type" not in tool_call_chunk:
|
|
260
|
+
tool_call_chunk["type"] = "tool_call_chunk"
|
|
261
|
+
yield tool_call_chunk
|
|
262
|
+
|
|
263
|
+
else:
|
|
264
|
+
new_block: types.NonStandardContentBlock = {
|
|
265
|
+
"type": "non_standard",
|
|
266
|
+
"value": block,
|
|
267
|
+
}
|
|
268
|
+
if "index" in new_block["value"]:
|
|
269
|
+
new_block["index"] = new_block["value"].pop("index")
|
|
270
|
+
yield new_block
|
|
271
|
+
|
|
272
|
+
return list(_iter_blocks())
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def translate_content(message: AIMessage) -> list[types.ContentBlock]:
|
|
276
|
+
"""Derive standard content blocks from a message with Bedrock Converse content."""
|
|
277
|
+
return _convert_to_v1_from_converse(message)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def translate_content_chunk(message: AIMessageChunk) -> list[types.ContentBlock]:
|
|
25
281
|
"""Derive standard content blocks from a chunk with Bedrock Converse content."""
|
|
26
|
-
|
|
27
|
-
if not WARNED:
|
|
28
|
-
warning_message = (
|
|
29
|
-
"Content block standardization is not yet fully supported for Bedrock "
|
|
30
|
-
"Converse."
|
|
31
|
-
)
|
|
32
|
-
warnings.warn(warning_message, stacklevel=2)
|
|
33
|
-
WARNED = True
|
|
34
|
-
raise NotImplementedError
|
|
282
|
+
return _convert_to_v1_from_converse(message)
|
|
35
283
|
|
|
36
284
|
|
|
37
285
|
def _register_bedrock_converse_translator() -> None:
|