mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-04 04:07:54 +00:00
add methods to deserialize prompts that were old (#14857)
This commit is contained in:
parent
714bef0cb6
commit
193f107cb5
@ -3,11 +3,16 @@ import json
|
||||
import os
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from langchain_core.load.mapping import SERIALIZABLE_MAPPING
|
||||
from langchain_core.load.mapping import (
|
||||
OLD_PROMPT_TEMPLATE_FORMATS,
|
||||
SERIALIZABLE_MAPPING,
|
||||
)
|
||||
from langchain_core.load.serializable import Serializable
|
||||
|
||||
DEFAULT_NAMESPACES = ["langchain", "langchain_core", "langchain_community"]
|
||||
|
||||
ALL_SERIALIZABLE_MAPPINGS = {**SERIALIZABLE_MAPPING, **OLD_PROMPT_TEMPLATE_FORMATS}
|
||||
|
||||
|
||||
class Reviver:
|
||||
"""Reviver for JSON objects."""
|
||||
@ -67,13 +72,13 @@ class Reviver:
|
||||
if namespace[0] in DEFAULT_NAMESPACES:
|
||||
# Get the importable path
|
||||
key = tuple(namespace + [name])
|
||||
if key not in SERIALIZABLE_MAPPING:
|
||||
if key not in ALL_SERIALIZABLE_MAPPINGS:
|
||||
raise ValueError(
|
||||
"Trying to deserialize something that cannot "
|
||||
"be deserialized in current version of langchain-core: "
|
||||
f"{key}"
|
||||
)
|
||||
import_path = SERIALIZABLE_MAPPING[key]
|
||||
import_path = ALL_SERIALIZABLE_MAPPINGS[key]
|
||||
# Split into module and name
|
||||
import_dir, import_obj = import_path[:-1], import_path[-1]
|
||||
# Import module
|
||||
|
@ -476,3 +476,162 @@ SERIALIZABLE_MAPPING = {
|
||||
"RunnableRetry",
|
||||
),
|
||||
}
|
||||
|
||||
# Needed for backwards compatibility for a few versions where we serialized
|
||||
# with langchain_core
|
||||
OLD_PROMPT_TEMPLATE_FORMATS = {
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"base",
|
||||
"BasePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"base",
|
||||
"BasePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"prompt",
|
||||
"PromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"MessagesPlaceholder",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"MessagesPlaceholder",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"ChatPromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"ChatPromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"HumanMessagePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"HumanMessagePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"SystemMessagePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"SystemMessagePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"BaseMessagePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"BaseMessagePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"BaseChatPromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"BaseChatPromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"ChatMessagePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"ChatMessagePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"few_shot_with_templates",
|
||||
"FewShotPromptWithTemplates",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"few_shot_with_templates",
|
||||
"FewShotPromptWithTemplates",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"pipeline",
|
||||
"PipelinePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"pipeline",
|
||||
"PipelinePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"string",
|
||||
"StringPromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"string",
|
||||
"StringPromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"BaseStringMessagePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"BaseStringMessagePromptTemplate",
|
||||
),
|
||||
(
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"AIMessagePromptTemplate",
|
||||
): (
|
||||
"langchain_core",
|
||||
"prompts",
|
||||
"chat",
|
||||
"AIMessagePromptTemplate",
|
||||
),
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ def test_interfaces() -> None:
|
||||
|
||||
|
||||
def _get_get_session_history(
|
||||
*, store: Optional[Dict[str, Any]] = None
|
||||
*,
|
||||
store: Optional[Dict[str, Any]] = None,
|
||||
) -> Callable[..., ChatMessageHistory]:
|
||||
chat_history_store = store if store is not None else {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user