|
1 | 1 | from __future__ import annotations as _annotations
|
2 | 2 |
|
3 |
| -import base64 |
4 | 3 | import io
|
5 | 4 | from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator
|
6 | 5 | from contextlib import asynccontextmanager
|
@@ -354,48 +353,13 @@ async def _map_user_prompt(
|
354 | 353 | else:
|
355 | 354 | raise RuntimeError('Only images and PDFs are supported for binary content')
|
356 | 355 | elif isinstance(item, ImageUrl):
|
357 |
| - try: |
358 |
| - response = await cached_async_http_client().get(item.url) |
359 |
| - response.raise_for_status() |
360 |
| - yield ImageBlockParam( |
361 |
| - source={ |
362 |
| - 'data': io.BytesIO(response.content), |
363 |
| - 'media_type': item.media_type, |
364 |
| - 'type': 'base64', |
365 |
| - }, |
366 |
| - type='image', |
367 |
| - ) |
368 |
| - except ValueError: |
369 |
| - # Download the file if can't find the mime type. |
370 |
| - client = cached_async_http_client() |
371 |
| - response = await client.get(item.url, follow_redirects=True) |
372 |
| - response.raise_for_status() |
373 |
| - base64_encoded = base64.b64encode(response.content).decode('utf-8') |
374 |
| - if (mime_type := response.headers['Content-Type']) in ( |
375 |
| - 'image/jpeg', |
376 |
| - 'image/png', |
377 |
| - 'image/gif', |
378 |
| - 'image/webp', |
379 |
| - ): |
380 |
| - yield ImageBlockParam( |
381 |
| - source={'data': base64_encoded, 'media_type': mime_type, 'type': 'base64'}, |
382 |
| - type='image', |
383 |
| - ) |
384 |
| - else: # pragma: no cover |
385 |
| - raise RuntimeError(f'Unsupported image type: {mime_type}') |
| 356 | + yield ImageBlockParam(source={'type': 'url', 'url': item.url}, type='image') |
386 | 357 | elif isinstance(item, DocumentUrl):
|
387 |
| - response = await cached_async_http_client().get(item.url) |
388 |
| - response.raise_for_status() |
389 | 358 | if item.media_type == 'application/pdf':
|
390 |
| - yield DocumentBlockParam( |
391 |
| - source=Base64PDFSourceParam( |
392 |
| - data=io.BytesIO(response.content), |
393 |
| - media_type=item.media_type, |
394 |
| - type='base64', |
395 |
| - ), |
396 |
| - type='document', |
397 |
| - ) |
| 359 | + yield DocumentBlockParam(source={'url': item.url, 'type': 'url'}, type='document') |
398 | 360 | elif item.media_type == 'text/plain':
|
| 361 | + response = await cached_async_http_client().get(item.url) |
| 362 | + response.raise_for_status() |
399 | 363 | yield DocumentBlockParam(
|
400 | 364 | source=PlainTextSourceParam(data=response.text, media_type=item.media_type, type='text'),
|
401 | 365 | type='document',
|
|
0 commit comments