Skip to content
Open
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
2 changes: 1 addition & 1 deletion metagpt/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ def download_model(file_url: str, target_folder: Path) -> Path:
file_name = file_url.split("/")[-1]
file_path = target_folder.joinpath(f"{file_name}")
if not file_path.exists():
file_path.mkdir(parents=True, exist_ok=True)
file_path.parent.mkdir(parents=True, exist_ok=True)
try:
response = requests.get(file_url, stream=True)
response.raise_for_status() # 检查请求是否成功
Expand Down
18 changes: 18 additions & 0 deletions tests/metagpt/utils/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uuid
from pathlib import Path
from typing import Any, Set
from unittest.mock import patch

import pytest
from pydantic import BaseModel
Expand All @@ -29,6 +30,7 @@
awrite,
check_cmd_exists,
concat_namespace,
download_model,
extract_and_encode_images,
extract_image_paths,
import_class_inst,
Expand Down Expand Up @@ -235,5 +237,21 @@ def test_extract_and_encode_images():
assert not extract_and_encode_images("a non-existing.jpg")


def test_download_model(tmp_path):
class _FakeResponse:
def raise_for_status(self):
return None

def iter_content(self, chunk_size=8192):
yield b"weights"

file_url = "https://example.com/groundingdino_swint_ogc.pth"
with patch("metagpt.utils.common.requests.get", return_value=_FakeResponse()):
file_path = download_model(file_url, tmp_path / "weights")

assert file_path.is_file()
assert file_path.read_bytes() == b"weights"


if __name__ == "__main__":
pytest.main([__file__, "-s"])
Loading