-
Notifications
You must be signed in to change notification settings - Fork 629
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
[#342] RAG - fix PDF format in vector database #551
Merged
+90
−4
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the terms described in the LICENSE file in | ||
# the root directory of this source tree. | ||
|
||
import base64 | ||
import mimetypes | ||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from llama_stack.apis.memory.memory import MemoryBankDocument, URL | ||
from llama_stack.providers.utils.memory.vector_store import content_from_doc | ||
|
||
DUMMY_PDF_PATH = Path(os.path.abspath(__file__)).parent / "fixtures" / "dummy.pdf" | ||
|
||
|
||
def read_file(file_path: str) -> bytes: | ||
with open(file_path, "rb") as file: | ||
return file.read() | ||
|
||
|
||
def data_url_from_file(file_path: str) -> str: | ||
with open(file_path, "rb") as file: | ||
file_content = file.read() | ||
|
||
base64_content = base64.b64encode(file_content).decode("utf-8") | ||
mime_type, _ = mimetypes.guess_type(file_path) | ||
|
||
data_url = f"data:{mime_type};base64,{base64_content}" | ||
|
||
return data_url | ||
|
||
|
||
class TestVectorStore: | ||
@pytest.mark.asyncio | ||
async def test_returns_content_from_pdf_data_uri(self): | ||
data_uri = data_url_from_file(DUMMY_PDF_PATH) | ||
doc = MemoryBankDocument( | ||
document_id="dummy", | ||
content=data_uri, | ||
mime_type="application/pdf", | ||
metadata={}, | ||
) | ||
content = await content_from_doc(doc) | ||
assert content == "Dummy PDF file" | ||
|
||
@pytest.mark.asyncio | ||
async def test_downloads_pdf_and_returns_content(self): | ||
# Using GitHub to host the PDF file | ||
url = "https://raw.githubusercontent.com/meta-llama/llama-stack/da035d69cfca915318eaf485770a467ca3c2a238/llama_stack/providers/tests/memory/fixtures/dummy.pdf" | ||
doc = MemoryBankDocument( | ||
document_id="dummy", | ||
content=url, | ||
mime_type="application/pdf", | ||
metadata={}, | ||
) | ||
content = await content_from_doc(doc) | ||
assert content == "Dummy PDF file" | ||
|
||
@pytest.mark.asyncio | ||
async def test_downloads_pdf_and_returns_content_with_url_object(self): | ||
# Using GitHub to host the PDF file | ||
url = "https://raw.githubusercontent.com/meta-llama/llama-stack/da035d69cfca915318eaf485770a467ca3c2a238/llama_stack/providers/tests/memory/fixtures/dummy.pdf" | ||
doc = MemoryBankDocument( | ||
document_id="dummy", | ||
content=URL( | ||
uri=url, | ||
), | ||
mime_type="application/pdf", | ||
metadata={}, | ||
) | ||
content = await content_from_doc(doc) | ||
assert content == "Dummy PDF file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did consider mocking but that would have introduced another dep that we don't have + make the test less realistic. So decided to do this.
But lmk if you prefer mocking here