Skip to content

Commit b4c1f69

Browse files
committed
remove image tools
not throughly tested yet
1 parent eea0dca commit b4c1f69

File tree

2 files changed

+2
-87
lines changed

2 files changed

+2
-87
lines changed

lib/idp_common_pkg/idp_common/extraction/agentic_idp.py

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
Any,
1818
Dict,
1919
List,
20-
Literal,
2120
Optional,
2221
Tuple,
2322
Type,
@@ -28,7 +27,7 @@
2827

2928
import jsonpatch
3029
from botocore.config import Config
31-
from PIL import Image, ImageEnhance, ImageOps
30+
from PIL import Image
3231
from pydantic import BaseModel, Field
3332
from strands import Agent, tool
3433
from strands.models.bedrock import BedrockModel
@@ -215,81 +214,6 @@ def apply_json_patches(
215214
return extraction_tool, apply_json_patches
216215

217216

218-
@tool
219-
def load_image(image_index: int, agent: Agent) -> Image.Image:
220-
"""
221-
Load an image from state as a PIL Image for viewing and analysis.
222-
223-
Args:
224-
image_index: Index of the image to load (0-based)
225-
226-
Returns:
227-
PIL Image object
228-
"""
229-
images = agent.state.get("images")
230-
if not images or str(image_index) not in images:
231-
raise ValueError(f"Image index {image_index} does not exist")
232-
233-
image_data = images[str(image_index)]
234-
if isinstance(image_data, str):
235-
# Convert binary string back to PIL Image
236-
import base64
237-
238-
img_bytes = base64.b64decode(image_data)
239-
return Image.open(io.BytesIO(img_bytes))
240-
else:
241-
# Legacy support for direct PIL images
242-
return image_data
243-
244-
245-
@tool
246-
def enhance_image(
247-
image_index: int,
248-
enhancement_type: Literal["brightness", "contrast", "sharpen", "grayscale"],
249-
agent: Agent,
250-
factor: float = 1.5,
251-
) -> str:
252-
"""
253-
Apply image enhancement for better extraction accuracy.
254-
255-
Args:
256-
image_index: Index of the image to enhance (0-based)
257-
enhancement_type: Type of enhancement (brightness, contrast, sharpen, grayscale)
258-
factor: Enhancement factor (for brightness/contrast)
259-
"""
260-
try:
261-
# Load the image
262-
image = load_image(image_index, agent)
263-
264-
# Apply enhancement
265-
if enhancement_type == "brightness":
266-
enhancer = ImageEnhance.Brightness(image)
267-
enhanced = enhancer.enhance(factor)
268-
elif enhancement_type == "contrast":
269-
enhancer = ImageEnhance.Contrast(image)
270-
enhanced = enhancer.enhance(factor)
271-
elif enhancement_type == "sharpen":
272-
enhancer = ImageEnhance.Sharpness(image)
273-
enhanced = enhancer.enhance(factor)
274-
elif enhancement_type == "grayscale":
275-
enhanced = ImageOps.grayscale(image)
276-
277-
# Convert enhanced image back to binary string and store
278-
img_buffer = io.BytesIO()
279-
enhanced.save(img_buffer, format="PNG")
280-
281-
img_binary = base64.b64encode(img_buffer.getvalue()).decode("utf-8")
282-
283-
images = agent.state.get("images")
284-
images[str(image_index)] = img_binary
285-
agent.state.set(key="images", value=images)
286-
287-
return f"Applied {enhancement_type} to image {image_index}"
288-
289-
except Exception as e:
290-
return f"Error enhancing image {image_index}: {str(e)}"
291-
292-
293217
SYSTEM_PROMPT = """
294218
You are a useful assistant that helps turn unstructured data into structured data using the provided tools.
295219
@@ -317,7 +241,6 @@ def enhance_image(
317241
4. Verify that all characters and formatting are preserved exactly as they appear
318242
5. When fixing errors, use JSON patches to target specific problems
319243
320-
Use load_image tool to view images when needed for better accuracy.
321244
322245
FINAL REVIEW (CRITICAL):
323246
After successfully using the extraction tool, you MUST:
@@ -334,7 +257,6 @@ async def structured_output_async(
334257
model_id: str,
335258
data_format: Type[TargetModel],
336259
prompt: Union[str, Message, Image.Image],
337-
enable_image_tools: bool = True,
338260
existing_data: Optional[BaseModel] = None,
339261
system_prompt: str | None = None,
340262
custom_instruction: str | None = None,
@@ -425,9 +347,7 @@ async def structured_output_async(
425347
)
426348

427349
# Prepare tools list
428-
tools = [extraction_tool, apply_json_patches, load_image]
429-
if enable_image_tools:
430-
tools.append(enhance_image)
350+
tools = [extraction_tool, apply_json_patches]
431351

432352
# Create agent with system prompt and tools
433353
schema_json = json.dumps(data_format.model_json_schema(), indent=2)
@@ -654,7 +574,6 @@ def structured_output(
654574
model_id: str,
655575
data_format: Type[BaseModel],
656576
prompt: Union[str, Message, Image.Image],
657-
enable_image_tools: bool = True,
658577
existing_data: Optional[BaseModel] = None,
659578
system_prompt: str | None = None,
660579
custom_instruction: str | None = None,
@@ -682,7 +601,6 @@ def structured_output(
682601
model_id: Model identifier (e.g., "us.anthropic.claude-sonnet-4-20250514-v1:0")
683602
data_format: Pydantic model class defining the expected structure
684603
prompt: Input content (text, image, or content blocks)
685-
enable_image_tools: Whether to enable image enhancement tools (default: True)
686604
existing_data: Optional existing data to update via patches
687605
system_prompt: **DISCOURAGED** - Custom system prompt. Only use if the default
688606
SYSTEM_PROMPT is completely unsuitable for your use case.
@@ -738,7 +656,6 @@ def run_in_new_loop():
738656
model_id=model_id,
739657
data_format=data_format,
740658
prompt=prompt,
741-
enable_image_tools=enable_image_tools,
742659
existing_data=existing_data,
743660
system_prompt=system_prompt,
744661
custom_instruction=custom_instruction,
@@ -770,7 +687,6 @@ def run_in_new_loop():
770687
model_id=model_id,
771688
data_format=data_format,
772689
prompt=prompt,
773-
enable_image_tools=enable_image_tools,
774690
existing_data=existing_data,
775691
system_prompt=system_prompt,
776692
custom_instruction=custom_instruction,

lib/idp_common_pkg/idp_common/extraction/service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,6 @@ def process_document_section(self, document: Document, section_id: str) -> Docum
13481348
model_id=model_id,
13491349
data_format=dynamic_model,
13501350
prompt=message_prompt, # pyright: ignore[reportArgumentType]
1351-
enable_image_tools=True,
13521351
custom_instruction=system_prompt,
13531352
review_agent=agentic_config.get("review_agent", False),
13541353
context="Extraction",

0 commit comments

Comments
 (0)