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
langchain_core/__init__.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""`langchain-core` defines the base abstractions for the LangChain ecosystem.
|
|
2
2
|
|
|
3
3
|
The interfaces for core components like chat models, LLMs, vector stores, retrievers,
|
|
4
4
|
and more are defined here. The universal invocation protocol (Runnables) along with
|
langchain_core/_api/__init__.py
CHANGED
|
@@ -40,40 +40,37 @@ def beta(
|
|
|
40
40
|
"""Decorator to mark a function, a class, or a property as beta.
|
|
41
41
|
|
|
42
42
|
When marking a classmethod, a staticmethod, or a property, the
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
underlying callable), but *over*
|
|
43
|
+
`@beta` decorator should go *under* `@classmethod` and
|
|
44
|
+
`@staticmethod` (i.e., `beta` should directly decorate the
|
|
45
|
+
underlying callable), but *over* `@property`.
|
|
46
46
|
|
|
47
|
-
When marking a class
|
|
48
|
-
multiple inheritance hierarchy,
|
|
49
|
-
(if
|
|
50
|
-
|
|
51
|
-
own (annotation-emitting)
|
|
47
|
+
When marking a class `C` intended to be used as a base class in a
|
|
48
|
+
multiple inheritance hierarchy, `C` *must* define an `__init__` method
|
|
49
|
+
(if `C` instead inherited its `__init__` from its own base class, then
|
|
50
|
+
`@beta` would mess up `__init__` inheritance when installing its
|
|
51
|
+
own (annotation-emitting) `C.__init__`).
|
|
52
52
|
|
|
53
53
|
Args:
|
|
54
|
-
message
|
|
54
|
+
message:
|
|
55
55
|
Override the default beta message. The %(since)s,
|
|
56
56
|
%(name)s, %(alternative)s, %(obj_type)s, %(addendum)s,
|
|
57
57
|
and %(removal)s format specifiers will be replaced by the
|
|
58
58
|
values of the respective arguments passed to this function.
|
|
59
|
-
name
|
|
59
|
+
name:
|
|
60
60
|
The name of the beta object.
|
|
61
|
-
obj_type
|
|
61
|
+
obj_type:
|
|
62
62
|
The object type being beta.
|
|
63
|
-
addendum
|
|
63
|
+
addendum:
|
|
64
64
|
Additional text appended directly to the final message.
|
|
65
65
|
|
|
66
66
|
Returns:
|
|
67
67
|
A decorator which can be used to mark functions or classes as beta.
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def the_function_to_annotate():
|
|
75
|
-
pass
|
|
76
|
-
|
|
69
|
+
```python
|
|
70
|
+
@beta
|
|
71
|
+
def the_function_to_annotate():
|
|
72
|
+
pass
|
|
73
|
+
```
|
|
77
74
|
"""
|
|
78
75
|
|
|
79
76
|
def beta(
|
|
@@ -82,62 +82,59 @@ def deprecated(
|
|
|
82
82
|
"""Decorator to mark a function, a class, or a property as deprecated.
|
|
83
83
|
|
|
84
84
|
When deprecating a classmethod, a staticmethod, or a property, the
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
underlying callable), but *over*
|
|
85
|
+
`@deprecated` decorator should go *under* `@classmethod` and
|
|
86
|
+
`@staticmethod` (i.e., `deprecated` should directly decorate the
|
|
87
|
+
underlying callable), but *over* `@property`.
|
|
88
88
|
|
|
89
|
-
When deprecating a class
|
|
90
|
-
multiple inheritance hierarchy,
|
|
91
|
-
(if
|
|
92
|
-
|
|
93
|
-
own (deprecation-emitting)
|
|
89
|
+
When deprecating a class `C` intended to be used as a base class in a
|
|
90
|
+
multiple inheritance hierarchy, `C` *must* define an `__init__` method
|
|
91
|
+
(if `C` instead inherited its `__init__` from its own base class, then
|
|
92
|
+
`@deprecated` would mess up `__init__` inheritance when installing its
|
|
93
|
+
own (deprecation-emitting) `C.__init__`).
|
|
94
94
|
|
|
95
95
|
Parameters are the same as for `warn_deprecated`, except that *obj_type*
|
|
96
96
|
defaults to 'class' if decorating a class, 'attribute' if decorating a
|
|
97
97
|
property, and 'function' otherwise.
|
|
98
98
|
|
|
99
99
|
Args:
|
|
100
|
-
since
|
|
100
|
+
since:
|
|
101
101
|
The release at which this API became deprecated.
|
|
102
|
-
message
|
|
102
|
+
message:
|
|
103
103
|
Override the default deprecation message. The %(since)s,
|
|
104
104
|
%(name)s, %(alternative)s, %(obj_type)s, %(addendum)s,
|
|
105
105
|
and %(removal)s format specifiers will be replaced by the
|
|
106
106
|
values of the respective arguments passed to this function.
|
|
107
|
-
name
|
|
107
|
+
name:
|
|
108
108
|
The name of the deprecated object.
|
|
109
|
-
alternative
|
|
109
|
+
alternative:
|
|
110
110
|
An alternative API that the user may use in place of the
|
|
111
111
|
deprecated API. The deprecation warning will tell the user
|
|
112
112
|
about this alternative if provided.
|
|
113
|
-
alternative_import:
|
|
113
|
+
alternative_import:
|
|
114
114
|
An alternative import that the user may use instead.
|
|
115
|
-
pending
|
|
116
|
-
If True
|
|
115
|
+
pending:
|
|
116
|
+
If `True`, uses a `PendingDeprecationWarning` instead of a
|
|
117
117
|
DeprecationWarning. Cannot be used together with removal.
|
|
118
|
-
obj_type
|
|
118
|
+
obj_type:
|
|
119
119
|
The object type being deprecated.
|
|
120
|
-
addendum
|
|
120
|
+
addendum:
|
|
121
121
|
Additional text appended directly to the final message.
|
|
122
|
-
removal
|
|
122
|
+
removal:
|
|
123
123
|
The expected removal version. With the default (an empty
|
|
124
124
|
string), a removal version is automatically computed from
|
|
125
125
|
since. Set to other Falsy values to not schedule a removal
|
|
126
126
|
date. Cannot be used together with pending.
|
|
127
|
-
package:
|
|
127
|
+
package:
|
|
128
128
|
The package of the deprecated object.
|
|
129
129
|
|
|
130
130
|
Returns:
|
|
131
131
|
A decorator to mark a function or class as deprecated.
|
|
132
132
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
def the_function_to_deprecate():
|
|
139
|
-
pass
|
|
140
|
-
|
|
133
|
+
```python
|
|
134
|
+
@deprecated("1.4.0")
|
|
135
|
+
def the_function_to_deprecate():
|
|
136
|
+
pass
|
|
137
|
+
```
|
|
141
138
|
"""
|
|
142
139
|
_validate_deprecation_params(
|
|
143
140
|
removal, alternative, alternative_import, pending=pending
|
|
@@ -372,7 +369,7 @@ def deprecated(
|
|
|
372
369
|
components = [
|
|
373
370
|
_message,
|
|
374
371
|
f"Use {_alternative} instead." if _alternative else "",
|
|
375
|
-
f"Use
|
|
372
|
+
f"Use `{_alternative_import}` instead." if _alternative_import else "",
|
|
376
373
|
_addendum,
|
|
377
374
|
]
|
|
378
375
|
details = " ".join([component.strip() for component in components if component])
|
|
@@ -440,7 +437,7 @@ def warn_deprecated(
|
|
|
440
437
|
alternative_import:
|
|
441
438
|
An alternative import that the user may use instead.
|
|
442
439
|
pending:
|
|
443
|
-
If True
|
|
440
|
+
If `True`, uses a `PendingDeprecationWarning` instead of a
|
|
444
441
|
DeprecationWarning. Cannot be used together with removal.
|
|
445
442
|
obj_type:
|
|
446
443
|
The object type being deprecated.
|
|
@@ -550,12 +547,10 @@ def rename_parameter(
|
|
|
550
547
|
A decorator indicating that a parameter was renamed.
|
|
551
548
|
|
|
552
549
|
Example:
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
def func(good_name): ...
|
|
558
|
-
|
|
550
|
+
```python
|
|
551
|
+
@_api.rename_parameter("3.1", "bad_name", "good_name")
|
|
552
|
+
def func(good_name): ...
|
|
553
|
+
```
|
|
559
554
|
"""
|
|
560
555
|
|
|
561
556
|
def decorator(f: Callable[_P, _R]) -> Callable[_P, _R]:
|
langchain_core/_import_utils.py
CHANGED
|
@@ -13,7 +13,7 @@ def import_attr(
|
|
|
13
13
|
|
|
14
14
|
Args:
|
|
15
15
|
attr_name: The name of the attribute to import.
|
|
16
|
-
module_name: The name of the module to import from. If None
|
|
16
|
+
module_name: The name of the module to import from. If `None`, the attribute
|
|
17
17
|
is imported from the package itself.
|
|
18
18
|
package: The name of the package where the module is located.
|
|
19
19
|
|
langchain_core/agents.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"""Schema definitions for representing agent actions, observations, and return values.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
!!! warning
|
|
4
|
+
The schema definitions are provided for backwards compatibility.
|
|
4
5
|
|
|
5
|
-
!!!
|
|
6
|
+
!!! warning
|
|
6
7
|
New agents should be built using the
|
|
7
8
|
[langgraph library](https://github.com/langchain-ai/langgraph), which provides a
|
|
8
9
|
simpler and more flexible way to define agents.
|
|
@@ -16,10 +17,10 @@ Agents use language models to choose a sequence of actions to take.
|
|
|
16
17
|
A basic agent works in the following manner:
|
|
17
18
|
|
|
18
19
|
1. Given a prompt an agent uses an LLM to request an action to take
|
|
19
|
-
|
|
20
|
+
(e.g., a tool to run).
|
|
20
21
|
2. The agent executes the action (e.g., runs the tool), and receives an observation.
|
|
21
22
|
3. The agent returns the observation to the LLM, which can then be used to generate
|
|
22
|
-
|
|
23
|
+
the next action.
|
|
23
24
|
4. When the agent reaches a stopping condition, it returns a final return value.
|
|
24
25
|
|
|
25
26
|
The schemas for the agents themselves are defined in langchain.agents.agent.
|
|
@@ -83,10 +84,10 @@ class AgentAction(Serializable):
|
|
|
83
84
|
|
|
84
85
|
@classmethod
|
|
85
86
|
def get_lc_namespace(cls) -> list[str]:
|
|
86
|
-
"""Get the namespace of the
|
|
87
|
+
"""Get the namespace of the LangChain object.
|
|
87
88
|
|
|
88
89
|
Returns:
|
|
89
|
-
|
|
90
|
+
`["langchain", "schema", "agent"]`
|
|
90
91
|
"""
|
|
91
92
|
return ["langchain", "schema", "agent"]
|
|
92
93
|
|
|
@@ -111,7 +112,7 @@ class AgentActionMessageLog(AgentAction):
|
|
|
111
112
|
if (tool, tool_input) cannot be used to fully recreate the LLM
|
|
112
113
|
prediction, and you need that LLM prediction (for future agent iteration).
|
|
113
114
|
Compared to `log`, this is useful when the underlying LLM is a
|
|
114
|
-
|
|
115
|
+
chat model (and therefore returns messages rather than a string)."""
|
|
115
116
|
# Ignoring type because we're overriding the type from AgentAction.
|
|
116
117
|
# And this is the correct thing to do in this case.
|
|
117
118
|
# The type literal is used for serialization purposes.
|
|
@@ -160,10 +161,10 @@ class AgentFinish(Serializable):
|
|
|
160
161
|
|
|
161
162
|
@classmethod
|
|
162
163
|
def get_lc_namespace(cls) -> list[str]:
|
|
163
|
-
"""Get the namespace of the
|
|
164
|
+
"""Get the namespace of the LangChain object.
|
|
164
165
|
|
|
165
166
|
Returns:
|
|
166
|
-
|
|
167
|
+
`["langchain", "schema", "agent"]`
|
|
167
168
|
"""
|
|
168
169
|
return ["langchain", "schema", "agent"]
|
|
169
170
|
|
langchain_core/caches.py
CHANGED
|
@@ -1,24 +1,15 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""`caches` provides an optional caching layer for language models.
|
|
2
2
|
|
|
3
3
|
!!! warning
|
|
4
|
-
|
|
4
|
+
This is a beta feature! Please be wary of deploying experimental code to production
|
|
5
|
+
unless you've taken appropriate precautions.
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
A cache is useful for two reasons:
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
- It can speed up your application by reducing the number of API calls you make
|
|
13
|
-
to the LLM provider.
|
|
14
|
-
|
|
15
|
-
Cache directly competes with Memory. See documentation for Pros and Cons.
|
|
16
|
-
|
|
17
|
-
**Class hierarchy:**
|
|
18
|
-
|
|
19
|
-
.. code-block::
|
|
20
|
-
|
|
21
|
-
BaseCache --> <name>Cache # Examples: InMemoryCache, RedisCache, GPTCache
|
|
9
|
+
1. It can save you money by reducing the number of API calls you make to the LLM
|
|
10
|
+
provider if you're often requesting the same completion multiple times.
|
|
11
|
+
2. It can speed up your application by reducing the number of API calls you make to the
|
|
12
|
+
LLM provider.
|
|
22
13
|
"""
|
|
23
14
|
|
|
24
15
|
from __future__ import annotations
|
|
@@ -40,8 +31,8 @@ class BaseCache(ABC):
|
|
|
40
31
|
|
|
41
32
|
The cache interface consists of the following methods:
|
|
42
33
|
|
|
43
|
-
- lookup: Look up a value based on a prompt and llm_string
|
|
44
|
-
- update: Update the cache based on a prompt and llm_string
|
|
34
|
+
- lookup: Look up a value based on a prompt and `llm_string`.
|
|
35
|
+
- update: Update the cache based on a prompt and `llm_string`.
|
|
45
36
|
- clear: Clear the cache.
|
|
46
37
|
|
|
47
38
|
In addition, the cache interface provides an async version of each method.
|
|
@@ -53,14 +44,14 @@ class BaseCache(ABC):
|
|
|
53
44
|
|
|
54
45
|
@abstractmethod
|
|
55
46
|
def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None:
|
|
56
|
-
"""Look up based on prompt and llm_string
|
|
47
|
+
"""Look up based on `prompt` and `llm_string`.
|
|
57
48
|
|
|
58
49
|
A cache implementation is expected to generate a key from the 2-tuple
|
|
59
50
|
of prompt and llm_string (e.g., by concatenating them with a delimiter).
|
|
60
51
|
|
|
61
52
|
Args:
|
|
62
|
-
prompt:
|
|
63
|
-
In the case of a
|
|
53
|
+
prompt: A string representation of the prompt.
|
|
54
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
64
55
|
serialization of the prompt into the language model.
|
|
65
56
|
llm_string: A string representation of the LLM configuration.
|
|
66
57
|
This is used to capture the invocation parameters of the LLM
|
|
@@ -69,27 +60,27 @@ class BaseCache(ABC):
|
|
|
69
60
|
representation.
|
|
70
61
|
|
|
71
62
|
Returns:
|
|
72
|
-
On a cache miss, return None
|
|
73
|
-
The cached value is a list of
|
|
63
|
+
On a cache miss, return `None`. On a cache hit, return the cached value.
|
|
64
|
+
The cached value is a list of `Generation` (or subclasses).
|
|
74
65
|
"""
|
|
75
66
|
|
|
76
67
|
@abstractmethod
|
|
77
68
|
def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None:
|
|
78
|
-
"""Update cache based on prompt and llm_string
|
|
69
|
+
"""Update cache based on `prompt` and `llm_string`.
|
|
79
70
|
|
|
80
71
|
The prompt and llm_string are used to generate a key for the cache.
|
|
81
72
|
The key should match that of the lookup method.
|
|
82
73
|
|
|
83
74
|
Args:
|
|
84
|
-
prompt:
|
|
85
|
-
In the case of a
|
|
75
|
+
prompt: A string representation of the prompt.
|
|
76
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
86
77
|
serialization of the prompt into the language model.
|
|
87
78
|
llm_string: A string representation of the LLM configuration.
|
|
88
79
|
This is used to capture the invocation parameters of the LLM
|
|
89
80
|
(e.g., model name, temperature, stop tokens, max tokens, etc.).
|
|
90
81
|
These invocation parameters are serialized into a string
|
|
91
82
|
representation.
|
|
92
|
-
return_val: The value to be cached. The value is a list of
|
|
83
|
+
return_val: The value to be cached. The value is a list of `Generation`
|
|
93
84
|
(or subclasses).
|
|
94
85
|
"""
|
|
95
86
|
|
|
@@ -98,14 +89,14 @@ class BaseCache(ABC):
|
|
|
98
89
|
"""Clear cache that can take additional keyword arguments."""
|
|
99
90
|
|
|
100
91
|
async def alookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None:
|
|
101
|
-
"""Async look up based on prompt and llm_string
|
|
92
|
+
"""Async look up based on `prompt` and `llm_string`.
|
|
102
93
|
|
|
103
94
|
A cache implementation is expected to generate a key from the 2-tuple
|
|
104
95
|
of prompt and llm_string (e.g., by concatenating them with a delimiter).
|
|
105
96
|
|
|
106
97
|
Args:
|
|
107
|
-
prompt:
|
|
108
|
-
In the case of a
|
|
98
|
+
prompt: A string representation of the prompt.
|
|
99
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
109
100
|
serialization of the prompt into the language model.
|
|
110
101
|
llm_string: A string representation of the LLM configuration.
|
|
111
102
|
This is used to capture the invocation parameters of the LLM
|
|
@@ -114,29 +105,29 @@ class BaseCache(ABC):
|
|
|
114
105
|
representation.
|
|
115
106
|
|
|
116
107
|
Returns:
|
|
117
|
-
On a cache miss, return None
|
|
118
|
-
The cached value is a list of
|
|
108
|
+
On a cache miss, return `None`. On a cache hit, return the cached value.
|
|
109
|
+
The cached value is a list of `Generation` (or subclasses).
|
|
119
110
|
"""
|
|
120
111
|
return await run_in_executor(None, self.lookup, prompt, llm_string)
|
|
121
112
|
|
|
122
113
|
async def aupdate(
|
|
123
114
|
self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE
|
|
124
115
|
) -> None:
|
|
125
|
-
"""Async update cache based on prompt and llm_string
|
|
116
|
+
"""Async update cache based on `prompt` and `llm_string`.
|
|
126
117
|
|
|
127
118
|
The prompt and llm_string are used to generate a key for the cache.
|
|
128
119
|
The key should match that of the look up method.
|
|
129
120
|
|
|
130
121
|
Args:
|
|
131
|
-
prompt:
|
|
132
|
-
In the case of a
|
|
122
|
+
prompt: A string representation of the prompt.
|
|
123
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
133
124
|
serialization of the prompt into the language model.
|
|
134
125
|
llm_string: A string representation of the LLM configuration.
|
|
135
126
|
This is used to capture the invocation parameters of the LLM
|
|
136
127
|
(e.g., model name, temperature, stop tokens, max tokens, etc.).
|
|
137
128
|
These invocation parameters are serialized into a string
|
|
138
129
|
representation.
|
|
139
|
-
return_val: The value to be cached. The value is a list of
|
|
130
|
+
return_val: The value to be cached. The value is a list of `Generation`
|
|
140
131
|
(or subclasses).
|
|
141
132
|
"""
|
|
142
133
|
return await run_in_executor(None, self.update, prompt, llm_string, return_val)
|
|
@@ -154,12 +145,11 @@ class InMemoryCache(BaseCache):
|
|
|
154
145
|
|
|
155
146
|
Args:
|
|
156
147
|
maxsize: The maximum number of items to store in the cache.
|
|
157
|
-
If None
|
|
148
|
+
If `None`, the cache has no maximum size.
|
|
158
149
|
If the cache exceeds the maximum size, the oldest items are removed.
|
|
159
|
-
Default is None.
|
|
160
150
|
|
|
161
151
|
Raises:
|
|
162
|
-
ValueError: If maxsize is less than or equal to 0
|
|
152
|
+
ValueError: If `maxsize` is less than or equal to `0`.
|
|
163
153
|
"""
|
|
164
154
|
self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {}
|
|
165
155
|
if maxsize is not None and maxsize <= 0:
|
|
@@ -168,28 +158,28 @@ class InMemoryCache(BaseCache):
|
|
|
168
158
|
self._maxsize = maxsize
|
|
169
159
|
|
|
170
160
|
def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None:
|
|
171
|
-
"""Look up based on prompt and llm_string
|
|
161
|
+
"""Look up based on `prompt` and `llm_string`.
|
|
172
162
|
|
|
173
163
|
Args:
|
|
174
|
-
prompt:
|
|
175
|
-
In the case of a
|
|
164
|
+
prompt: A string representation of the prompt.
|
|
165
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
176
166
|
serialization of the prompt into the language model.
|
|
177
167
|
llm_string: A string representation of the LLM configuration.
|
|
178
168
|
|
|
179
169
|
Returns:
|
|
180
|
-
On a cache miss, return None
|
|
170
|
+
On a cache miss, return `None`. On a cache hit, return the cached value.
|
|
181
171
|
"""
|
|
182
172
|
return self._cache.get((prompt, llm_string), None)
|
|
183
173
|
|
|
184
174
|
def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None:
|
|
185
|
-
"""Update cache based on prompt and llm_string
|
|
175
|
+
"""Update cache based on `prompt` and `llm_string`.
|
|
186
176
|
|
|
187
177
|
Args:
|
|
188
|
-
prompt:
|
|
189
|
-
In the case of a
|
|
178
|
+
prompt: A string representation of the prompt.
|
|
179
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
190
180
|
serialization of the prompt into the language model.
|
|
191
181
|
llm_string: A string representation of the LLM configuration.
|
|
192
|
-
return_val: The value to be cached. The value is a list of
|
|
182
|
+
return_val: The value to be cached. The value is a list of `Generation`
|
|
193
183
|
(or subclasses).
|
|
194
184
|
"""
|
|
195
185
|
if self._maxsize is not None and len(self._cache) == self._maxsize:
|
|
@@ -202,30 +192,30 @@ class InMemoryCache(BaseCache):
|
|
|
202
192
|
self._cache = {}
|
|
203
193
|
|
|
204
194
|
async def alookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None:
|
|
205
|
-
"""Async look up based on prompt and llm_string
|
|
195
|
+
"""Async look up based on `prompt` and `llm_string`.
|
|
206
196
|
|
|
207
197
|
Args:
|
|
208
|
-
prompt:
|
|
209
|
-
In the case of a
|
|
198
|
+
prompt: A string representation of the prompt.
|
|
199
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
210
200
|
serialization of the prompt into the language model.
|
|
211
201
|
llm_string: A string representation of the LLM configuration.
|
|
212
202
|
|
|
213
203
|
Returns:
|
|
214
|
-
On a cache miss, return None
|
|
204
|
+
On a cache miss, return `None`. On a cache hit, return the cached value.
|
|
215
205
|
"""
|
|
216
206
|
return self.lookup(prompt, llm_string)
|
|
217
207
|
|
|
218
208
|
async def aupdate(
|
|
219
209
|
self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE
|
|
220
210
|
) -> None:
|
|
221
|
-
"""Async update cache based on prompt and llm_string
|
|
211
|
+
"""Async update cache based on `prompt` and `llm_string`.
|
|
222
212
|
|
|
223
213
|
Args:
|
|
224
|
-
prompt:
|
|
225
|
-
In the case of a
|
|
214
|
+
prompt: A string representation of the prompt.
|
|
215
|
+
In the case of a chat model, the prompt is a non-trivial
|
|
226
216
|
serialization of the prompt into the language model.
|
|
227
217
|
llm_string: A string representation of the LLM configuration.
|
|
228
|
-
return_val: The value to be cached. The value is a list of
|
|
218
|
+
return_val: The value to be cached. The value is a list of `Generation`
|
|
229
219
|
(or subclasses).
|
|
230
220
|
"""
|
|
231
221
|
self.update(prompt, llm_string, return_val)
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
"""**Callback handlers** allow listening to events in LangChain.
|
|
2
|
-
|
|
3
|
-
**Class hierarchy:**
|
|
4
|
-
|
|
5
|
-
.. code-block::
|
|
6
|
-
|
|
7
|
-
BaseCallbackHandler --> <name>CallbackHandler # Example: AimCallbackHandler
|
|
8
|
-
"""
|
|
1
|
+
"""**Callback handlers** allow listening to events in LangChain."""
|
|
9
2
|
|
|
10
3
|
from typing import TYPE_CHECKING
|
|
11
4
|
|