Skip to content

Commit

Permalink
improvement: fix chat prompt loading from config (#13818)
Browse files Browse the repository at this point in the history
Add loader for loading chat prompt from config file.

fixed: #13667

@efriis 
@baskaryan
  • Loading branch information
umair313 committed Nov 27, 2023
1 parent 8a3e0c9 commit b3e08f9
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions libs/core/langchain_core/prompts/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from langchain_core.output_parsers.string import StrOutputParser
from langchain_core.prompts.base import BasePromptTemplate
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.utils import try_load_from_hub
Expand Down Expand Up @@ -154,7 +155,21 @@ def _load_prompt_from_file(file: Union[str, Path]) -> BasePromptTemplate:
return load_prompt_from_config(config)


def _load_chat_prompt(config: Dict) -> ChatPromptTemplate:
"""Load chat prompt from config"""

messages = config.pop("messages")
template = messages[0]["prompt"].pop("template") if messages else None
config.pop("input_variables")

if not template:
raise ValueError("Can't load chat prompt without template")

return ChatPromptTemplate.from_template(template=template, **config)


type_to_loader_dict: Dict[str, Callable[[dict], BasePromptTemplate]] = {
"prompt": _load_prompt,
"few_shot": _load_few_shot_prompt,
"chat": _load_chat_prompt,
}

0 comments on commit b3e08f9

Please sign in to comment.