Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Make langchain rag copy-able

chenmoneygithub130e3d2874e5
diff --git a/dspy/predict/langchain.py b/dspy/predict/langchain.py
index 32e7250df..d61256995 100644
--- a/dspy/predict/langchain.py
+++ b/dspy/predict/langchain.py
@@ -1,5 +1,6 @@
import copy
import random
+from functools import reduce
from langchain_core.pydantic_v1 import Extra
from langchain_core.runnables import Runnable
@@ -163,3 +164,29 @@ class LangChainModule(dspy.Module):
def invoke(self, d, *args, **kwargs):
return self.forward(**d).output
+ def reset_copy(self):
+ """Override `reset_copy()` to skip parts that cannot be deep copied."""
+ if self.chain is None:
+ obj = copy.deepcopy(self)
+ else:
+ chain = self.chain
+ steps = []
+ for step in chain.steps:
+ if isinstance(step, LangChainPredict):
+ # Only copy the LangChainPredict object.
+ steps.append(copy.deepcopy(step))
+ else:
+ steps.append(step)
+
+ chain_copy = reduce(lambda x, y: x | y, steps)
+ # Temporarily remove the chain from `self` instance for deep copying.
+ self.chain = None
+ obj = copy.deepcopy(self)
+ obj.chain = chain_copy
+ # Put back the chain to `self` instance.
+ self.chain = chain
+
+ for param in obj.parameters():
+ param.reset()
+
+ return obj
diff --git a/tests/predict/test_langchain.py b/tests/predict/test_langchain.py
new file mode 100644
index 000000000..62a6665d3
--- /dev/null
+++ b/tests/predict/test_langchain.py
@@ -0,0 +1,58 @@
+import pytest
+pytest.importorskip("langchain")
+
+import os
+from langchain import hub
+from langchain_chroma import Chroma
+from langchain_core.output_parsers import StrOutputParser
+from langchain_core.runnables import RunnablePassthrough
+from langchain_core.documents import Document
+from langchain_openai import ChatOpenAI
+from langchain_text_splitters import RecursiveCharacterTextSplitter
+from langchain_community.embeddings import FakeEmbeddings
+
+
+from dspy.predict.langchain import LangChainModule, LangChainPredict
+
+def test_copying_module():
+ os.environ["OPENAI_API_KEY"] = "fake-key"
+ llm = ChatOpenAI(model="gpt-4o-mini")
+ docs = [Document(
+ page_content="Hello, world!",
+ metadata={"source": "https://example.com"}
+ )]
+
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10, chunk_overlap=10)
+ splits = text_splitter.split_documents(docs)
+ vectorstore = Chroma.from_documents(documents=splits, embedding=FakeEmbeddings(size=5))
+
+ # Retrieve and generate using the relevant snippets of the blog.
+ retriever = vectorstore.as_retriever()
+
+ prompt = hub.pull("rlm/rag-prompt")
+
+
+ def format_docs(docs):
+ return "\n\n".join(doc.page_content for doc in docs)
+
+
+ rag_chain = (
+ {"context": retriever | format_docs, "question": RunnablePassthrough()}
+ | LangChainPredict(prompt, llm)
+ | StrOutputParser()
+ )
+ # Now we wrap it in LangChainModule.
+ rag_dspy_module = LangChainModule(rag_chain)
+
+ copied_module = rag_dspy_module.reset_copy()
+ assert len(copied_module.chain.steps) == len(rag_dspy_module.chain.steps)
+ for (module, copied_module) in zip(rag_dspy_module.chain.steps, copied_module.chain.steps):
+ if isinstance(module, LangChainPredict):
+ # The LangChainPredict modules are deep copied.
+ assert module != copied_module
+ assert module.langchain_llm.model_name == copied_module.langchain_llm.model_name
+ else:
+ # The rest of the modules are just copied by reference.
+ assert module == copied_module
+ # Clean up.
+ os.environ["OPENAI_API_KEY"] = None