langchain-core 1.0.0a6__py3-none-any.whl → 1.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +23 -26
  4. langchain_core/_api/deprecation.py +51 -64
  5. langchain_core/_api/path.py +3 -6
  6. langchain_core/_import_utils.py +3 -4
  7. langchain_core/agents.py +55 -48
  8. langchain_core/caches.py +65 -66
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +321 -336
  11. langchain_core/callbacks/file.py +44 -44
  12. langchain_core/callbacks/manager.py +454 -514
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +32 -32
  15. langchain_core/callbacks/usage.py +60 -57
  16. langchain_core/chat_history.py +53 -68
  17. langchain_core/document_loaders/base.py +27 -25
  18. langchain_core/document_loaders/blob_loaders.py +1 -1
  19. langchain_core/document_loaders/langsmith.py +44 -48
  20. langchain_core/documents/__init__.py +23 -3
  21. langchain_core/documents/base.py +102 -94
  22. langchain_core/documents/compressor.py +10 -10
  23. langchain_core/documents/transformers.py +34 -35
  24. langchain_core/embeddings/fake.py +50 -54
  25. langchain_core/example_selectors/length_based.py +2 -2
  26. langchain_core/example_selectors/semantic_similarity.py +28 -32
  27. langchain_core/exceptions.py +21 -20
  28. langchain_core/globals.py +3 -151
  29. langchain_core/indexing/__init__.py +1 -1
  30. langchain_core/indexing/api.py +121 -126
  31. langchain_core/indexing/base.py +73 -75
  32. langchain_core/indexing/in_memory.py +4 -6
  33. langchain_core/language_models/__init__.py +14 -29
  34. langchain_core/language_models/_utils.py +58 -61
  35. langchain_core/language_models/base.py +82 -172
  36. langchain_core/language_models/chat_models.py +329 -402
  37. langchain_core/language_models/fake.py +11 -11
  38. langchain_core/language_models/fake_chat_models.py +42 -36
  39. langchain_core/language_models/llms.py +189 -269
  40. langchain_core/load/dump.py +9 -12
  41. langchain_core/load/load.py +18 -28
  42. langchain_core/load/mapping.py +2 -4
  43. langchain_core/load/serializable.py +42 -40
  44. langchain_core/messages/__init__.py +10 -16
  45. langchain_core/messages/ai.py +148 -148
  46. langchain_core/messages/base.py +53 -51
  47. langchain_core/messages/block_translators/__init__.py +19 -22
  48. langchain_core/messages/block_translators/anthropic.py +6 -6
  49. langchain_core/messages/block_translators/bedrock_converse.py +5 -5
  50. langchain_core/messages/block_translators/google_genai.py +10 -7
  51. langchain_core/messages/block_translators/google_vertexai.py +4 -32
  52. langchain_core/messages/block_translators/groq.py +117 -21
  53. langchain_core/messages/block_translators/langchain_v0.py +5 -5
  54. langchain_core/messages/block_translators/openai.py +11 -11
  55. langchain_core/messages/chat.py +2 -6
  56. langchain_core/messages/content.py +339 -330
  57. langchain_core/messages/function.py +6 -10
  58. langchain_core/messages/human.py +24 -31
  59. langchain_core/messages/modifier.py +2 -2
  60. langchain_core/messages/system.py +19 -29
  61. langchain_core/messages/tool.py +74 -90
  62. langchain_core/messages/utils.py +484 -510
  63. langchain_core/output_parsers/__init__.py +13 -10
  64. langchain_core/output_parsers/base.py +61 -61
  65. langchain_core/output_parsers/format_instructions.py +9 -4
  66. langchain_core/output_parsers/json.py +12 -10
  67. langchain_core/output_parsers/list.py +21 -23
  68. langchain_core/output_parsers/openai_functions.py +49 -47
  69. langchain_core/output_parsers/openai_tools.py +30 -23
  70. langchain_core/output_parsers/pydantic.py +13 -14
  71. langchain_core/output_parsers/string.py +5 -5
  72. langchain_core/output_parsers/transform.py +15 -17
  73. langchain_core/output_parsers/xml.py +35 -34
  74. langchain_core/outputs/__init__.py +1 -1
  75. langchain_core/outputs/chat_generation.py +18 -18
  76. langchain_core/outputs/chat_result.py +1 -3
  77. langchain_core/outputs/generation.py +16 -16
  78. langchain_core/outputs/llm_result.py +10 -10
  79. langchain_core/prompt_values.py +13 -19
  80. langchain_core/prompts/__init__.py +3 -27
  81. langchain_core/prompts/base.py +81 -86
  82. langchain_core/prompts/chat.py +308 -351
  83. langchain_core/prompts/dict.py +6 -6
  84. langchain_core/prompts/few_shot.py +81 -88
  85. langchain_core/prompts/few_shot_with_templates.py +11 -13
  86. langchain_core/prompts/image.py +12 -14
  87. langchain_core/prompts/loading.py +4 -6
  88. langchain_core/prompts/message.py +7 -7
  89. langchain_core/prompts/prompt.py +24 -39
  90. langchain_core/prompts/string.py +26 -10
  91. langchain_core/prompts/structured.py +49 -53
  92. langchain_core/rate_limiters.py +51 -60
  93. langchain_core/retrievers.py +61 -198
  94. langchain_core/runnables/base.py +1551 -1656
  95. langchain_core/runnables/branch.py +68 -70
  96. langchain_core/runnables/config.py +72 -89
  97. langchain_core/runnables/configurable.py +145 -161
  98. langchain_core/runnables/fallbacks.py +102 -96
  99. langchain_core/runnables/graph.py +91 -97
  100. langchain_core/runnables/graph_ascii.py +27 -28
  101. langchain_core/runnables/graph_mermaid.py +42 -51
  102. langchain_core/runnables/graph_png.py +43 -16
  103. langchain_core/runnables/history.py +175 -177
  104. langchain_core/runnables/passthrough.py +151 -167
  105. langchain_core/runnables/retry.py +46 -51
  106. langchain_core/runnables/router.py +30 -35
  107. langchain_core/runnables/schema.py +75 -80
  108. langchain_core/runnables/utils.py +60 -67
  109. langchain_core/stores.py +85 -121
  110. langchain_core/structured_query.py +8 -8
  111. langchain_core/sys_info.py +29 -29
  112. langchain_core/tools/__init__.py +1 -14
  113. langchain_core/tools/base.py +306 -245
  114. langchain_core/tools/convert.py +160 -155
  115. langchain_core/tools/render.py +10 -10
  116. langchain_core/tools/retriever.py +12 -11
  117. langchain_core/tools/simple.py +19 -24
  118. langchain_core/tools/structured.py +32 -39
  119. langchain_core/tracers/__init__.py +1 -9
  120. langchain_core/tracers/base.py +97 -99
  121. langchain_core/tracers/context.py +29 -52
  122. langchain_core/tracers/core.py +49 -53
  123. langchain_core/tracers/evaluation.py +11 -11
  124. langchain_core/tracers/event_stream.py +65 -64
  125. langchain_core/tracers/langchain.py +21 -21
  126. langchain_core/tracers/log_stream.py +45 -45
  127. langchain_core/tracers/memory_stream.py +3 -3
  128. langchain_core/tracers/root_listeners.py +16 -16
  129. langchain_core/tracers/run_collector.py +2 -4
  130. langchain_core/tracers/schemas.py +0 -129
  131. langchain_core/tracers/stdout.py +3 -3
  132. langchain_core/utils/__init__.py +1 -4
  133. langchain_core/utils/_merge.py +2 -2
  134. langchain_core/utils/aiter.py +57 -61
  135. langchain_core/utils/env.py +9 -9
  136. langchain_core/utils/function_calling.py +94 -188
  137. langchain_core/utils/html.py +7 -8
  138. langchain_core/utils/input.py +9 -6
  139. langchain_core/utils/interactive_env.py +1 -1
  140. langchain_core/utils/iter.py +36 -40
  141. langchain_core/utils/json.py +4 -3
  142. langchain_core/utils/json_schema.py +9 -9
  143. langchain_core/utils/mustache.py +8 -10
  144. langchain_core/utils/pydantic.py +35 -37
  145. langchain_core/utils/strings.py +6 -9
  146. langchain_core/utils/usage.py +1 -1
  147. langchain_core/utils/utils.py +66 -62
  148. langchain_core/vectorstores/base.py +182 -216
  149. langchain_core/vectorstores/in_memory.py +101 -176
  150. langchain_core/vectorstores/utils.py +5 -5
  151. langchain_core/version.py +1 -1
  152. langchain_core-1.0.4.dist-info/METADATA +69 -0
  153. langchain_core-1.0.4.dist-info/RECORD +172 -0
  154. {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.4.dist-info}/WHEEL +1 -1
  155. langchain_core/memory.py +0 -120
  156. langchain_core/messages/block_translators/ollama.py +0 -47
  157. langchain_core/prompts/pipeline.py +0 -138
  158. langchain_core/pydantic_v1/__init__.py +0 -30
  159. langchain_core/pydantic_v1/dataclasses.py +0 -23
  160. langchain_core/pydantic_v1/main.py +0 -23
  161. langchain_core/tracers/langchain_v1.py +0 -31
  162. langchain_core/utils/loading.py +0 -35
  163. langchain_core-1.0.0a6.dist-info/METADATA +0 -67
  164. langchain_core-1.0.0a6.dist-info/RECORD +0 -181
  165. langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
@@ -6,17 +6,14 @@ import contextlib
6
6
  import json
7
7
  import typing
8
8
  from abc import ABC, abstractmethod
9
- from collections.abc import Mapping
9
+ from collections.abc import Callable, Mapping
10
10
  from functools import cached_property
11
11
  from pathlib import Path
12
12
  from typing import (
13
13
  TYPE_CHECKING,
14
14
  Any,
15
- Callable,
16
15
  Generic,
17
- Optional,
18
16
  TypeVar,
19
- Union,
20
17
  )
21
18
 
22
19
  import yaml
@@ -49,24 +46,30 @@ class BasePromptTemplate(
49
46
 
50
47
  input_variables: list[str]
51
48
  """A list of the names of the variables whose values are required as inputs to the
52
- prompt."""
49
+ prompt.
50
+ """
53
51
  optional_variables: list[str] = Field(default=[])
54
- """optional_variables: A list of the names of the variables for placeholder
55
- or MessagePlaceholder that are optional. These variables are auto inferred
56
- from the prompt and user need not provide them."""
52
+ """A list of the names of the variables for placeholder or `MessagePlaceholder` that
53
+ are optional.
54
+
55
+ These variables are auto inferred from the prompt and user need not provide them.
56
+ """
57
57
  input_types: typing.Dict[str, Any] = Field(default_factory=dict, exclude=True) # noqa: UP006
58
58
  """A dictionary of the types of the variables the prompt template expects.
59
- If not provided, all variables are assumed to be strings."""
60
- output_parser: Optional[BaseOutputParser] = None
59
+
60
+ If not provided, all variables are assumed to be strings.
61
+ """
62
+ output_parser: BaseOutputParser | None = None
61
63
  """How to parse the output of calling an LLM on this formatted prompt."""
62
64
  partial_variables: Mapping[str, Any] = Field(default_factory=dict)
63
65
  """A dictionary of the partial variables the prompt template carries.
64
66
 
65
- Partial variables populate the template so that you don't need to
66
- pass them in every time you call the prompt."""
67
- metadata: Optional[typing.Dict[str, Any]] = None # noqa: UP006
67
+ Partial variables populate the template so that you don't need to pass them in every
68
+ time you call the prompt.
69
+ """
70
+ metadata: typing.Dict[str, Any] | None = None # noqa: UP006
68
71
  """Metadata to be used for tracing."""
69
- tags: Optional[list[str]] = None
72
+ tags: list[str] | None = None
70
73
  """Tags to be used for tracing."""
71
74
 
72
75
  @model_validator(mode="after")
@@ -99,16 +102,16 @@ class BasePromptTemplate(
99
102
 
100
103
  @classmethod
101
104
  def get_lc_namespace(cls) -> list[str]:
102
- """Get the namespace of the langchain object.
105
+ """Get the namespace of the LangChain object.
103
106
 
104
107
  Returns:
105
- ``["langchain", "schema", "prompt_template"]``
108
+ `["langchain", "schema", "prompt_template"]`
106
109
  """
107
110
  return ["langchain", "schema", "prompt_template"]
108
111
 
109
112
  @classmethod
110
113
  def is_lc_serializable(cls) -> bool:
111
- """Return True as this class is serializable."""
114
+ """Return `True` as this class is serializable."""
112
115
  return True
113
116
 
114
117
  model_config = ConfigDict(
@@ -123,19 +126,17 @@ class BasePromptTemplate(
123
126
  @override
124
127
  def OutputType(self) -> Any:
125
128
  """Return the output type of the prompt."""
126
- return Union[StringPromptValue, ChatPromptValueConcrete]
129
+ return StringPromptValue | ChatPromptValueConcrete
127
130
 
128
131
  @override
129
- def get_input_schema(
130
- self, config: Optional[RunnableConfig] = None
131
- ) -> type[BaseModel]:
132
+ def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
132
133
  """Get the input schema for the prompt.
133
134
 
134
135
  Args:
135
- config: RunnableConfig, configuration for the prompt.
136
+ config: Configuration for the prompt.
136
137
 
137
138
  Returns:
138
- Type[BaseModel]: The input schema for the prompt.
139
+ The input schema for the prompt.
139
140
  """
140
141
  # This is correct, but pydantic typings/mypy don't think so.
141
142
  required_input_variables = {
@@ -195,16 +196,16 @@ class BasePromptTemplate(
195
196
 
196
197
  @override
197
198
  def invoke(
198
- self, input: dict, config: Optional[RunnableConfig] = None, **kwargs: Any
199
+ self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
199
200
  ) -> PromptValue:
200
201
  """Invoke the prompt.
201
202
 
202
203
  Args:
203
- input: Dict, input to the prompt.
204
- config: RunnableConfig, configuration for the prompt.
204
+ input: Input to the prompt.
205
+ config: Configuration for the prompt.
205
206
 
206
207
  Returns:
207
- PromptValue: The output of the prompt.
208
+ The output of the prompt.
208
209
  """
209
210
  config = ensure_config(config)
210
211
  if self.metadata:
@@ -221,16 +222,16 @@ class BasePromptTemplate(
221
222
 
222
223
  @override
223
224
  async def ainvoke(
224
- self, input: dict, config: Optional[RunnableConfig] = None, **kwargs: Any
225
+ self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
225
226
  ) -> PromptValue:
226
227
  """Async invoke the prompt.
227
228
 
228
229
  Args:
229
- input: Dict, input to the prompt.
230
- config: RunnableConfig, configuration for the prompt.
230
+ input: Input to the prompt.
231
+ config: Configuration for the prompt.
231
232
 
232
233
  Returns:
233
- PromptValue: The output of the prompt.
234
+ The output of the prompt.
234
235
  """
235
236
  config = ensure_config(config)
236
237
  if self.metadata:
@@ -247,34 +248,34 @@ class BasePromptTemplate(
247
248
 
248
249
  @abstractmethod
249
250
  def format_prompt(self, **kwargs: Any) -> PromptValue:
250
- """Create Prompt Value.
251
+ """Create `PromptValue`.
251
252
 
252
253
  Args:
253
- kwargs: Any arguments to be passed to the prompt template.
254
+ **kwargs: Any arguments to be passed to the prompt template.
254
255
 
255
256
  Returns:
256
- PromptValue: The output of the prompt.
257
+ The output of the prompt.
257
258
  """
258
259
 
259
260
  async def aformat_prompt(self, **kwargs: Any) -> PromptValue:
260
- """Async create Prompt Value.
261
+ """Async create `PromptValue`.
261
262
 
262
263
  Args:
263
- kwargs: Any arguments to be passed to the prompt template.
264
+ **kwargs: Any arguments to be passed to the prompt template.
264
265
 
265
266
  Returns:
266
- PromptValue: The output of the prompt.
267
+ The output of the prompt.
267
268
  """
268
269
  return self.format_prompt(**kwargs)
269
270
 
270
- def partial(self, **kwargs: Union[str, Callable[[], str]]) -> BasePromptTemplate:
271
+ def partial(self, **kwargs: str | Callable[[], str]) -> BasePromptTemplate:
271
272
  """Return a partial of the prompt template.
272
273
 
273
274
  Args:
274
- kwargs: Union[str, Callable[[], str]], partial variables to set.
275
+ **kwargs: Partial variables to set.
275
276
 
276
277
  Returns:
277
- BasePromptTemplate: A partial of the prompt template.
278
+ A partial of the prompt template.
278
279
  """
279
280
  prompt_dict = self.__dict__.copy()
280
281
  prompt_dict["input_variables"] = list(
@@ -295,34 +296,30 @@ class BasePromptTemplate(
295
296
  """Format the prompt with the inputs.
296
297
 
297
298
  Args:
298
- kwargs: Any arguments to be passed to the prompt template.
299
+ **kwargs: Any arguments to be passed to the prompt template.
299
300
 
300
301
  Returns:
301
302
  A formatted string.
302
303
 
303
304
  Example:
304
-
305
- .. code-block:: python
306
-
305
+ ```python
307
306
  prompt.format(variable1="foo")
308
-
307
+ ```
309
308
  """
310
309
 
311
310
  async def aformat(self, **kwargs: Any) -> FormatOutputType:
312
311
  """Async format the prompt with the inputs.
313
312
 
314
313
  Args:
315
- kwargs: Any arguments to be passed to the prompt template.
314
+ **kwargs: Any arguments to be passed to the prompt template.
316
315
 
317
316
  Returns:
318
317
  A formatted string.
319
318
 
320
319
  Example:
321
-
322
- .. code-block:: python
323
-
320
+ ```python
324
321
  await prompt.aformat(variable1="foo")
325
-
322
+ ```
326
323
  """
327
324
  return self.format(**kwargs)
328
325
 
@@ -335,17 +332,17 @@ class BasePromptTemplate(
335
332
  """Return dictionary representation of prompt.
336
333
 
337
334
  Args:
338
- kwargs: Any additional arguments to pass to the dictionary.
335
+ **kwargs: Any additional arguments to pass to the dictionary.
339
336
 
340
337
  Returns:
341
- Dict: Dictionary representation of the prompt.
338
+ Dictionary representation of the prompt.
342
339
  """
343
340
  prompt_dict = super().model_dump(**kwargs)
344
341
  with contextlib.suppress(NotImplementedError):
345
342
  prompt_dict["_type"] = self._prompt_type
346
343
  return prompt_dict
347
344
 
348
- def save(self, file_path: Union[Path, str]) -> None:
345
+ def save(self, file_path: Path | str) -> None:
349
346
  """Save the prompt.
350
347
 
351
348
  Args:
@@ -357,10 +354,9 @@ class BasePromptTemplate(
357
354
  NotImplementedError: If the prompt type is not implemented.
358
355
 
359
356
  Example:
360
- .. code-block:: python
361
-
357
+ ```python
362
358
  prompt.save(file_path="path/prompt.yaml")
363
-
359
+ ```
364
360
  """
365
361
  if self.partial_variables:
366
362
  msg = "Cannot save prompt with partial variables."
@@ -412,35 +408,34 @@ def format_document(doc: Document, prompt: BasePromptTemplate[str]) -> str:
412
408
 
413
409
  First, this pulls information from the document from two sources:
414
410
 
415
- 1. page_content:
416
- This takes the information from the `document.page_content`
417
- and assigns it to a variable named `page_content`.
418
- 2. metadata:
419
- This takes information from `document.metadata` and assigns
420
- it to variables of the same name.
411
+ 1. `page_content`:
412
+ This takes the information from the `document.page_content` and assigns it to a
413
+ variable named `page_content`.
414
+ 2. `metadata`:
415
+ This takes information from `document.metadata` and assigns it to variables of
416
+ the same name.
421
417
 
422
418
  Those variables are then passed into the `prompt` to produce a formatted string.
423
419
 
424
420
  Args:
425
- doc: Document, the page_content and metadata will be used to create
421
+ doc: `Document`, the `page_content` and `metadata` will be used to create
426
422
  the final string.
427
- prompt: BasePromptTemplate, will be used to format the page_content
428
- and metadata into the final string.
423
+ prompt: `BasePromptTemplate`, will be used to format the `page_content`
424
+ and `metadata` into the final string.
429
425
 
430
426
  Returns:
431
- string of the document formatted.
427
+ String of the document formatted.
432
428
 
433
429
  Example:
434
- .. code-block:: python
435
-
436
- from langchain_core.documents import Document
437
- from langchain_core.prompts import PromptTemplate
438
-
439
- doc = Document(page_content="This is a joke", metadata={"page": "1"})
440
- prompt = PromptTemplate.from_template("Page {page}: {page_content}")
441
- format_document(doc, prompt)
442
- >>> "Page 1: This is a joke"
443
-
430
+ ```python
431
+ from langchain_core.documents import Document
432
+ from langchain_core.prompts import PromptTemplate
433
+
434
+ doc = Document(page_content="This is a joke", metadata={"page": "1"})
435
+ prompt = PromptTemplate.from_template("Page {page}: {page_content}")
436
+ format_document(doc, prompt)
437
+ >>> "Page 1: This is a joke"
438
+ ```
444
439
  """
445
440
  return prompt.format(**_get_document_info(doc, prompt))
446
441
 
@@ -450,22 +445,22 @@ async def aformat_document(doc: Document, prompt: BasePromptTemplate[str]) -> st
450
445
 
451
446
  First, this pulls information from the document from two sources:
452
447
 
453
- 1. page_content:
454
- This takes the information from the `document.page_content`
455
- and assigns it to a variable named `page_content`.
456
- 2. metadata:
457
- This takes information from `document.metadata` and assigns
458
- it to variables of the same name.
448
+ 1. `page_content`:
449
+ This takes the information from the `document.page_content` and assigns it to a
450
+ variable named `page_content`.
451
+ 2. `metadata`:
452
+ This takes information from `document.metadata` and assigns it to variables of
453
+ the same name.
459
454
 
460
455
  Those variables are then passed into the `prompt` to produce a formatted string.
461
456
 
462
457
  Args:
463
- doc: Document, the page_content and metadata will be used to create
458
+ doc: `Document`, the `page_content` and `metadata` will be used to create
464
459
  the final string.
465
- prompt: BasePromptTemplate, will be used to format the page_content
466
- and metadata into the final string.
460
+ prompt: `BasePromptTemplate`, will be used to format the `page_content`
461
+ and `metadata` into the final string.
467
462
 
468
463
  Returns:
469
- string of the document formatted.
464
+ String of the document formatted.
470
465
  """
471
466
  return await prompt.aformat(**_get_document_info(doc, prompt))