langchain-core 0.3.75__py3-none-any.whl → 0.3.77__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 +22 -44
- langchain_core/_api/deprecation.py +30 -17
- 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 +1 -2
- langchain_core/callbacks/base.py +28 -15
- langchain_core/callbacks/manager.py +83 -71
- langchain_core/callbacks/usage.py +6 -4
- langchain_core/chat_history.py +29 -21
- langchain_core/document_loaders/base.py +34 -9
- langchain_core/document_loaders/langsmith.py +4 -1
- langchain_core/documents/base.py +35 -10
- 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 +88 -76
- langchain_core/indexing/base.py +5 -8
- langchain_core/indexing/in_memory.py +23 -3
- langchain_core/language_models/__init__.py +3 -2
- langchain_core/language_models/base.py +31 -20
- langchain_core/language_models/chat_models.py +98 -27
- langchain_core/language_models/fake_chat_models.py +10 -9
- langchain_core/language_models/llms.py +52 -18
- langchain_core/load/dump.py +2 -3
- langchain_core/load/load.py +15 -1
- langchain_core/load/serializable.py +39 -44
- langchain_core/memory.py +7 -3
- langchain_core/messages/ai.py +53 -24
- langchain_core/messages/base.py +43 -22
- langchain_core/messages/chat.py +4 -1
- langchain_core/messages/content_blocks.py +23 -2
- langchain_core/messages/function.py +9 -5
- langchain_core/messages/human.py +13 -10
- langchain_core/messages/modifier.py +1 -0
- langchain_core/messages/system.py +11 -8
- langchain_core/messages/tool.py +60 -29
- langchain_core/messages/utils.py +250 -131
- 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 +6 -1
- langchain_core/output_parsers/pydantic.py +4 -0
- langchain_core/output_parsers/string.py +5 -1
- langchain_core/output_parsers/xml.py +19 -19
- langchain_core/outputs/chat_generation.py +25 -10
- langchain_core/outputs/generation.py +14 -3
- langchain_core/outputs/llm_result.py +8 -1
- langchain_core/prompt_values.py +16 -6
- langchain_core/prompts/base.py +4 -9
- langchain_core/prompts/chat.py +89 -57
- langchain_core/prompts/dict.py +16 -8
- langchain_core/prompts/few_shot.py +12 -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 +406 -186
- langchain_core/runnables/branch.py +14 -19
- langchain_core/runnables/config.py +9 -15
- langchain_core/runnables/configurable.py +34 -19
- langchain_core/runnables/fallbacks.py +20 -13
- langchain_core/runnables/graph.py +48 -38
- langchain_core/runnables/graph_ascii.py +41 -18
- langchain_core/runnables/graph_mermaid.py +54 -25
- langchain_core/runnables/graph_png.py +27 -31
- langchain_core/runnables/history.py +55 -58
- langchain_core/runnables/passthrough.py +44 -21
- langchain_core/runnables/retry.py +44 -23
- langchain_core/runnables/router.py +9 -8
- langchain_core/runnables/schema.py +2 -0
- langchain_core/runnables/utils.py +51 -89
- langchain_core/stores.py +19 -31
- langchain_core/sys_info.py +9 -8
- langchain_core/tools/base.py +37 -28
- langchain_core/tools/convert.py +26 -15
- langchain_core/tools/simple.py +36 -8
- langchain_core/tools/structured.py +25 -12
- 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 +45 -34
- langchain_core/tracers/langchain.py +12 -3
- langchain_core/tracers/langchain_v1.py +10 -2
- langchain_core/tracers/log_stream.py +56 -17
- 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 +15 -7
- langchain_core/utils/env.py +3 -0
- langchain_core/utils/function_calling.py +50 -28
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +12 -4
- langchain_core/utils/json.py +12 -3
- langchain_core/utils/json_schema.py +156 -40
- langchain_core/utils/loading.py +5 -1
- langchain_core/utils/mustache.py +24 -15
- langchain_core/utils/pydantic.py +38 -9
- langchain_core/utils/utils.py +25 -9
- langchain_core/vectorstores/base.py +7 -20
- langchain_core/vectorstores/in_memory.py +23 -17
- langchain_core/vectorstores/utils.py +18 -12
- langchain_core/version.py +1 -1
- langchain_core-0.3.77.dist-info/METADATA +67 -0
- langchain_core-0.3.77.dist-info/RECORD +174 -0
- langchain_core-0.3.75.dist-info/METADATA +0 -106
- langchain_core-0.3.75.dist-info/RECORD +0 -174
- {langchain_core-0.3.75.dist-info → langchain_core-0.3.77.dist-info}/WHEEL +0 -0
- {langchain_core-0.3.75.dist-info → langchain_core-0.3.77.dist-info}/entry_points.txt +0 -0
|
@@ -144,7 +144,10 @@ class BaseOutputParser(
|
|
|
144
144
|
|
|
145
145
|
def parse(self, text: str) -> bool:
|
|
146
146
|
cleaned_text = text.strip().upper()
|
|
147
|
-
if cleaned_text not in (
|
|
147
|
+
if cleaned_text not in (
|
|
148
|
+
self.true_val.upper(),
|
|
149
|
+
self.false_val.upper(),
|
|
150
|
+
):
|
|
148
151
|
raise OutputParserException(
|
|
149
152
|
f"BooleanOutputParser expected output value to either be "
|
|
150
153
|
f"{self.true_val} or {self.false_val} (case-insensitive). "
|
|
@@ -156,7 +159,7 @@ class BaseOutputParser(
|
|
|
156
159
|
def _type(self) -> str:
|
|
157
160
|
return "boolean_output_parser"
|
|
158
161
|
|
|
159
|
-
"""
|
|
162
|
+
"""
|
|
160
163
|
|
|
161
164
|
@property
|
|
162
165
|
@override
|
|
@@ -46,13 +46,13 @@ class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
46
46
|
def _diff(self, prev: Optional[Any], next: Any) -> Any:
|
|
47
47
|
return jsonpatch.make_patch(prev, next).patch
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
@staticmethod
|
|
50
|
+
def _get_schema(pydantic_object: type[TBaseModel]) -> dict[str, Any]:
|
|
50
51
|
if issubclass(pydantic_object, pydantic.BaseModel):
|
|
51
52
|
return pydantic_object.model_json_schema()
|
|
52
|
-
|
|
53
|
-
return pydantic_object.schema()
|
|
54
|
-
return None
|
|
53
|
+
return pydantic_object.schema()
|
|
55
54
|
|
|
55
|
+
@override
|
|
56
56
|
def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
|
|
57
57
|
"""Parse the result of an LLM call to a JSON object.
|
|
58
58
|
|
|
@@ -143,10 +143,7 @@ class CommaSeparatedListOutputParser(ListOutputParser):
|
|
|
143
143
|
|
|
144
144
|
@classmethod
|
|
145
145
|
def is_lc_serializable(cls) -> bool:
|
|
146
|
-
"""
|
|
147
|
-
|
|
148
|
-
Returns True.
|
|
149
|
-
"""
|
|
146
|
+
"""Return True as this class is serializable."""
|
|
150
147
|
return True
|
|
151
148
|
|
|
152
149
|
@classmethod
|
|
@@ -154,11 +151,11 @@ class CommaSeparatedListOutputParser(ListOutputParser):
|
|
|
154
151
|
"""Get the namespace of the langchain object.
|
|
155
152
|
|
|
156
153
|
Returns:
|
|
157
|
-
|
|
158
|
-
Default is ["langchain", "output_parsers", "list"].
|
|
154
|
+
``["langchain", "output_parsers", "list"]``
|
|
159
155
|
"""
|
|
160
156
|
return ["langchain", "output_parsers", "list"]
|
|
161
157
|
|
|
158
|
+
@override
|
|
162
159
|
def get_format_instructions(self) -> str:
|
|
163
160
|
"""Return the format instructions for the comma-separated list output."""
|
|
164
161
|
return (
|
|
@@ -166,6 +163,7 @@ class CommaSeparatedListOutputParser(ListOutputParser):
|
|
|
166
163
|
"eg: `foo, bar, baz` or `foo,bar,baz`"
|
|
167
164
|
)
|
|
168
165
|
|
|
166
|
+
@override
|
|
169
167
|
def parse(self, text: str) -> list[str]:
|
|
170
168
|
"""Parse the output of an LLM call.
|
|
171
169
|
|
|
@@ -213,15 +211,8 @@ class NumberedListOutputParser(ListOutputParser):
|
|
|
213
211
|
"""
|
|
214
212
|
return re.findall(self.pattern, text)
|
|
215
213
|
|
|
214
|
+
@override
|
|
216
215
|
def parse_iter(self, text: str) -> Iterator[re.Match]:
|
|
217
|
-
"""Parse the output of an LLM call.
|
|
218
|
-
|
|
219
|
-
Args:
|
|
220
|
-
text: The output of an LLM call.
|
|
221
|
-
|
|
222
|
-
Yields:
|
|
223
|
-
A match object for each part of the output.
|
|
224
|
-
"""
|
|
225
216
|
return re.finditer(self.pattern, text)
|
|
226
217
|
|
|
227
218
|
@property
|
|
@@ -235,6 +226,7 @@ class MarkdownListOutputParser(ListOutputParser):
|
|
|
235
226
|
pattern: str = r"^\s*[-*]\s([^\n]+)$"
|
|
236
227
|
"""The pattern to match a Markdown list item."""
|
|
237
228
|
|
|
229
|
+
@override
|
|
238
230
|
def get_format_instructions(self) -> str:
|
|
239
231
|
"""Return the format instructions for the Markdown list output."""
|
|
240
232
|
return "Your response should be a markdown list, eg: `- foo\n- bar\n- baz`"
|
|
@@ -250,15 +242,8 @@ class MarkdownListOutputParser(ListOutputParser):
|
|
|
250
242
|
"""
|
|
251
243
|
return re.findall(self.pattern, text, re.MULTILINE)
|
|
252
244
|
|
|
245
|
+
@override
|
|
253
246
|
def parse_iter(self, text: str) -> Iterator[re.Match]:
|
|
254
|
-
"""Parse the output of an LLM call.
|
|
255
|
-
|
|
256
|
-
Args:
|
|
257
|
-
text: The output of an LLM call.
|
|
258
|
-
|
|
259
|
-
Yields:
|
|
260
|
-
A match object for each part of the output.
|
|
261
|
-
"""
|
|
262
247
|
return re.finditer(self.pattern, text, re.MULTILINE)
|
|
263
248
|
|
|
264
249
|
@property
|
|
@@ -261,6 +261,9 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser):
|
|
|
261
261
|
result: The result of the LLM call.
|
|
262
262
|
partial: Whether to parse partial JSON objects. Default is False.
|
|
263
263
|
|
|
264
|
+
Raises:
|
|
265
|
+
ValueError: If the pydantic schema is not valid.
|
|
266
|
+
|
|
264
267
|
Returns:
|
|
265
268
|
The parsed JSON object.
|
|
266
269
|
"""
|
|
@@ -231,6 +231,9 @@ class JsonOutputKeyToolsParser(JsonOutputToolsParser):
|
|
|
231
231
|
If False, the output will be the full JSON object.
|
|
232
232
|
Default is False.
|
|
233
233
|
|
|
234
|
+
Raises:
|
|
235
|
+
OutputParserException: If the generation is not a chat generation.
|
|
236
|
+
|
|
234
237
|
Returns:
|
|
235
238
|
The parsed tool calls.
|
|
236
239
|
"""
|
|
@@ -316,7 +319,9 @@ class PydanticToolsParser(JsonOutputToolsParser):
|
|
|
316
319
|
The parsed Pydantic objects.
|
|
317
320
|
|
|
318
321
|
Raises:
|
|
319
|
-
|
|
322
|
+
ValueError: If the tool call arguments are not a dict.
|
|
323
|
+
ValidationError: If the tool call arguments do not conform
|
|
324
|
+
to the Pydantic model.
|
|
320
325
|
"""
|
|
321
326
|
json_results = super().parse_result(result, partial=partial)
|
|
322
327
|
if not json_results:
|
|
@@ -54,6 +54,10 @@ class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):
|
|
|
54
54
|
all the keys that have been returned so far.
|
|
55
55
|
Defaults to False.
|
|
56
56
|
|
|
57
|
+
Raises:
|
|
58
|
+
OutputParserException: If the result is not valid JSON
|
|
59
|
+
or does not conform to the pydantic model.
|
|
60
|
+
|
|
57
61
|
Returns:
|
|
58
62
|
The parsed pydantic object.
|
|
59
63
|
"""
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""String output parser."""
|
|
2
2
|
|
|
3
|
+
from typing_extensions import override
|
|
4
|
+
|
|
3
5
|
from langchain_core.output_parsers.transform import BaseTransformOutputParser
|
|
4
6
|
|
|
5
7
|
|
|
@@ -19,7 +21,8 @@ class StrOutputParser(BaseTransformOutputParser[str]):
|
|
|
19
21
|
def get_lc_namespace(cls) -> list[str]:
|
|
20
22
|
"""Get the namespace of the langchain object.
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
Returns:
|
|
25
|
+
``["langchain", "schema", "output_parser"]``
|
|
23
26
|
"""
|
|
24
27
|
return ["langchain", "schema", "output_parser"]
|
|
25
28
|
|
|
@@ -28,6 +31,7 @@ class StrOutputParser(BaseTransformOutputParser[str]):
|
|
|
28
31
|
"""Return the output parser type for serialization."""
|
|
29
32
|
return "default"
|
|
30
33
|
|
|
34
|
+
@override
|
|
31
35
|
def parse(self, text: str) -> str:
|
|
32
36
|
"""Returns the input text with no changes."""
|
|
33
37
|
return text
|
|
@@ -15,6 +15,14 @@ from langchain_core.messages import BaseMessage
|
|
|
15
15
|
from langchain_core.output_parsers.transform import BaseTransformOutputParser
|
|
16
16
|
from langchain_core.runnables.utils import AddableDict
|
|
17
17
|
|
|
18
|
+
try:
|
|
19
|
+
from defusedxml import ElementTree # type: ignore[import-untyped]
|
|
20
|
+
from defusedxml.ElementTree import XMLParser # type: ignore[import-untyped]
|
|
21
|
+
|
|
22
|
+
_HAS_DEFUSEDXML = True
|
|
23
|
+
except ImportError:
|
|
24
|
+
_HAS_DEFUSEDXML = False
|
|
25
|
+
|
|
18
26
|
XML_FORMAT_INSTRUCTIONS = """The output should be formatted as a XML file.
|
|
19
27
|
1. Output should conform to the tags below.
|
|
20
28
|
2. If tags are not given, make them on your own.
|
|
@@ -50,17 +58,13 @@ class _StreamingParser:
|
|
|
50
58
|
parser is requested.
|
|
51
59
|
"""
|
|
52
60
|
if parser == "defusedxml":
|
|
53
|
-
|
|
54
|
-
from defusedxml.ElementTree import ( # type: ignore[import-untyped]
|
|
55
|
-
XMLParser,
|
|
56
|
-
)
|
|
57
|
-
except ImportError as e:
|
|
61
|
+
if not _HAS_DEFUSEDXML:
|
|
58
62
|
msg = (
|
|
59
63
|
"defusedxml is not installed. "
|
|
60
64
|
"Please install it to use the defusedxml parser."
|
|
61
65
|
"You can install it with `pip install defusedxml` "
|
|
62
66
|
)
|
|
63
|
-
raise ImportError(msg)
|
|
67
|
+
raise ImportError(msg)
|
|
64
68
|
parser_ = XMLParser(target=TreeBuilder())
|
|
65
69
|
else:
|
|
66
70
|
parser_ = None
|
|
@@ -136,9 +140,6 @@ class _StreamingParser:
|
|
|
136
140
|
"""Close the parser.
|
|
137
141
|
|
|
138
142
|
This should be called after all chunks have been parsed.
|
|
139
|
-
|
|
140
|
-
Raises:
|
|
141
|
-
xml.etree.ElementTree.ParseError: If the XML is not well-formed.
|
|
142
143
|
"""
|
|
143
144
|
# Ignore ParseError. This will ignore any incomplete XML at the end of the input
|
|
144
145
|
with contextlib.suppress(xml.etree.ElementTree.ParseError):
|
|
@@ -154,14 +155,15 @@ class XMLOutputParser(BaseTransformOutputParser):
|
|
|
154
155
|
Note this may not be perfect depending on the LLM implementation.
|
|
155
156
|
|
|
156
157
|
For example, with tags=["foo", "bar", "baz"]:
|
|
157
|
-
1. A well-formatted XML instance:
|
|
158
|
-
"<foo>\n <bar>\n <baz></baz>\n </bar>\n</foo>"
|
|
159
158
|
|
|
160
|
-
|
|
161
|
-
|
|
159
|
+
1. A well-formatted XML instance:
|
|
160
|
+
"<foo>\n <bar>\n <baz></baz>\n </bar>\n</foo>"
|
|
161
|
+
|
|
162
|
+
2. A badly-formatted XML instance (missing closing tag for 'bar'):
|
|
163
|
+
"<foo>\n <bar>\n </foo>"
|
|
162
164
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
+
3. A badly-formatted XML instance (unexpected 'tag' element):
|
|
166
|
+
"<foo>\n <tag>\n </tag>\n</foo>"
|
|
165
167
|
"""
|
|
166
168
|
encoding_matcher: re.Pattern = re.compile(
|
|
167
169
|
r"<([^>]*encoding[^>]*)>\n(.*)", re.MULTILINE | re.DOTALL
|
|
@@ -209,16 +211,14 @@ class XMLOutputParser(BaseTransformOutputParser):
|
|
|
209
211
|
# Imports are temporarily placed here to avoid issue with caching on CI
|
|
210
212
|
# likely if you're reading this you can move them to the top of the file
|
|
211
213
|
if self.parser == "defusedxml":
|
|
212
|
-
|
|
213
|
-
from defusedxml import ElementTree # type: ignore[import-untyped]
|
|
214
|
-
except ImportError as e:
|
|
214
|
+
if not _HAS_DEFUSEDXML:
|
|
215
215
|
msg = (
|
|
216
216
|
"defusedxml is not installed. "
|
|
217
217
|
"Please install it to use the defusedxml parser."
|
|
218
218
|
"You can install it with `pip install defusedxml`"
|
|
219
219
|
"See https://github.com/tiran/defusedxml for more details"
|
|
220
220
|
)
|
|
221
|
-
raise ImportError(msg)
|
|
221
|
+
raise ImportError(msg)
|
|
222
222
|
et = ElementTree # Use the defusedxml parser
|
|
223
223
|
else:
|
|
224
224
|
et = ET # Use the standard library parser
|
|
@@ -15,14 +15,14 @@ from langchain_core.utils._merge import merge_dicts
|
|
|
15
15
|
class ChatGeneration(Generation):
|
|
16
16
|
"""A single chat generation output.
|
|
17
17
|
|
|
18
|
-
A subclass of Generation that represents the response from a chat model
|
|
18
|
+
A subclass of ``Generation`` that represents the response from a chat model
|
|
19
19
|
that generates chat messages.
|
|
20
20
|
|
|
21
|
-
The
|
|
22
|
-
Most of the time, the message will be of type
|
|
21
|
+
The ``message`` attribute is a structured representation of the chat message.
|
|
22
|
+
Most of the time, the message will be of type ``AIMessage``.
|
|
23
23
|
|
|
24
24
|
Users working with chat models will usually access information via either
|
|
25
|
-
|
|
25
|
+
``AIMessage`` (returned from runnable interfaces) or ``LLMResult`` (available
|
|
26
26
|
via callbacks).
|
|
27
27
|
"""
|
|
28
28
|
|
|
@@ -31,6 +31,7 @@ class ChatGeneration(Generation):
|
|
|
31
31
|
|
|
32
32
|
.. warning::
|
|
33
33
|
SHOULD NOT BE SET DIRECTLY!
|
|
34
|
+
|
|
34
35
|
"""
|
|
35
36
|
message: BaseMessage
|
|
36
37
|
"""The message output by the chat model."""
|
|
@@ -69,9 +70,9 @@ class ChatGeneration(Generation):
|
|
|
69
70
|
|
|
70
71
|
|
|
71
72
|
class ChatGenerationChunk(ChatGeneration):
|
|
72
|
-
"""ChatGeneration chunk.
|
|
73
|
+
"""``ChatGeneration`` chunk.
|
|
73
74
|
|
|
74
|
-
ChatGeneration chunks can be concatenated with other ChatGeneration chunks.
|
|
75
|
+
``ChatGeneration`` chunks can be concatenated with other ``ChatGeneration`` chunks.
|
|
75
76
|
"""
|
|
76
77
|
|
|
77
78
|
message: BaseMessageChunk
|
|
@@ -83,11 +84,18 @@ class ChatGenerationChunk(ChatGeneration):
|
|
|
83
84
|
def __add__(
|
|
84
85
|
self, other: Union[ChatGenerationChunk, list[ChatGenerationChunk]]
|
|
85
86
|
) -> ChatGenerationChunk:
|
|
86
|
-
"""Concatenate two
|
|
87
|
+
"""Concatenate two ``ChatGenerationChunk``s.
|
|
87
88
|
|
|
88
89
|
Args:
|
|
89
|
-
other: The other ChatGenerationChunk or list of
|
|
90
|
-
concatenate.
|
|
90
|
+
other: The other ``ChatGenerationChunk`` or list of ``ChatGenerationChunk``
|
|
91
|
+
to concatenate.
|
|
92
|
+
|
|
93
|
+
Raises:
|
|
94
|
+
TypeError: If other is not a ``ChatGenerationChunk`` or list of
|
|
95
|
+
``ChatGenerationChunk``.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
A new ``ChatGenerationChunk`` concatenated from self and other.
|
|
91
99
|
"""
|
|
92
100
|
if isinstance(other, ChatGenerationChunk):
|
|
93
101
|
generation_info = merge_dicts(
|
|
@@ -116,7 +124,14 @@ class ChatGenerationChunk(ChatGeneration):
|
|
|
116
124
|
def merge_chat_generation_chunks(
|
|
117
125
|
chunks: list[ChatGenerationChunk],
|
|
118
126
|
) -> Union[ChatGenerationChunk, None]:
|
|
119
|
-
"""Merge a list of
|
|
127
|
+
"""Merge a list of ``ChatGenerationChunk``s into a single ``ChatGenerationChunk``.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
chunks: A list of ``ChatGenerationChunk`` to merge.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
A merged ``ChatGenerationChunk``, or None if the input list is empty.
|
|
134
|
+
"""
|
|
120
135
|
if not chunks:
|
|
121
136
|
return None
|
|
122
137
|
|
|
@@ -39,14 +39,15 @@ class Generation(Serializable):
|
|
|
39
39
|
|
|
40
40
|
@classmethod
|
|
41
41
|
def is_lc_serializable(cls) -> bool:
|
|
42
|
-
"""Return
|
|
42
|
+
"""Return True as this class is serializable."""
|
|
43
43
|
return True
|
|
44
44
|
|
|
45
45
|
@classmethod
|
|
46
46
|
def get_lc_namespace(cls) -> list[str]:
|
|
47
47
|
"""Get the namespace of the langchain object.
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
Returns:
|
|
50
|
+
``["langchain", "schema", "output"]``
|
|
50
51
|
"""
|
|
51
52
|
return ["langchain", "schema", "output"]
|
|
52
53
|
|
|
@@ -55,7 +56,17 @@ class GenerationChunk(Generation):
|
|
|
55
56
|
"""Generation chunk, which can be concatenated with other Generation chunks."""
|
|
56
57
|
|
|
57
58
|
def __add__(self, other: GenerationChunk) -> GenerationChunk:
|
|
58
|
-
"""Concatenate two
|
|
59
|
+
"""Concatenate two ``GenerationChunk``s.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
other: Another ``GenerationChunk`` to concatenate with.
|
|
63
|
+
|
|
64
|
+
Raises:
|
|
65
|
+
TypeError: If other is not a ``GenerationChunk``.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
A new ``GenerationChunk`` concatenated from self and other.
|
|
69
|
+
"""
|
|
59
70
|
if isinstance(other, GenerationChunk):
|
|
60
71
|
generation_info = merge_dicts(
|
|
61
72
|
self.generation_info or {},
|
|
@@ -91,7 +91,14 @@ class LLMResult(BaseModel):
|
|
|
91
91
|
return llm_results
|
|
92
92
|
|
|
93
93
|
def __eq__(self, other: object) -> bool:
|
|
94
|
-
"""Check for LLMResult equality by ignoring any metadata related to runs.
|
|
94
|
+
"""Check for ``LLMResult`` equality by ignoring any metadata related to runs.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
other: Another ``LLMResult`` object to compare against.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
True if the generations and ``llm_output`` are equal, False otherwise.
|
|
101
|
+
"""
|
|
95
102
|
if not isinstance(other, LLMResult):
|
|
96
103
|
return NotImplemented
|
|
97
104
|
return (
|
langchain_core/prompt_values.py
CHANGED
|
@@ -30,7 +30,7 @@ class PromptValue(Serializable, ABC):
|
|
|
30
30
|
|
|
31
31
|
@classmethod
|
|
32
32
|
def is_lc_serializable(cls) -> bool:
|
|
33
|
-
"""Return
|
|
33
|
+
"""Return True as this class is serializable."""
|
|
34
34
|
return True
|
|
35
35
|
|
|
36
36
|
@classmethod
|
|
@@ -38,7 +38,9 @@ class PromptValue(Serializable, ABC):
|
|
|
38
38
|
"""Get the namespace of the langchain object.
|
|
39
39
|
|
|
40
40
|
This is used to determine the namespace of the object when serializing.
|
|
41
|
-
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
``["langchain", "schema", "prompt"]``
|
|
42
44
|
"""
|
|
43
45
|
return ["langchain", "schema", "prompt"]
|
|
44
46
|
|
|
@@ -63,7 +65,9 @@ class StringPromptValue(PromptValue):
|
|
|
63
65
|
"""Get the namespace of the langchain object.
|
|
64
66
|
|
|
65
67
|
This is used to determine the namespace of the object when serializing.
|
|
66
|
-
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
``["langchain", "prompts", "base"]``
|
|
67
71
|
"""
|
|
68
72
|
return ["langchain", "prompts", "base"]
|
|
69
73
|
|
|
@@ -98,7 +102,9 @@ class ChatPromptValue(PromptValue):
|
|
|
98
102
|
"""Get the namespace of the langchain object.
|
|
99
103
|
|
|
100
104
|
This is used to determine the namespace of the object when serializing.
|
|
101
|
-
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
``["langchain", "prompts", "chat"]``
|
|
102
108
|
"""
|
|
103
109
|
return ["langchain", "prompts", "chat"]
|
|
104
110
|
|
|
@@ -107,8 +113,12 @@ class ImageURL(TypedDict, total=False):
|
|
|
107
113
|
"""Image URL."""
|
|
108
114
|
|
|
109
115
|
detail: Literal["auto", "low", "high"]
|
|
110
|
-
"""Specifies the detail level of the image. Defaults to
|
|
111
|
-
Can be
|
|
116
|
+
"""Specifies the detail level of the image. Defaults to ``'auto'``.
|
|
117
|
+
Can be ``'auto'``, ``'low'``, or ``'high'``.
|
|
118
|
+
|
|
119
|
+
This follows OpenAI's Chat Completion API's image URL format.
|
|
120
|
+
|
|
121
|
+
"""
|
|
112
122
|
|
|
113
123
|
url: str
|
|
114
124
|
"""Either a URL of the image or the base64 encoded image data."""
|
langchain_core/prompts/base.py
CHANGED
|
@@ -101,16 +101,14 @@ class BasePromptTemplate(
|
|
|
101
101
|
def get_lc_namespace(cls) -> list[str]:
|
|
102
102
|
"""Get the namespace of the langchain object.
|
|
103
103
|
|
|
104
|
-
Returns
|
|
104
|
+
Returns:
|
|
105
|
+
``["langchain", "schema", "prompt_template"]``
|
|
105
106
|
"""
|
|
106
107
|
return ["langchain", "schema", "prompt_template"]
|
|
107
108
|
|
|
108
109
|
@classmethod
|
|
109
110
|
def is_lc_serializable(cls) -> bool:
|
|
110
|
-
"""Return
|
|
111
|
-
|
|
112
|
-
Returns True.
|
|
113
|
-
"""
|
|
111
|
+
"""Return True as this class is serializable."""
|
|
114
112
|
return True
|
|
115
113
|
|
|
116
114
|
model_config = ConfigDict(
|
|
@@ -212,7 +210,7 @@ class BasePromptTemplate(
|
|
|
212
210
|
if self.metadata:
|
|
213
211
|
config["metadata"] = {**config["metadata"], **self.metadata}
|
|
214
212
|
if self.tags:
|
|
215
|
-
config["tags"]
|
|
213
|
+
config["tags"] += self.tags
|
|
216
214
|
return self._call_with_config(
|
|
217
215
|
self._format_prompt_with_error_handling,
|
|
218
216
|
input,
|
|
@@ -341,9 +339,6 @@ class BasePromptTemplate(
|
|
|
341
339
|
|
|
342
340
|
Returns:
|
|
343
341
|
Dict: Dictionary representation of the prompt.
|
|
344
|
-
|
|
345
|
-
Raises:
|
|
346
|
-
NotImplementedError: If the prompt type is not implemented.
|
|
347
342
|
"""
|
|
348
343
|
prompt_dict = super().model_dump(**kwargs)
|
|
349
344
|
with contextlib.suppress(NotImplementedError):
|