KeyBERT icon indicating copy to clipboard operation
KeyBERT copied to clipboard

LangChain Deprecation Warning

Open sergey-koba-mobidev opened this issue 11 months ago • 5 comments

/keybert/llm/_langchain.py:102: LangChainDeprecationWarning: The method Chain.run was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:~invoke instead. keywords = self.chain.run(input_documents=[input_document], question=self.prompt).strip()

the code

from langchain.chains.question_answering import load_qa_chain

chain = load_qa_chain(model)

from keybert.llm import LangChain
from keybert import KeyLLM

# Create your LLM
llm = LangChain(chain)

# Load it in KeyLLM
kw_model = KeyLLM(llm)

# Extract keywords
keywords = kw_model.extract_keywords(document)

sergey-koba-mobidev avatar Feb 24 '25 10:02 sergey-koba-mobidev

Thanks for sharing this! I should be a straightforward PR by replacing .run with .invoke instead. If you want, a PR would be appreciated.

MaartenGr avatar Feb 28 '25 11:02 MaartenGr

@MaartenGr Some additional context that may be helpful. According to LangChain's API reference, load_qa_chain is also deprecated since version 0.2.13.

An alternative could be create_stuff_documents_chain, or a simple home made chain like prompt | llm

Some minor changes may be required on keybert.llm.LangChain api, as well as the LangChain section of KeyBERT documentation. My understanding is that KeyBERT tries to support langchain without taking it as a dependency, so provides a thin wrapper on a straightforward chain load_qa_chain. A concern of using the alternative create_stuff_documents_chain is that the prompt is mandatory for this chain, so if keybert.llm.LangChain.__init__ still want to take a prompt (a second prompt), it has to append / replace the chain's baked-in prompt, it could be tricky. May need to remove the prompt arg from keybert.llm.LangChain.__init__, so all prompt goes to create_stuff_documents_chain. chain.invoke can still populate the documents and candidates with no problem.

Sorry if it is out of the scope of this issue. I could send a PR if it makes sense and is on the roadmap of KeyBERT

shengbo-ma avatar Mar 03 '25 06:03 shengbo-ma

@shengbo-ma Based on how KeyBERT uses a very minimal functionality of langchain, I would opt for the easiest and most minimal approach here. I believe there is no need to make this more complex than necessary and perhaps even just use a basic chain.

If you have the time, a PR would be much appreciated 😄

MaartenGr avatar Mar 04 '25 12:03 MaartenGr

Based on how KeyBERT uses a very minimal functionality of langchain, I would opt for the easiest and most minimal approach here. I believe there is no need to make this more complex than necessary and perhaps even just use a basic chain. If you have the time, a PR would be much appreciated 😄

@MaartenGr Make great sense to me. Will send a PR.

shengbo-ma avatar Mar 04 '25 16:03 shengbo-ma

@MaartenGr See #261

shengbo-ma avatar Mar 10 '25 00:03 shengbo-ma