langchain-core 1.0.0a8__py3-none-any.whl → 1.0.0rc2__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 +0 -1
- langchain_core/_api/beta_decorator.py +17 -20
- langchain_core/_api/deprecation.py +30 -35
- langchain_core/_import_utils.py +1 -1
- langchain_core/agents.py +10 -9
- langchain_core/caches.py +46 -56
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +232 -243
- langchain_core/callbacks/file.py +33 -33
- langchain_core/callbacks/manager.py +353 -416
- langchain_core/callbacks/stdout.py +21 -22
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +54 -51
- langchain_core/chat_history.py +43 -58
- langchain_core/document_loaders/base.py +21 -21
- langchain_core/document_loaders/langsmith.py +22 -22
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +46 -49
- langchain_core/documents/transformers.py +28 -29
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/semantic_similarity.py +4 -6
- langchain_core/exceptions.py +7 -8
- langchain_core/indexing/api.py +19 -25
- langchain_core/indexing/base.py +24 -24
- langchain_core/language_models/__init__.py +11 -27
- langchain_core/language_models/_utils.py +53 -54
- langchain_core/language_models/base.py +30 -24
- langchain_core/language_models/chat_models.py +123 -148
- langchain_core/language_models/fake_chat_models.py +7 -7
- langchain_core/language_models/llms.py +14 -16
- langchain_core/load/dump.py +3 -4
- langchain_core/load/load.py +7 -16
- langchain_core/load/serializable.py +37 -36
- langchain_core/messages/__init__.py +1 -16
- langchain_core/messages/ai.py +122 -123
- langchain_core/messages/base.py +31 -31
- langchain_core/messages/block_translators/__init__.py +17 -17
- langchain_core/messages/block_translators/anthropic.py +3 -3
- langchain_core/messages/block_translators/bedrock_converse.py +3 -3
- langchain_core/messages/block_translators/google_genai.py +5 -4
- 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 +3 -3
- langchain_core/messages/block_translators/openai.py +5 -5
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +222 -209
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +17 -24
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +12 -22
- langchain_core/messages/tool.py +53 -69
- langchain_core/messages/utils.py +399 -417
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +46 -47
- langchain_core/output_parsers/json.py +3 -4
- langchain_core/output_parsers/list.py +2 -2
- langchain_core/output_parsers/openai_functions.py +46 -44
- langchain_core/output_parsers/openai_tools.py +11 -16
- langchain_core/output_parsers/pydantic.py +10 -11
- langchain_core/output_parsers/string.py +2 -2
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +1 -1
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +14 -14
- langchain_core/outputs/generation.py +6 -6
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompt_values.py +11 -11
- langchain_core/prompts/__init__.py +3 -23
- langchain_core/prompts/base.py +33 -38
- langchain_core/prompts/chat.py +222 -229
- langchain_core/prompts/dict.py +3 -3
- langchain_core/prompts/few_shot.py +76 -83
- langchain_core/prompts/few_shot_with_templates.py +7 -9
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +1 -1
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +20 -23
- langchain_core/prompts/string.py +20 -8
- langchain_core/prompts/structured.py +26 -27
- langchain_core/rate_limiters.py +50 -58
- langchain_core/retrievers.py +41 -182
- langchain_core/runnables/base.py +565 -597
- langchain_core/runnables/branch.py +8 -8
- langchain_core/runnables/config.py +37 -44
- langchain_core/runnables/configurable.py +9 -10
- langchain_core/runnables/fallbacks.py +9 -9
- langchain_core/runnables/graph.py +46 -50
- langchain_core/runnables/graph_ascii.py +19 -18
- langchain_core/runnables/graph_mermaid.py +20 -31
- langchain_core/runnables/graph_png.py +7 -7
- langchain_core/runnables/history.py +22 -22
- langchain_core/runnables/passthrough.py +11 -11
- langchain_core/runnables/retry.py +3 -3
- langchain_core/runnables/router.py +2 -2
- langchain_core/runnables/schema.py +33 -33
- langchain_core/runnables/utils.py +30 -34
- langchain_core/stores.py +72 -102
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +70 -71
- langchain_core/tools/convert.py +100 -104
- langchain_core/tools/render.py +9 -9
- langchain_core/tools/retriever.py +7 -7
- langchain_core/tools/simple.py +6 -7
- langchain_core/tools/structured.py +18 -24
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +35 -35
- langchain_core/tracers/context.py +12 -17
- langchain_core/tracers/event_stream.py +3 -3
- langchain_core/tracers/langchain.py +8 -8
- langchain_core/tracers/log_stream.py +17 -18
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +2 -2
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +1 -2
- langchain_core/utils/__init__.py +1 -1
- langchain_core/utils/aiter.py +32 -32
- langchain_core/utils/env.py +5 -5
- langchain_core/utils/function_calling.py +59 -154
- langchain_core/utils/html.py +4 -4
- langchain_core/utils/input.py +3 -3
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +20 -20
- langchain_core/utils/json.py +1 -1
- langchain_core/utils/json_schema.py +2 -2
- langchain_core/utils/mustache.py +5 -5
- langchain_core/utils/pydantic.py +17 -17
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/utils.py +25 -28
- langchain_core/vectorstores/base.py +55 -87
- langchain_core/vectorstores/in_memory.py +83 -85
- langchain_core/vectorstores/utils.py +2 -2
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/METADATA +23 -11
- langchain_core-1.0.0rc2.dist-info/RECORD +172 -0
- langchain_core/memory.py +0 -120
- 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-1.0.0a8.dist-info/RECORD +0 -176
- {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/WHEEL +0 -0
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
"""**OutputParser** classes parse the output of an LLM call.
|
|
2
|
-
|
|
3
|
-
**Class hierarchy:**
|
|
4
|
-
|
|
5
|
-
.. code-block::
|
|
6
|
-
|
|
7
|
-
BaseLLMOutputParser --> BaseOutputParser --> <name>OutputParser # ListOutputParser, PydanticOutputParser
|
|
8
|
-
|
|
9
|
-
**Main helpers:**
|
|
10
|
-
|
|
11
|
-
.. code-block::
|
|
12
|
-
|
|
13
|
-
Serializable, Generation, PromptValue
|
|
14
|
-
""" # noqa: E501
|
|
1
|
+
"""**OutputParser** classes parse the output of an LLM call."""
|
|
15
2
|
|
|
16
3
|
from typing import TYPE_CHECKING
|
|
17
4
|
|
|
@@ -31,13 +31,13 @@ class BaseLLMOutputParser(ABC, Generic[T]):
|
|
|
31
31
|
|
|
32
32
|
@abstractmethod
|
|
33
33
|
def parse_result(self, result: list[Generation], *, partial: bool = False) -> T:
|
|
34
|
-
"""Parse a list of candidate model
|
|
34
|
+
"""Parse a list of candidate model `Generation` objects into a specific format.
|
|
35
35
|
|
|
36
36
|
Args:
|
|
37
|
-
result: A list of
|
|
38
|
-
to be different candidate outputs for a single model input.
|
|
37
|
+
result: A list of `Generation` to be parsed. The `Generation` objects are
|
|
38
|
+
assumed to be different candidate outputs for a single model input.
|
|
39
39
|
partial: Whether to parse the output as a partial result. This is useful
|
|
40
|
-
for parsers that can parse partial results.
|
|
40
|
+
for parsers that can parse partial results.
|
|
41
41
|
|
|
42
42
|
Returns:
|
|
43
43
|
Structured output.
|
|
@@ -46,17 +46,17 @@ class BaseLLMOutputParser(ABC, Generic[T]):
|
|
|
46
46
|
async def aparse_result(
|
|
47
47
|
self, result: list[Generation], *, partial: bool = False
|
|
48
48
|
) -> T:
|
|
49
|
-
"""Async parse a list of candidate model
|
|
49
|
+
"""Async parse a list of candidate model `Generation` objects into a specific format.
|
|
50
50
|
|
|
51
51
|
Args:
|
|
52
|
-
result: A list of
|
|
52
|
+
result: A list of `Generation` to be parsed. The Generations are assumed
|
|
53
53
|
to be different candidate outputs for a single model input.
|
|
54
54
|
partial: Whether to parse the output as a partial result. This is useful
|
|
55
|
-
for parsers that can parse partial results.
|
|
55
|
+
for parsers that can parse partial results.
|
|
56
56
|
|
|
57
57
|
Returns:
|
|
58
58
|
Structured output.
|
|
59
|
-
"""
|
|
59
|
+
""" # noqa: E501
|
|
60
60
|
return await run_in_executor(None, self.parse_result, result, partial=partial)
|
|
61
61
|
|
|
62
62
|
|
|
@@ -134,29 +134,28 @@ class BaseOutputParser(
|
|
|
134
134
|
Output parsers help structure language model responses.
|
|
135
135
|
|
|
136
136
|
Example:
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
137
|
+
```python
|
|
138
|
+
class BooleanOutputParser(BaseOutputParser[bool]):
|
|
139
|
+
true_val: str = "YES"
|
|
140
|
+
false_val: str = "NO"
|
|
141
|
+
|
|
142
|
+
def parse(self, text: str) -> bool:
|
|
143
|
+
cleaned_text = text.strip().upper()
|
|
144
|
+
if cleaned_text not in (
|
|
145
|
+
self.true_val.upper(),
|
|
146
|
+
self.false_val.upper(),
|
|
147
|
+
):
|
|
148
|
+
raise OutputParserException(
|
|
149
|
+
f"BooleanOutputParser expected output value to either be "
|
|
150
|
+
f"{self.true_val} or {self.false_val} (case-insensitive). "
|
|
151
|
+
f"Received {cleaned_text}."
|
|
152
|
+
)
|
|
153
|
+
return cleaned_text == self.true_val.upper()
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def _type(self) -> str:
|
|
157
|
+
return "boolean_output_parser"
|
|
158
|
+
```
|
|
160
159
|
"""
|
|
161
160
|
|
|
162
161
|
@property
|
|
@@ -173,7 +172,7 @@ class BaseOutputParser(
|
|
|
173
172
|
This property is inferred from the first type argument of the class.
|
|
174
173
|
|
|
175
174
|
Raises:
|
|
176
|
-
TypeError: If the class doesn't have an inferable OutputType
|
|
175
|
+
TypeError: If the class doesn't have an inferable `OutputType`.
|
|
177
176
|
"""
|
|
178
177
|
for base in self.__class__.mro():
|
|
179
178
|
if hasattr(base, "__pydantic_generic_metadata__"):
|
|
@@ -235,16 +234,16 @@ class BaseOutputParser(
|
|
|
235
234
|
|
|
236
235
|
@override
|
|
237
236
|
def parse_result(self, result: list[Generation], *, partial: bool = False) -> T:
|
|
238
|
-
"""Parse a list of candidate model
|
|
237
|
+
"""Parse a list of candidate model `Generation` objects into a specific format.
|
|
239
238
|
|
|
240
|
-
The return value is parsed from only the first Generation in the result, which
|
|
241
|
-
is assumed to be the highest-likelihood Generation
|
|
239
|
+
The return value is parsed from only the first `Generation` in the result, which
|
|
240
|
+
is assumed to be the highest-likelihood `Generation`.
|
|
242
241
|
|
|
243
242
|
Args:
|
|
244
|
-
result: A list of
|
|
245
|
-
to be different candidate outputs for a single model input.
|
|
243
|
+
result: A list of `Generation` to be parsed. The `Generation` objects are
|
|
244
|
+
assumed to be different candidate outputs for a single model input.
|
|
246
245
|
partial: Whether to parse the output as a partial result. This is useful
|
|
247
|
-
for parsers that can parse partial results.
|
|
246
|
+
for parsers that can parse partial results.
|
|
248
247
|
|
|
249
248
|
Returns:
|
|
250
249
|
Structured output.
|
|
@@ -265,20 +264,20 @@ class BaseOutputParser(
|
|
|
265
264
|
async def aparse_result(
|
|
266
265
|
self, result: list[Generation], *, partial: bool = False
|
|
267
266
|
) -> T:
|
|
268
|
-
"""Async parse a list of candidate model
|
|
267
|
+
"""Async parse a list of candidate model `Generation` objects into a specific format.
|
|
269
268
|
|
|
270
|
-
The return value is parsed from only the first Generation in the result, which
|
|
271
|
-
is assumed to be the highest-likelihood Generation
|
|
269
|
+
The return value is parsed from only the first `Generation` in the result, which
|
|
270
|
+
is assumed to be the highest-likelihood `Generation`.
|
|
272
271
|
|
|
273
272
|
Args:
|
|
274
|
-
result: A list of
|
|
275
|
-
to be different candidate outputs for a single model input.
|
|
273
|
+
result: A list of `Generation` to be parsed. The `Generation` objects are
|
|
274
|
+
assumed to be different candidate outputs for a single model input.
|
|
276
275
|
partial: Whether to parse the output as a partial result. This is useful
|
|
277
|
-
for parsers that can parse partial results.
|
|
276
|
+
for parsers that can parse partial results.
|
|
278
277
|
|
|
279
278
|
Returns:
|
|
280
279
|
Structured output.
|
|
281
|
-
"""
|
|
280
|
+
""" # noqa: E501
|
|
282
281
|
return await run_in_executor(None, self.parse_result, result, partial=partial)
|
|
283
282
|
|
|
284
283
|
async def aparse(self, text: str) -> T:
|
|
@@ -300,13 +299,13 @@ class BaseOutputParser(
|
|
|
300
299
|
) -> Any:
|
|
301
300
|
"""Parse the output of an LLM call with the input prompt for context.
|
|
302
301
|
|
|
303
|
-
The prompt is largely provided in the event the OutputParser wants
|
|
302
|
+
The prompt is largely provided in the event the `OutputParser` wants
|
|
304
303
|
to retry or fix the output in some way, and needs information from
|
|
305
304
|
the prompt to do so.
|
|
306
305
|
|
|
307
306
|
Args:
|
|
308
307
|
completion: String output of a language model.
|
|
309
|
-
prompt: Input PromptValue
|
|
308
|
+
prompt: Input `PromptValue`.
|
|
310
309
|
|
|
311
310
|
Returns:
|
|
312
311
|
Structured output.
|
|
@@ -40,7 +40,7 @@ class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
40
40
|
|
|
41
41
|
pydantic_object: Annotated[type[TBaseModel] | None, SkipValidation()] = None # type: ignore[valid-type]
|
|
42
42
|
"""The Pydantic object to use for validation.
|
|
43
|
-
If None
|
|
43
|
+
If `None`, no validation is performed."""
|
|
44
44
|
|
|
45
45
|
@override
|
|
46
46
|
def _diff(self, prev: Any | None, next: Any) -> Any:
|
|
@@ -59,10 +59,9 @@ class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
59
59
|
Args:
|
|
60
60
|
result: The result of the LLM call.
|
|
61
61
|
partial: Whether to parse partial JSON objects.
|
|
62
|
-
If True
|
|
62
|
+
If `True`, the output will be a JSON object containing
|
|
63
63
|
all the keys that have been returned so far.
|
|
64
|
-
If False
|
|
65
|
-
Default is False.
|
|
64
|
+
If `False`, the output will be the full JSON object.
|
|
66
65
|
|
|
67
66
|
Returns:
|
|
68
67
|
The parsed JSON object.
|
|
@@ -146,10 +146,10 @@ class CommaSeparatedListOutputParser(ListOutputParser):
|
|
|
146
146
|
|
|
147
147
|
@classmethod
|
|
148
148
|
def get_lc_namespace(cls) -> list[str]:
|
|
149
|
-
"""Get the namespace of the
|
|
149
|
+
"""Get the namespace of the LangChain object.
|
|
150
150
|
|
|
151
151
|
Returns:
|
|
152
|
-
|
|
152
|
+
`["langchain", "output_parsers", "list"]`
|
|
153
153
|
"""
|
|
154
154
|
return ["langchain", "output_parsers", "list"]
|
|
155
155
|
|
|
@@ -31,13 +31,13 @@ class OutputFunctionsParser(BaseGenerationOutputParser[Any]):
|
|
|
31
31
|
|
|
32
32
|
Args:
|
|
33
33
|
result: The result of the LLM call.
|
|
34
|
-
partial: Whether to parse partial JSON objects.
|
|
34
|
+
partial: Whether to parse partial JSON objects.
|
|
35
35
|
|
|
36
36
|
Returns:
|
|
37
37
|
The parsed JSON object.
|
|
38
38
|
|
|
39
39
|
Raises:
|
|
40
|
-
OutputParserException
|
|
40
|
+
`OutputParserException`: If the output is not valid JSON.
|
|
41
41
|
"""
|
|
42
42
|
generation = result[0]
|
|
43
43
|
if not isinstance(generation, ChatGeneration):
|
|
@@ -56,7 +56,7 @@ class OutputFunctionsParser(BaseGenerationOutputParser[Any]):
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
class JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):
|
|
59
|
-
"""Parse an output as the
|
|
59
|
+
"""Parse an output as the JSON object."""
|
|
60
60
|
|
|
61
61
|
strict: bool = False
|
|
62
62
|
"""Whether to allow non-JSON-compliant strings.
|
|
@@ -82,13 +82,13 @@ class JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
82
82
|
|
|
83
83
|
Args:
|
|
84
84
|
result: The result of the LLM call.
|
|
85
|
-
partial: Whether to parse partial JSON objects.
|
|
85
|
+
partial: Whether to parse partial JSON objects.
|
|
86
86
|
|
|
87
87
|
Returns:
|
|
88
88
|
The parsed JSON object.
|
|
89
89
|
|
|
90
90
|
Raises:
|
|
91
|
-
|
|
91
|
+
OutputParserExcept`ion: If the output is not valid JSON.
|
|
92
92
|
"""
|
|
93
93
|
if len(result) != 1:
|
|
94
94
|
msg = f"Expected exactly one result, but got {len(result)}"
|
|
@@ -155,7 +155,7 @@ class JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
155
155
|
|
|
156
156
|
|
|
157
157
|
class JsonKeyOutputFunctionsParser(JsonOutputFunctionsParser):
|
|
158
|
-
"""Parse an output as the element of the
|
|
158
|
+
"""Parse an output as the element of the JSON object."""
|
|
159
159
|
|
|
160
160
|
key_name: str
|
|
161
161
|
"""The name of the key to return."""
|
|
@@ -165,7 +165,7 @@ class JsonKeyOutputFunctionsParser(JsonOutputFunctionsParser):
|
|
|
165
165
|
|
|
166
166
|
Args:
|
|
167
167
|
result: The result of the LLM call.
|
|
168
|
-
partial: Whether to parse partial JSON objects.
|
|
168
|
+
partial: Whether to parse partial JSON objects.
|
|
169
169
|
|
|
170
170
|
Returns:
|
|
171
171
|
The parsed JSON object.
|
|
@@ -177,48 +177,50 @@ class JsonKeyOutputFunctionsParser(JsonOutputFunctionsParser):
|
|
|
177
177
|
|
|
178
178
|
|
|
179
179
|
class PydanticOutputFunctionsParser(OutputFunctionsParser):
|
|
180
|
-
"""Parse an output as a
|
|
180
|
+
"""Parse an output as a Pydantic object.
|
|
181
181
|
|
|
182
|
-
This parser is used to parse the output of a
|
|
183
|
-
|
|
182
|
+
This parser is used to parse the output of a chat model that uses OpenAI function
|
|
183
|
+
format to invoke functions.
|
|
184
184
|
|
|
185
|
-
The parser extracts the function call invocation and matches
|
|
186
|
-
|
|
185
|
+
The parser extracts the function call invocation and matches them to the Pydantic
|
|
186
|
+
schema provided.
|
|
187
187
|
|
|
188
|
-
An exception will be raised if the function call does not match
|
|
189
|
-
the provided schema.
|
|
188
|
+
An exception will be raised if the function call does not match the provided schema.
|
|
190
189
|
|
|
191
190
|
Example:
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
"
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
chat_generation = ChatGeneration(message=message)
|
|
191
|
+
```python
|
|
192
|
+
message = AIMessage(
|
|
193
|
+
content="This is a test message",
|
|
194
|
+
additional_kwargs={
|
|
195
|
+
"function_call": {
|
|
196
|
+
"name": "cookie",
|
|
197
|
+
"arguments": json.dumps({"name": "value", "age": 10}),
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
)
|
|
201
|
+
chat_generation = ChatGeneration(message=message)
|
|
204
202
|
|
|
205
|
-
class Cookie(BaseModel):
|
|
206
|
-
name: str
|
|
207
|
-
age: int
|
|
208
203
|
|
|
209
|
-
|
|
210
|
-
|
|
204
|
+
class Cookie(BaseModel):
|
|
205
|
+
name: str
|
|
206
|
+
age: int
|
|
211
207
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
208
|
+
|
|
209
|
+
class Dog(BaseModel):
|
|
210
|
+
species: str
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# Full output
|
|
214
|
+
parser = PydanticOutputFunctionsParser(
|
|
215
|
+
pydantic_schema={"cookie": Cookie, "dog": Dog}
|
|
216
|
+
)
|
|
217
|
+
result = parser.parse_result([chat_generation])
|
|
218
|
+
```
|
|
217
219
|
|
|
218
220
|
"""
|
|
219
221
|
|
|
220
222
|
pydantic_schema: type[BaseModel] | dict[str, type[BaseModel]]
|
|
221
|
-
"""The
|
|
223
|
+
"""The Pydantic schema to parse the output with.
|
|
222
224
|
|
|
223
225
|
If multiple schemas are provided, then the function name will be used to
|
|
224
226
|
determine which schema to use.
|
|
@@ -227,7 +229,7 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser):
|
|
|
227
229
|
@model_validator(mode="before")
|
|
228
230
|
@classmethod
|
|
229
231
|
def validate_schema(cls, values: dict) -> Any:
|
|
230
|
-
"""Validate the
|
|
232
|
+
"""Validate the Pydantic schema.
|
|
231
233
|
|
|
232
234
|
Args:
|
|
233
235
|
values: The values to validate.
|
|
@@ -236,7 +238,7 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser):
|
|
|
236
238
|
The validated values.
|
|
237
239
|
|
|
238
240
|
Raises:
|
|
239
|
-
ValueError: If the schema is not a
|
|
241
|
+
ValueError: If the schema is not a Pydantic schema.
|
|
240
242
|
"""
|
|
241
243
|
schema = values["pydantic_schema"]
|
|
242
244
|
if "args_only" not in values:
|
|
@@ -259,10 +261,10 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser):
|
|
|
259
261
|
|
|
260
262
|
Args:
|
|
261
263
|
result: The result of the LLM call.
|
|
262
|
-
partial: Whether to parse partial JSON objects.
|
|
264
|
+
partial: Whether to parse partial JSON objects.
|
|
263
265
|
|
|
264
266
|
Raises:
|
|
265
|
-
ValueError: If the
|
|
267
|
+
ValueError: If the Pydantic schema is not valid.
|
|
266
268
|
|
|
267
269
|
Returns:
|
|
268
270
|
The parsed JSON object.
|
|
@@ -285,13 +287,13 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser):
|
|
|
285
287
|
elif issubclass(pydantic_schema, BaseModelV1):
|
|
286
288
|
pydantic_args = pydantic_schema.parse_raw(args)
|
|
287
289
|
else:
|
|
288
|
-
msg = f"Unsupported
|
|
290
|
+
msg = f"Unsupported Pydantic schema: {pydantic_schema}"
|
|
289
291
|
raise ValueError(msg)
|
|
290
292
|
return pydantic_args
|
|
291
293
|
|
|
292
294
|
|
|
293
295
|
class PydanticAttrOutputFunctionsParser(PydanticOutputFunctionsParser):
|
|
294
|
-
"""Parse an output as an attribute of a
|
|
296
|
+
"""Parse an output as an attribute of a Pydantic object."""
|
|
295
297
|
|
|
296
298
|
attr_name: str
|
|
297
299
|
"""The name of the attribute to return."""
|
|
@@ -302,7 +304,7 @@ class PydanticAttrOutputFunctionsParser(PydanticOutputFunctionsParser):
|
|
|
302
304
|
|
|
303
305
|
Args:
|
|
304
306
|
result: The result of the LLM call.
|
|
305
|
-
partial: Whether to parse partial JSON objects.
|
|
307
|
+
partial: Whether to parse partial JSON objects.
|
|
306
308
|
|
|
307
309
|
Returns:
|
|
308
310
|
The parsed JSON object.
|
|
@@ -31,10 +31,9 @@ def parse_tool_call(
|
|
|
31
31
|
|
|
32
32
|
Args:
|
|
33
33
|
raw_tool_call: The raw tool call to parse.
|
|
34
|
-
partial: Whether to parse partial JSON.
|
|
34
|
+
partial: Whether to parse partial JSON.
|
|
35
35
|
strict: Whether to allow non-JSON-compliant strings.
|
|
36
|
-
|
|
37
|
-
return_id: Whether to return the tool call id. Default is True.
|
|
36
|
+
return_id: Whether to return the tool call id.
|
|
38
37
|
|
|
39
38
|
Returns:
|
|
40
39
|
The parsed tool call.
|
|
@@ -105,10 +104,9 @@ def parse_tool_calls(
|
|
|
105
104
|
|
|
106
105
|
Args:
|
|
107
106
|
raw_tool_calls: The raw tool calls to parse.
|
|
108
|
-
partial: Whether to parse partial JSON.
|
|
107
|
+
partial: Whether to parse partial JSON.
|
|
109
108
|
strict: Whether to allow non-JSON-compliant strings.
|
|
110
|
-
|
|
111
|
-
return_id: Whether to return the tool call id. Default is True.
|
|
109
|
+
return_id: Whether to return the tool call id.
|
|
112
110
|
|
|
113
111
|
Returns:
|
|
114
112
|
The parsed tool calls.
|
|
@@ -148,7 +146,7 @@ class JsonOutputToolsParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
148
146
|
first_tool_only: bool = False
|
|
149
147
|
"""Whether to return only the first tool call.
|
|
150
148
|
|
|
151
|
-
If False
|
|
149
|
+
If `False`, the result will be a list of tool calls, or an empty list
|
|
152
150
|
if no tool calls are found.
|
|
153
151
|
|
|
154
152
|
If true, and multiple tool calls are found, only the first one will be returned,
|
|
@@ -162,10 +160,9 @@ class JsonOutputToolsParser(BaseCumulativeTransformOutputParser[Any]):
|
|
|
162
160
|
Args:
|
|
163
161
|
result: The result of the LLM call.
|
|
164
162
|
partial: Whether to parse partial JSON.
|
|
165
|
-
If True
|
|
163
|
+
If `True`, the output will be a JSON object containing
|
|
166
164
|
all the keys that have been returned so far.
|
|
167
|
-
If False
|
|
168
|
-
Default is False.
|
|
165
|
+
If `False`, the output will be the full JSON object.
|
|
169
166
|
|
|
170
167
|
Returns:
|
|
171
168
|
The parsed tool calls.
|
|
@@ -226,10 +223,9 @@ class JsonOutputKeyToolsParser(JsonOutputToolsParser):
|
|
|
226
223
|
Args:
|
|
227
224
|
result: The result of the LLM call.
|
|
228
225
|
partial: Whether to parse partial JSON.
|
|
229
|
-
If True
|
|
226
|
+
If `True`, the output will be a JSON object containing
|
|
230
227
|
all the keys that have been returned so far.
|
|
231
|
-
If False
|
|
232
|
-
Default is False.
|
|
228
|
+
If `False`, the output will be the full JSON object.
|
|
233
229
|
|
|
234
230
|
Raises:
|
|
235
231
|
OutputParserException: If the generation is not a chat generation.
|
|
@@ -310,10 +306,9 @@ class PydanticToolsParser(JsonOutputToolsParser):
|
|
|
310
306
|
Args:
|
|
311
307
|
result: The result of the LLM call.
|
|
312
308
|
partial: Whether to parse partial JSON.
|
|
313
|
-
If True
|
|
309
|
+
If `True`, the output will be a JSON object containing
|
|
314
310
|
all the keys that have been returned so far.
|
|
315
|
-
If False
|
|
316
|
-
Default is False.
|
|
311
|
+
If `False`, the output will be the full JSON object.
|
|
317
312
|
|
|
318
313
|
Returns:
|
|
319
314
|
The parsed Pydantic objects.
|
|
@@ -17,10 +17,10 @@ from langchain_core.utils.pydantic import (
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):
|
|
20
|
-
"""Parse an output using a
|
|
20
|
+
"""Parse an output using a Pydantic model."""
|
|
21
21
|
|
|
22
22
|
pydantic_object: Annotated[type[TBaseModel], SkipValidation()]
|
|
23
|
-
"""The
|
|
23
|
+
"""The Pydantic model to parse."""
|
|
24
24
|
|
|
25
25
|
def _parse_obj(self, obj: dict) -> TBaseModel:
|
|
26
26
|
try:
|
|
@@ -45,21 +45,20 @@ class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):
|
|
|
45
45
|
def parse_result(
|
|
46
46
|
self, result: list[Generation], *, partial: bool = False
|
|
47
47
|
) -> TBaseModel | None:
|
|
48
|
-
"""Parse the result of an LLM call to a
|
|
48
|
+
"""Parse the result of an LLM call to a Pydantic object.
|
|
49
49
|
|
|
50
50
|
Args:
|
|
51
51
|
result: The result of the LLM call.
|
|
52
52
|
partial: Whether to parse partial JSON objects.
|
|
53
|
-
If True
|
|
53
|
+
If `True`, the output will be a JSON object containing
|
|
54
54
|
all the keys that have been returned so far.
|
|
55
|
-
Defaults to False.
|
|
56
55
|
|
|
57
56
|
Raises:
|
|
58
|
-
OutputParserException
|
|
59
|
-
or does not conform to the
|
|
57
|
+
`OutputParserException`: If the result is not valid JSON
|
|
58
|
+
or does not conform to the Pydantic model.
|
|
60
59
|
|
|
61
60
|
Returns:
|
|
62
|
-
The parsed
|
|
61
|
+
The parsed Pydantic object.
|
|
63
62
|
"""
|
|
64
63
|
try:
|
|
65
64
|
json_object = super().parse_result(result)
|
|
@@ -70,13 +69,13 @@ class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):
|
|
|
70
69
|
raise
|
|
71
70
|
|
|
72
71
|
def parse(self, text: str) -> TBaseModel:
|
|
73
|
-
"""Parse the output of an LLM call to a
|
|
72
|
+
"""Parse the output of an LLM call to a Pydantic object.
|
|
74
73
|
|
|
75
74
|
Args:
|
|
76
75
|
text: The output of the LLM call.
|
|
77
76
|
|
|
78
77
|
Returns:
|
|
79
|
-
The parsed
|
|
78
|
+
The parsed Pydantic object.
|
|
80
79
|
"""
|
|
81
80
|
return super().parse(text)
|
|
82
81
|
|
|
@@ -107,7 +106,7 @@ class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):
|
|
|
107
106
|
@property
|
|
108
107
|
@override
|
|
109
108
|
def OutputType(self) -> type[TBaseModel]:
|
|
110
|
-
"""Return the
|
|
109
|
+
"""Return the Pydantic model."""
|
|
111
110
|
return self.pydantic_object
|
|
112
111
|
|
|
113
112
|
|
|
@@ -19,10 +19,10 @@ class StrOutputParser(BaseTransformOutputParser[str]):
|
|
|
19
19
|
|
|
20
20
|
@classmethod
|
|
21
21
|
def get_lc_namespace(cls) -> list[str]:
|
|
22
|
-
"""Get the namespace of the
|
|
22
|
+
"""Get the namespace of the LangChain object.
|
|
23
23
|
|
|
24
24
|
Returns:
|
|
25
|
-
|
|
25
|
+
`["langchain", "schema", "output_parser"]`
|
|
26
26
|
"""
|
|
27
27
|
return ["langchain", "schema", "output_parser"]
|
|
28
28
|
|
|
@@ -64,7 +64,7 @@ class BaseTransformOutputParser(BaseOutputParser[T]):
|
|
|
64
64
|
Args:
|
|
65
65
|
input: The input to transform.
|
|
66
66
|
config: The configuration to use for the transformation.
|
|
67
|
-
kwargs: Additional keyword arguments.
|
|
67
|
+
**kwargs: Additional keyword arguments.
|
|
68
68
|
|
|
69
69
|
Yields:
|
|
70
70
|
The transformed output.
|
|
@@ -85,7 +85,7 @@ class BaseTransformOutputParser(BaseOutputParser[T]):
|
|
|
85
85
|
Args:
|
|
86
86
|
input: The input to transform.
|
|
87
87
|
config: The configuration to use for the transformation.
|
|
88
|
-
kwargs: Additional keyword arguments.
|
|
88
|
+
**kwargs: Additional keyword arguments.
|
|
89
89
|
|
|
90
90
|
Yields:
|
|
91
91
|
The transformed output.
|
|
@@ -82,7 +82,7 @@ class _StreamingParser:
|
|
|
82
82
|
chunk: A chunk of text to parse. This can be a string or a BaseMessage.
|
|
83
83
|
|
|
84
84
|
Yields:
|
|
85
|
-
|
|
85
|
+
A dictionary representing the parsed XML element.
|
|
86
86
|
|
|
87
87
|
Raises:
|
|
88
88
|
xml.etree.ElementTree.ParseError: If the XML is not well-formed.
|
|
@@ -12,7 +12,7 @@ When invoking models via the standard runnable methods (e.g. invoke, batch, etc.
|
|
|
12
12
|
- LLMs will return regular text strings.
|
|
13
13
|
|
|
14
14
|
In addition, users can access the raw output of either LLMs or chat models via
|
|
15
|
-
callbacks. The
|
|
15
|
+
callbacks. The `on_chat_model_end` and `on_llm_end` callbacks will return an
|
|
16
16
|
LLMResult object containing the generated outputs and any additional information
|
|
17
17
|
returned by the model provider.
|
|
18
18
|
|