Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLMRouterChain uses deprecated predict_and_parse method #6819

Closed
3 of 14 tasks
amosjyng opened this issue Jun 27, 2023 · 21 comments
Closed
3 of 14 tasks

LLMRouterChain uses deprecated predict_and_parse method #6819

amosjyng opened this issue Jun 27, 2023 · 21 comments
Labels
01 bug Confirmed bug needs work PRs that need more work

Comments

@amosjyng
Copy link
Contributor

System Info

langchain v0.0.216, Python 3.11.3 on WSL2

Who can help?

@hwchase17

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Follow the first example at https://python.langchain.com/docs/modules/chains/foundational/router

Expected behavior

This line gets triggered:

The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.

As suggested by the error, we can make the following code changes to pass the output parser directly to LLMChain by changing this line to this:

llm_chain = LLMChain(llm=llm, prompt=prompt, output_parser=prompt.output_parser)

And calling LLMChain.__call__ instead of LLMChain.predict_and_parse by changing these lines to this:

cast(
    Dict[str, Any],
    self.llm_chain(inputs, callbacks=callbacks),
)

Unfortunately, while this avoids the warning, it creates a new error:

ValueError: Missing some output keys: {'destination', 'next_inputs'}

because LLMChain currently assumes the existence of a single self.output_key and produces this as output:

{'text': {'destination': 'physics', 'next_inputs': {'input': 'What is black body radiation?'}}}

Even modifying that function to return the keys if the parsed output is a dict triggers the same error, but for the missing key of "text" instead. predict_and_parse avoids this fate by skipping output validation entirely.

It appears changes may have to be a bit more involved here if LLMRouterChain is to keep using LLMChain.

@dosubot dosubot bot added 01 bug Confirmed bug needs work PRs that need more work labels Jun 27, 2023
@tvmaly
Copy link

tvmaly commented Jul 3, 2023

I am seeing the same issue on v0.0.221 , Python 3.10.6 Windows 10

@AI-Chef
Copy link
Contributor

AI-Chef commented Jul 11, 2023

I have the same issue on v0.0.229, Python v3.10.12

@liaokaime
Copy link

liaokaime commented Jul 12, 2023

I have the same issue on v0.0.230, Python v3.10.6 Windows 11

@xzhang8g
Copy link

same issue here

@alexminza
Copy link

langchain v0.0.232

[/opt/homebrew/lib/python3.11/site-packages/langchain/chains/llm.py:275](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.11/site-packages/langchain/chains/llm.py:275): UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.

@bbirdxr
Copy link

bbirdxr commented Jul 20, 2023

langchain v0.0.235, Python 3.9.17 Windows 10

@ellisxu
Copy link

ellisxu commented Jul 28, 2023

langchain v0.0.240, Python 3.10.10 macOS Ventura

@mpearce-bain
Copy link

langchain v0.0.244, Python 3.10.11 Windows 10

@ZHANGJUN-OK
Copy link

I am seeing the same issue on v0.0.257 Python 3.9.12 RedHat

@prdy20
Copy link

prdy20 commented Aug 23, 2023

same issue v0.0.270 python 3.11.3 windows

@gtmray
Copy link

gtmray commented Aug 29, 2023

Same warning on v0.0.275 python 3.11.3 WSL on Windows 11

@ja4h3ad
Copy link

ja4h3ad commented Aug 30, 2023

I also get the same error when using this option:

print(compressed_docs[0].page_content)

If I remove the page_content method, I do not get this error. Also if I use this (CharacterTextSplitter), I do not see the error

text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=500)

Here is a sample function where this happens:

def demo(question):
    '''
    
    Follow the steps below to fill out this function:
    '''
    # PART ONE:
    
    loader = TextLoader('/map/to/document/data.txt', encoding='utf8')
    documents = loader.load()
     
    
    # PART TWO
    # Split the document into chunks (you choose how and what size)
    # text_splitter = CharacterTextSplitter.from_tiktoken_encoder(chunk_size=1000)
    
    text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
    chunk_size=1000, chunk_overlap=100, separators=[" ", ",", "\n"])
    docs = text_splitter.split_documents(documents)
    
    # PART THREE
    # EMBED THE Documents (now in chunks) to a persisted ChromaDB
    embedding_function = OpenAIEmbeddings()
    db = Chroma.from_documents(docs, embedding_function, persist_directory='./App')
    db.persist
     

    # PART FOUR
    # Use ChatOpenAI and ContextualCompressionRetriever to return the most
    # relevant part of the documents.
    llm = ChatOpenAI(temperature=0)
    compressor = LLMChainExtractor.from_llm(llm)
    compression_retreiver = ContextualCompressionRetriever(base_compressor=compressor,
                                                          base_retriever=db.as_retriever())
    compressed_docs=compression_retreiver.get_relevant_documents(question)

     

    print(compressed_docs[0].page_content)

@mikeymice
Copy link

y'all any fix to it? What are we supposed to do

@ton77v
Copy link

ton77v commented Sep 12, 2023

Got the same warning while using load_qa_chain chain_type='map_rerank',

@RoderickVM
Copy link

I posted a very similar issue #10462, using SelfQueryRetriever. I received some sort of solution from the chatbot. However, I don't have a clue how to implement it.

@ellisxu
Copy link

ellisxu commented Sep 15, 2023

I posted a very similar issue #10462, using SelfQueryRetriever. I received some sort of solution from the chatbot. However, I don't have a clue how to implement it.

I solved it by extending SelfQueryRetriever and overwriting the _get_relevant_documents method. Below is an example (In this example, I overwrite _aget_relevant_documents, because I need async features in my case. You can do the same to _get_relevant_documents. ):

class AsyncSelfQueryRetriever(SelfQueryRetriever):
    async def _aget_relevant_documents(
        self, query: str, *, run_manager: AsyncCallbackManagerForRetrieverRun
    ) -> List[Document]:
        """Asynchronously get documents relevant to a query.
        Args:
            query: String to find relevant documents for
            run_manager: The callbacks handler to use
        Returns:
            List of relevant documents
        """
        inputs = self.llm_chain.prep_inputs({"query": query})

        structured_query = cast(
            StructuredQuery,
            # Instead of calling 'self.llm_chain.predict_and_parse' here, 
            # I changed it to leveraging 'self.llm_chain.prompt.output_parser.parse' 
            # and 'self.llm_chain.apredict'
            # ↓↓↓↓↓↓↓
            self.llm_chain.prompt.output_parser.parse(
                await self.llm_chain.apredict(
                    callbacks=run_manager.get_child(), **inputs
                )
            ),
        )
        if self.verbose:
            print(structured_query)
        new_query, new_kwargs = self.structured_query_translator.visit_structured_query(
            structured_query
        )
        if structured_query.limit is not None:
            new_kwargs["k"] = structured_query.limit

        if self.use_original_query:
            new_query = query

        search_kwargs = {**self.search_kwargs, **new_kwargs}
        docs = await self.vectorstore.asearch(
            new_query, self.search_type, **search_kwargs
        )
        return docs

@a92340a
Copy link

a92340a commented Oct 5, 2023

I posted a very similar issue #10462, using SelfQueryRetriever. I received some sort of solution from the chatbot. However, I don't have a clue how to implement it.

I solved it by extending SelfQueryRetriever and overwriting the _get_relevant_documents method. Below is an example (In this example, I overwrite _aget_relevant_documents, because I need async features in my case. You can do the same to _get_relevant_documents. ):

Hi! @ellisxu Very appreciated with your sharing! Can you show the packages which are imported from?
Thanks a lot!

@ellisxu
Copy link

ellisxu commented Oct 15, 2023

I posted a very similar issue #10462, using SelfQueryRetriever. I received some sort of solution from the chatbot. However, I don't have a clue how to implement it.

I solved it by extending SelfQueryRetriever and overwriting the _get_relevant_documents method. Below is an example (In this example, I overwrite _aget_relevant_documents, because I need async features in my case. You can do the same to _get_relevant_documents. ):

Hi! @ellisxu Very appreciated with your sharing! Can you show the packages which are imported from? Thanks a lot!

The package is from langchain.retrievers.self_query.base import SelfQueryRetriever and the langchain version I use is 0.0.279.

@nickeleres
Copy link

I am still running into this with SelfQueryRetriever using langchain==0.0.302....is there any resolution here?

@ellisxu
Copy link

ellisxu commented Nov 22, 2023

I am still running into this with SelfQueryRetriever using langchain==0.0.302....is there any resolution here?

#6819 (comment) Try this. :)

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Feb 21, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Feb 28, 2024
@tvmaly
Copy link

tvmaly commented Feb 29, 2024

This is unfortunate as this part of Lang Chain is used in the DeepLearningAI course

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 bug Confirmed bug needs work PRs that need more work
Projects
None yet
Development

No branches or pull requests