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.

Files changed (142) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +0 -1
  3. langchain_core/_api/beta_decorator.py +17 -20
  4. langchain_core/_api/deprecation.py +30 -35
  5. langchain_core/_import_utils.py +1 -1
  6. langchain_core/agents.py +10 -9
  7. langchain_core/caches.py +46 -56
  8. langchain_core/callbacks/__init__.py +1 -8
  9. langchain_core/callbacks/base.py +232 -243
  10. langchain_core/callbacks/file.py +33 -33
  11. langchain_core/callbacks/manager.py +353 -416
  12. langchain_core/callbacks/stdout.py +21 -22
  13. langchain_core/callbacks/streaming_stdout.py +32 -32
  14. langchain_core/callbacks/usage.py +54 -51
  15. langchain_core/chat_history.py +43 -58
  16. langchain_core/document_loaders/base.py +21 -21
  17. langchain_core/document_loaders/langsmith.py +22 -22
  18. langchain_core/documents/__init__.py +0 -1
  19. langchain_core/documents/base.py +46 -49
  20. langchain_core/documents/transformers.py +28 -29
  21. langchain_core/embeddings/fake.py +50 -54
  22. langchain_core/example_selectors/semantic_similarity.py +4 -6
  23. langchain_core/exceptions.py +7 -8
  24. langchain_core/indexing/api.py +19 -25
  25. langchain_core/indexing/base.py +24 -24
  26. langchain_core/language_models/__init__.py +11 -27
  27. langchain_core/language_models/_utils.py +53 -54
  28. langchain_core/language_models/base.py +30 -24
  29. langchain_core/language_models/chat_models.py +123 -148
  30. langchain_core/language_models/fake_chat_models.py +7 -7
  31. langchain_core/language_models/llms.py +14 -16
  32. langchain_core/load/dump.py +3 -4
  33. langchain_core/load/load.py +7 -16
  34. langchain_core/load/serializable.py +37 -36
  35. langchain_core/messages/__init__.py +1 -16
  36. langchain_core/messages/ai.py +122 -123
  37. langchain_core/messages/base.py +31 -31
  38. langchain_core/messages/block_translators/__init__.py +17 -17
  39. langchain_core/messages/block_translators/anthropic.py +3 -3
  40. langchain_core/messages/block_translators/bedrock_converse.py +3 -3
  41. langchain_core/messages/block_translators/google_genai.py +5 -4
  42. langchain_core/messages/block_translators/google_vertexai.py +4 -32
  43. langchain_core/messages/block_translators/groq.py +117 -21
  44. langchain_core/messages/block_translators/langchain_v0.py +3 -3
  45. langchain_core/messages/block_translators/openai.py +5 -5
  46. langchain_core/messages/chat.py +2 -6
  47. langchain_core/messages/content.py +222 -209
  48. langchain_core/messages/function.py +6 -10
  49. langchain_core/messages/human.py +17 -24
  50. langchain_core/messages/modifier.py +2 -2
  51. langchain_core/messages/system.py +12 -22
  52. langchain_core/messages/tool.py +53 -69
  53. langchain_core/messages/utils.py +399 -417
  54. langchain_core/output_parsers/__init__.py +1 -14
  55. langchain_core/output_parsers/base.py +46 -47
  56. langchain_core/output_parsers/json.py +3 -4
  57. langchain_core/output_parsers/list.py +2 -2
  58. langchain_core/output_parsers/openai_functions.py +46 -44
  59. langchain_core/output_parsers/openai_tools.py +11 -16
  60. langchain_core/output_parsers/pydantic.py +10 -11
  61. langchain_core/output_parsers/string.py +2 -2
  62. langchain_core/output_parsers/transform.py +2 -2
  63. langchain_core/output_parsers/xml.py +1 -1
  64. langchain_core/outputs/__init__.py +1 -1
  65. langchain_core/outputs/chat_generation.py +14 -14
  66. langchain_core/outputs/generation.py +6 -6
  67. langchain_core/outputs/llm_result.py +5 -5
  68. langchain_core/prompt_values.py +11 -11
  69. langchain_core/prompts/__init__.py +3 -23
  70. langchain_core/prompts/base.py +33 -38
  71. langchain_core/prompts/chat.py +222 -229
  72. langchain_core/prompts/dict.py +3 -3
  73. langchain_core/prompts/few_shot.py +76 -83
  74. langchain_core/prompts/few_shot_with_templates.py +7 -9
  75. langchain_core/prompts/image.py +12 -14
  76. langchain_core/prompts/loading.py +1 -1
  77. langchain_core/prompts/message.py +3 -3
  78. langchain_core/prompts/prompt.py +20 -23
  79. langchain_core/prompts/string.py +20 -8
  80. langchain_core/prompts/structured.py +26 -27
  81. langchain_core/rate_limiters.py +50 -58
  82. langchain_core/retrievers.py +41 -182
  83. langchain_core/runnables/base.py +565 -597
  84. langchain_core/runnables/branch.py +8 -8
  85. langchain_core/runnables/config.py +37 -44
  86. langchain_core/runnables/configurable.py +9 -10
  87. langchain_core/runnables/fallbacks.py +9 -9
  88. langchain_core/runnables/graph.py +46 -50
  89. langchain_core/runnables/graph_ascii.py +19 -18
  90. langchain_core/runnables/graph_mermaid.py +20 -31
  91. langchain_core/runnables/graph_png.py +7 -7
  92. langchain_core/runnables/history.py +22 -22
  93. langchain_core/runnables/passthrough.py +11 -11
  94. langchain_core/runnables/retry.py +3 -3
  95. langchain_core/runnables/router.py +2 -2
  96. langchain_core/runnables/schema.py +33 -33
  97. langchain_core/runnables/utils.py +30 -34
  98. langchain_core/stores.py +72 -102
  99. langchain_core/sys_info.py +27 -29
  100. langchain_core/tools/__init__.py +1 -14
  101. langchain_core/tools/base.py +70 -71
  102. langchain_core/tools/convert.py +100 -104
  103. langchain_core/tools/render.py +9 -9
  104. langchain_core/tools/retriever.py +7 -7
  105. langchain_core/tools/simple.py +6 -7
  106. langchain_core/tools/structured.py +18 -24
  107. langchain_core/tracers/__init__.py +1 -9
  108. langchain_core/tracers/base.py +35 -35
  109. langchain_core/tracers/context.py +12 -17
  110. langchain_core/tracers/event_stream.py +3 -3
  111. langchain_core/tracers/langchain.py +8 -8
  112. langchain_core/tracers/log_stream.py +17 -18
  113. langchain_core/tracers/memory_stream.py +3 -3
  114. langchain_core/tracers/root_listeners.py +2 -2
  115. langchain_core/tracers/schemas.py +0 -129
  116. langchain_core/tracers/stdout.py +1 -2
  117. langchain_core/utils/__init__.py +1 -1
  118. langchain_core/utils/aiter.py +32 -32
  119. langchain_core/utils/env.py +5 -5
  120. langchain_core/utils/function_calling.py +59 -154
  121. langchain_core/utils/html.py +4 -4
  122. langchain_core/utils/input.py +3 -3
  123. langchain_core/utils/interactive_env.py +1 -1
  124. langchain_core/utils/iter.py +20 -20
  125. langchain_core/utils/json.py +1 -1
  126. langchain_core/utils/json_schema.py +2 -2
  127. langchain_core/utils/mustache.py +5 -5
  128. langchain_core/utils/pydantic.py +17 -17
  129. langchain_core/utils/strings.py +5 -5
  130. langchain_core/utils/utils.py +25 -28
  131. langchain_core/vectorstores/base.py +55 -87
  132. langchain_core/vectorstores/in_memory.py +83 -85
  133. langchain_core/vectorstores/utils.py +2 -2
  134. langchain_core/version.py +1 -1
  135. {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/METADATA +23 -11
  136. langchain_core-1.0.0rc2.dist-info/RECORD +172 -0
  137. langchain_core/memory.py +0 -120
  138. langchain_core/pydantic_v1/__init__.py +0 -30
  139. langchain_core/pydantic_v1/dataclasses.py +0 -23
  140. langchain_core/pydantic_v1/main.py +0 -23
  141. langchain_core-1.0.0a8.dist-info/RECORD +0 -176
  142. {langchain_core-1.0.0a8.dist-info → langchain_core-1.0.0rc2.dist-info}/WHEEL +0 -0
@@ -5,7 +5,6 @@ Some examples of what you can do with these functions include:
5
5
  * Convert messages to strings (serialization)
6
6
  * Convert messages from dicts to Message objects (deserialization)
7
7
  * Filter messages from a list of messages based on name, type or id etc.
8
-
9
8
  """
10
9
 
11
10
  from __future__ import annotations
@@ -87,6 +86,7 @@ AnyMessage = Annotated[
87
86
  | Annotated[ToolMessageChunk, Tag(tag="ToolMessageChunk")],
88
87
  Field(discriminator=Discriminator(_get_type)),
89
88
  ]
89
+ """"A type representing any defined `Message` or `MessageChunk` type."""
90
90
 
91
91
 
92
92
  def get_buffer_string(
@@ -96,10 +96,8 @@ def get_buffer_string(
96
96
 
97
97
  Args:
98
98
  messages: Messages to be converted to strings.
99
- human_prefix: The prefix to prepend to contents of ``HumanMessage``s.
100
- Default is ``'Human'``.
101
- ai_prefix: The prefix to prepend to contents of ``AIMessage``. Default is
102
- ``'AI'``.
99
+ human_prefix: The prefix to prepend to contents of `HumanMessage`s.
100
+ ai_prefix: The prefix to prepend to contents of `AIMessage`.
103
101
 
104
102
  Returns:
105
103
  A single string concatenation of all input messages.
@@ -108,17 +106,16 @@ def get_buffer_string(
108
106
  ValueError: If an unsupported message type is encountered.
109
107
 
110
108
  Example:
111
- .. code-block:: python
112
-
113
- from langchain_core import AIMessage, HumanMessage
114
-
115
- messages = [
116
- HumanMessage(content="Hi, how are you?"),
117
- AIMessage(content="Good, how are you?"),
118
- ]
119
- get_buffer_string(messages)
120
- # -> "Human: Hi, how are you?\nAI: Good, how are you?"
121
-
109
+ ```python
110
+ from langchain_core import AIMessage, HumanMessage
111
+
112
+ messages = [
113
+ HumanMessage(content="Hi, how are you?"),
114
+ AIMessage(content="Good, how are you?"),
115
+ ]
116
+ get_buffer_string(messages)
117
+ # -> "Human: Hi, how are you?\nAI: Good, how are you?"
118
+ ```
122
119
  """
123
120
  string_messages = []
124
121
  for m in messages:
@@ -178,7 +175,7 @@ def _message_from_dict(message: dict) -> BaseMessage:
178
175
 
179
176
 
180
177
  def messages_from_dict(messages: Sequence[dict]) -> list[BaseMessage]:
181
- """Convert a sequence of messages from dicts to ``Message`` objects.
178
+ """Convert a sequence of messages from dicts to `Message` objects.
182
179
 
183
180
  Args:
184
181
  messages: Sequence of messages (as dicts) to convert.
@@ -191,7 +188,7 @@ def messages_from_dict(messages: Sequence[dict]) -> list[BaseMessage]:
191
188
 
192
189
 
193
190
  def message_chunk_to_message(chunk: BaseMessage) -> BaseMessage:
194
- """Convert a message chunk to a ``Message``.
191
+ """Convert a message chunk to a `Message`.
195
192
 
196
193
  Args:
197
194
  chunk: Message chunk to convert.
@@ -213,6 +210,7 @@ def message_chunk_to_message(chunk: BaseMessage) -> BaseMessage:
213
210
  MessageLikeRepresentation = (
214
211
  BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]
215
212
  )
213
+ """A type representing the various ways a message can be represented."""
216
214
 
217
215
 
218
216
  def _create_message_from_message_type(
@@ -224,24 +222,24 @@ def _create_message_from_message_type(
224
222
  id: str | None = None,
225
223
  **additional_kwargs: Any,
226
224
  ) -> BaseMessage:
227
- """Create a message from a ``Message`` type and content string.
225
+ """Create a message from a `Message` type and content string.
228
226
 
229
227
  Args:
230
- message_type: (str) the type of the message (e.g., ``'human'``, ``'ai'``, etc.).
228
+ message_type: (str) the type of the message (e.g., `'human'`, `'ai'`, etc.).
231
229
  content: (str) the content string.
232
- name: (str) the name of the message. Default is None.
233
- tool_call_id: (str) the tool call id. Default is None.
234
- tool_calls: (list[dict[str, Any]]) the tool calls. Default is None.
235
- id: (str) the id of the message. Default is None.
230
+ name: (str) the name of the message.
231
+ tool_call_id: (str) the tool call id.
232
+ tool_calls: (list[dict[str, Any]]) the tool calls.
233
+ id: (str) the id of the message.
236
234
  additional_kwargs: (dict[str, Any]) additional keyword arguments.
237
235
 
238
236
  Returns:
239
237
  a message of the appropriate type.
240
238
 
241
239
  Raises:
242
- ValueError: if the message type is not one of ``'human'``, ``'user'``, ``'ai'``,
243
- ``'assistant'``, ``'function'``, ``'tool'``, ``'system'``, or
244
- ``'developer'``.
240
+ ValueError: if the message type is not one of `'human'`, `'user'`, `'ai'`,
241
+ `'assistant'`, `'function'`, `'tool'`, `'system'`, or
242
+ `'developer'`.
245
243
  """
246
244
  kwargs: dict[str, Any] = {}
247
245
  if name is not None:
@@ -307,21 +305,21 @@ def _create_message_from_message_type(
307
305
 
308
306
 
309
307
  def _convert_to_message(message: MessageLikeRepresentation) -> BaseMessage:
310
- """Instantiate a ``Message`` from a variety of message formats.
308
+ """Instantiate a `Message` from a variety of message formats.
311
309
 
312
310
  The message format can be one of the following:
313
311
 
314
- - ``BaseMessagePromptTemplate``
315
- - ``BaseMessage``
316
- - 2-tuple of (role string, template); e.g., (``'human'``, ``'{user_input}'``)
312
+ - `BaseMessagePromptTemplate`
313
+ - `BaseMessage`
314
+ - 2-tuple of (role string, template); e.g., (`'human'`, `'{user_input}'`)
317
315
  - dict: a message dict with role and content keys
318
- - string: shorthand for (``'human'``, template); e.g., ``'{user_input}'``
316
+ - string: shorthand for (`'human'`, template); e.g., `'{user_input}'`
319
317
 
320
318
  Args:
321
319
  message: a representation of a message in one of the supported formats.
322
320
 
323
321
  Returns:
324
- an instance of a message or a message template.
322
+ An instance of a message or a message template.
325
323
 
326
324
  Raises:
327
325
  NotImplementedError: if the message type is not supported.
@@ -423,79 +421,78 @@ def filter_messages(
423
421
  exclude_ids: Sequence[str] | None = None,
424
422
  exclude_tool_calls: Sequence[str] | bool | None = None,
425
423
  ) -> list[BaseMessage]:
426
- """Filter messages based on ``name``, ``type`` or ``id``.
424
+ """Filter messages based on `name`, `type` or `id`.
427
425
 
428
426
  Args:
429
427
  messages: Sequence Message-like objects to filter.
430
- include_names: Message names to include. Default is None.
431
- exclude_names: Messages names to exclude. Default is None.
428
+ include_names: Message names to include.
429
+ exclude_names: Messages names to exclude.
432
430
  include_types: Message types to include. Can be specified as string names
433
- (e.g. ``'system'``, ``'human'``, ``'ai'``, ...) or as ``BaseMessage``
434
- classes (e.g. ``SystemMessage``, ``HumanMessage``, ``AIMessage``, ...).
435
- Default is None.
431
+ (e.g. `'system'`, `'human'`, `'ai'`, ...) or as `BaseMessage`
432
+ classes (e.g. `SystemMessage`, `HumanMessage`, `AIMessage`, ...).
433
+
436
434
  exclude_types: Message types to exclude. Can be specified as string names
437
- (e.g. ``'system'``, ``'human'``, ``'ai'``, ...) or as ``BaseMessage``
438
- classes (e.g. ``SystemMessage``, ``HumanMessage``, ``AIMessage``, ...).
439
- Default is None.
440
- include_ids: Message IDs to include. Default is None.
441
- exclude_ids: Message IDs to exclude. Default is None.
442
- exclude_tool_calls: Tool call IDs to exclude. Default is None.
435
+ (e.g. `'system'`, `'human'`, `'ai'`, ...) or as `BaseMessage`
436
+ classes (e.g. `SystemMessage`, `HumanMessage`, `AIMessage`, ...).
437
+
438
+ include_ids: Message IDs to include.
439
+ exclude_ids: Message IDs to exclude.
440
+ exclude_tool_calls: Tool call IDs to exclude.
443
441
  Can be one of the following:
444
- - ``True``: all ``AIMessage``s with tool calls and all
445
- ``ToolMessage``s will be excluded.
442
+ - `True`: all `AIMessage`s with tool calls and all
443
+ `ToolMessage` objects will be excluded.
446
444
  - a sequence of tool call IDs to exclude:
447
- - ``ToolMessage``s with the corresponding tool call ID will be
448
- excluded.
449
- - The ``tool_calls`` in the AIMessage will be updated to exclude
450
- matching tool calls. If all ``tool_calls`` are filtered from an
451
- AIMessage, the whole message is excluded.
445
+ - `ToolMessage` objects with the corresponding tool call ID will be
446
+ excluded.
447
+ - The `tool_calls` in the AIMessage will be updated to exclude
448
+ matching tool calls. If all `tool_calls` are filtered from an
449
+ AIMessage, the whole message is excluded.
452
450
 
453
451
  Returns:
454
- A list of Messages that meets at least one of the ``incl_*`` conditions and none
455
- of the ``excl_*`` conditions. If not ``incl_*`` conditions are specified then
452
+ A list of Messages that meets at least one of the `incl_*` conditions and none
453
+ of the `excl_*` conditions. If not `incl_*` conditions are specified then
456
454
  anything that is not explicitly excluded will be included.
457
455
 
458
456
  Raises:
459
457
  ValueError: If two incompatible arguments are provided.
460
458
 
461
459
  Example:
462
- .. code-block:: python
463
-
464
- from langchain_core.messages import (
465
- filter_messages,
466
- AIMessage,
467
- HumanMessage,
468
- SystemMessage,
469
- )
470
-
471
- messages = [
472
- SystemMessage("you're a good assistant."),
473
- HumanMessage("what's your name", id="foo", name="example_user"),
474
- AIMessage("steve-o", id="bar", name="example_assistant"),
475
- HumanMessage(
476
- "what's your favorite color",
477
- id="baz",
478
- ),
479
- AIMessage(
480
- "silicon blue",
481
- id="blah",
482
- ),
483
- ]
484
-
485
- filter_messages(
486
- messages,
487
- incl_names=("example_user", "example_assistant"),
488
- incl_types=("system",),
489
- excl_ids=("bar",),
490
- )
491
-
492
- .. code-block:: python
493
-
494
- [
495
- SystemMessage("you're a good assistant."),
496
- HumanMessage("what's your name", id="foo", name="example_user"),
497
- ]
460
+ ```python
461
+ from langchain_core.messages import (
462
+ filter_messages,
463
+ AIMessage,
464
+ HumanMessage,
465
+ SystemMessage,
466
+ )
498
467
 
468
+ messages = [
469
+ SystemMessage("you're a good assistant."),
470
+ HumanMessage("what's your name", id="foo", name="example_user"),
471
+ AIMessage("steve-o", id="bar", name="example_assistant"),
472
+ HumanMessage(
473
+ "what's your favorite color",
474
+ id="baz",
475
+ ),
476
+ AIMessage(
477
+ "silicon blue",
478
+ id="blah",
479
+ ),
480
+ ]
481
+
482
+ filter_messages(
483
+ messages,
484
+ incl_names=("example_user", "example_assistant"),
485
+ incl_types=("system",),
486
+ excl_ids=("bar",),
487
+ )
488
+ ```
489
+
490
+ ```python
491
+ [
492
+ SystemMessage("you're a good assistant."),
493
+ HumanMessage("what's your name", id="foo", name="example_user"),
494
+ ]
495
+ ```
499
496
  """
500
497
  messages = convert_to_messages(messages)
501
498
  filtered: list[BaseMessage] = []
@@ -565,13 +562,12 @@ def merge_message_runs(
565
562
  r"""Merge consecutive Messages of the same type.
566
563
 
567
564
  !!! note
568
- ToolMessages are not merged, as each has a distinct tool call id that can't be
569
- merged.
565
+ `ToolMessage` objects are not merged, as each has a distinct tool call id that
566
+ can't be merged.
570
567
 
571
568
  Args:
572
569
  messages: Sequence Message-like objects to merge.
573
570
  chunk_separator: Specify the string to be inserted between message chunks.
574
- Defaults to ``'\n'``.
575
571
 
576
572
  Returns:
577
573
  list of BaseMessages with consecutive runs of message types merged into single
@@ -579,87 +575,86 @@ def merge_message_runs(
579
575
  the merged content is a concatenation of the two strings with a new-line
580
576
  separator.
581
577
  The separator inserted between message chunks can be controlled by specifying
582
- any string with ``chunk_separator``. If at least one of the messages has a list
578
+ any string with `chunk_separator`. If at least one of the messages has a list
583
579
  of content blocks, the merged content is a list of content blocks.
584
580
 
585
581
  Example:
582
+ ```python
583
+ from langchain_core.messages import (
584
+ merge_message_runs,
585
+ AIMessage,
586
+ HumanMessage,
587
+ SystemMessage,
588
+ ToolCall,
589
+ )
586
590
 
587
- .. code-block:: python
588
-
589
- from langchain_core.messages import (
590
- merge_message_runs,
591
- AIMessage,
592
- HumanMessage,
593
- SystemMessage,
594
- ToolCall,
595
- )
596
-
597
- messages = [
598
- SystemMessage("you're a good assistant."),
599
- HumanMessage(
600
- "what's your favorite color",
601
- id="foo",
602
- ),
603
- HumanMessage(
604
- "wait your favorite food",
605
- id="bar",
606
- ),
607
- AIMessage(
591
+ messages = [
592
+ SystemMessage("you're a good assistant."),
593
+ HumanMessage(
594
+ "what's your favorite color",
595
+ id="foo",
596
+ ),
597
+ HumanMessage(
598
+ "wait your favorite food",
599
+ id="bar",
600
+ ),
601
+ AIMessage(
602
+ "my favorite colo",
603
+ tool_calls=[
604
+ ToolCall(
605
+ name="blah_tool", args={"x": 2}, id="123", type="tool_call"
606
+ )
607
+ ],
608
+ id="baz",
609
+ ),
610
+ AIMessage(
611
+ [{"type": "text", "text": "my favorite dish is lasagna"}],
612
+ tool_calls=[
613
+ ToolCall(
614
+ name="blah_tool",
615
+ args={"x": -10},
616
+ id="456",
617
+ type="tool_call",
618
+ )
619
+ ],
620
+ id="blur",
621
+ ),
622
+ ]
623
+
624
+ merge_message_runs(messages)
625
+ ```
626
+
627
+ ```python
628
+ [
629
+ SystemMessage("you're a good assistant."),
630
+ HumanMessage(
631
+ "what's your favorite color\\n"
632
+ "wait your favorite food", id="foo",
633
+ ),
634
+ AIMessage(
635
+ [
608
636
  "my favorite colo",
609
- tool_calls=[
610
- ToolCall(
611
- name="blah_tool", args={"x": 2}, id="123", type="tool_call"
612
- )
613
- ],
614
- id="baz",
615
- ),
616
- AIMessage(
617
- [{"type": "text", "text": "my favorite dish is lasagna"}],
618
- tool_calls=[
619
- ToolCall(
620
- name="blah_tool",
621
- args={"x": -10},
622
- id="456",
623
- type="tool_call",
624
- )
625
- ],
626
- id="blur",
627
- ),
628
- ]
629
-
630
- merge_message_runs(messages)
631
-
632
- .. code-block:: python
633
-
634
- [
635
- SystemMessage("you're a good assistant."),
636
- HumanMessage(
637
- "what's your favorite color\\n"
638
- "wait your favorite food", id="foo",
639
- ),
640
- AIMessage(
641
- [
642
- "my favorite colo",
643
- {"type": "text", "text": "my favorite dish is lasagna"}
644
- ],
645
- tool_calls=[
646
- ToolCall({
647
- "name": "blah_tool",
648
- "args": {"x": 2},
649
- "id": "123",
650
- "type": "tool_call"
651
- }),
652
- ToolCall({
653
- "name": "blah_tool",
654
- "args": {"x": -10},
655
- "id": "456",
656
- "type": "tool_call"
657
- })
658
- ]
659
- id="baz"
660
- ),
661
- ]
637
+ {"type": "text", "text": "my favorite dish is lasagna"}
638
+ ],
639
+ tool_calls=[
640
+ ToolCall({
641
+ "name": "blah_tool",
642
+ "args": {"x": 2},
643
+ "id": "123",
644
+ "type": "tool_call"
645
+ }),
646
+ ToolCall({
647
+ "name": "blah_tool",
648
+ "args": {"x": -10},
649
+ "id": "456",
650
+ "type": "tool_call"
651
+ })
652
+ ]
653
+ id="baz"
654
+ ),
655
+ ]
662
656
 
657
+ ```
663
658
  """
664
659
  if not messages:
665
660
  return []
@@ -706,153 +701,146 @@ def trim_messages(
706
701
  ) -> list[BaseMessage]:
707
702
  r"""Trim messages to be below a token count.
708
703
 
709
- ``trim_messages`` can be used to reduce the size of a chat history to a specified
710
- token count or specified message count.
704
+ `trim_messages` can be used to reduce the size of a chat history to a specified
705
+ token or message count.
711
706
 
712
707
  In either case, if passing the trimmed chat history back into a chat model
713
708
  directly, the resulting chat history should usually satisfy the following
714
709
  properties:
715
710
 
716
711
  1. The resulting chat history should be valid. Most chat models expect that chat
717
- history starts with either (1) a ``HumanMessage`` or (2) a ``SystemMessage``
718
- followed by a ``HumanMessage``. To achieve this, set ``start_on='human'``.
719
- In addition, generally a ``ToolMessage`` can only appear after an ``AIMessage``
720
- that involved a tool call.
721
- Please see the following link for more information about messages:
722
- https://python.langchain.com/docs/concepts/#messages
712
+ history starts with either (1) a `HumanMessage` or (2) a `SystemMessage`
713
+ followed by a `HumanMessage`. To achieve this, set `start_on='human'`.
714
+ In addition, generally a `ToolMessage` can only appear after an `AIMessage`
715
+ that involved a tool call.
723
716
  2. It includes recent messages and drops old messages in the chat history.
724
- To achieve this set the ``strategy='last'``.
725
- 3. Usually, the new chat history should include the ``SystemMessage`` if it
726
- was present in the original chat history since the ``SystemMessage`` includes
727
- special instructions to the chat model. The ``SystemMessage`` is almost always
728
- the first message in the history if present. To achieve this set the
729
- ``include_system=True``.
717
+ To achieve this set the `strategy='last'`.
718
+ 3. Usually, the new chat history should include the `SystemMessage` if it
719
+ was present in the original chat history since the `SystemMessage` includes
720
+ special instructions to the chat model. The `SystemMessage` is almost always
721
+ the first message in the history if present. To achieve this set the
722
+ `include_system=True`.
730
723
 
731
724
  !!! note
732
- The examples below show how to configure ``trim_messages`` to achieve a behavior
725
+ The examples below show how to configure `trim_messages` to achieve a behavior
733
726
  consistent with the above properties.
734
727
 
735
728
  Args:
736
729
  messages: Sequence of Message-like objects to trim.
737
730
  max_tokens: Max token count of trimmed messages.
738
- token_counter: Function or llm for counting tokens in a ``BaseMessage`` or a
739
- list of ``BaseMessage``. If a ``BaseLanguageModel`` is passed in then
740
- ``BaseLanguageModel.get_num_tokens_from_messages()`` will be used.
741
- Set to ``len`` to count the number of **messages** in the chat history.
731
+ token_counter: Function or llm for counting tokens in a `BaseMessage` or a
732
+ list of `BaseMessage`. If a `BaseLanguageModel` is passed in then
733
+ `BaseLanguageModel.get_num_tokens_from_messages()` will be used.
734
+ Set to `len` to count the number of **messages** in the chat history.
742
735
 
743
736
  !!! note
744
- Use ``count_tokens_approximately`` to get fast, approximate token
737
+ Use `count_tokens_approximately` to get fast, approximate token
745
738
  counts.
746
- This is recommended for using ``trim_messages`` on the hot path, where
739
+ This is recommended for using `trim_messages` on the hot path, where
747
740
  exact token counting is not necessary.
748
741
 
749
742
  strategy: Strategy for trimming.
750
- - ``'first'``: Keep the first ``<= n_count`` tokens of the messages.
751
- - ``'last'``: Keep the last ``<= n_count`` tokens of the messages.
752
- Default is ``'last'``.
743
+ - `'first'`: Keep the first `<= n_count` tokens of the messages.
744
+ - `'last'`: Keep the last `<= n_count` tokens of the messages.
753
745
  allow_partial: Whether to split a message if only part of the message can be
754
- included. If ``strategy='last'`` then the last partial contents of a message
755
- are included. If ``strategy='first'`` then the first partial contents of a
746
+ included. If `strategy='last'` then the last partial contents of a message
747
+ are included. If `strategy='first'` then the first partial contents of a
756
748
  message are included.
757
- Default is False.
758
749
  end_on: The message type to end on. If specified then every message after the
759
- last occurrence of this type is ignored. If ``strategy='last'`` then this
760
- is done before we attempt to get the last ``max_tokens``. If
761
- ``strategy='first'`` then this is done after we get the first
762
- ``max_tokens``. Can be specified as string names (e.g. ``'system'``,
763
- ``'human'``, ``'ai'``, ...) or as ``BaseMessage`` classes (e.g.
764
- ``SystemMessage``, ``HumanMessage``, ``AIMessage``, ...). Can be a single
750
+ last occurrence of this type is ignored. If `strategy='last'` then this
751
+ is done before we attempt to get the last `max_tokens`. If
752
+ `strategy='first'` then this is done after we get the first
753
+ `max_tokens`. Can be specified as string names (e.g. `'system'`,
754
+ `'human'`, `'ai'`, ...) or as `BaseMessage` classes (e.g.
755
+ `SystemMessage`, `HumanMessage`, `AIMessage`, ...). Can be a single
765
756
  type or a list of types.
766
- Default is None.
757
+
767
758
  start_on: The message type to start on. Should only be specified if
768
- ``strategy='last'``. If specified then every message before
759
+ `strategy='last'`. If specified then every message before
769
760
  the first occurrence of this type is ignored. This is done after we trim
770
- the initial messages to the last ``max_tokens``. Does not
771
- apply to a ``SystemMessage`` at index 0 if ``include_system=True``. Can be
772
- specified as string names (e.g. ``'system'``, ``'human'``, ``'ai'``, ...) or
773
- as ``BaseMessage`` classes (e.g. ``SystemMessage``, ``HumanMessage``,
774
- ``AIMessage``, ...). Can be a single type or a list of types.
775
- Default is None.
776
- include_system: Whether to keep the SystemMessage if there is one at index 0.
777
- Should only be specified if ``strategy="last"``.
778
- Default is False.
779
- text_splitter: Function or ``langchain_text_splitters.TextSplitter`` for
761
+ the initial messages to the last `max_tokens`. Does not
762
+ apply to a `SystemMessage` at index 0 if `include_system=True`. Can be
763
+ specified as string names (e.g. `'system'`, `'human'`, `'ai'`, ...) or
764
+ as `BaseMessage` classes (e.g. `SystemMessage`, `HumanMessage`,
765
+ `AIMessage`, ...). Can be a single type or a list of types.
766
+
767
+ include_system: Whether to keep the `SystemMessage` if there is one at index
768
+ `0`. Should only be specified if `strategy="last"`.
769
+ text_splitter: Function or `langchain_text_splitters.TextSplitter` for
780
770
  splitting the string contents of a message. Only used if
781
- ``allow_partial=True``. If ``strategy='last'`` then the last split tokens
782
- from a partial message will be included. if ``strategy='first'`` then the
771
+ `allow_partial=True`. If `strategy='last'` then the last split tokens
772
+ from a partial message will be included. if `strategy='first'` then the
783
773
  first split tokens from a partial message will be included. Token splitter
784
774
  assumes that separators are kept, so that split contents can be directly
785
775
  concatenated to recreate the original text. Defaults to splitting on
786
776
  newlines.
787
777
 
788
778
  Returns:
789
- list of trimmed ``BaseMessage``.
779
+ List of trimmed `BaseMessage`.
790
780
 
791
781
  Raises:
792
782
  ValueError: if two incompatible arguments are specified or an unrecognized
793
- ``strategy`` is specified.
783
+ `strategy` is specified.
794
784
 
795
785
  Example:
796
- Trim chat history based on token count, keeping the ``SystemMessage`` if
797
- present, and ensuring that the chat history starts with a ``HumanMessage`` (
798
- or a ``SystemMessage`` followed by a ``HumanMessage``).
799
-
800
- .. code-block:: python
801
-
802
- from langchain_core.messages import (
803
- AIMessage,
804
- HumanMessage,
805
- BaseMessage,
806
- SystemMessage,
807
- trim_messages,
808
- )
809
-
810
- messages = [
811
- SystemMessage(
812
- "you're a good assistant, you always respond with a joke."
813
- ),
814
- HumanMessage("i wonder why it's called langchain"),
815
- AIMessage(
816
- 'Well, I guess they thought "WordRope" and "SentenceString" just '
817
- "didn't have the same ring to it!"
818
- ),
819
- HumanMessage("and who is harrison chasing anyways"),
820
- AIMessage(
821
- "Hmmm let me think.\n\nWhy, he's probably chasing after the last "
822
- "cup of coffee in the office!"
823
- ),
824
- HumanMessage("what do you call a speechless parrot"),
825
- ]
826
-
827
-
828
- trim_messages(
829
- messages,
830
- max_tokens=45,
831
- strategy="last",
832
- token_counter=ChatOpenAI(model="gpt-4o"),
833
- # Most chat models expect that chat history starts with either:
834
- # (1) a HumanMessage or
835
- # (2) a SystemMessage followed by a HumanMessage
836
- start_on="human",
837
- # Usually, we want to keep the SystemMessage
838
- # if it's present in the original history.
839
- # The SystemMessage has special instructions for the model.
840
- include_system=True,
841
- allow_partial=False,
842
- )
786
+ Trim chat history based on token count, keeping the `SystemMessage` if
787
+ present, and ensuring that the chat history starts with a `HumanMessage` (
788
+ or a `SystemMessage` followed by a `HumanMessage`).
789
+
790
+ ```python
791
+ from langchain_core.messages import (
792
+ AIMessage,
793
+ HumanMessage,
794
+ BaseMessage,
795
+ SystemMessage,
796
+ trim_messages,
797
+ )
843
798
 
844
- .. code-block:: python
799
+ messages = [
800
+ SystemMessage("you're a good assistant, you always respond with a joke."),
801
+ HumanMessage("i wonder why it's called langchain"),
802
+ AIMessage(
803
+ 'Well, I guess they thought "WordRope" and "SentenceString" just '
804
+ "didn't have the same ring to it!"
805
+ ),
806
+ HumanMessage("and who is harrison chasing anyways"),
807
+ AIMessage(
808
+ "Hmmm let me think.\n\nWhy, he's probably chasing after the last "
809
+ "cup of coffee in the office!"
810
+ ),
811
+ HumanMessage("what do you call a speechless parrot"),
812
+ ]
813
+
814
+
815
+ trim_messages(
816
+ messages,
817
+ max_tokens=45,
818
+ strategy="last",
819
+ token_counter=ChatOpenAI(model="gpt-4o"),
820
+ # Most chat models expect that chat history starts with either:
821
+ # (1) a HumanMessage or
822
+ # (2) a SystemMessage followed by a HumanMessage
823
+ start_on="human",
824
+ # Usually, we want to keep the SystemMessage
825
+ # if it's present in the original history.
826
+ # The SystemMessage has special instructions for the model.
827
+ include_system=True,
828
+ allow_partial=False,
829
+ )
830
+ ```
845
831
 
846
- [
847
- SystemMessage(
848
- content="you're a good assistant, you always respond with a joke."
849
- ),
850
- HumanMessage(content="what do you call a speechless parrot"),
851
- ]
832
+ ```python
833
+ [
834
+ SystemMessage(
835
+ content="you're a good assistant, you always respond with a joke."
836
+ ),
837
+ HumanMessage(content="what do you call a speechless parrot"),
838
+ ]
839
+ ```
852
840
 
853
- Trim chat history based on the message count, keeping the ``SystemMessage`` if
854
- present, and ensuring that the chat history starts with a ``HumanMessage`` (
855
- or a ``SystemMessage`` followed by a ``HumanMessage``).
841
+ Trim chat history based on the message count, keeping the `SystemMessage` if
842
+ present, and ensuring that the chat history starts with a `HumanMessage` (
843
+ or a `SystemMessage` followed by a `HumanMessage`).
856
844
 
857
845
  trim_messages(
858
846
  messages,
@@ -874,100 +862,95 @@ def trim_messages(
874
862
  allow_partial=False,
875
863
  )
876
864
 
877
- .. code-block:: python
878
-
879
- [
880
- SystemMessage(
881
- content="you're a good assistant, you always respond with a joke."
882
- ),
883
- HumanMessage(content="and who is harrison chasing anyways"),
884
- AIMessage(
885
- content="Hmmm let me think.\n\nWhy, he's probably chasing after "
886
- "the last cup of coffee in the office!"
887
- ),
888
- HumanMessage(content="what do you call a speechless parrot"),
889
- ]
890
-
891
-
865
+ ```python
866
+ [
867
+ SystemMessage(
868
+ content="you're a good assistant, you always respond with a joke."
869
+ ),
870
+ HumanMessage(content="and who is harrison chasing anyways"),
871
+ AIMessage(
872
+ content="Hmmm let me think.\n\nWhy, he's probably chasing after "
873
+ "the last cup of coffee in the office!"
874
+ ),
875
+ HumanMessage(content="what do you call a speechless parrot"),
876
+ ]
877
+ ```
892
878
  Trim chat history using a custom token counter function that counts the
893
879
  number of tokens in each message.
894
880
 
895
- .. code-block:: python
896
-
897
- messages = [
898
- SystemMessage("This is a 4 token text. The full message is 10 tokens."),
899
- HumanMessage(
900
- "This is a 4 token text. The full message is 10 tokens.", id="first"
901
- ),
902
- AIMessage(
903
- [
904
- {"type": "text", "text": "This is the FIRST 4 token block."},
905
- {"type": "text", "text": "This is the SECOND 4 token block."},
906
- ],
907
- id="second",
908
- ),
909
- HumanMessage(
910
- "This is a 4 token text. The full message is 10 tokens.", id="third"
911
- ),
912
- AIMessage(
913
- "This is a 4 token text. The full message is 10 tokens.",
914
- id="fourth",
915
- ),
916
- ]
917
-
918
-
919
- def dummy_token_counter(messages: list[BaseMessage]) -> int:
920
- # treat each message like it adds 3 default tokens at the beginning
921
- # of the message and at the end of the message. 3 + 4 + 3 = 10 tokens
922
- # per message.
923
-
924
- default_content_len = 4
925
- default_msg_prefix_len = 3
926
- default_msg_suffix_len = 3
927
-
928
- count = 0
929
- for msg in messages:
930
- if isinstance(msg.content, str):
931
- count += (
932
- default_msg_prefix_len
933
- + default_content_len
934
- + default_msg_suffix_len
935
- )
936
- if isinstance(msg.content, list):
937
- count += (
938
- default_msg_prefix_len
939
- + len(msg.content) * default_content_len
940
- + default_msg_suffix_len
941
- )
942
- return count
943
-
944
- First 30 tokens, allowing partial messages:
945
- .. code-block:: python
946
-
947
- trim_messages(
948
- messages,
949
- max_tokens=30,
950
- token_counter=dummy_token_counter,
951
- strategy="first",
952
- allow_partial=True,
953
- )
954
-
955
- .. code-block:: python
956
-
881
+ ```python
882
+ messages = [
883
+ SystemMessage("This is a 4 token text. The full message is 10 tokens."),
884
+ HumanMessage(
885
+ "This is a 4 token text. The full message is 10 tokens.", id="first"
886
+ ),
887
+ AIMessage(
957
888
  [
958
- SystemMessage(
959
- "This is a 4 token text. The full message is 10 tokens."
960
- ),
961
- HumanMessage(
962
- "This is a 4 token text. The full message is 10 tokens.",
963
- id="first",
964
- ),
965
- AIMessage(
966
- [{"type": "text", "text": "This is the FIRST 4 token block."}],
967
- id="second",
968
- ),
969
- ]
889
+ {"type": "text", "text": "This is the FIRST 4 token block."},
890
+ {"type": "text", "text": "This is the SECOND 4 token block."},
891
+ ],
892
+ id="second",
893
+ ),
894
+ HumanMessage(
895
+ "This is a 4 token text. The full message is 10 tokens.", id="third"
896
+ ),
897
+ AIMessage(
898
+ "This is a 4 token text. The full message is 10 tokens.",
899
+ id="fourth",
900
+ ),
901
+ ]
902
+
903
+
904
+ def dummy_token_counter(messages: list[BaseMessage]) -> int:
905
+ # treat each message like it adds 3 default tokens at the beginning
906
+ # of the message and at the end of the message. 3 + 4 + 3 = 10 tokens
907
+ # per message.
908
+
909
+ default_content_len = 4
910
+ default_msg_prefix_len = 3
911
+ default_msg_suffix_len = 3
912
+
913
+ count = 0
914
+ for msg in messages:
915
+ if isinstance(msg.content, str):
916
+ count += (
917
+ default_msg_prefix_len
918
+ + default_content_len
919
+ + default_msg_suffix_len
920
+ )
921
+ if isinstance(msg.content, list):
922
+ count += (
923
+ default_msg_prefix_len
924
+ + len(msg.content) * default_content_len
925
+ + default_msg_suffix_len
926
+ )
927
+ return count
928
+ ```
970
929
 
930
+ First 30 tokens, allowing partial messages:
931
+ ```python
932
+ trim_messages(
933
+ messages,
934
+ max_tokens=30,
935
+ token_counter=dummy_token_counter,
936
+ strategy="first",
937
+ allow_partial=True,
938
+ )
939
+ ```
940
+
941
+ ```python
942
+ [
943
+ SystemMessage("This is a 4 token text. The full message is 10 tokens."),
944
+ HumanMessage(
945
+ "This is a 4 token text. The full message is 10 tokens.",
946
+ id="first",
947
+ ),
948
+ AIMessage(
949
+ [{"type": "text", "text": "This is the FIRST 4 token block."}],
950
+ id="second",
951
+ ),
952
+ ]
953
+ ```
971
954
  """
972
955
  # Validate arguments
973
956
  if start_on and strategy == "first":
@@ -1042,21 +1025,21 @@ def convert_to_openai_messages(
1042
1025
  messages: Message-like object or iterable of objects whose contents are
1043
1026
  in OpenAI, Anthropic, Bedrock Converse, or VertexAI formats.
1044
1027
  text_format: How to format string or text block contents:
1045
- - ``'string'``:
1028
+ - `'string'`:
1046
1029
  If a message has a string content, this is left as a string. If
1047
- a message has content blocks that are all of type ``'text'``, these
1030
+ a message has content blocks that are all of type `'text'`, these
1048
1031
  are joined with a newline to make a single string. If a message has
1049
- content blocks and at least one isn't of type ``'text'``, then
1032
+ content blocks and at least one isn't of type `'text'`, then
1050
1033
  all blocks are left as dicts.
1051
- - ``'block'``:
1034
+ - `'block'`:
1052
1035
  If a message has a string content, this is turned into a list
1053
- with a single content block of type ``'text'``. If a message has
1036
+ with a single content block of type `'text'`. If a message has
1054
1037
  content blocks these are left as is.
1055
1038
  include_id: Whether to include message ids in the openai messages, if they
1056
1039
  are present in the source messages.
1057
1040
 
1058
1041
  Raises:
1059
- ValueError: if an unrecognized ``text_format`` is specified, or if a message
1042
+ ValueError: if an unrecognized `text_format` is specified, or if a message
1060
1043
  content block is missing expected keys.
1061
1044
 
1062
1045
  Returns:
@@ -1070,50 +1053,49 @@ def convert_to_openai_messages(
1070
1053
  message dicts is returned.
1071
1054
 
1072
1055
  Example:
1056
+ ```python
1057
+ from langchain_core.messages import (
1058
+ convert_to_openai_messages,
1059
+ AIMessage,
1060
+ SystemMessage,
1061
+ ToolMessage,
1062
+ )
1073
1063
 
1074
- .. code-block:: python
1075
-
1076
- from langchain_core.messages import (
1077
- convert_to_openai_messages,
1078
- AIMessage,
1079
- SystemMessage,
1080
- ToolMessage,
1081
- )
1082
-
1083
- messages = [
1084
- SystemMessage([{"type": "text", "text": "foo"}]),
1085
- {
1086
- "role": "user",
1087
- "content": [
1088
- {"type": "text", "text": "whats in this"},
1089
- {
1090
- "type": "image_url",
1091
- "image_url": {"url": "data:image/png;base64,'/9j/4AAQSk'"},
1092
- },
1093
- ],
1094
- },
1095
- AIMessage(
1096
- "",
1097
- tool_calls=[
1098
- {
1099
- "name": "analyze",
1100
- "args": {"baz": "buz"},
1101
- "id": "1",
1102
- "type": "tool_call",
1103
- }
1104
- ],
1105
- ),
1106
- ToolMessage("foobar", tool_call_id="1", name="bar"),
1107
- {"role": "assistant", "content": "thats nice"},
1108
- ]
1109
- oai_messages = convert_to_openai_messages(messages)
1110
- # -> [
1111
- # {'role': 'system', 'content': 'foo'},
1112
- # {'role': 'user', 'content': [{'type': 'text', 'text': 'whats in this'}, {'type': 'image_url', 'image_url': {'url': "data:image/png;base64,'/9j/4AAQSk'"}}]},
1113
- # {'role': 'assistant', 'tool_calls': [{'type': 'function', 'id': '1','function': {'name': 'analyze', 'arguments': '{"baz": "buz"}'}}], 'content': ''},
1114
- # {'role': 'tool', 'name': 'bar', 'content': 'foobar'},
1115
- # {'role': 'assistant', 'content': 'thats nice'}
1116
- # ]
1064
+ messages = [
1065
+ SystemMessage([{"type": "text", "text": "foo"}]),
1066
+ {
1067
+ "role": "user",
1068
+ "content": [
1069
+ {"type": "text", "text": "whats in this"},
1070
+ {
1071
+ "type": "image_url",
1072
+ "image_url": {"url": "data:image/png;base64,'/9j/4AAQSk'"},
1073
+ },
1074
+ ],
1075
+ },
1076
+ AIMessage(
1077
+ "",
1078
+ tool_calls=[
1079
+ {
1080
+ "name": "analyze",
1081
+ "args": {"baz": "buz"},
1082
+ "id": "1",
1083
+ "type": "tool_call",
1084
+ }
1085
+ ],
1086
+ ),
1087
+ ToolMessage("foobar", tool_call_id="1", name="bar"),
1088
+ {"role": "assistant", "content": "thats nice"},
1089
+ ]
1090
+ oai_messages = convert_to_openai_messages(messages)
1091
+ # -> [
1092
+ # {'role': 'system', 'content': 'foo'},
1093
+ # {'role': 'user', 'content': [{'type': 'text', 'text': 'whats in this'}, {'type': 'image_url', 'image_url': {'url': "data:image/png;base64,'/9j/4AAQSk'"}}]},
1094
+ # {'role': 'assistant', 'tool_calls': [{'type': 'function', 'id': '1','function': {'name': 'analyze', 'arguments': '{"baz": "buz"}'}}], 'content': ''},
1095
+ # {'role': 'tool', 'name': 'bar', 'content': 'foobar'},
1096
+ # {'role': 'assistant', 'content': 'thats nice'}
1097
+ # ]
1098
+ ```
1117
1099
 
1118
1100
  !!! version-added "Added in version 0.3.11"
1119
1101
 
@@ -1695,13 +1677,13 @@ def count_tokens_approximately(
1695
1677
  Args:
1696
1678
  messages: List of messages to count tokens for.
1697
1679
  chars_per_token: Number of characters per token to use for the approximation.
1698
- Default is 4 (one token corresponds to ~4 chars for common English text).
1680
+ One token corresponds to ~4 chars for common English text.
1699
1681
  You can also specify float values for more fine-grained control.
1700
- `See more here. <https://platform.openai.com/tokenizer>`__
1701
- extra_tokens_per_message: Number of extra tokens to add per message.
1702
- Default is 3 (special tokens, including beginning/end of message).
1682
+ [See more here](https://platform.openai.com/tokenizer).
1683
+ extra_tokens_per_message: Number of extra tokens to add per message, e.g.
1684
+ special tokens, including beginning/end of message.
1703
1685
  You can also specify float values for more fine-grained control.
1704
- `See more here. <https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb>`__
1686
+ [See more here](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb).
1705
1687
  count_name: Whether to include message names in the count.
1706
1688
  Enabled by default.
1707
1689