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

Added llm tests to the local test set. #100

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies = [
"SpeechRecognition",
"pathvalidate",
"charset-normalizer",
"openai",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure about this - perhaps should be an optional dep?

]

[project.urls]
Expand Down Expand Up @@ -77,3 +78,6 @@ exclude_lines = [
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]

[tool.hatch.build.targets.sdist]
only-include = ["src/markitdown"]
2 changes: 1 addition & 1 deletion src/markitdown/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present Adam Fourney <adamfo@microsoft.com>
#
# SPDX-License-Identifier: MIT
__version__ = "0.0.1a1"
__version__ = "0.0.1a2"
2 changes: 0 additions & 2 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,6 @@ def _get_llm_description(self, local_path, extension, client, model, prompt=None
if prompt is None or prompt.strip() == "":
prompt = "Write a detailed caption for this image."

sys.stderr.write(f"llm Prompt:\n{prompt}\n")

data_uri = ""
with open(local_path, "rb") as image_file:
content_type, encoding = mimetypes.guess_type("_dummy" + extension)
Expand Down
Empty file modified tests/test_files/test.docx
100755 → 100644
Empty file.
Empty file modified tests/test_files/test.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified tests/test_files/test.xlsx
100755 → 100644
Empty file.
Binary file added tests/test_files/test_llm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified tests/test_files/test_with_comment.docx
100755 → 100644
Empty file.
34 changes: 34 additions & 0 deletions tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
skip_remote = (
True if os.environ.get("GITHUB_ACTIONS") else False
) # Don't run these tests in CI


# Don't run the llm tests without a key and the client library
skip_llm = False if os.environ.get("OPENAI_API_KEY") else True
try:
import openai
except ModuleNotFoundError:
skip_llm = True

# Skip exiftool tests if not installed
skip_exiftool = shutil.which("exiftool") is None

TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "test_files")
Expand Down Expand Up @@ -107,6 +117,10 @@
"髙橋淳,35,名古屋",
]

LLM_TEST_STRINGS = [
"5bda1dd6",
]


@pytest.mark.skipif(
skip_remote,
Expand Down Expand Up @@ -229,8 +243,28 @@ def test_markitdown_exiftool() -> None:
assert target in result.text_content


@pytest.mark.skipif(
skip_llm,
reason="do not run llm tests without a key",
)
def test_markitdown_llm() -> None:
client = openai.OpenAI()
markitdown = MarkItDown(llm_client=client, llm_model="gpt-4o")

result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test_llm.jpg"))

for test_string in LLM_TEST_STRINGS:
assert test_string in result.text_content

# This is not super precise. It would also accept "red square", "blue circle",
# "the square is not blue", etc. But it's sufficient for this test.
for test_string in ["red", "circle", "blue", "square"]:
assert test_string in result.text_content.lower()


if __name__ == "__main__":
"""Runs this file's tests from the command line."""
test_markitdown_remote()
test_markitdown_local()
test_markitdown_exiftool()
test_markitdown_llm()
Loading