Files
wehub-resource-sync 0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

115 lines
3.4 KiB
Python

import base64
import typing
from urllib.request import urlopen
# Type definitions for improved code readability
class Vars(typing.TypedDict):
image_url: str
class Provider(typing.TypedDict):
id: str
label: typing.Optional[str]
class PromptFunctionContext(typing.TypedDict):
vars: Vars
provider: Provider
def get_image_base64(image_url: str) -> tuple[str, str]:
"""
Fetch an image from a URL and convert it to a base64-encoded string.
Args:
image_url (str): The URL of the image to fetch.
Returns:
tuple[str, str]: The base64-encoded image data and media type.
"""
with urlopen(image_url) as response:
media_type = response.headers.get("Content-Type", "image/jpeg").split(";")[0]
return base64.b64encode(response.read()).decode("utf-8"), media_type
# System prompt for image description task
system_prompt = "Describe the image in a few words"
def format_image_prompt(context: PromptFunctionContext) -> list[dict[str, typing.Any]]:
"""
Format the prompt for image analysis based on the AI provider.
This function generates a formatted prompt for different AI providers,
tailoring the structure based on the provider's requirements.
Args:
context (PromptFunctionContext): A dictionary containing provider information and variables.
Returns:
list[dict[str, typing.Any]]: A list of dictionaries representing the formatted prompt.
Raises:
ValueError: If an unsupported provider is specified.
"""
provider_id = context["provider"]["id"]
if (
provider_id.startswith("bedrock:anthropic")
or provider_id.startswith("bedrock:us.anthropic")
or provider_id.startswith("anthropic:")
):
image_data, media_type = get_image_base64(context["vars"]["image_url"])
return [
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": media_type,
"data": image_data,
},
}
],
},
]
if provider_id.startswith("google:gemini"):
image_data, media_type = get_image_base64(context["vars"]["image_url"])
return [
{
"parts": [
{
"inline_data": {
"mime_type": media_type,
"data": image_data,
}
},
{"text": system_prompt},
]
}
]
# label might not exist
if context["provider"].get("label") == "custom label for gpt-4.1":
return [
{
"role": "system",
"content": [{"type": "text", "text": system_prompt}],
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": context["vars"]["image_url"],
},
}
],
},
]
raise ValueError(f"Unsupported provider: {context['provider']}")