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: Add encoding config to json for load_prompt() #26935

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion libs/core/langchain_core/prompts/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ def _load_template(var_name: str, config: dict) -> dict:
)
# Pop the template path from the config.
template_path = Path(config.pop(f"{var_name}_path"))
if f"{var_name}_encoding" in config:
encoding = config.pop(f"{var_name}_encoding")
else:
encoding = None
# Load the template.
if template_path.suffix == ".txt":
with open(template_path) as f:
with open(template_path, encoding=encoding) as f:
template = f.read()
else:
raise ValueError
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_type": "prompt",
"input_variables": ["adjective", "content"],
"template_path": "simple_template_cp932.txt",
"template_encoding": "cp932"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_type": "prompt",
"input_variables": ["adjective", "content"],
"template_path": "simple_template_utf8.txt",
"template_encoding": "utf-8"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tell me a {adjective} joke about {content}. SJIS���{��e�X�g
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tell me a {adjective} joke about {content}. AÁĂȀ🐌😁
22 changes: 22 additions & 0 deletions libs/core/tests/unit_tests/prompts/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ def test_loading_with_template_as_file() -> None:
assert prompt == expected_prompt


def test_loading_from_json_utf8() -> None:
"""Test loading when the template is a file."""
with change_directory(EXAMPLE_DIR):
prompt = load_prompt("simple_prompt_with_template_file_utf8.json")
expected_prompt = PromptTemplate(
input_variables=["adjective", "content"],
template="Tell me a {adjective} joke about {content}. AÁĂȀ🐌😁",
)
assert prompt == expected_prompt


def test_loading_from_json_cp932() -> None:
"""Test loading when the template is a file."""
with change_directory(EXAMPLE_DIR):
prompt = load_prompt("simple_prompt_with_template_file_cp932.json")
expected_prompt = PromptTemplate(
input_variables=["adjective", "content"],
template="Tell me a {adjective} joke about {content}. SJIS日本語テスト",
)
assert prompt == expected_prompt


def test_loading_few_shot_prompt_from_yaml() -> None:
"""Test loading few shot prompt from yaml."""
with change_directory(EXAMPLE_DIR):
Expand Down