langchain-core 0.3.75__py3-none-any.whl → 0.3.77__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/_api/beta_decorator.py +22 -44
- langchain_core/_api/deprecation.py +30 -17
- langchain_core/_api/path.py +19 -2
- langchain_core/_import_utils.py +7 -0
- langchain_core/agents.py +10 -6
- langchain_core/beta/runnables/context.py +1 -2
- langchain_core/callbacks/base.py +28 -15
- langchain_core/callbacks/manager.py +83 -71
- langchain_core/callbacks/usage.py +6 -4
- langchain_core/chat_history.py +29 -21
- langchain_core/document_loaders/base.py +34 -9
- langchain_core/document_loaders/langsmith.py +4 -1
- langchain_core/documents/base.py +35 -10
- langchain_core/documents/transformers.py +4 -2
- langchain_core/embeddings/fake.py +8 -5
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/exceptions.py +7 -0
- langchain_core/globals.py +17 -28
- langchain_core/indexing/api.py +88 -76
- langchain_core/indexing/base.py +5 -8
- langchain_core/indexing/in_memory.py +23 -3
- langchain_core/language_models/__init__.py +3 -2
- langchain_core/language_models/base.py +31 -20
- langchain_core/language_models/chat_models.py +98 -27
- langchain_core/language_models/fake_chat_models.py +10 -9
- langchain_core/language_models/llms.py +52 -18
- langchain_core/load/dump.py +2 -3
- langchain_core/load/load.py +15 -1
- langchain_core/load/serializable.py +39 -44
- langchain_core/memory.py +7 -3
- langchain_core/messages/ai.py +53 -24
- langchain_core/messages/base.py +43 -22
- langchain_core/messages/chat.py +4 -1
- langchain_core/messages/content_blocks.py +23 -2
- langchain_core/messages/function.py +9 -5
- langchain_core/messages/human.py +13 -10
- langchain_core/messages/modifier.py +1 -0
- langchain_core/messages/system.py +11 -8
- langchain_core/messages/tool.py +60 -29
- langchain_core/messages/utils.py +250 -131
- langchain_core/output_parsers/base.py +5 -2
- langchain_core/output_parsers/json.py +4 -4
- langchain_core/output_parsers/list.py +7 -22
- langchain_core/output_parsers/openai_functions.py +3 -0
- langchain_core/output_parsers/openai_tools.py +6 -1
- langchain_core/output_parsers/pydantic.py +4 -0
- langchain_core/output_parsers/string.py +5 -1
- langchain_core/output_parsers/xml.py +19 -19
- langchain_core/outputs/chat_generation.py +25 -10
- langchain_core/outputs/generation.py +14 -3
- langchain_core/outputs/llm_result.py +8 -1
- langchain_core/prompt_values.py +16 -6
- langchain_core/prompts/base.py +4 -9
- langchain_core/prompts/chat.py +89 -57
- langchain_core/prompts/dict.py +16 -8
- langchain_core/prompts/few_shot.py +12 -11
- langchain_core/prompts/few_shot_with_templates.py +5 -1
- langchain_core/prompts/image.py +12 -5
- langchain_core/prompts/message.py +5 -6
- langchain_core/prompts/pipeline.py +13 -8
- langchain_core/prompts/prompt.py +22 -8
- langchain_core/prompts/string.py +18 -10
- langchain_core/prompts/structured.py +7 -2
- langchain_core/rate_limiters.py +2 -2
- langchain_core/retrievers.py +7 -6
- langchain_core/runnables/base.py +406 -186
- langchain_core/runnables/branch.py +14 -19
- langchain_core/runnables/config.py +9 -15
- langchain_core/runnables/configurable.py +34 -19
- langchain_core/runnables/fallbacks.py +20 -13
- langchain_core/runnables/graph.py +48 -38
- langchain_core/runnables/graph_ascii.py +41 -18
- langchain_core/runnables/graph_mermaid.py +54 -25
- langchain_core/runnables/graph_png.py +27 -31
- langchain_core/runnables/history.py +55 -58
- langchain_core/runnables/passthrough.py +44 -21
- langchain_core/runnables/retry.py +44 -23
- langchain_core/runnables/router.py +9 -8
- langchain_core/runnables/schema.py +2 -0
- langchain_core/runnables/utils.py +51 -89
- langchain_core/stores.py +19 -31
- langchain_core/sys_info.py +9 -8
- langchain_core/tools/base.py +37 -28
- langchain_core/tools/convert.py +26 -15
- langchain_core/tools/simple.py +36 -8
- langchain_core/tools/structured.py +25 -12
- langchain_core/tracers/base.py +2 -2
- langchain_core/tracers/context.py +5 -1
- langchain_core/tracers/core.py +109 -39
- langchain_core/tracers/evaluation.py +22 -26
- langchain_core/tracers/event_stream.py +45 -34
- langchain_core/tracers/langchain.py +12 -3
- langchain_core/tracers/langchain_v1.py +10 -2
- langchain_core/tracers/log_stream.py +56 -17
- langchain_core/tracers/root_listeners.py +4 -20
- langchain_core/tracers/run_collector.py +6 -16
- langchain_core/tracers/schemas.py +5 -1
- langchain_core/utils/aiter.py +15 -7
- langchain_core/utils/env.py +3 -0
- langchain_core/utils/function_calling.py +50 -28
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +12 -4
- langchain_core/utils/json.py +12 -3
- langchain_core/utils/json_schema.py +156 -40
- langchain_core/utils/loading.py +5 -1
- langchain_core/utils/mustache.py +24 -15
- langchain_core/utils/pydantic.py +38 -9
- langchain_core/utils/utils.py +25 -9
- langchain_core/vectorstores/base.py +7 -20
- langchain_core/vectorstores/in_memory.py +23 -17
- langchain_core/vectorstores/utils.py +18 -12
- langchain_core/version.py +1 -1
- langchain_core-0.3.77.dist-info/METADATA +67 -0
- langchain_core-0.3.77.dist-info/RECORD +174 -0
- langchain_core-0.3.75.dist-info/METADATA +0 -106
- langchain_core-0.3.75.dist-info/RECORD +0 -174
- {langchain_core-0.3.75.dist-info → langchain_core-0.3.77.dist-info}/WHEEL +0 -0
- {langchain_core-0.3.75.dist-info → langchain_core-0.3.77.dist-info}/entry_points.txt +0 -0
langchain_core/prompts/string.py
CHANGED
|
@@ -15,6 +15,14 @@ from langchain_core.utils import get_colored_text, mustache
|
|
|
15
15
|
from langchain_core.utils.formatting import formatter
|
|
16
16
|
from langchain_core.utils.interactive_env import is_interactive_env
|
|
17
17
|
|
|
18
|
+
try:
|
|
19
|
+
from jinja2 import Environment, meta
|
|
20
|
+
from jinja2.sandbox import SandboxedEnvironment
|
|
21
|
+
|
|
22
|
+
_HAS_JINJA2 = True
|
|
23
|
+
except ImportError:
|
|
24
|
+
_HAS_JINJA2 = False
|
|
25
|
+
|
|
18
26
|
PromptTemplateFormat = Literal["f-string", "mustache", "jinja2"]
|
|
19
27
|
|
|
20
28
|
|
|
@@ -40,9 +48,7 @@ def jinja2_formatter(template: str, /, **kwargs: Any) -> str:
|
|
|
40
48
|
Raises:
|
|
41
49
|
ImportError: If jinja2 is not installed.
|
|
42
50
|
"""
|
|
43
|
-
|
|
44
|
-
from jinja2.sandbox import SandboxedEnvironment
|
|
45
|
-
except ImportError as e:
|
|
51
|
+
if not _HAS_JINJA2:
|
|
46
52
|
msg = (
|
|
47
53
|
"jinja2 not installed, which is needed to use the jinja2_formatter. "
|
|
48
54
|
"Please install it with `pip install jinja2`."
|
|
@@ -50,7 +56,7 @@ def jinja2_formatter(template: str, /, **kwargs: Any) -> str:
|
|
|
50
56
|
"Do not expand jinja2 templates using unverified or user-controlled "
|
|
51
57
|
"inputs as that can result in arbitrary Python code execution."
|
|
52
58
|
)
|
|
53
|
-
raise ImportError(msg)
|
|
59
|
+
raise ImportError(msg)
|
|
54
60
|
|
|
55
61
|
# This uses a sandboxed environment to prevent arbitrary code execution.
|
|
56
62
|
# Jinja2 uses an opt-out rather than opt-in approach for sand-boxing.
|
|
@@ -88,14 +94,12 @@ def validate_jinja2(template: str, input_variables: list[str]) -> None:
|
|
|
88
94
|
|
|
89
95
|
|
|
90
96
|
def _get_jinja2_variables_from_template(template: str) -> set[str]:
|
|
91
|
-
|
|
92
|
-
from jinja2 import Environment, meta
|
|
93
|
-
except ImportError as e:
|
|
97
|
+
if not _HAS_JINJA2:
|
|
94
98
|
msg = (
|
|
95
99
|
"jinja2 not installed, which is needed to use the jinja2_formatter. "
|
|
96
100
|
"Please install it with `pip install jinja2`."
|
|
97
101
|
)
|
|
98
|
-
raise ImportError(msg)
|
|
102
|
+
raise ImportError(msg)
|
|
99
103
|
env = Environment() # noqa: S701
|
|
100
104
|
ast = env.parse(template)
|
|
101
105
|
return meta.find_undeclared_variables(ast)
|
|
@@ -166,7 +170,7 @@ def mustache_schema(
|
|
|
166
170
|
prefix = section_stack.pop()
|
|
167
171
|
elif type_ in {"section", "inverted section"}:
|
|
168
172
|
section_stack.append(prefix)
|
|
169
|
-
prefix
|
|
173
|
+
prefix += tuple(key.split("."))
|
|
170
174
|
fields[prefix] = False
|
|
171
175
|
elif type_ in {"variable", "no escape"}:
|
|
172
176
|
fields[prefix + tuple(key.split("."))] = True
|
|
@@ -268,7 +272,11 @@ class StringPromptTemplate(BasePromptTemplate, ABC):
|
|
|
268
272
|
|
|
269
273
|
@classmethod
|
|
270
274
|
def get_lc_namespace(cls) -> list[str]:
|
|
271
|
-
"""Get the namespace of the langchain object.
|
|
275
|
+
"""Get the namespace of the langchain object.
|
|
276
|
+
|
|
277
|
+
Returns:
|
|
278
|
+
``["langchain", "prompts", "base"]``
|
|
279
|
+
"""
|
|
272
280
|
return ["langchain", "prompts", "base"]
|
|
273
281
|
|
|
274
282
|
def format_prompt(self, **kwargs: Any) -> PromptValue:
|
|
@@ -68,8 +68,11 @@ class StructuredPrompt(ChatPromptTemplate):
|
|
|
68
68
|
def get_lc_namespace(cls) -> list[str]:
|
|
69
69
|
"""Get the namespace of the langchain object.
|
|
70
70
|
|
|
71
|
-
For example, if the class is
|
|
72
|
-
namespace is ["langchain", "llms", "openai"]
|
|
71
|
+
For example, if the class is ``langchain.llms.openai.OpenAI``, then the
|
|
72
|
+
namespace is ``["langchain", "llms", "openai"]``
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
The namespace of the langchain object.
|
|
73
76
|
"""
|
|
74
77
|
return cls.__module__.split(".")
|
|
75
78
|
|
|
@@ -89,10 +92,12 @@ class StructuredPrompt(ChatPromptTemplate):
|
|
|
89
92
|
|
|
90
93
|
from langchain_core.prompts import StructuredPrompt
|
|
91
94
|
|
|
95
|
+
|
|
92
96
|
class OutputSchema(BaseModel):
|
|
93
97
|
name: str
|
|
94
98
|
value: int
|
|
95
99
|
|
|
100
|
+
|
|
96
101
|
template = StructuredPrompt(
|
|
97
102
|
[
|
|
98
103
|
("human", "Hello, how are you?"),
|
langchain_core/rate_limiters.py
CHANGED
|
@@ -110,9 +110,9 @@ class InMemoryRateLimiter(BaseRateLimiter):
|
|
|
110
110
|
)
|
|
111
111
|
|
|
112
112
|
from langchain_anthropic import ChatAnthropic
|
|
113
|
+
|
|
113
114
|
model = ChatAnthropic(
|
|
114
|
-
model_name="claude-3-opus-20240229",
|
|
115
|
-
rate_limiter=rate_limiter
|
|
115
|
+
model_name="claude-3-opus-20240229", rate_limiter=rate_limiter
|
|
116
116
|
)
|
|
117
117
|
|
|
118
118
|
for _ in range(5):
|
langchain_core/retrievers.py
CHANGED
|
@@ -31,6 +31,7 @@ from typing_extensions import Self, TypedDict, override
|
|
|
31
31
|
|
|
32
32
|
from langchain_core._api import deprecated
|
|
33
33
|
from langchain_core.callbacks import Callbacks
|
|
34
|
+
from langchain_core.callbacks.manager import AsyncCallbackManager, CallbackManager
|
|
34
35
|
from langchain_core.documents import Document
|
|
35
36
|
from langchain_core.runnables import (
|
|
36
37
|
Runnable,
|
|
@@ -109,6 +110,7 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
109
110
|
|
|
110
111
|
from sklearn.metrics.pairwise import cosine_similarity
|
|
111
112
|
|
|
113
|
+
|
|
112
114
|
class TFIDFRetriever(BaseRetriever, BaseModel):
|
|
113
115
|
vectorizer: Any
|
|
114
116
|
docs: list[Document]
|
|
@@ -122,10 +124,12 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
122
124
|
# Ip -- (n_docs,x), Op -- (n_docs,n_Feats)
|
|
123
125
|
query_vec = self.vectorizer.transform([query])
|
|
124
126
|
# Op -- (n_docs,1) -- Cosine Sim with each doc
|
|
125
|
-
results = cosine_similarity(self.tfidf_array, query_vec).reshape(
|
|
127
|
+
results = cosine_similarity(self.tfidf_array, query_vec).reshape(
|
|
128
|
+
(-1,)
|
|
129
|
+
)
|
|
126
130
|
return [self.docs[i] for i in results.argsort()[-self.k :][::-1]]
|
|
127
131
|
|
|
128
|
-
"""
|
|
132
|
+
"""
|
|
129
133
|
|
|
130
134
|
model_config = ConfigDict(
|
|
131
135
|
arbitrary_types_allowed=True,
|
|
@@ -233,8 +237,6 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
233
237
|
retriever.invoke("query")
|
|
234
238
|
|
|
235
239
|
"""
|
|
236
|
-
from langchain_core.callbacks.manager import CallbackManager
|
|
237
|
-
|
|
238
240
|
config = ensure_config(config)
|
|
239
241
|
inheritable_metadata = {
|
|
240
242
|
**(config.get("metadata") or {}),
|
|
@@ -298,8 +300,6 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
298
300
|
await retriever.ainvoke("query")
|
|
299
301
|
|
|
300
302
|
"""
|
|
301
|
-
from langchain_core.callbacks.manager import AsyncCallbackManager
|
|
302
|
-
|
|
303
303
|
config = ensure_config(config)
|
|
304
304
|
inheritable_metadata = {
|
|
305
305
|
**(config.get("metadata") or {}),
|
|
@@ -359,6 +359,7 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):
|
|
|
359
359
|
Args:
|
|
360
360
|
query: String to find relevant documents for
|
|
361
361
|
run_manager: The callback handler to use
|
|
362
|
+
|
|
362
363
|
Returns:
|
|
363
364
|
List of relevant documents
|
|
364
365
|
"""
|