-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvision.py
More file actions
40 lines (36 loc) · 1.07 KB
/
Copy pathvision.py
File metadata and controls
40 lines (36 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
视觉理解 - 上传图片让模型分析
"""
import base64
from openai import OpenAI
client = OpenAI(api_key="sk-xxx", base_url="http://xdhdancer.top/v1")
# 方法 1:用图片 URL
resp = client.chat.completions.create(
model="claude-opus-4-7",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "图里是什么?"},
{"type": "image_url", "image_url": {
"url": "https://example.com/sample.jpg"
}},
],
}],
)
print(resp.choices[0].message.content)
# 方法 2:本地图片转 base64
def image_to_data_url(path: str) -> str:
with open(path, "rb") as f:
b64 = base64.b64encode(f.read()).decode()
return f"data:image/jpeg;base64,{b64}"
resp = client.chat.completions.create(
model="gpt-5",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image."},
{"type": "image_url", "image_url": {"url": image_to_data_url("./photo.jpg")}},
],
}],
)
print(resp.choices[0].message.content)