LangChain classes implement standard methods for serialization. Serializing LangChain objects using these methods confer some advantages:
To save and load LangChain objects using this system, use the dumpd
, dumps
, load
, and loads
functions in the load module of langchain-core
. These functions support JSON and JSON-serializable objects.
All LangChain objects that inherit from Serializable are JSON-serializable. Examples include messages, document objects (e.g., as returned from retrievers), and most Runnables, such as chat models, retrievers, and chains implemented with the LangChain Expression Language.
from langchain_core.load import dumpd, dumps, load, loads
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
prompt = ChatPromptTemplate.from_messages(
[
("system", "Translate the following into {language}:"),
("user", "{text}"),
],
)
llm = ChatOpenAI(model="gpt-4o-mini", api_key="llm-api-key")
chain = prompt | llm
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"runnable",
"RunnableSequence"
],
"kwargs": {
"first": {
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"prompts",
"chat",
"ChatPromptTemplate"
],
"kwargs": {
"input_variables": [
"language",
"text"
],
"messages": [
{
"lc": 1,
"type": "constructor",
Note that the API key is withheld from the serialized representations. Parameters that are considered secret are specified by the .lc_secrets
attribute of the LangChain object:
Specifying secrets_map
in load
and loads
will load the corresponding secrets onto the de-serialized LangChain object.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4