File tree Expand file tree Collapse file tree 5 files changed +46
-16
lines changed Expand file tree Collapse file tree 5 files changed +46
-16
lines changed Original file line number Diff line number Diff line change 2
2
from ._anthropic import ChatAnthropic , ChatBedrockAnthropic
3
3
from ._auto import ChatAuto
4
4
from ._chat import Chat
5
- from ._content import ContentToolRequest , ContentToolResult
5
+ from ._content import ContentToolRequest , ContentToolResult , ContentToolResultImage
6
6
from ._content_image import content_image_file , content_image_plot , content_image_url
7
7
from ._content_pdf import content_pdf_file , content_pdf_url
8
8
from ._databricks import ChatDatabricks
46
46
"content_pdf_url" ,
47
47
"ContentToolRequest" ,
48
48
"ContentToolResult" ,
49
+ "ContentToolResultImage" ,
49
50
"interpolate" ,
50
51
"interpolate_file" ,
51
52
"Provider" ,
Original file line number Diff line number Diff line change 17
17
ContentText ,
18
18
ContentToolRequest ,
19
19
ContentToolResult ,
20
+ ContentToolResultImage ,
21
+ ContentToolResultResource ,
20
22
)
21
23
from ._logging import log_model_default
22
24
from ._provider import Provider , StandardModelParamNames , StandardModelParams
@@ -515,8 +517,26 @@ def _as_content_block(content: Content) -> "ContentBlockParam":
515
517
"tool_use_id" : content .id ,
516
518
"is_error" : content .error is not None ,
517
519
}
518
- # Anthropic supports non-text contents like ImageBlockParam
519
- res ["content" ] = content .get_model_value () # type: ignore
520
+
521
+ if isinstance (content , ContentToolResultImage ):
522
+ res ["content" ] = [
523
+ {
524
+ "type" : "image" ,
525
+ "source" : {
526
+ "type" : "base64" ,
527
+ "media_type" : content .mime_type ,
528
+ "data" : content .value ,
529
+ },
530
+ }
531
+ ]
532
+ elif isinstance (content , ContentToolResultResource ):
533
+ raise NotImplementedError (
534
+ "ContentToolResultResource is not currently supported by Anthropic."
535
+ )
536
+ else :
537
+ # Anthropic supports non-text contents like ImageBlockParam
538
+ res ["content" ] = content .get_model_value () # type: ignore
539
+
520
540
return res
521
541
522
542
raise ValueError (f"Unknown content type: { type (content )} " )
Original file line number Diff line number Diff line change 16
16
ContentText ,
17
17
ContentToolRequest ,
18
18
ContentToolResult ,
19
+ ContentToolResultImage ,
20
+ ContentToolResultResource ,
19
21
)
20
22
from ._logging import log_model_default
21
23
from ._merge import merge_dicts
@@ -424,6 +426,13 @@ def _as_part_type(self, content: Content) -> "Part":
424
426
)
425
427
)
426
428
elif isinstance (content , ContentToolResult ):
429
+ if isinstance (
430
+ content , (ContentToolResultImage , ContentToolResultResource )
431
+ ):
432
+ raise NotImplementedError (
433
+ "Tool results with images or resources aren't supported by Google (Gemini). "
434
+ )
435
+
427
436
if content .error :
428
437
resp = {"error" : content .error }
429
438
else :
Original file line number Diff line number Diff line change 18
18
ContentText ,
19
19
ContentToolRequest ,
20
20
ContentToolResult ,
21
+ ContentToolResultImage ,
22
+ ContentToolResultResource ,
21
23
)
22
24
from ._logging import log_model_default
23
25
from ._merge import merge_dicts
@@ -482,6 +484,12 @@ def _as_message_param(turns: list[Turn]) -> list["ChatCompletionMessageParam"]:
482
484
}
483
485
)
484
486
elif isinstance (x , ContentToolResult ):
487
+ if isinstance (
488
+ x , (ContentToolResultImage , ContentToolResultResource )
489
+ ):
490
+ raise NotImplementedError (
491
+ "OpenAI does not support tool results with images or resources."
492
+ )
485
493
tool_results .append (
486
494
ChatCompletionToolMessageParam (
487
495
# Currently, OpenAI only allows for text content in tool results
Original file line number Diff line number Diff line change 2
2
3
3
import httpx
4
4
import pytest
5
-
6
- from chatlas import ChatAnthropic , ContentToolResult
5
+ from chatlas import ChatAnthropic , ContentToolResultImage
7
6
8
7
from .conftest import (
9
8
assert_data_extraction ,
@@ -108,17 +107,10 @@ def get_picture():
108
107
# Local copy of https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png
109
108
with open (test_images_dir / "dice.png" , "rb" ) as image :
110
109
bytez = image .read ()
111
- res = [
112
- {
113
- "type" : "image" ,
114
- "source" : {
115
- "type" : "base64" ,
116
- "media_type" : "image/png" ,
117
- "data" : base64 .b64encode (bytez ).decode ("utf-8" ),
118
- },
119
- }
120
- ]
121
- return ContentToolResult (value = res , model_format = "as_is" )
110
+ return ContentToolResultImage (
111
+ value = base64 .b64encode (bytez ).decode ("utf-8" ),
112
+ mime_type = "image/png" ,
113
+ )
122
114
123
115
chat = ChatAnthropic ()
124
116
chat .register_tool (get_picture )
You can’t perform that action at this time.
0 commit comments