langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.22k stars 14.72k forks source link

langchain dumps not always implemented - Improve langchain runnable serialization #25664

Open yairyairyair opened 3 weeks ago

yairyairyair commented 3 weeks ago

Checked other resources

Example Code

from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import BaseTool
from langchain_core.load import dumps

prompt = PromptTemplate.from_template("Answer the following questions as best you can. You have access to the following tools:\n\n{tools}\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [{tool_names}]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: {input}\nThought:{agent_scratchpad}")

# Choose the LLM to use
llm = ChatOpenAI(model="gpt-3.5-turbo")

tools: list[BaseTool] = []

# Construct the ReAct agent
agent = create_react_agent(llm, tools, prompt)

# dumps to a file
with open('react-ai-agent.json', 'w') as f:
    f.write(dumps(agent))

Error Message and Stack Trace (if applicable)

RUNNABLE_FILE_PATH = "./react-ai-agent.json"
with open(RUNNABLE_FILE_PATH, 'r') as f:
  agent = loads(f.read(), secrets_from_env=True)
NotImplementedError: Trying to load an object that doesn't implement serialization: {'lc': 1, 'type': 'not_implemented', 'id': ['langchain_core', 'runnables', 'base', 'RunnableLambda'], 'repr': "RunnableLambda(lambda x: format_log_to_str(x['intermediate_steps']))"}

Description

Basically i just want to dumps a runnable and loads it, i want it to work for any langchain runnable

System Info

generic langchain installation

yairyairyair commented 3 weeks ago

If to be more specific these are the not_implemented parts in the json

              "agent_scratchpad": {
                "lc": 1,
                "type": "not_implemented",
                "id": ["langchain_core", "runnables", "base", "RunnableLambda"],
                "repr": "RunnableLambda(lambda x: format_log_to_str(x['intermediate_steps']))"
              }
"last": {
      "lc": 1,
      "type": "not_implemented",
      "id": [
        "langchain",
        "agents",
        "output_parsers",
        "react_single_input",
        "ReActSingleInputOutputParser"
      ],
      "repr": "ReActSingleInputOutputParser()",
      "name": "ReActSingleInputOutputParser",
...
}
baskaryan commented 2 weeks ago

@yairyairyair thanks for the feedback.

Runnables can contain arbitrary logic (you can wrap any python function in a Runnable), so the only way to be able to serialize anything would be to do something unsafe like pickling python objects.

Out of curiosity, what are you using serialization for?

yairyairyair commented 2 weeks ago

Tried using pickle, dill and jsonpickle all didnt work. Dont mind the security aspect of it. Using it to move runnables from one service to another.