langchain-ai / langchain

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

FlashrankRerank validate_environment creates a Ranker client even if a custom client is passed #23338

Closed rhlarora84 closed 21 hours ago

rhlarora84 commented 3 months ago

Checked other resources

Example Code

The following code is used to create a Ranker object which downloads the model into a custom directory and then FlashrankRerank is initialized.

def create_ms_marco_mini_llm():
    logger.info("Download model ms_marco_mini_llm")
    model_name = "ms-marco-MiniLM-L-12-v2"
    ranker = Ranker(model_name=model_name, max_length=1024, cache_dir=model_dir)
    return FlashrankRerank(client=ranker, model=model_name)

The validate_environment method in FlashrankRerank goes to create another Ranker object causing it to download model into a different directory

     @root_validator(pre=True)
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that api key and python package exists in environment."""
        try:
            from flashrank import Ranker
        except ImportError:
            raise ImportError(
                "Could not import flashrank python package. "
                "Please install it with `pip install flashrank`."
            )

        values["model"] = values.get("model", DEFAULT_MODEL_NAME)
        values["client"] = Ranker(model_name=values["model"])
        return values

Error Message and Stack Trace (if applicable)

No response

Description

FlashrankRerank should check if the client is already initialized before creating a ranker.

System Info

langchain==0.2.5 langchain-anthropic==0.1.15 langchain-aws==0.1.6 langchain-community==0.2.5 langchain-core==0.2.7 langchain-experimental==0.0.61 langchain-openai==0.1.8 langchain-text-splitters==0.2.1 openinference-instrumentation-langchain==0.1.19

wenngong commented 3 months ago

pr #23350