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

Encounter KeyError: 'user_agent' while using CohereRerank() #12899

Closed
5 of 14 tasks
plaban1981 opened this issue Nov 5, 2023 · 6 comments
Closed
5 of 14 tasks

Encounter KeyError: 'user_agent' while using CohereRerank() #12899

plaban1981 opened this issue Nov 5, 2023 · 6 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules Ɑ: vector store Related to vector store module

Comments

@plaban1981
Copy link

System Info

langchain==0.0.330
Python 3.10.12

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

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import CohereRerank

compressor = CohereRerank()

Error Encountered

KeyError Traceback (most recent call last)
in <cell line: 5>()
3
4 #
----> 5 compressor = CohereRerank()
6 #
7 compression_retriever = ContextualCompressionRetriever(

2 frames
/usr/local/lib/python3.10/dist-packages/pydantic/main.cpython-310-x86_64-linux-gnu.so in pydantic.main.BaseModel.init()

/usr/local/lib/python3.10/dist-packages/pydantic/main.cpython-310-x86_64-linux-gnu.so in pydantic.main.validate_model()

/usr/local/lib/python3.10/dist-packages/langchain/retrievers/document_compressors/cohere_rerank.py in validate_environment(cls, values)
53 values, "cohere_api_key", "COHERE_API_KEY"
54 )
---> 55 client_name = values["user_agent"]
56 values["client"] = cohere.Client(cohere_api_key, client_name=client_name)
57 return values

KeyError: 'user_agent'

Expected behavior

The below error should not have been encountered as the COHERE API Key has alreday been set and cohere.Client() is workin with the api key provided


KeyError Traceback (most recent call last)
in <cell line: 5>()
3
4 #
----> 5 compressor = CohereRerank()
6 #
7 compression_retriever = ContextualCompressionRetriever(

2 frames
/usr/local/lib/python3.10/dist-packages/pydantic/main.cpython-310-x86_64-linux-gnu.so in pydantic.main.BaseModel.init()

/usr/local/lib/python3.10/dist-packages/pydantic/main.cpython-310-x86_64-linux-gnu.so in pydantic.main.validate_model()

/usr/local/lib/python3.10/dist-packages/langchain/retrievers/document_compressors/cohere_rerank.py in validate_environment(cls, values)
53 values, "cohere_api_key", "COHERE_API_KEY"
54 )
---> 55 client_name = values["user_agent"]
56 values["client"] = cohere.Client(cohere_api_key, client_name=client_name)
57 return values

KeyError: 'user_agent'

Copy link

dosubot bot commented Nov 5, 2023

🤖

Hello @plaban1981,

I'm Dosu, a friendly bot here to assist you with LangChain. I'm equipped to help debug issues, answer your questions, and guide you on how to contribute to the repository. While we wait for a human maintainer, I'm here to help.

I'm currently reviewing the details of your issue and will respond with a full answer shortly.

@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules Ɑ: vector store Related to vector store module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Nov 5, 2023
Copy link

dosubot bot commented Nov 5, 2023

🤖

It seems like you're encountering a KeyError for 'user_agent' when trying to use the CohereRerank() function in LangChain. This error is likely due to the 'user_agent' key not being provided when creating an instance of the CohereRerank class. In the validate_environment method of the CohereRerank class, the 'user_agent' key is expected to be present in the values dictionary. If it is not present, a KeyError will be raised.

To fix this issue, ensure that you are providing the 'user_agent' key when creating an instance of the CohereRerank class. Here is an example:

from langchain.retrievers.document_compressors import CohereRerank

compressor = CohereRerank(user_agent="my-app")

In this example, "my-app" is the identifier for the application making the request. You can replace it with your own identifier.

Additionally, I found a similar solved issue in the LangChain repository. The issue with the CohereRerank client field being required but missing can be resolved by initializing and passing the client object in cohere_rerank.py. A workaround was also provided by using custom code:

from typing import ForwardRef
from pydantic import BaseModel
from langchain.retrievers.document_compressors import CohereRerank

class CustomCohereRerank(CohereRerank):
    class Config(BaseModel.Config):
        arbitrary_types_allowed = True

CustomCohereRerank.update_forward_refs()

compressor = CustomCohereRerank(client=co)

Where co is co = Client(key).

You can find more details about this issue here.

I hope this helps! If you have any other questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@gardner
Copy link
Contributor

gardner commented Nov 22, 2023

A workaround:

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import CohereRerank
from os import getenv

class CustomCohereRerank(CohereRerank):
    user_agent = 'gardner'

def get_compressor(retriever):
    if getenv("COHERE_API_KEY", False) != False:
        return ContextualCompressionRetriever(
            base_compressor=CustomCohereRerank(), base_retriever=retriever
        )
    else:
        return retriever

@gardner
Copy link
Contributor

gardner commented Nov 25, 2023

@billytrend-cohere do you recognize this part of the code?

@yoch
Copy link
Contributor

yoch commented Nov 29, 2023

Hi,

Same issue here with up to date Langchain version.

The workaround is simply to pass the user_agent explicitely, but according to the documentation I'm not intended to do so.

compressor = CohereRerank(
    model='rerank-multilingual-v2.0',
    user_agent="langchain", # fixes the KeyError: 'user_agent'
)

EDIT
I checked the source code, and the problem is clearly coming from that


Default values are not passed with root_validator(pre=True), it must be pre=False to work properly with user_agent default value.

yoch added a commit to yoch/langchain that referenced this issue Nov 29, 2023
@yoch yoch mentioned this issue Nov 29, 2023
baskaryan added a commit that referenced this issue Nov 29, 2023
- **Description:** use post field validation for `CohereRerank`
  - **Issue:** #12899 and #13058
  - **Dependencies:** 
  - **Tag maintainer:** @baskaryan

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
@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 28, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 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 Mar 13, 2024
@gardner
Copy link
Contributor

gardner commented Mar 14, 2024

@dosubot this was fixed in #14029

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules Ɑ: vector store Related to vector store module
Projects
None yet
Development

No branches or pull requests

3 participants