|
16 | 16 |
|
17 | 17 | from __future__ import annotations
|
18 | 18 |
|
| 19 | +import base64 |
19 | 20 | from functools import cached_property
|
20 | 21 | import logging
|
21 | 22 | import os
|
|
45 | 46 |
|
46 | 47 | logger = logging.getLogger("google_adk." + __name__)
|
47 | 48 |
|
48 |
| -MAX_TOKEN = 1024 |
| 49 | +MAX_TOKEN = 8192 |
49 | 50 |
|
50 | 51 |
|
51 | 52 | class ClaudeRequest(BaseModel):
|
@@ -105,15 +106,53 @@ def part_to_message_block(
|
105 | 106 | content=content,
|
106 | 107 | is_error=False,
|
107 | 108 | )
|
108 |
| - raise NotImplementedError("Not supported yet.") |
| 109 | + if ( |
| 110 | + part.inline_data |
| 111 | + and part.inline_data.mime_type |
| 112 | + and part.inline_data.mime_type.startswith("image") |
| 113 | + ): |
| 114 | + data = base64.b64encode(part.inline_data.data).decode() |
| 115 | + return anthropic_types.ImageBlockParam( |
| 116 | + type="image", |
| 117 | + source=dict( |
| 118 | + type="base64", media_type=part.inline_data.mime_type, data=data |
| 119 | + ), |
| 120 | + ) |
| 121 | + if part.executable_code: |
| 122 | + return anthropic_types.TextBlockParam( |
| 123 | + type="text", |
| 124 | + text="Code:```python\n" + part.executable_code.code + "\n```", |
| 125 | + # language=part.executable_code.language, |
| 126 | + ) |
| 127 | + if part.code_execution_result: |
| 128 | + return anthropic_types.TextBlockParam( |
| 129 | + text="Execution Result:```code_output\n" |
| 130 | + + part.code_execution_result.output |
| 131 | + + "\n```", |
| 132 | + type="text", |
| 133 | + ) |
| 134 | + |
| 135 | + raise NotImplementedError(f"Not supported yet: {part}") |
109 | 136 |
|
110 | 137 |
|
111 | 138 | def content_to_message_param(
|
112 | 139 | content: types.Content,
|
113 | 140 | ) -> anthropic_types.MessageParam:
|
| 141 | + message_block = [] |
| 142 | + for part in content.parts or []: |
| 143 | + # Image data is not supported in Claude for model turns. |
| 144 | + if ( |
| 145 | + part.inline_data |
| 146 | + and part.inline_data.mime_type |
| 147 | + and part.inline_data.mime_type.startswith("image") |
| 148 | + ): |
| 149 | + continue |
| 150 | + |
| 151 | + message_block.append(part_to_message_block(part)) |
| 152 | + |
114 | 153 | return {
|
115 | 154 | "role": to_claude_role(content.role),
|
116 |
| - "content": [part_to_message_block(part) for part in content.parts or []], |
| 155 | + "content": message_block, |
117 | 156 | }
|
118 | 157 |
|
119 | 158 |
|
|
0 commit comments