mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-25 16:18:13 +00:00
Fix #2642: Add local file path handling to AddImageTool for Claude 3.7 Sonnet
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -283,6 +283,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
|||||||
] or tool_calling.tool_name.casefold().replace("_", " ") in [
|
] or tool_calling.tool_name.casefold().replace("_", " ") in [
|
||||||
name.casefold().strip() for name in self.tool_name_to_tool_map
|
name.casefold().strip() for name in self.tool_name_to_tool_map
|
||||||
]:
|
]:
|
||||||
|
if tool_calling.tool_name.casefold().strip() == self._i18n.tools("add_image")["name"].casefold().strip():
|
||||||
|
tool_calling.kwargs['llm'] = self.llm
|
||||||
|
|
||||||
tool_result = tool_usage.use(tool_calling, agent_action.text)
|
tool_result = tool_usage.use(tool_calling, agent_action.text)
|
||||||
tool = self.tool_name_to_tool_map.get(tool_calling.tool_name)
|
tool = self.tool_name_to_tool_map.get(tool_calling.tool_name)
|
||||||
if tool:
|
if tool:
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from typing import Dict, Optional, Union
|
from typing import Dict, Optional, Union
|
||||||
|
import os
|
||||||
|
import base64
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
@@ -29,6 +31,20 @@ class AddImageTool(BaseTool):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
action = action or i18n.tools("add_image")["default_action"] # type: ignore
|
action = action or i18n.tools("add_image")["default_action"] # type: ignore
|
||||||
|
|
||||||
|
if os.path.exists(image_url):
|
||||||
|
try:
|
||||||
|
with open(image_url, "rb") as image_file:
|
||||||
|
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
|
||||||
|
image_url = f"data:image/jpeg;base64,{encoded_string}"
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"Error encoding image: {e}")
|
||||||
|
|
||||||
|
using_claude_3_7 = False
|
||||||
|
if "llm" in kwargs and hasattr(kwargs["llm"], "model"):
|
||||||
|
model_name = kwargs["llm"].model
|
||||||
|
using_claude_3_7 = "claude-3-7" in model_name.lower()
|
||||||
|
|
||||||
content = [
|
content = [
|
||||||
{"type": "text", "text": action},
|
{"type": "text", "text": action},
|
||||||
{
|
{
|
||||||
|
|||||||
57
tests/tools/agent_tools/test_add_image_tool.py
Normal file
57
tests/tools/agent_tools/test_add_image_tool.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import os
|
||||||
|
import base64
|
||||||
|
import pytest
|
||||||
|
from unittest.mock import patch, MagicMock
|
||||||
|
|
||||||
|
from crewai.tools.agent_tools.add_image_tool import AddImageTool
|
||||||
|
|
||||||
|
|
||||||
|
class TestAddImageTool:
|
||||||
|
def setup_method(self):
|
||||||
|
self.tool = AddImageTool()
|
||||||
|
os.makedirs("tests/tools/agent_tools/test_files", exist_ok=True)
|
||||||
|
|
||||||
|
def test_add_image_with_url(self):
|
||||||
|
result = self.tool._run(image_url="https://example.com/image.jpg")
|
||||||
|
assert result["role"] == "user"
|
||||||
|
assert len(result["content"]) == 2
|
||||||
|
assert result["content"][0]["type"] == "text"
|
||||||
|
assert result["content"][1]["type"] == "image_url"
|
||||||
|
assert result["content"][1]["image_url"]["url"] == "https://example.com/image.jpg"
|
||||||
|
|
||||||
|
def test_add_image_with_local_file(self):
|
||||||
|
test_file_path = "tests/tools/agent_tools/test_files/test_image.jpg"
|
||||||
|
|
||||||
|
with patch("builtins.open", MagicMock()), \
|
||||||
|
patch("base64.b64encode", return_value=b"test_encoded_content"), \
|
||||||
|
patch("os.path.exists", return_value=True):
|
||||||
|
|
||||||
|
result = self.tool._run(image_url=test_file_path)
|
||||||
|
|
||||||
|
assert result["role"] == "user"
|
||||||
|
assert len(result["content"]) == 2
|
||||||
|
assert result["content"][0]["type"] == "text"
|
||||||
|
assert result["content"][1]["type"] == "image_url"
|
||||||
|
assert result["content"][1]["image_url"]["url"].startswith("data:image/jpeg;base64,")
|
||||||
|
|
||||||
|
def test_add_image_with_claude_3_7_model(self):
|
||||||
|
mock_llm = MagicMock()
|
||||||
|
mock_llm.model = "claude-3-7-sonnet-latest"
|
||||||
|
|
||||||
|
with patch("os.path.exists", return_value=False):
|
||||||
|
result = self.tool._run(
|
||||||
|
image_url="https://example.com/image.jpg",
|
||||||
|
llm=mock_llm
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["role"] == "user"
|
||||||
|
assert len(result["content"]) == 2
|
||||||
|
assert result["content"][0]["type"] == "text"
|
||||||
|
assert result["content"][1]["type"] == "image_url"
|
||||||
|
assert result["content"][1]["image_url"]["url"] == "https://example.com/image.jpg"
|
||||||
|
|
||||||
|
def test_add_image_with_invalid_path(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
with patch("os.path.exists", return_value=True), \
|
||||||
|
patch("builtins.open", side_effect=FileNotFoundError()):
|
||||||
|
self.tool._run(image_url="/invalid/path/to/image.jpg")
|
||||||
BIN
tests/tools/agent_tools/test_files/test_image.jpg
Normal file
BIN
tests/tools/agent_tools/test_files/test_image.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 944 B |
Reference in New Issue
Block a user