-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtesting.py
More file actions
59 lines (52 loc) · 2.02 KB
/
Copy pathtesting.py
File metadata and controls
59 lines (52 loc) · 2.02 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import base64
import os
import openai
############################################################################
# 1. Set API key and base URL correctly
############################################################################
openai.api_key = os.getenv("OPENAI_API_KEY")
# Ensure the correct base URL (without extra /chat/completions)
openai.api_base = os.getenv("GPT4O_API_BASE", "https://api.openai.com/v1")
############################################################################
# 2. Encode the screenshot properly
############################################################################
image_file_path = "test_screenshot.png"
if not os.path.exists(image_file_path):
raise FileNotFoundError(f"Screenshot not found: {image_file_path}")
with open(image_file_path, "rb") as f:
encoded_image = base64.b64encode(f.read()).decode("utf-8")
############################################################################
# 3. Construct messages using correct OpenAI format
############################################################################
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": (
"Here is an image. Please describe exactly what you see in it. "
"What color is the largest section and any major text visible?"
)
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{encoded_image}"
}
}
]
}
]
############################################################################
# 4. Make the ChatCompletion call using the correct format
############################################################################
try:
response = openai.ChatCompletion.create(
model="gpt-4o",
messages=messages,
max_tokens=300
)
print(response.choices[0].message.content)
except Exception as e:
print("Error from GPT-4o endpoint:", e)