langchain-core 0.3.79__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 +23 -26
- langchain_core/_api/deprecation.py +52 -65
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +19 -19
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +323 -334
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +441 -507
- 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 +48 -63
- langchain_core/document_loaders/base.py +23 -23
- langchain_core/document_loaders/langsmith.py +37 -37
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +62 -65
- langchain_core/documents/compressor.py +4 -4
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +10 -11
- langchain_core/globals.py +3 -151
- langchain_core/indexing/api.py +61 -66
- langchain_core/indexing/base.py +58 -58
- langchain_core/indexing/in_memory.py +3 -3
- langchain_core/language_models/__init__.py +14 -27
- langchain_core/language_models/_utils.py +270 -84
- langchain_core/language_models/base.py +55 -162
- langchain_core/language_models/chat_models.py +442 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +61 -39
- langchain_core/language_models/llms.py +123 -231
- langchain_core/load/dump.py +4 -5
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +39 -40
- langchain_core/messages/__init__.py +61 -22
- langchain_core/messages/ai.py +368 -163
- langchain_core/messages/base.py +214 -43
- 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 -6
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +41 -38
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +38 -28
- langchain_core/messages/tool.py +96 -103
- langchain_core/messages/utils.py +478 -504
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +58 -61
- langchain_core/output_parsers/json.py +7 -8
- langchain_core/output_parsers/list.py +5 -7
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +14 -19
- langchain_core/output_parsers/pydantic.py +12 -13
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +8 -10
- 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 +8 -8
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +12 -12
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +45 -55
- langchain_core/prompts/chat.py +254 -313
- langchain_core/prompts/dict.py +5 -5
- 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 +6 -8
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +4 -4
- langchain_core/prompts/structured.py +42 -50
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +49 -190
- langchain_core/runnables/base.py +1484 -1709
- langchain_core/runnables/branch.py +45 -61
- langchain_core/runnables/config.py +80 -88
- langchain_core/runnables/configurable.py +117 -134
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +85 -95
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- langchain_core/runnables/schema.py +79 -74
- langchain_core/runnables/utils.py +62 -68
- langchain_core/stores.py +81 -115
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +179 -187
- langchain_core/tools/convert.py +131 -139
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +11 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +30 -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 +50 -60
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +115 -70
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +43 -43
- 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 +46 -8
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -191
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +37 -42
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +8 -8
- langchain_core/utils/mustache.py +9 -11
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +80 -54
- langchain_core/vectorstores/base.py +129 -164
- langchain_core/vectorstores/in_memory.py +99 -174
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.3.79.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 -447
- langchain_core/memory.py +0 -120
- langchain_core/messages/content_blocks.py +0 -176
- 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-0.3.79.dist-info/RECORD +0 -174
- langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
|
@@ -15,11 +15,11 @@ from langchain_core.utils._merge import merge_dicts
|
|
|
15
15
|
class FunctionMessage(BaseMessage):
|
|
16
16
|
"""Message for passing the result of executing a tool back to a model.
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
do not contain the
|
|
18
|
+
`FunctionMessage` are an older version of the `ToolMessage` schema, and
|
|
19
|
+
do not contain the `tool_call_id` field.
|
|
20
20
|
|
|
21
|
-
The
|
|
22
|
-
tool call response.
|
|
21
|
+
The `tool_call_id` field is used to associate the tool call request with the
|
|
22
|
+
tool call response. Useful in situations where a chat model is able
|
|
23
23
|
to request multiple tool calls in parallel.
|
|
24
24
|
|
|
25
25
|
"""
|
|
@@ -28,7 +28,7 @@ class FunctionMessage(BaseMessage):
|
|
|
28
28
|
"""The name of the function that was executed."""
|
|
29
29
|
|
|
30
30
|
type: Literal["function"] = "function"
|
|
31
|
-
"""The type of the message (used for serialization).
|
|
31
|
+
"""The type of the message (used for serialization)."""
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
class FunctionMessageChunk(FunctionMessage, BaseMessageChunk):
|
|
@@ -38,11 +38,7 @@ class FunctionMessageChunk(FunctionMessage, BaseMessageChunk):
|
|
|
38
38
|
# to make sure that the chunk variant can be discriminated from the
|
|
39
39
|
# non-chunk variant.
|
|
40
40
|
type: Literal["FunctionMessageChunk"] = "FunctionMessageChunk" # type: ignore[assignment]
|
|
41
|
-
"""The type of the message (used for serialization).
|
|
42
|
-
|
|
43
|
-
Defaults to ``'FunctionMessageChunk'``.
|
|
44
|
-
|
|
45
|
-
"""
|
|
41
|
+
"""The type of the message (used for serialization)."""
|
|
46
42
|
|
|
47
43
|
@override
|
|
48
44
|
def __add__(self, other: Any) -> BaseMessageChunk: # type: ignore[override]
|
langchain_core/messages/human.py
CHANGED
|
@@ -1,59 +1,63 @@
|
|
|
1
1
|
"""Human message."""
|
|
2
2
|
|
|
3
|
-
from typing import Any, Literal,
|
|
3
|
+
from typing import Any, Literal, cast, overload
|
|
4
4
|
|
|
5
|
+
from langchain_core.messages import content as types
|
|
5
6
|
from langchain_core.messages.base import BaseMessage, BaseMessageChunk
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class HumanMessage(BaseMessage):
|
|
9
|
-
"""Message from
|
|
10
|
+
"""Message from the user.
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
A `HumanMessage` is a message that is passed in from a user to the model.
|
|
12
13
|
|
|
13
14
|
Example:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
print(model.invoke(messages))
|
|
27
|
-
|
|
28
|
-
"""
|
|
29
|
-
|
|
30
|
-
example: bool = False
|
|
31
|
-
"""Use to denote that a message is part of an example conversation.
|
|
32
|
-
|
|
33
|
-
At the moment, this is ignored by most models. Usage is discouraged.
|
|
34
|
-
Defaults to False.
|
|
35
|
-
|
|
15
|
+
```python
|
|
16
|
+
from langchain_core.messages import HumanMessage, SystemMessage
|
|
17
|
+
|
|
18
|
+
messages = [
|
|
19
|
+
SystemMessage(content="You are a helpful assistant! Your name is Bob."),
|
|
20
|
+
HumanMessage(content="What is your name?"),
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
# Instantiate a chat model and invoke it with the messages
|
|
24
|
+
model = ...
|
|
25
|
+
print(model.invoke(messages))
|
|
26
|
+
```
|
|
36
27
|
"""
|
|
37
28
|
|
|
38
29
|
type: Literal["human"] = "human"
|
|
39
|
-
"""The type of the message (used for serialization).
|
|
30
|
+
"""The type of the message (used for serialization)."""
|
|
40
31
|
|
|
41
|
-
|
|
32
|
+
@overload
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
content: str | list[str | dict],
|
|
36
|
+
**kwargs: Any,
|
|
37
|
+
) -> None: ...
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
@overload
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
content: str | list[str | dict] | None = None,
|
|
43
|
+
content_blocks: list[types.ContentBlock] | None = None,
|
|
44
|
+
**kwargs: Any,
|
|
45
|
+
) -> None: ...
|
|
44
46
|
|
|
45
47
|
def __init__(
|
|
46
48
|
self,
|
|
47
|
-
content:
|
|
49
|
+
content: str | list[str | dict] | None = None,
|
|
50
|
+
content_blocks: list[types.ContentBlock] | None = None,
|
|
48
51
|
**kwargs: Any,
|
|
49
52
|
) -> None:
|
|
50
|
-
"""
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
"""Specify `content` as positional arg or `content_blocks` for typing."""
|
|
54
|
+
if content_blocks is not None:
|
|
55
|
+
super().__init__(
|
|
56
|
+
content=cast("str | list[str | dict]", content_blocks),
|
|
57
|
+
**kwargs,
|
|
58
|
+
)
|
|
59
|
+
else:
|
|
60
|
+
super().__init__(content=content, **kwargs)
|
|
57
61
|
|
|
58
62
|
|
|
59
63
|
class HumanMessageChunk(HumanMessage, BaseMessageChunk):
|
|
@@ -63,5 +67,4 @@ class HumanMessageChunk(HumanMessage, BaseMessageChunk):
|
|
|
63
67
|
# to make sure that the chunk variant can be discriminated from the
|
|
64
68
|
# non-chunk variant.
|
|
65
69
|
type: Literal["HumanMessageChunk"] = "HumanMessageChunk" # type: ignore[assignment]
|
|
66
|
-
"""The type of the message (used for serialization).
|
|
67
|
-
Defaults to "HumanMessageChunk"."""
|
|
70
|
+
"""The type of the message (used for serialization)."""
|
|
@@ -9,7 +9,7 @@ class RemoveMessage(BaseMessage):
|
|
|
9
9
|
"""Message responsible for deleting other messages."""
|
|
10
10
|
|
|
11
11
|
type: Literal["remove"] = "remove"
|
|
12
|
-
"""The type of the message (used for serialization).
|
|
12
|
+
"""The type of the message (used for serialization)."""
|
|
13
13
|
|
|
14
14
|
def __init__(
|
|
15
15
|
self,
|
|
@@ -20,7 +20,7 @@ class RemoveMessage(BaseMessage):
|
|
|
20
20
|
|
|
21
21
|
Args:
|
|
22
22
|
id: The ID of the message to remove.
|
|
23
|
-
kwargs: Additional fields to pass to the message.
|
|
23
|
+
**kwargs: Additional fields to pass to the message.
|
|
24
24
|
|
|
25
25
|
Raises:
|
|
26
26
|
ValueError: If the 'content' field is passed in kwargs.
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""System message."""
|
|
2
2
|
|
|
3
|
-
from typing import Any, Literal,
|
|
3
|
+
from typing import Any, Literal, cast, overload
|
|
4
4
|
|
|
5
|
+
from langchain_core.messages import content as types
|
|
5
6
|
from langchain_core.messages.base import BaseMessage, BaseMessageChunk
|
|
6
7
|
|
|
7
8
|
|
|
@@ -12,38 +13,51 @@ class SystemMessage(BaseMessage):
|
|
|
12
13
|
of input messages.
|
|
13
14
|
|
|
14
15
|
Example:
|
|
16
|
+
```python
|
|
17
|
+
from langchain_core.messages import HumanMessage, SystemMessage
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
messages = [
|
|
21
|
-
SystemMessage(content="You are a helpful assistant! Your name is Bob."),
|
|
22
|
-
HumanMessage(content="What is your name?"),
|
|
23
|
-
]
|
|
24
|
-
|
|
25
|
-
# Define a chat model and invoke it with the messages
|
|
26
|
-
print(model.invoke(messages))
|
|
19
|
+
messages = [
|
|
20
|
+
SystemMessage(content="You are a helpful assistant! Your name is Bob."),
|
|
21
|
+
HumanMessage(content="What is your name?"),
|
|
22
|
+
]
|
|
27
23
|
|
|
24
|
+
# Define a chat model and invoke it with the messages
|
|
25
|
+
print(model.invoke(messages))
|
|
26
|
+
```
|
|
28
27
|
"""
|
|
29
28
|
|
|
30
29
|
type: Literal["system"] = "system"
|
|
31
|
-
"""The type of the message (used for serialization).
|
|
30
|
+
"""The type of the message (used for serialization)."""
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
@overload
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
content: str | list[str | dict],
|
|
36
|
+
**kwargs: Any,
|
|
37
|
+
) -> None: ...
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
@overload
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
content: str | list[str | dict] | None = None,
|
|
43
|
+
content_blocks: list[types.ContentBlock] | None = None,
|
|
44
|
+
**kwargs: Any,
|
|
45
|
+
) -> None: ...
|
|
36
46
|
|
|
37
47
|
def __init__(
|
|
38
|
-
self,
|
|
48
|
+
self,
|
|
49
|
+
content: str | list[str | dict] | None = None,
|
|
50
|
+
content_blocks: list[types.ContentBlock] | None = None,
|
|
51
|
+
**kwargs: Any,
|
|
39
52
|
) -> None:
|
|
40
|
-
"""
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
"""Specify `content` as positional arg or `content_blocks` for typing."""
|
|
54
|
+
if content_blocks is not None:
|
|
55
|
+
super().__init__(
|
|
56
|
+
content=cast("str | list[str | dict]", content_blocks),
|
|
57
|
+
**kwargs,
|
|
58
|
+
)
|
|
59
|
+
else:
|
|
60
|
+
super().__init__(content=content, **kwargs)
|
|
47
61
|
|
|
48
62
|
|
|
49
63
|
class SystemMessageChunk(SystemMessage, BaseMessageChunk):
|
|
@@ -53,8 +67,4 @@ class SystemMessageChunk(SystemMessage, BaseMessageChunk):
|
|
|
53
67
|
# to make sure that the chunk variant can be discriminated from the
|
|
54
68
|
# non-chunk variant.
|
|
55
69
|
type: Literal["SystemMessageChunk"] = "SystemMessageChunk" # type: ignore[assignment]
|
|
56
|
-
"""The type of the message (used for serialization).
|
|
57
|
-
|
|
58
|
-
Defaults to ``'SystemMessageChunk'``.
|
|
59
|
-
|
|
60
|
-
"""
|
|
70
|
+
"""The type of the message (used for serialization)."""
|
langchain_core/messages/tool.py
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
"""Messages for tools."""
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
|
-
from typing import Any, Literal,
|
|
4
|
+
from typing import Any, Literal, cast, overload
|
|
5
5
|
from uuid import UUID
|
|
6
6
|
|
|
7
7
|
from pydantic import Field, model_validator
|
|
8
8
|
from typing_extensions import NotRequired, TypedDict, override
|
|
9
9
|
|
|
10
|
+
from langchain_core.messages import content as types
|
|
10
11
|
from langchain_core.messages.base import BaseMessage, BaseMessageChunk, merge_content
|
|
12
|
+
from langchain_core.messages.content import InvalidToolCall
|
|
11
13
|
from langchain_core.utils._merge import merge_dicts, merge_obj
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
class ToolOutputMixin:
|
|
15
17
|
"""Mixin for objects that tools can return directly.
|
|
16
18
|
|
|
17
|
-
If a custom BaseTool is invoked with a
|
|
18
|
-
not an instance of
|
|
19
|
-
a string and wrapped in a
|
|
19
|
+
If a custom BaseTool is invoked with a `ToolCall` and the output of custom code is
|
|
20
|
+
not an instance of `ToolOutputMixin`, the output will automatically be coerced to
|
|
21
|
+
a string and wrapped in a `ToolMessage`.
|
|
20
22
|
|
|
21
23
|
"""
|
|
22
24
|
|
|
@@ -24,42 +26,39 @@ class ToolOutputMixin:
|
|
|
24
26
|
class ToolMessage(BaseMessage, ToolOutputMixin):
|
|
25
27
|
"""Message for passing the result of executing a tool back to a model.
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
is encoded inside the
|
|
29
|
+
`ToolMessage` objects contain the result of a tool invocation. Typically, the result
|
|
30
|
+
is encoded inside the `content` field.
|
|
29
31
|
|
|
30
|
-
Example: A
|
|
32
|
+
Example: A `ToolMessage` representing a result of `42` from a tool call with id
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
```python
|
|
35
|
+
from langchain_core.messages import ToolMessage
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
ToolMessage(content="42", tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL")
|
|
38
|
+
```
|
|
35
39
|
|
|
36
|
-
|
|
40
|
+
Example: A `ToolMessage` where only part of the tool output is sent to the model
|
|
41
|
+
and the full output is passed in to artifact.
|
|
37
42
|
|
|
43
|
+
```python
|
|
44
|
+
from langchain_core.messages import ToolMessage
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
|
|
46
|
+
tool_output = {
|
|
47
|
+
"stdout": "From the graph we can see that the correlation between "
|
|
48
|
+
"x and y is ...",
|
|
49
|
+
"stderr": None,
|
|
50
|
+
"artifacts": {"type": "image", "base64_data": "/9j/4gIcSU..."},
|
|
51
|
+
}
|
|
41
52
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
tool_output = {
|
|
49
|
-
"stdout": "From the graph we can see that the correlation between "
|
|
50
|
-
"x and y is ...",
|
|
51
|
-
"stderr": None,
|
|
52
|
-
"artifacts": {"type": "image", "base64_data": "/9j/4gIcSU..."},
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
ToolMessage(
|
|
56
|
-
content=tool_output["stdout"],
|
|
57
|
-
artifact=tool_output,
|
|
58
|
-
tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL",
|
|
59
|
-
)
|
|
53
|
+
ToolMessage(
|
|
54
|
+
content=tool_output["stdout"],
|
|
55
|
+
artifact=tool_output,
|
|
56
|
+
tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL",
|
|
57
|
+
)
|
|
58
|
+
```
|
|
60
59
|
|
|
61
|
-
The
|
|
62
|
-
tool call response.
|
|
60
|
+
The `tool_call_id` field is used to associate the tool call request with the
|
|
61
|
+
tool call response. Useful in situations where a chat model is able
|
|
63
62
|
to request multiple tool calls in parallel.
|
|
64
63
|
|
|
65
64
|
"""
|
|
@@ -68,11 +67,7 @@ class ToolMessage(BaseMessage, ToolOutputMixin):
|
|
|
68
67
|
"""Tool call that this message is responding to."""
|
|
69
68
|
|
|
70
69
|
type: Literal["tool"] = "tool"
|
|
71
|
-
"""The type of the message (used for serialization).
|
|
72
|
-
|
|
73
|
-
Defaults to ``'tool'``.
|
|
74
|
-
|
|
75
|
-
"""
|
|
70
|
+
"""The type of the message (used for serialization)."""
|
|
76
71
|
|
|
77
72
|
artifact: Any = None
|
|
78
73
|
"""Artifact of the Tool execution which is not meant to be sent to the model.
|
|
@@ -81,21 +76,15 @@ class ToolMessage(BaseMessage, ToolOutputMixin):
|
|
|
81
76
|
a subset of the full tool output is being passed as message content but the full
|
|
82
77
|
output is needed in other parts of the code.
|
|
83
78
|
|
|
84
|
-
.. versionadded:: 0.2.17
|
|
85
|
-
|
|
86
79
|
"""
|
|
87
80
|
|
|
88
81
|
status: Literal["success", "error"] = "success"
|
|
89
|
-
"""Status of the tool invocation.
|
|
90
|
-
|
|
91
|
-
.. versionadded:: 0.2.24
|
|
92
|
-
|
|
93
|
-
"""
|
|
82
|
+
"""Status of the tool invocation."""
|
|
94
83
|
|
|
95
84
|
additional_kwargs: dict = Field(default_factory=dict, repr=False)
|
|
96
|
-
"""Currently inherited from BaseMessage
|
|
85
|
+
"""Currently inherited from `BaseMessage`, but not used."""
|
|
97
86
|
response_metadata: dict = Field(default_factory=dict, repr=False)
|
|
98
|
-
"""Currently inherited from BaseMessage
|
|
87
|
+
"""Currently inherited from `BaseMessage`, but not used."""
|
|
99
88
|
|
|
100
89
|
@model_validator(mode="before")
|
|
101
90
|
@classmethod
|
|
@@ -142,18 +131,43 @@ class ToolMessage(BaseMessage, ToolOutputMixin):
|
|
|
142
131
|
values["tool_call_id"] = str(tool_call_id)
|
|
143
132
|
return values
|
|
144
133
|
|
|
134
|
+
@overload
|
|
135
|
+
def __init__(
|
|
136
|
+
self,
|
|
137
|
+
content: str | list[str | dict],
|
|
138
|
+
**kwargs: Any,
|
|
139
|
+
) -> None: ...
|
|
140
|
+
|
|
141
|
+
@overload
|
|
142
|
+
def __init__(
|
|
143
|
+
self,
|
|
144
|
+
content: str | list[str | dict] | None = None,
|
|
145
|
+
content_blocks: list[types.ContentBlock] | None = None,
|
|
146
|
+
**kwargs: Any,
|
|
147
|
+
) -> None: ...
|
|
148
|
+
|
|
145
149
|
def __init__(
|
|
146
150
|
self,
|
|
147
|
-
content:
|
|
151
|
+
content: str | list[str | dict] | None = None,
|
|
152
|
+
content_blocks: list[types.ContentBlock] | None = None,
|
|
148
153
|
**kwargs: Any,
|
|
149
154
|
) -> None:
|
|
150
|
-
"""Initialize
|
|
155
|
+
"""Initialize a `ToolMessage`.
|
|
156
|
+
|
|
157
|
+
Specify `content` as positional arg or `content_blocks` for typing.
|
|
151
158
|
|
|
152
159
|
Args:
|
|
153
|
-
content: The
|
|
160
|
+
content: The contents of the message.
|
|
161
|
+
content_blocks: Typed standard content.
|
|
154
162
|
**kwargs: Additional fields.
|
|
155
163
|
"""
|
|
156
|
-
|
|
164
|
+
if content_blocks is not None:
|
|
165
|
+
super().__init__(
|
|
166
|
+
content=cast("str | list[str | dict]", content_blocks),
|
|
167
|
+
**kwargs,
|
|
168
|
+
)
|
|
169
|
+
else:
|
|
170
|
+
super().__init__(content=content, **kwargs)
|
|
157
171
|
|
|
158
172
|
|
|
159
173
|
class ToolMessageChunk(ToolMessage, BaseMessageChunk):
|
|
@@ -189,16 +203,15 @@ class ToolMessageChunk(ToolMessage, BaseMessageChunk):
|
|
|
189
203
|
|
|
190
204
|
|
|
191
205
|
class ToolCall(TypedDict):
|
|
192
|
-
"""Represents
|
|
206
|
+
"""Represents an AI's request to call a tool.
|
|
193
207
|
|
|
194
208
|
Example:
|
|
209
|
+
```python
|
|
210
|
+
{"name": "foo", "args": {"a": 1}, "id": "123"}
|
|
211
|
+
```
|
|
195
212
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
{"name": "foo", "args": {"a": 1}, "id": "123"}
|
|
199
|
-
|
|
200
|
-
This represents a request to call the tool named ``'foo'`` with arguments
|
|
201
|
-
``{"a": 1}`` and an identifier of ``'123'``.
|
|
213
|
+
This represents a request to call the tool named `'foo'` with arguments
|
|
214
|
+
`{"a": 1}` and an identifier of `'123'`.
|
|
202
215
|
|
|
203
216
|
"""
|
|
204
217
|
|
|
@@ -206,7 +219,7 @@ class ToolCall(TypedDict):
|
|
|
206
219
|
"""The name of the tool to be called."""
|
|
207
220
|
args: dict[str, Any]
|
|
208
221
|
"""The arguments to the tool call."""
|
|
209
|
-
id:
|
|
222
|
+
id: str | None
|
|
210
223
|
"""An identifier associated with the tool call.
|
|
211
224
|
|
|
212
225
|
An identifier is needed to associate a tool call request with a tool
|
|
@@ -220,7 +233,7 @@ def tool_call(
|
|
|
220
233
|
*,
|
|
221
234
|
name: str,
|
|
222
235
|
args: dict[str, Any],
|
|
223
|
-
id:
|
|
236
|
+
id: str | None,
|
|
224
237
|
) -> ToolCall:
|
|
225
238
|
"""Create a tool call.
|
|
226
239
|
|
|
@@ -236,43 +249,41 @@ def tool_call(
|
|
|
236
249
|
|
|
237
250
|
|
|
238
251
|
class ToolCallChunk(TypedDict):
|
|
239
|
-
"""A chunk of a tool call (
|
|
252
|
+
"""A chunk of a tool call (yielded when streaming).
|
|
240
253
|
|
|
241
|
-
When merging
|
|
254
|
+
When merging `ToolCallChunk`s (e.g., via `AIMessageChunk.__add__`),
|
|
242
255
|
all string attributes are concatenated. Chunks are only merged if their
|
|
243
|
-
values of
|
|
256
|
+
values of `index` are equal and not None.
|
|
244
257
|
|
|
245
258
|
Example:
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
).tool_call_chunks == [ToolCallChunk(name="foo", args='{"a":1}', index=0)]
|
|
256
|
-
|
|
259
|
+
```python
|
|
260
|
+
left_chunks = [ToolCallChunk(name="foo", args='{"a":', index=0)]
|
|
261
|
+
right_chunks = [ToolCallChunk(name=None, args="1}", index=0)]
|
|
262
|
+
|
|
263
|
+
(
|
|
264
|
+
AIMessageChunk(content="", tool_call_chunks=left_chunks)
|
|
265
|
+
+ AIMessageChunk(content="", tool_call_chunks=right_chunks)
|
|
266
|
+
).tool_call_chunks == [ToolCallChunk(name="foo", args='{"a":1}', index=0)]
|
|
267
|
+
```
|
|
257
268
|
"""
|
|
258
269
|
|
|
259
|
-
name:
|
|
270
|
+
name: str | None
|
|
260
271
|
"""The name of the tool to be called."""
|
|
261
|
-
args:
|
|
272
|
+
args: str | None
|
|
262
273
|
"""The arguments to the tool call."""
|
|
263
|
-
id:
|
|
274
|
+
id: str | None
|
|
264
275
|
"""An identifier associated with the tool call."""
|
|
265
|
-
index:
|
|
276
|
+
index: int | None
|
|
266
277
|
"""The index of the tool call in a sequence."""
|
|
267
278
|
type: NotRequired[Literal["tool_call_chunk"]]
|
|
268
279
|
|
|
269
280
|
|
|
270
281
|
def tool_call_chunk(
|
|
271
282
|
*,
|
|
272
|
-
name:
|
|
273
|
-
args:
|
|
274
|
-
id:
|
|
275
|
-
index:
|
|
283
|
+
name: str | None = None,
|
|
284
|
+
args: str | None = None,
|
|
285
|
+
id: str | None = None,
|
|
286
|
+
index: int | None = None,
|
|
276
287
|
) -> ToolCallChunk:
|
|
277
288
|
"""Create a tool call chunk.
|
|
278
289
|
|
|
@@ -290,30 +301,12 @@ def tool_call_chunk(
|
|
|
290
301
|
)
|
|
291
302
|
|
|
292
303
|
|
|
293
|
-
class InvalidToolCall(TypedDict):
|
|
294
|
-
"""Allowance for errors made by LLM.
|
|
295
|
-
|
|
296
|
-
Here we add an ``error`` key to surface errors made during generation
|
|
297
|
-
(e.g., invalid JSON arguments.)
|
|
298
|
-
"""
|
|
299
|
-
|
|
300
|
-
name: Optional[str]
|
|
301
|
-
"""The name of the tool to be called."""
|
|
302
|
-
args: Optional[str]
|
|
303
|
-
"""The arguments to the tool call."""
|
|
304
|
-
id: Optional[str]
|
|
305
|
-
"""An identifier associated with the tool call."""
|
|
306
|
-
error: Optional[str]
|
|
307
|
-
"""An error message associated with the tool call."""
|
|
308
|
-
type: NotRequired[Literal["invalid_tool_call"]]
|
|
309
|
-
|
|
310
|
-
|
|
311
304
|
def invalid_tool_call(
|
|
312
305
|
*,
|
|
313
|
-
name:
|
|
314
|
-
args:
|
|
315
|
-
id:
|
|
316
|
-
error:
|
|
306
|
+
name: str | None = None,
|
|
307
|
+
args: str | None = None,
|
|
308
|
+
id: str | None = None,
|
|
309
|
+
error: str | None = None,
|
|
317
310
|
) -> InvalidToolCall:
|
|
318
311
|
"""Create an invalid tool call.
|
|
319
312
|
|