mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-09 16:36:23 +00:00
Fix loading of ImagePromptTemplate (#16868)
We didn't override the namespace of the ImagePromptTemplate, so it is listed as being in langchain.schema This updates the mapping to let the loader deserialize. Alternatively, we could make a slight breaking change and update the namespace of the ImagePromptTemplate since we haven't broadly publicized/documented it yet..
This commit is contained in:
parent
6fc2835255
commit
131c043864
@ -115,6 +115,12 @@ SERIALIZABLE_MAPPING: Dict[Tuple[str, ...], Tuple[str, ...]] = {
|
|||||||
"chat",
|
"chat",
|
||||||
"SystemMessagePromptTemplate",
|
"SystemMessagePromptTemplate",
|
||||||
),
|
),
|
||||||
|
("langchain", "prompts", "image", "ImagePromptTemplate"): (
|
||||||
|
"langchain_core",
|
||||||
|
"prompts",
|
||||||
|
"image",
|
||||||
|
"ImagePromptTemplate",
|
||||||
|
),
|
||||||
("langchain", "schema", "agent", "AgentActionMessageLog"): (
|
("langchain", "schema", "agent", "AgentActionMessageLog"): (
|
||||||
"langchain_core",
|
"langchain_core",
|
||||||
"agents",
|
"agents",
|
||||||
@ -510,6 +516,12 @@ _OG_SERIALIZABLE_MAPPING: Dict[Tuple[str, ...], Tuple[str, ...]] = {
|
|||||||
"system",
|
"system",
|
||||||
"SystemMessage",
|
"SystemMessage",
|
||||||
),
|
),
|
||||||
|
("langchain", "schema", "prompt_template", "ImagePromptTemplate"): (
|
||||||
|
"langchain_core",
|
||||||
|
"prompts",
|
||||||
|
"image",
|
||||||
|
"ImagePromptTemplate",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Needed for backwards compatibility for a few versions where we serialized
|
# Needed for backwards compatibility for a few versions where we serialized
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from typing import Any
|
from typing import Any, List
|
||||||
|
|
||||||
from langchain_core.prompt_values import ImagePromptValue, ImageURL, PromptValue
|
from langchain_core.prompt_values import ImagePromptValue, ImageURL, PromptValue
|
||||||
from langchain_core.prompts.base import BasePromptTemplate
|
from langchain_core.prompts.base import BasePromptTemplate
|
||||||
@ -30,6 +30,11 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]):
|
|||||||
"""Return the prompt type key."""
|
"""Return the prompt type key."""
|
||||||
return "image-prompt"
|
return "image-prompt"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_lc_namespace(cls) -> List[str]:
|
||||||
|
"""Get the namespace of the langchain object."""
|
||||||
|
return ["langchain", "prompts", "image"]
|
||||||
|
|
||||||
def format_prompt(self, **kwargs: Any) -> PromptValue:
|
def format_prompt(self, **kwargs: Any) -> PromptValue:
|
||||||
"""Create Chat Messages."""
|
"""Create Chat Messages."""
|
||||||
return ImagePromptValue(image_url=self.format(**kwargs))
|
return ImagePromptValue(image_url=self.format(**kwargs))
|
||||||
|
109
libs/core/tests/unit_tests/prompts/test_image.py
Normal file
109
libs/core/tests/unit_tests/prompts/test_image.py
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from langchain_core.load import dump, loads
|
||||||
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
|
|
||||||
|
|
||||||
|
def test_image_prompt_template_deserializable() -> None:
|
||||||
|
"""Test that the image prompt template is serializable."""
|
||||||
|
loads(
|
||||||
|
dump.dumps(
|
||||||
|
ChatPromptTemplate.from_messages(
|
||||||
|
[("system", [{"type": "image", "image_url": "{img}"}])]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_image_prompt_template_deserializable_old() -> None:
|
||||||
|
"""Test that the image prompt template is serializable."""
|
||||||
|
loads(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"lc": 1,
|
||||||
|
"type": "constructor",
|
||||||
|
"id": ["langchain", "prompts", "chat", "ChatPromptTemplate"],
|
||||||
|
"kwargs": {
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"lc": 1,
|
||||||
|
"type": "constructor",
|
||||||
|
"id": [
|
||||||
|
"langchain",
|
||||||
|
"prompts",
|
||||||
|
"chat",
|
||||||
|
"SystemMessagePromptTemplate",
|
||||||
|
],
|
||||||
|
"kwargs": {
|
||||||
|
"prompt": [
|
||||||
|
{
|
||||||
|
"lc": 1,
|
||||||
|
"type": "constructor",
|
||||||
|
"id": [
|
||||||
|
"langchain",
|
||||||
|
"prompts",
|
||||||
|
"prompt",
|
||||||
|
"PromptTemplate",
|
||||||
|
],
|
||||||
|
"kwargs": {
|
||||||
|
"template": "Foo",
|
||||||
|
"input_variables": [],
|
||||||
|
"template_format": "f-string",
|
||||||
|
"partial_variables": {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lc": 1,
|
||||||
|
"type": "constructor",
|
||||||
|
"id": [
|
||||||
|
"langchain",
|
||||||
|
"prompts",
|
||||||
|
"chat",
|
||||||
|
"HumanMessagePromptTemplate",
|
||||||
|
],
|
||||||
|
"kwargs": {
|
||||||
|
"prompt": [
|
||||||
|
{
|
||||||
|
"lc": 1,
|
||||||
|
"type": "constructor",
|
||||||
|
"id": [
|
||||||
|
"langchain",
|
||||||
|
"prompts",
|
||||||
|
"image",
|
||||||
|
"ImagePromptTemplate",
|
||||||
|
],
|
||||||
|
"kwargs": {
|
||||||
|
"template": {
|
||||||
|
"url": "data:image/png;base64,{img}"
|
||||||
|
},
|
||||||
|
"input_variables": ["img"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"lc": 1,
|
||||||
|
"type": "constructor",
|
||||||
|
"id": [
|
||||||
|
"langchain",
|
||||||
|
"prompts",
|
||||||
|
"prompt",
|
||||||
|
"PromptTemplate",
|
||||||
|
],
|
||||||
|
"kwargs": {
|
||||||
|
"template": "{input}",
|
||||||
|
"input_variables": ["input"],
|
||||||
|
"template_format": "f-string",
|
||||||
|
"partial_variables": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"input_variables": ["img", "input"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
@ -40,8 +40,21 @@ def import_all_modules(package_name: str) -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def test_serializable_mapping() -> None:
|
def test_serializable_mapping() -> None:
|
||||||
|
# This should have had a different namespace, as it was never
|
||||||
|
# exported from the langchain module, but we keep for whoever has
|
||||||
|
# already serialized it.
|
||||||
|
to_skip = {
|
||||||
|
("langchain", "prompts", "image", "ImagePromptTemplate"): (
|
||||||
|
"langchain_core",
|
||||||
|
"prompts",
|
||||||
|
"image",
|
||||||
|
"ImagePromptTemplate",
|
||||||
|
),
|
||||||
|
}
|
||||||
serializable_modules = import_all_modules("langchain")
|
serializable_modules = import_all_modules("langchain")
|
||||||
missing = set(SERIALIZABLE_MAPPING).difference(serializable_modules)
|
missing = set(SERIALIZABLE_MAPPING).difference(
|
||||||
|
set(serializable_modules).union(to_skip)
|
||||||
|
)
|
||||||
assert missing == set()
|
assert missing == set()
|
||||||
extra = set(serializable_modules).difference(SERIALIZABLE_MAPPING)
|
extra = set(serializable_modules).difference(SERIALIZABLE_MAPPING)
|
||||||
assert extra == set()
|
assert extra == set()
|
||||||
|
Loading…
Reference in New Issue
Block a user