diff --git a/langchain/prompts/base.py b/langchain/prompts/base.py index cf99215e75..6b1ef32421 100644 --- a/langchain/prompts/base.py +++ b/langchain/prompts/base.py @@ -135,6 +135,17 @@ class BasePromptTemplate(BaseModel, ABC): prompt.format(variable1="foo") """ + @property + @abstractmethod + def _prompt_type(self) -> str: + """Return the prompt type key.""" + + def dict(self, **kwargs: Any) -> Dict: + """Return dictionary representation of prompt.""" + prompt_dict = super().dict() + prompt_dict["_type"] = self._prompt_type + return prompt_dict + def save(self, file_path: Union[Path, str]) -> None: """Save the prompt. diff --git a/langchain/prompts/few_shot.py b/langchain/prompts/few_shot.py index 0119132d35..71b4df3798 100644 --- a/langchain/prompts/few_shot.py +++ b/langchain/prompts/few_shot.py @@ -109,11 +109,13 @@ class FewShotPromptTemplate(BasePromptTemplate, BaseModel): # Format the template with the input variables. return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs) + @property + def _prompt_type(self) -> str: + """Return the prompt type key.""" + return "few_shot" + def dict(self, **kwargs: Any) -> Dict: """Return a dictionary of the prompt.""" if self.example_selector: raise ValueError("Saving an example selector is not currently supported") - - prompt_dict = super().dict() - prompt_dict["_type"] = "few_shot" - return prompt_dict + return super().dict(**kwargs) diff --git a/langchain/prompts/loading.py b/langchain/prompts/loading.py index dfd7c3adec..651525ee0a 100644 --- a/langchain/prompts/loading.py +++ b/langchain/prompts/loading.py @@ -83,7 +83,7 @@ def _load_few_shot_prompt(config: dict) -> FewShotPromptTemplate: ) config["example_prompt"] = load_prompt(config.pop("example_prompt_path")) else: - config["example_prompt"] = _load_prompt(config["example_prompt"]) + config["example_prompt"] = load_prompt_from_config(config["example_prompt"]) # Load the examples. config = _load_examples(config) return FewShotPromptTemplate(**config) diff --git a/langchain/prompts/prompt.py b/langchain/prompts/prompt.py index 3e53f46004..fe9a798c5e 100644 --- a/langchain/prompts/prompt.py +++ b/langchain/prompts/prompt.py @@ -31,6 +31,11 @@ class PromptTemplate(BasePromptTemplate, BaseModel): template_format: str = "f-string" """The format of the prompt template. Options are: 'f-string', 'jinja2'.""" + @property + def _prompt_type(self) -> str: + """Return the prompt type key.""" + return "prompt" + class Config: """Configuration for this pydantic object."""