Description
Description
Added local file path handling in add image tool
Steps to Reproduce
This is the built in code in which i added local file path handling as well which was not there so it is working fine with open ai and most of the anthropic model as well but with claude 3.7 sonnet it gives none response error can you please help me with this what is the issue .....
from typing import Dict, Optional, Union
from pydantic import BaseModel, Field
from crewai.tools.base_tool import BaseTool
from crewai.utilities import I18N
import os
import base64
i18n = I18N()
class AddImageToolSchema(BaseModel):
image_url: str = Field(..., description="The URL or path of the image to add")
action: Optional[str] = Field(
default=None, description="Optional context or question about the image"
)
class AddImageTool(BaseTool):
"""Tool for adding images to the content"""
name: str = Field(default_factory=lambda: i18n.tools("add_image")["name"]) # type: ignore
description: str = Field(default_factory=lambda: i18n.tools("add_image")["description"]) # type: ignore
args_schema: type[BaseModel] = AddImageToolSchema
def _run(
self,
image_url: str,
action: Optional[str] = None,
**kwargs,
) -> dict:
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}")
else:
image_url=image_url
content = [
{"type": "text", "text": action},
{
"type": "image_url",
"image_url": {
"url": image_url,
},
},
]
return {"role": "user", "content": content}
Expected behavior
Please help me with this
All models has different way of handling image input but i think that is not the issue because it is working with other anthropic models
Screenshots/Code snippets
from typing import Dict, Optional, Union
from pydantic import BaseModel, Field
from crewai.tools.base_tool import BaseTool
from crewai.utilities import I18N
import os
import base64
i18n = I18N()
class AddImageToolSchema(BaseModel):
image_url: str = Field(..., description="The URL or path of the image to add")
action: Optional[str] = Field(
default=None, description="Optional context or question about the image"
)
class AddImageTool(BaseTool):
"""Tool for adding images to the content"""
name: str = Field(default_factory=lambda: i18n.tools("add_image")["name"]) # type: ignore
description: str = Field(default_factory=lambda: i18n.tools("add_image")["description"]) # type: ignore
args_schema: type[BaseModel] = AddImageToolSchema
def _run(
self,
image_url: str,
action: Optional[str] = None,
**kwargs,
) -> dict:
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}")
else:
image_url=image_url
content = [
{"type": "text", "text": action},
{
"type": "image_url",
"image_url": {
"url": image_url,
},
},
]
return {"role": "user", "content": content}
Operating System
Ubuntu 20.04
Python Version
3.10
crewAI Version
0.108.0
crewAI Tools Version
0.38.0
Virtual Environment
Venv
Evidence
Please help me with this what could be the issue
Possible Solution
it is not working for claude 3.7 sonnet
Additional context
.