add save and load tfidf vectorizer and docs for TFIDFRetriever (#8112)
This is to add save_local and load_local to tfidf_vectorizer and docs in
tfidf_retriever to make the vectorizer reusable.
<!-- Thank you for contributing to LangChain!
Replace this comment with:
- Description: add save_local and load_local to tfidf_vectorizer and
docs in tfidf_retriever
- Issue: None
- Dependencies: None
- Tag maintainer: @rlancemartin, @eyurtsev
- Twitter handle: @MlopsJ
Please make sure you're PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.
If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use.
Maintainer responsibilities:
- General / Misc / if you don't know who to tag: @baskaryan
- DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev
- Models / Prompts: @hwchase17, @baskaryan
- Memory: @hwchase17
- Agents / Tools / Toolkits: @hinthornw
- Tracing / Callbacks: @agola11
- Async: @agola11
If no one reviews your PR within a few days, feel free to @-mention the
same people again.
See contribution guidelines for more information on how to write/run
tests, lint, etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
---------
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
2023-08-04 06:06:27 +00:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
from tempfile import TemporaryDirectory
|
|
|
|
|
2023-05-24 17:02:09 +00:00
|
|
|
import pytest
|
2023-11-21 16:35:29 +00:00
|
|
|
from langchain_core.documents import Document
|
2023-05-24 17:02:09 +00:00
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.retrievers.tfidf import TFIDFRetriever
|
2023-04-25 05:19:58 +00:00
|
|
|
|
|
|
|
|
2023-05-24 17:02:09 +00:00
|
|
|
@pytest.mark.requires("sklearn")
|
2023-04-25 05:19:58 +00:00
|
|
|
def test_from_texts() -> None:
|
|
|
|
input_texts = ["I have a pen.", "Do you have a pen?", "I have a bag."]
|
|
|
|
tfidf_retriever = TFIDFRetriever.from_texts(texts=input_texts)
|
|
|
|
assert len(tfidf_retriever.docs) == 3
|
|
|
|
assert tfidf_retriever.tfidf_array.toarray().shape == (3, 5)
|
|
|
|
|
|
|
|
|
2023-05-24 17:02:09 +00:00
|
|
|
@pytest.mark.requires("sklearn")
|
2023-04-25 05:19:58 +00:00
|
|
|
def test_from_texts_with_tfidf_params() -> None:
|
|
|
|
input_texts = ["I have a pen.", "Do you have a pen?", "I have a bag."]
|
|
|
|
tfidf_retriever = TFIDFRetriever.from_texts(
|
|
|
|
texts=input_texts, tfidf_params={"min_df": 2}
|
|
|
|
)
|
|
|
|
# should count only multiple words (have, pan)
|
|
|
|
assert tfidf_retriever.tfidf_array.toarray().shape == (3, 2)
|
2023-05-24 17:02:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("sklearn")
|
|
|
|
def test_from_documents() -> None:
|
|
|
|
input_docs = [
|
|
|
|
Document(page_content="I have a pen."),
|
|
|
|
Document(page_content="Do you have a pen?"),
|
|
|
|
Document(page_content="I have a bag."),
|
|
|
|
]
|
|
|
|
tfidf_retriever = TFIDFRetriever.from_documents(documents=input_docs)
|
|
|
|
assert len(tfidf_retriever.docs) == 3
|
|
|
|
assert tfidf_retriever.tfidf_array.toarray().shape == (3, 5)
|
add save and load tfidf vectorizer and docs for TFIDFRetriever (#8112)
This is to add save_local and load_local to tfidf_vectorizer and docs in
tfidf_retriever to make the vectorizer reusable.
<!-- Thank you for contributing to LangChain!
Replace this comment with:
- Description: add save_local and load_local to tfidf_vectorizer and
docs in tfidf_retriever
- Issue: None
- Dependencies: None
- Tag maintainer: @rlancemartin, @eyurtsev
- Twitter handle: @MlopsJ
Please make sure you're PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.
If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use.
Maintainer responsibilities:
- General / Misc / if you don't know who to tag: @baskaryan
- DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev
- Models / Prompts: @hwchase17, @baskaryan
- Memory: @hwchase17
- Agents / Tools / Toolkits: @hinthornw
- Tracing / Callbacks: @agola11
- Async: @agola11
If no one reviews your PR within a few days, feel free to @-mention the
same people again.
See contribution guidelines for more information on how to write/run
tests, lint, etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
---------
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
2023-08-04 06:06:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("sklearn")
|
|
|
|
def test_save_local_load_local() -> None:
|
|
|
|
input_texts = ["I have a pen.", "Do you have a pen?", "I have a bag."]
|
|
|
|
tfidf_retriever = TFIDFRetriever.from_texts(texts=input_texts)
|
|
|
|
|
|
|
|
file_name = "tfidf_vectorizer"
|
|
|
|
temp_timestamp = datetime.utcnow().strftime("%Y%m%d-%H%M%S")
|
|
|
|
with TemporaryDirectory(suffix="_" + temp_timestamp + "/") as temp_folder:
|
|
|
|
tfidf_retriever.save_local(
|
|
|
|
folder_path=temp_folder,
|
|
|
|
file_name=file_name,
|
|
|
|
)
|
|
|
|
assert os.path.exists(os.path.join(temp_folder, f"{file_name}.joblib"))
|
|
|
|
assert os.path.exists(os.path.join(temp_folder, f"{file_name}.pkl"))
|
|
|
|
|
|
|
|
loaded_tfidf_retriever = TFIDFRetriever.load_local(
|
|
|
|
folder_path=temp_folder,
|
|
|
|
file_name=file_name,
|
|
|
|
)
|
|
|
|
assert len(loaded_tfidf_retriever.docs) == 3
|
|
|
|
assert loaded_tfidf_retriever.tfidf_array.toarray().shape == (3, 5)
|