feat: add vision prompt script
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import base64
|
||||
import mimetypes
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
from openai import OpenAI
|
||||
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
IMAGE_PATH = BASE_DIR / "03_43431636157.jpg"
|
||||
|
||||
|
||||
def image_to_data_url(path):
|
||||
if not path.exists():
|
||||
raise FileNotFoundError("Image not found: {}".format(path))
|
||||
|
||||
mime_type = mimetypes.guess_type(str(path))[0] or "image/jpeg"
|
||||
image_base64 = base64.b64encode(path.read_bytes()).decode("ascii")
|
||||
return "data:{};base64,{}".format(mime_type, image_base64)
|
||||
|
||||
|
||||
client = OpenAI(
|
||||
base_url=os.environ.get("OPENAI_BASE_URL", "http://127.0.0.1:8080/v1"),
|
||||
api_key=os.environ.get("OPENAI_API_KEY", "pwd"),
|
||||
http_client=httpx.Client(trust_env=False),
|
||||
)
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model=os.environ.get("OPENAI_MODEL", "gpt-5.5"),
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": (
|
||||
"请观察这张图片中人物身上 T 恤胸前的印花。"
|
||||
"只回答印花是什么,包括图案形状、主要颜色、可能的文字或符号;"
|
||||
"如果文字看不清,请明确说明不要编造。"
|
||||
),
|
||||
},
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {"url": image_to_data_url(IMAGE_PATH)},
|
||||
},
|
||||
],
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
print(response.choices[0].message.content)
|
||||
Reference in New Issue
Block a user