Serialization
LangChain Python and LangChain JS share a serialization scheme. You can
check if a LangChain class is serializable by running with the
is_lc_serializable
class method.
from langchain.llms import OpenAI
from langchain.llms.loading import load_llm
OpenAI.is_lc_serializable()
True
llm = OpenAI(model="gpt-3.5-turbo-instruct")
Dump
Any serializable object can be serialized to a dict or json string.
from langchain.load import dumpd, dumps
dumpd(llm)
{'lc': 1,
'type': 'constructor',
'id': ['langchain', 'llms', 'openai', 'OpenAI'],
'kwargs': {'model': 'gpt-3.5-turbo-instruct',
'openai_api_key': {'lc': 1, 'type': 'secret', 'id': ['OPENAI_API_KEY']}}}
dumps(llm)
'{"lc": 1, "type": "constructor", "id": ["langchain", "llms", "openai", "OpenAI"], "kwargs": {"model": "gpt-3.5-turbo-instruct", "openai_api_key": {"lc": 1, "type": "secret", "id": ["OPENAI_API_KEY"]}}}'
Load
Any serialized object can be loaded.
from langchain.load import loads
from langchain.load.load import load
loaded_1 = load(dumpd(llm))
loaded_2 = loads(dumps(llm))
print(loaded_1.invoke("How are you doing?"))
I am an AI and do not have the capability to experience emotions. But thank you for asking. Is there anything I can assist you with?