Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: update load chat prompt template multi mesages #27584

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions libs/core/langchain_core/prompts/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,21 @@ def _load_prompt_from_file(
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")
template = config.pop("template")

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

return ChatPromptTemplate.from_template(template=template, **config)
messages = []
if isinstance(template, str):
messages.append(("human", template))

elif isinstance(template, list):
for item in template:
messages.append((item["role"], item["content"]))

return ChatPromptTemplate(messages=messages, **config)


type_to_loader_dict: dict[str, Callable[[dict], BasePromptTemplate]] = {
Expand Down
19 changes: 19 additions & 0 deletions libs/core/tests/unit_tests/examples/simple_chat_prompt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"_type": "chat",
"input_variables": [
"adjective"
],
"partial_variables": {
"content": "dogs"
},
"template": [
{
"role": "system",
"content": "You are a comedian"
},
{
"role": "human",
"content": "Tell me a {adjective} joke about {content}."
}
]
}
10 changes: 10 additions & 0 deletions libs/core/tests/unit_tests/examples/simple_chat_prompt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_type: chat
input_variables:
["adjective"]
partial_variables:
content: dogs
template:
- role: system
content: "You are a comedian"
- role: human
content: "Tell me a {adjective} joke about {content}."
29 changes: 29 additions & 0 deletions libs/core/tests/unit_tests/prompts/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest

from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts.loading import load_prompt
from langchain_core.prompts.prompt import PromptTemplate
Expand Down Expand Up @@ -36,6 +37,20 @@ def test_loading_from_yaml() -> None:
assert prompt == expected_prompt


def test_loading_chat_from_yaml() -> None:
"""Test loading from yaml file."""
prompt = load_prompt(EXAMPLE_DIR / "simple_chat_prompt.yaml")
expected_prompt = ChatPromptTemplate(
input_variables=["adjective"],
partial_variables={"content": "dogs"},
messages=[
("system", "You are a comedian"),
("human", "Tell me a {adjective} joke about {content}."),
],
)
assert prompt == expected_prompt


def test_loading_from_json() -> None:
"""Test loading from json file."""
prompt = load_prompt(EXAMPLE_DIR / "simple_prompt.json")
Expand All @@ -46,6 +61,20 @@ def test_loading_from_json() -> None:
assert prompt == expected_prompt


def test_loading_chat_from_json() -> None:
"""Test loading from json file."""
prompt = load_prompt(EXAMPLE_DIR / "simple_chat_prompt.json")
expected_prompt = ChatPromptTemplate(
input_variables=["adjective"],
partial_variables={"content": "dogs"},
messages=[
("system", "You are a comedian"),
("human", "Tell me a {adjective} joke about {content}."),
],
)
assert prompt == expected_prompt


def test_loading_jinja_from_json() -> None:
"""Test that loading jinja2 format prompts from JSON raises ValueError."""
prompt_path = EXAMPLE_DIR / "jinja_injection_prompt.json"
Expand Down
Loading