mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-22 14:49:29 +00:00
add prompt type (#730)
This commit is contained in:
parent
374e510f94
commit
cc70565886
@ -135,6 +135,17 @@ class BasePromptTemplate(BaseModel, ABC):
|
|||||||
prompt.format(variable1="foo")
|
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:
|
def save(self, file_path: Union[Path, str]) -> None:
|
||||||
"""Save the prompt.
|
"""Save the prompt.
|
||||||
|
|
||||||
|
@ -109,11 +109,13 @@ class FewShotPromptTemplate(BasePromptTemplate, BaseModel):
|
|||||||
# Format the template with the input variables.
|
# Format the template with the input variables.
|
||||||
return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)
|
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:
|
def dict(self, **kwargs: Any) -> Dict:
|
||||||
"""Return a dictionary of the prompt."""
|
"""Return a dictionary of the prompt."""
|
||||||
if self.example_selector:
|
if self.example_selector:
|
||||||
raise ValueError("Saving an example selector is not currently supported")
|
raise ValueError("Saving an example selector is not currently supported")
|
||||||
|
return super().dict(**kwargs)
|
||||||
prompt_dict = super().dict()
|
|
||||||
prompt_dict["_type"] = "few_shot"
|
|
||||||
return prompt_dict
|
|
||||||
|
@ -83,7 +83,7 @@ def _load_few_shot_prompt(config: dict) -> FewShotPromptTemplate:
|
|||||||
)
|
)
|
||||||
config["example_prompt"] = load_prompt(config.pop("example_prompt_path"))
|
config["example_prompt"] = load_prompt(config.pop("example_prompt_path"))
|
||||||
else:
|
else:
|
||||||
config["example_prompt"] = _load_prompt(config["example_prompt"])
|
config["example_prompt"] = load_prompt_from_config(config["example_prompt"])
|
||||||
# Load the examples.
|
# Load the examples.
|
||||||
config = _load_examples(config)
|
config = _load_examples(config)
|
||||||
return FewShotPromptTemplate(**config)
|
return FewShotPromptTemplate(**config)
|
||||||
|
@ -31,6 +31,11 @@ class PromptTemplate(BasePromptTemplate, BaseModel):
|
|||||||
template_format: str = "f-string"
|
template_format: str = "f-string"
|
||||||
"""The format of the prompt template. Options are: 'f-string', 'jinja2'."""
|
"""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:
|
class Config:
|
||||||
"""Configuration for this pydantic object."""
|
"""Configuration for this pydantic object."""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user