MCPcopy
hub / github.com/langchain-ai/langchain / PromptTemplate

Class PromptTemplate

libs/core/langchain_core/prompts/prompt.py:19–275  ·  view source on GitHub ↗

Prompt template for a language model. A prompt template consists of a string template. It accepts a set of parameters from the user that can be used to generate a prompt for a language model. The template can be formatted using either f-strings (default) or jinja2 syntax. *Securit

Source from the content-addressed store, hash-verified

17
18
19class PromptTemplate(StringPromptTemplate):
20 """Prompt template for a language model.
21
22 A prompt template consists of a string template. It accepts a set of parameters
23 from the user that can be used to generate a prompt for a language model.
24
25 The template can be formatted using either f-strings (default) or jinja2 syntax.
26
27 *Security warning*: Prefer using `template_format="f-string"` instead of
28 `template_format="jinja2"`, or make sure to NEVER accept jinja2 templates
29 from untrusted sources as they may lead to arbitrary Python code execution.
30
31 As of LangChain 0.0.329, Jinja2 templates will be rendered using
32 Jinja2's SandboxedEnvironment by default. This sand-boxing should
33 be treated as a best-effort approach rather than a guarantee of security,
34 as it is an opt-out rather than opt-in approach.
35
36 Despite the sand-boxing, we recommend to never use jinja2 templates
37 from untrusted sources.
38
39 Example:
40
41 .. code-block:: python
42
43 from langchain_core.prompts import PromptTemplate
44
45 # Instantiation using from_template (recommended)
46 prompt = PromptTemplate.from_template("Say {foo}")
47 prompt.format(foo="bar")
48
49 # Instantiation using initializer
50 prompt = PromptTemplate(template="Say {foo}")
51 """
52
53 @property
54 def lc_attributes(self) -> Dict[str, Any]:
55 return {
56 "template_format": self.template_format,
57 }
58
59 @classmethod
60 def get_lc_namespace(cls) -> List[str]:
61 """Get the namespace of the langchain object."""
62 return ["langchain", "prompts", "prompt"]
63
64 input_variables: List[str]
65 """A list of the names of the variables the prompt template expects."""
66
67 template: str
68 """The prompt template."""
69
70 template_format: Literal["f-string", "mustache", "jinja2"] = "f-string"
71 """The format of the prompt template.
72 Options are: 'f-string', 'mustache', 'jinja2'."""
73
74 validate_template: bool = False
75 """Whether or not to try validating the template."""
76

Callers 15

chain.pyFile · 0.90
chain.pyFile · 0.90
blurb_matcher.pyFile · 0.90
selectorFunction · 0.90
messagesFunction · 0.90
test_prompt_validFunction · 0.90

Calls

no outgoing calls