Skip to content

Commit cc70565

Browse files
authored
add prompt type (#730)
1 parent 374e510 commit cc70565

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

langchain/prompts/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ def format(self, **kwargs: Any) -> str:
135135
prompt.format(variable1="foo")
136136
"""
137137

138+
@property
139+
@abstractmethod
140+
def _prompt_type(self) -> str:
141+
"""Return the prompt type key."""
142+
143+
def dict(self, **kwargs: Any) -> Dict:
144+
"""Return dictionary representation of prompt."""
145+
prompt_dict = super().dict()
146+
prompt_dict["_type"] = self._prompt_type
147+
return prompt_dict
148+
138149
def save(self, file_path: Union[Path, str]) -> None:
139150
"""Save the prompt.
140151

langchain/prompts/few_shot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ def format(self, **kwargs: Any) -> str:
109109
# Format the template with the input variables.
110110
return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)
111111

112+
@property
113+
def _prompt_type(self) -> str:
114+
"""Return the prompt type key."""
115+
return "few_shot"
116+
112117
def dict(self, **kwargs: Any) -> Dict:
113118
"""Return a dictionary of the prompt."""
114119
if self.example_selector:
115120
raise ValueError("Saving an example selector is not currently supported")
116-
117-
prompt_dict = super().dict()
118-
prompt_dict["_type"] = "few_shot"
119-
return prompt_dict
121+
return super().dict(**kwargs)

langchain/prompts/loading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _load_few_shot_prompt(config: dict) -> FewShotPromptTemplate:
8383
)
8484
config["example_prompt"] = load_prompt(config.pop("example_prompt_path"))
8585
else:
86-
config["example_prompt"] = _load_prompt(config["example_prompt"])
86+
config["example_prompt"] = load_prompt_from_config(config["example_prompt"])
8787
# Load the examples.
8888
config = _load_examples(config)
8989
return FewShotPromptTemplate(**config)

langchain/prompts/prompt.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class PromptTemplate(BasePromptTemplate, BaseModel):
3131
template_format: str = "f-string"
3232
"""The format of the prompt template. Options are: 'f-string', 'jinja2'."""
3333

34+
@property
35+
def _prompt_type(self) -> str:
36+
"""Return the prompt type key."""
37+
return "prompt"
38+
3439
class Config:
3540
"""Configuration for this pydantic object."""
3641

0 commit comments

Comments
 (0)