langchain-core 1.0.0a6__py3-none-any.whl → 1.0.3__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.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +51 -64
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +20 -22
- langchain_core/caches.py +65 -66
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +321 -336
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +436 -513
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +53 -68
- langchain_core/document_loaders/base.py +27 -25
- langchain_core/document_loaders/blob_loaders.py +1 -1
- langchain_core/document_loaders/langsmith.py +44 -48
- langchain_core/documents/__init__.py +23 -3
- langchain_core/documents/base.py +98 -90
- langchain_core/documents/compressor.py +10 -10
- langchain_core/documents/transformers.py +34 -35
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +28 -32
- langchain_core/exceptions.py +21 -20
- langchain_core/globals.py +3 -151
- langchain_core/indexing/__init__.py +1 -1
- langchain_core/indexing/api.py +121 -126
- langchain_core/indexing/base.py +73 -75
- langchain_core/indexing/in_memory.py +4 -6
- langchain_core/language_models/__init__.py +14 -29
- langchain_core/language_models/_utils.py +58 -61
- langchain_core/language_models/base.py +53 -162
- langchain_core/language_models/chat_models.py +298 -387
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +125 -235
- langchain_core/load/dump.py +9 -12
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +42 -40
- langchain_core/messages/__init__.py +10 -16
- langchain_core/messages/ai.py +148 -148
- langchain_core/messages/base.py +53 -51
- langchain_core/messages/block_translators/__init__.py +19 -22
- langchain_core/messages/block_translators/anthropic.py +6 -6
- langchain_core/messages/block_translators/bedrock_converse.py +5 -5
- langchain_core/messages/block_translators/google_genai.py +10 -7
- 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 +5 -5
- langchain_core/messages/block_translators/openai.py +11 -11
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +337 -328
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +24 -31
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +19 -29
- langchain_core/messages/tool.py +74 -90
- langchain_core/messages/utils.py +474 -504
- langchain_core/output_parsers/__init__.py +13 -10
- langchain_core/output_parsers/base.py +61 -61
- langchain_core/output_parsers/format_instructions.py +9 -4
- langchain_core/output_parsers/json.py +12 -10
- langchain_core/output_parsers/list.py +21 -23
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +16 -21
- langchain_core/output_parsers/pydantic.py +13 -14
- langchain_core/output_parsers/string.py +5 -5
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +35 -34
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +10 -11
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +11 -17
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -56
- langchain_core/prompts/chat.py +275 -325
- langchain_core/prompts/dict.py +5 -5
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +4 -6
- langchain_core/prompts/message.py +3 -3
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +26 -10
- langchain_core/prompts/structured.py +49 -53
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +61 -198
- langchain_core/runnables/base.py +1476 -1626
- langchain_core/runnables/branch.py +53 -57
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +120 -137
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +91 -97
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- langchain_core/runnables/schema.py +75 -80
- langchain_core/runnables/utils.py +60 -67
- langchain_core/stores.py +85 -121
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +284 -229
- langchain_core/tools/convert.py +160 -155
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +32 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +65 -64
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +45 -45
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +2 -2
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +89 -186
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +36 -40
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +9 -9
- langchain_core/utils/mustache.py +8 -10
- langchain_core/utils/pydantic.py +33 -35
- langchain_core/utils/strings.py +6 -9
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +66 -62
- langchain_core/vectorstores/base.py +182 -216
- langchain_core/vectorstores/in_memory.py +101 -176
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- langchain_core-1.0.3.dist-info/METADATA +69 -0
- langchain_core-1.0.3.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.3.dist-info}/WHEEL +1 -1
- langchain_core/memory.py +0 -120
- langchain_core/messages/block_translators/ollama.py +0 -47
- langchain_core/prompts/pipeline.py +0 -138
- 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/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-1.0.0a6.dist-info/METADATA +0 -67
- langchain_core-1.0.0a6.dist-info/RECORD +0 -181
- langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
langchain_core/prompts/dict.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
4
|
from functools import cached_property
|
|
5
|
-
from typing import Any, Literal
|
|
5
|
+
from typing import Any, Literal
|
|
6
6
|
|
|
7
7
|
from typing_extensions import override
|
|
8
8
|
|
|
@@ -48,7 +48,7 @@ class DictPromptTemplate(RunnableSerializable[dict, dict]):
|
|
|
48
48
|
|
|
49
49
|
@override
|
|
50
50
|
def invoke(
|
|
51
|
-
self, input: dict, config:
|
|
51
|
+
self, input: dict, config: RunnableConfig | None = None, **kwargs: Any
|
|
52
52
|
) -> dict:
|
|
53
53
|
return self._call_with_config(
|
|
54
54
|
lambda x: self.format(**x),
|
|
@@ -74,10 +74,10 @@ class DictPromptTemplate(RunnableSerializable[dict, dict]):
|
|
|
74
74
|
|
|
75
75
|
@classmethod
|
|
76
76
|
def get_lc_namespace(cls) -> list[str]:
|
|
77
|
-
"""Get the namespace of the
|
|
77
|
+
"""Get the namespace of the LangChain object.
|
|
78
78
|
|
|
79
79
|
Returns:
|
|
80
|
-
|
|
80
|
+
`["langchain_core", "prompts", "dict"]`
|
|
81
81
|
"""
|
|
82
82
|
return ["langchain_core", "prompts", "dict"]
|
|
83
83
|
|
|
@@ -85,7 +85,7 @@ class DictPromptTemplate(RunnableSerializable[dict, dict]):
|
|
|
85
85
|
"""Human-readable representation.
|
|
86
86
|
|
|
87
87
|
Args:
|
|
88
|
-
html: Whether to format as HTML.
|
|
88
|
+
html: Whether to format as HTML.
|
|
89
89
|
|
|
90
90
|
Returns:
|
|
91
91
|
Human-readable representation.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from typing import TYPE_CHECKING, Any, Literal
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Literal
|
|
6
6
|
|
|
7
7
|
from pydantic import (
|
|
8
8
|
BaseModel,
|
|
@@ -33,11 +33,11 @@ if TYPE_CHECKING:
|
|
|
33
33
|
class _FewShotPromptTemplateMixin(BaseModel):
|
|
34
34
|
"""Prompt template that contains few shot examples."""
|
|
35
35
|
|
|
36
|
-
examples:
|
|
36
|
+
examples: list[dict] | None = None
|
|
37
37
|
"""Examples to format into the prompt.
|
|
38
38
|
Either this or example_selector should be provided."""
|
|
39
39
|
|
|
40
|
-
example_selector:
|
|
40
|
+
example_selector: BaseExampleSelector | None = None
|
|
41
41
|
"""ExampleSelector to choose the examples to format into the prompt.
|
|
42
42
|
Either this or examples should be provided."""
|
|
43
43
|
|
|
@@ -229,7 +229,7 @@ class FewShotPromptTemplate(_FewShotPromptTemplateMixin, StringPromptTemplate):
|
|
|
229
229
|
"""Return the prompt type key."""
|
|
230
230
|
return "few_shot"
|
|
231
231
|
|
|
232
|
-
def save(self, file_path:
|
|
232
|
+
def save(self, file_path: Path | str) -> None:
|
|
233
233
|
"""Save the prompt template to a file.
|
|
234
234
|
|
|
235
235
|
Args:
|
|
@@ -268,104 +268,97 @@ class FewShotChatMessagePromptTemplate(
|
|
|
268
268
|
Prompt template with a fixed list of examples (matching the sample
|
|
269
269
|
conversation above):
|
|
270
270
|
|
|
271
|
-
|
|
271
|
+
```python
|
|
272
|
+
from langchain_core.prompts import (
|
|
273
|
+
FewShotChatMessagePromptTemplate,
|
|
274
|
+
ChatPromptTemplate,
|
|
275
|
+
)
|
|
272
276
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
+
examples = [
|
|
278
|
+
{"input": "2+2", "output": "4"},
|
|
279
|
+
{"input": "2+3", "output": "5"},
|
|
280
|
+
]
|
|
277
281
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
282
|
+
example_prompt = ChatPromptTemplate.from_messages(
|
|
283
|
+
[
|
|
284
|
+
("human", "What is {input}?"),
|
|
285
|
+
("ai", "{output}"),
|
|
281
286
|
]
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
final_prompt = ChatPromptTemplate.from_messages(
|
|
297
|
-
[
|
|
298
|
-
("system", "You are a helpful AI Assistant"),
|
|
299
|
-
few_shot_prompt,
|
|
300
|
-
("human", "{input}"),
|
|
301
|
-
]
|
|
302
|
-
)
|
|
303
|
-
final_prompt.format(input="What is 4+4?")
|
|
304
|
-
|
|
305
|
-
Prompt template with dynamically selected examples:
|
|
306
|
-
|
|
307
|
-
.. code-block:: python
|
|
308
|
-
|
|
309
|
-
from langchain_core.prompts import SemanticSimilarityExampleSelector
|
|
310
|
-
from langchain_core.embeddings import OpenAIEmbeddings
|
|
311
|
-
from langchain_core.vectorstores import Chroma
|
|
312
|
-
|
|
313
|
-
examples = [
|
|
314
|
-
{"input": "2+2", "output": "4"},
|
|
315
|
-
{"input": "2+3", "output": "5"},
|
|
316
|
-
{"input": "2+4", "output": "6"},
|
|
317
|
-
# ...
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
few_shot_prompt = FewShotChatMessagePromptTemplate(
|
|
290
|
+
examples=examples,
|
|
291
|
+
# This is a prompt template used to format each individual example.
|
|
292
|
+
example_prompt=example_prompt,
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
final_prompt = ChatPromptTemplate.from_messages(
|
|
296
|
+
[
|
|
297
|
+
("system", "You are a helpful AI Assistant"),
|
|
298
|
+
few_shot_prompt,
|
|
299
|
+
("human", "{input}"),
|
|
318
300
|
]
|
|
301
|
+
)
|
|
302
|
+
final_prompt.format(input="What is 4+4?")
|
|
303
|
+
```
|
|
319
304
|
|
|
320
|
-
|
|
321
|
-
embeddings = OpenAIEmbeddings()
|
|
322
|
-
vectorstore = Chroma.from_texts(
|
|
323
|
-
to_vectorize, embeddings, metadatas=examples
|
|
324
|
-
)
|
|
325
|
-
example_selector = SemanticSimilarityExampleSelector(
|
|
326
|
-
vectorstore=vectorstore
|
|
327
|
-
)
|
|
328
|
-
|
|
329
|
-
from langchain_core import SystemMessage
|
|
330
|
-
from langchain_core.prompts import HumanMessagePromptTemplate
|
|
331
|
-
from langchain_core.prompts.few_shot import FewShotChatMessagePromptTemplate
|
|
332
|
-
|
|
333
|
-
few_shot_prompt = FewShotChatMessagePromptTemplate(
|
|
334
|
-
# Which variable(s) will be passed to the example selector.
|
|
335
|
-
input_variables=["input"],
|
|
336
|
-
example_selector=example_selector,
|
|
337
|
-
# Define how each example will be formatted.
|
|
338
|
-
# In this case, each example will become 2 messages:
|
|
339
|
-
# 1 human, and 1 AI
|
|
340
|
-
example_prompt=(
|
|
341
|
-
HumanMessagePromptTemplate.from_template("{input}")
|
|
342
|
-
+ AIMessagePromptTemplate.from_template("{output}")
|
|
343
|
-
),
|
|
344
|
-
)
|
|
345
|
-
# Define the overall prompt.
|
|
346
|
-
final_prompt = (
|
|
347
|
-
SystemMessagePromptTemplate.from_template(
|
|
348
|
-
"You are a helpful AI Assistant"
|
|
349
|
-
)
|
|
350
|
-
+ few_shot_prompt
|
|
351
|
-
+ HumanMessagePromptTemplate.from_template("{input}")
|
|
352
|
-
)
|
|
353
|
-
# Show the prompt
|
|
354
|
-
print(final_prompt.format_messages(input="What's 3+3?")) # noqa: T201
|
|
305
|
+
Prompt template with dynamically selected examples:
|
|
355
306
|
|
|
356
|
-
|
|
357
|
-
|
|
307
|
+
```python
|
|
308
|
+
from langchain_core.prompts import SemanticSimilarityExampleSelector
|
|
309
|
+
from langchain_core.embeddings import OpenAIEmbeddings
|
|
310
|
+
from langchain_core.vectorstores import Chroma
|
|
358
311
|
|
|
359
|
-
|
|
360
|
-
|
|
312
|
+
examples = [
|
|
313
|
+
{"input": "2+2", "output": "4"},
|
|
314
|
+
{"input": "2+3", "output": "5"},
|
|
315
|
+
{"input": "2+4", "output": "6"},
|
|
316
|
+
# ...
|
|
317
|
+
]
|
|
361
318
|
|
|
319
|
+
to_vectorize = [" ".join(example.values()) for example in examples]
|
|
320
|
+
embeddings = OpenAIEmbeddings()
|
|
321
|
+
vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=examples)
|
|
322
|
+
example_selector = SemanticSimilarityExampleSelector(vectorstore=vectorstore)
|
|
323
|
+
|
|
324
|
+
from langchain_core import SystemMessage
|
|
325
|
+
from langchain_core.prompts import HumanMessagePromptTemplate
|
|
326
|
+
from langchain_core.prompts.few_shot import FewShotChatMessagePromptTemplate
|
|
327
|
+
|
|
328
|
+
few_shot_prompt = FewShotChatMessagePromptTemplate(
|
|
329
|
+
# Which variable(s) will be passed to the example selector.
|
|
330
|
+
input_variables=["input"],
|
|
331
|
+
example_selector=example_selector,
|
|
332
|
+
# Define how each example will be formatted.
|
|
333
|
+
# In this case, each example will become 2 messages:
|
|
334
|
+
# 1 human, and 1 AI
|
|
335
|
+
example_prompt=(
|
|
336
|
+
HumanMessagePromptTemplate.from_template("{input}")
|
|
337
|
+
+ AIMessagePromptTemplate.from_template("{output}")
|
|
338
|
+
),
|
|
339
|
+
)
|
|
340
|
+
# Define the overall prompt.
|
|
341
|
+
final_prompt = (
|
|
342
|
+
SystemMessagePromptTemplate.from_template("You are a helpful AI Assistant")
|
|
343
|
+
+ few_shot_prompt
|
|
344
|
+
+ HumanMessagePromptTemplate.from_template("{input}")
|
|
345
|
+
)
|
|
346
|
+
# Show the prompt
|
|
347
|
+
print(final_prompt.format_messages(input="What's 3+3?")) # noqa: T201
|
|
348
|
+
|
|
349
|
+
# Use within an LLM
|
|
350
|
+
from langchain_core.chat_models import ChatAnthropic
|
|
351
|
+
|
|
352
|
+
chain = final_prompt | ChatAnthropic(model="claude-3-haiku-20240307")
|
|
353
|
+
chain.invoke({"input": "What's 3+3?"})
|
|
354
|
+
```
|
|
362
355
|
"""
|
|
363
356
|
|
|
364
357
|
input_variables: list[str] = Field(default_factory=list)
|
|
365
358
|
"""A list of the names of the variables the prompt template will use
|
|
366
359
|
to pass to the example_selector, if provided."""
|
|
367
360
|
|
|
368
|
-
example_prompt:
|
|
361
|
+
example_prompt: BaseMessagePromptTemplate | BaseChatPromptTemplate
|
|
369
362
|
"""The class to format each example."""
|
|
370
363
|
|
|
371
364
|
@classmethod
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Prompt template that contains few shot examples."""
|
|
2
2
|
|
|
3
3
|
from pathlib import Path
|
|
4
|
-
from typing import Any
|
|
4
|
+
from typing import Any
|
|
5
5
|
|
|
6
6
|
from pydantic import ConfigDict, model_validator
|
|
7
7
|
from typing_extensions import Self
|
|
@@ -17,7 +17,7 @@ from langchain_core.prompts.string import (
|
|
|
17
17
|
class FewShotPromptWithTemplates(StringPromptTemplate):
|
|
18
18
|
"""Prompt template that contains few shot examples."""
|
|
19
19
|
|
|
20
|
-
examples:
|
|
20
|
+
examples: list[dict] | None = None
|
|
21
21
|
"""Examples to format into the prompt.
|
|
22
22
|
Either this or example_selector should be provided."""
|
|
23
23
|
|
|
@@ -34,7 +34,7 @@ class FewShotPromptWithTemplates(StringPromptTemplate):
|
|
|
34
34
|
example_separator: str = "\n\n"
|
|
35
35
|
"""String separator used to join the prefix, the examples, and suffix."""
|
|
36
36
|
|
|
37
|
-
prefix:
|
|
37
|
+
prefix: StringPromptTemplate | None = None
|
|
38
38
|
"""A PromptTemplate to put before the examples."""
|
|
39
39
|
|
|
40
40
|
template_format: PromptTemplateFormat = "f-string"
|
|
@@ -46,10 +46,10 @@ class FewShotPromptWithTemplates(StringPromptTemplate):
|
|
|
46
46
|
|
|
47
47
|
@classmethod
|
|
48
48
|
def get_lc_namespace(cls) -> list[str]:
|
|
49
|
-
"""Get the namespace of the
|
|
49
|
+
"""Get the namespace of the LangChain object.
|
|
50
50
|
|
|
51
51
|
Returns:
|
|
52
|
-
|
|
52
|
+
`["langchain", "prompts", "few_shot_with_templates"]`
|
|
53
53
|
"""
|
|
54
54
|
return ["langchain", "prompts", "few_shot_with_templates"]
|
|
55
55
|
|
|
@@ -116,17 +116,15 @@ class FewShotPromptWithTemplates(StringPromptTemplate):
|
|
|
116
116
|
"""Format the prompt with the inputs.
|
|
117
117
|
|
|
118
118
|
Args:
|
|
119
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
119
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
120
120
|
|
|
121
121
|
Returns:
|
|
122
122
|
A formatted string.
|
|
123
123
|
|
|
124
124
|
Example:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
prompt.format(variable1="foo")
|
|
129
|
-
|
|
125
|
+
```python
|
|
126
|
+
prompt.format(variable1="foo")
|
|
127
|
+
```
|
|
130
128
|
"""
|
|
131
129
|
kwargs = self._merge_partial_and_user_variables(**kwargs)
|
|
132
130
|
# Get the examples to use.
|
|
@@ -165,7 +163,7 @@ class FewShotPromptWithTemplates(StringPromptTemplate):
|
|
|
165
163
|
"""Async format the prompt with the inputs.
|
|
166
164
|
|
|
167
165
|
Args:
|
|
168
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
166
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
169
167
|
|
|
170
168
|
Returns:
|
|
171
169
|
A formatted string.
|
|
@@ -210,7 +208,7 @@ class FewShotPromptWithTemplates(StringPromptTemplate):
|
|
|
210
208
|
"""Return the prompt type key."""
|
|
211
209
|
return "few_shot_with_templates"
|
|
212
210
|
|
|
213
|
-
def save(self, file_path:
|
|
211
|
+
def save(self, file_path: Path | str) -> None:
|
|
214
212
|
"""Save the prompt to a file.
|
|
215
213
|
|
|
216
214
|
Args:
|
langchain_core/prompts/image.py
CHANGED
|
@@ -26,8 +26,8 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
26
26
|
"""Create an image prompt template.
|
|
27
27
|
|
|
28
28
|
Raises:
|
|
29
|
-
ValueError: If the input variables contain
|
|
30
|
-
|
|
29
|
+
ValueError: If the input variables contain `'url'`, `'path'`, or
|
|
30
|
+
`'detail'`.
|
|
31
31
|
"""
|
|
32
32
|
if "input_variables" not in kwargs:
|
|
33
33
|
kwargs["input_variables"] = []
|
|
@@ -49,10 +49,10 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
49
49
|
|
|
50
50
|
@classmethod
|
|
51
51
|
def get_lc_namespace(cls) -> list[str]:
|
|
52
|
-
"""Get the namespace of the
|
|
52
|
+
"""Get the namespace of the LangChain object.
|
|
53
53
|
|
|
54
54
|
Returns:
|
|
55
|
-
|
|
55
|
+
`["langchain", "prompts", "image"]`
|
|
56
56
|
"""
|
|
57
57
|
return ["langchain", "prompts", "image"]
|
|
58
58
|
|
|
@@ -60,7 +60,7 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
60
60
|
"""Format the prompt with the inputs.
|
|
61
61
|
|
|
62
62
|
Args:
|
|
63
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
63
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
64
64
|
|
|
65
65
|
Returns:
|
|
66
66
|
A formatted string.
|
|
@@ -71,7 +71,7 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
71
71
|
"""Async format the prompt with the inputs.
|
|
72
72
|
|
|
73
73
|
Args:
|
|
74
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
74
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
75
75
|
|
|
76
76
|
Returns:
|
|
77
77
|
A formatted string.
|
|
@@ -85,7 +85,7 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
85
85
|
"""Format the prompt with the inputs.
|
|
86
86
|
|
|
87
87
|
Args:
|
|
88
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
88
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
89
89
|
|
|
90
90
|
Returns:
|
|
91
91
|
A formatted string.
|
|
@@ -93,14 +93,12 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
93
93
|
Raises:
|
|
94
94
|
ValueError: If the url is not provided.
|
|
95
95
|
ValueError: If the url is not a string.
|
|
96
|
-
ValueError: If
|
|
96
|
+
ValueError: If `'path'` is provided in the template or kwargs.
|
|
97
97
|
|
|
98
98
|
Example:
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
prompt.format(variable1="foo")
|
|
103
|
-
|
|
99
|
+
```python
|
|
100
|
+
prompt.format(variable1="foo")
|
|
101
|
+
```
|
|
104
102
|
"""
|
|
105
103
|
formatted = {}
|
|
106
104
|
for k, v in self.template.items():
|
|
@@ -134,7 +132,7 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|
|
134
132
|
"""Async format the prompt with the inputs.
|
|
135
133
|
|
|
136
134
|
Args:
|
|
137
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
135
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
138
136
|
|
|
139
137
|
Returns:
|
|
140
138
|
A formatted string.
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
4
|
import logging
|
|
5
|
+
from collections.abc import Callable
|
|
5
6
|
from pathlib import Path
|
|
6
|
-
from typing import Callable, Optional, Union
|
|
7
7
|
|
|
8
8
|
import yaml
|
|
9
9
|
|
|
@@ -134,14 +134,12 @@ def _load_prompt(config: dict) -> PromptTemplate:
|
|
|
134
134
|
return PromptTemplate(**config)
|
|
135
135
|
|
|
136
136
|
|
|
137
|
-
def load_prompt(
|
|
138
|
-
path: Union[str, Path], encoding: Optional[str] = None
|
|
139
|
-
) -> BasePromptTemplate:
|
|
137
|
+
def load_prompt(path: str | Path, encoding: str | None = None) -> BasePromptTemplate:
|
|
140
138
|
"""Unified method for loading a prompt from LangChainHub or local fs.
|
|
141
139
|
|
|
142
140
|
Args:
|
|
143
141
|
path: Path to the prompt file.
|
|
144
|
-
encoding: Encoding of the file.
|
|
142
|
+
encoding: Encoding of the file.
|
|
145
143
|
|
|
146
144
|
Returns:
|
|
147
145
|
A PromptTemplate object.
|
|
@@ -160,7 +158,7 @@ def load_prompt(
|
|
|
160
158
|
|
|
161
159
|
|
|
162
160
|
def _load_prompt_from_file(
|
|
163
|
-
file:
|
|
161
|
+
file: str | Path, encoding: str | None = None
|
|
164
162
|
) -> BasePromptTemplate:
|
|
165
163
|
"""Load prompt from file."""
|
|
166
164
|
# Convert file to a Path object.
|
|
@@ -23,10 +23,10 @@ class BaseMessagePromptTemplate(Serializable, ABC):
|
|
|
23
23
|
|
|
24
24
|
@classmethod
|
|
25
25
|
def get_lc_namespace(cls) -> list[str]:
|
|
26
|
-
"""Get the namespace of the
|
|
26
|
+
"""Get the namespace of the LangChain object.
|
|
27
27
|
|
|
28
28
|
Returns:
|
|
29
|
-
|
|
29
|
+
`["langchain", "prompts", "chat"]`
|
|
30
30
|
"""
|
|
31
31
|
return ["langchain", "prompts", "chat"]
|
|
32
32
|
|
|
@@ -68,7 +68,7 @@ class BaseMessagePromptTemplate(Serializable, ABC):
|
|
|
68
68
|
"""Human-readable representation.
|
|
69
69
|
|
|
70
70
|
Args:
|
|
71
|
-
html: Whether to format as HTML.
|
|
71
|
+
html: Whether to format as HTML.
|
|
72
72
|
|
|
73
73
|
Returns:
|
|
74
74
|
Human-readable representation.
|
langchain_core/prompts/prompt.py
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import warnings
|
|
6
5
|
from pathlib import Path
|
|
7
|
-
from typing import TYPE_CHECKING, Any
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
8
7
|
|
|
9
8
|
from pydantic import BaseModel, model_validator
|
|
10
9
|
from typing_extensions import override
|
|
@@ -45,18 +44,16 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
45
44
|
from untrusted sources.
|
|
46
45
|
|
|
47
46
|
Example:
|
|
47
|
+
```python
|
|
48
|
+
from langchain_core.prompts import PromptTemplate
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
# Instantiation using from_template (recommended)
|
|
54
|
-
prompt = PromptTemplate.from_template("Say {foo}")
|
|
55
|
-
prompt.format(foo="bar")
|
|
56
|
-
|
|
57
|
-
# Instantiation using initializer
|
|
58
|
-
prompt = PromptTemplate(template="Say {foo}")
|
|
50
|
+
# Instantiation using from_template (recommended)
|
|
51
|
+
prompt = PromptTemplate.from_template("Say {foo}")
|
|
52
|
+
prompt.format(foo="bar")
|
|
59
53
|
|
|
54
|
+
# Instantiation using initializer
|
|
55
|
+
prompt = PromptTemplate(template="Say {foo}")
|
|
56
|
+
```
|
|
60
57
|
"""
|
|
61
58
|
|
|
62
59
|
@property
|
|
@@ -69,10 +66,10 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
69
66
|
@classmethod
|
|
70
67
|
@override
|
|
71
68
|
def get_lc_namespace(cls) -> list[str]:
|
|
72
|
-
"""Get the namespace of the
|
|
69
|
+
"""Get the namespace of the LangChain object.
|
|
73
70
|
|
|
74
71
|
Returns:
|
|
75
|
-
|
|
72
|
+
`["langchain", "prompts", "prompt"]`
|
|
76
73
|
"""
|
|
77
74
|
return ["langchain", "prompts", "prompt"]
|
|
78
75
|
|
|
@@ -145,10 +142,10 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
145
142
|
Raises:
|
|
146
143
|
ValueError: If the template formats are not f-string or if there are
|
|
147
144
|
conflicting partial variables.
|
|
148
|
-
NotImplementedError: If the other object is not a
|
|
145
|
+
NotImplementedError: If the other object is not a `PromptTemplate` or str.
|
|
149
146
|
|
|
150
147
|
Returns:
|
|
151
|
-
A new
|
|
148
|
+
A new `PromptTemplate` that is the combination of the two.
|
|
152
149
|
"""
|
|
153
150
|
# Allow for easy combining
|
|
154
151
|
if isinstance(other, PromptTemplate):
|
|
@@ -192,7 +189,7 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
192
189
|
"""Format the prompt with the inputs.
|
|
193
190
|
|
|
194
191
|
Args:
|
|
195
|
-
kwargs: Any arguments to be passed to the prompt template.
|
|
192
|
+
**kwargs: Any arguments to be passed to the prompt template.
|
|
196
193
|
|
|
197
194
|
Returns:
|
|
198
195
|
A formatted string.
|
|
@@ -223,7 +220,7 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
223
220
|
example_separator: The separator to use in between examples. Defaults
|
|
224
221
|
to two new line characters.
|
|
225
222
|
prefix: String that should go before any examples. Generally includes
|
|
226
|
-
examples.
|
|
223
|
+
examples.
|
|
227
224
|
|
|
228
225
|
Returns:
|
|
229
226
|
The final prompt generated.
|
|
@@ -234,32 +231,21 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
234
231
|
@classmethod
|
|
235
232
|
def from_file(
|
|
236
233
|
cls,
|
|
237
|
-
template_file:
|
|
238
|
-
|
|
239
|
-
encoding: Optional[str] = None,
|
|
234
|
+
template_file: str | Path,
|
|
235
|
+
encoding: str | None = None,
|
|
240
236
|
**kwargs: Any,
|
|
241
237
|
) -> PromptTemplate:
|
|
242
238
|
"""Load a prompt from a file.
|
|
243
239
|
|
|
244
240
|
Args:
|
|
245
241
|
template_file: The path to the file containing the prompt template.
|
|
246
|
-
input_variables: [DEPRECATED] A list of variable names the final prompt
|
|
247
|
-
template will expect. Defaults to None.
|
|
248
242
|
encoding: The encoding system for opening the template file.
|
|
249
243
|
If not provided, will use the OS default.
|
|
250
244
|
|
|
251
|
-
input_variables is ignored as from_file now delegates to from_template().
|
|
252
|
-
|
|
253
245
|
Returns:
|
|
254
246
|
The prompt loaded from the file.
|
|
255
247
|
"""
|
|
256
248
|
template = Path(template_file).read_text(encoding=encoding)
|
|
257
|
-
if input_variables:
|
|
258
|
-
warnings.warn(
|
|
259
|
-
"`input_variables' is deprecated and ignored.",
|
|
260
|
-
DeprecationWarning,
|
|
261
|
-
stacklevel=2,
|
|
262
|
-
)
|
|
263
249
|
return cls.from_template(template=template, **kwargs)
|
|
264
250
|
|
|
265
251
|
@classmethod
|
|
@@ -268,7 +254,7 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
268
254
|
template: str,
|
|
269
255
|
*,
|
|
270
256
|
template_format: PromptTemplateFormat = "f-string",
|
|
271
|
-
partial_variables:
|
|
257
|
+
partial_variables: dict[str, Any] | None = None,
|
|
272
258
|
**kwargs: Any,
|
|
273
259
|
) -> PromptTemplate:
|
|
274
260
|
"""Load a prompt template from a template.
|
|
@@ -289,14 +275,13 @@ class PromptTemplate(StringPromptTemplate):
|
|
|
289
275
|
Args:
|
|
290
276
|
template: The template to load.
|
|
291
277
|
template_format: The format of the template. Use `jinja2` for jinja2,
|
|
292
|
-
|
|
293
|
-
Defaults to `f-string`.
|
|
278
|
+
`mustache` for mustache, and `f-string` for f-strings.
|
|
294
279
|
partial_variables: A dictionary of variables that can be used to partially
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
kwargs: Any other arguments to pass to the prompt template.
|
|
280
|
+
fill in the template. For example, if the template is
|
|
281
|
+
`"{variable1} {variable2}"`, and `partial_variables` is
|
|
282
|
+
`{"variable1": "foo"}`, then the final prompt will be
|
|
283
|
+
`"foo {variable2}"`.
|
|
284
|
+
**kwargs: Any other arguments to pass to the prompt template.
|
|
300
285
|
|
|
301
286
|
Returns:
|
|
302
287
|
The prompt template loaded from the template.
|