forked from openai/openai-cua-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
78 lines (60 loc) · 2.03 KB
/
utils.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import requests
from dotenv import load_dotenv
import json
import base64
from PIL import Image
from io import BytesIO
import io
from urllib.parse import urlparse
load_dotenv(override=True)
BLOCKED_DOMAINS = [
"maliciousbook.com",
"evilvideos.com",
"darkwebforum.com",
"shadytok.com",
"suspiciouspins.com",
"ilanbigio.com",
]
def pp(obj):
print(json.dumps(obj, indent=4))
def show_image(base_64_image):
image_data = base64.b64decode(base_64_image)
image = Image.open(BytesIO(image_data))
image.show()
def calculate_image_dimensions(base_64_image):
image_data = base64.b64decode(base_64_image)
image = Image.open(io.BytesIO(image_data))
return image.size
def sanitize_message(msg: dict) -> dict:
"""Return a copy of the message with image_url omitted for computer_call_output messages."""
if msg.get("type") == "computer_call_output":
output = msg.get("output", {})
if isinstance(output, dict):
sanitized = msg.copy()
sanitized["output"] = {**output, "image_url": "[omitted]"}
return sanitized
return msg
def create_response(**kwargs):
url = "https://api.openai.com/v1/responses"
headers = {
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}",
"Content-Type": "application/json",
# TODO: remove for launch
"Openai-beta": "responses=v1",
}
openai_org = os.getenv("OPENAI_ORG")
if openai_org:
headers["Openai-Organization"] = openai_org
response = requests.post(url, headers=headers, json=kwargs)
if response.status_code != 200:
print(f"Error: {response.status_code} {response.text}")
return response.json()
def check_blocklisted_url(url: str) -> None:
"""Raise ValueError if the given URL (including subdomains) is in the blocklist."""
hostname = urlparse(url).hostname or ""
if any(
hostname == blocked or hostname.endswith(f".{blocked}")
for blocked in BLOCKED_DOMAINS
):
raise ValueError(f"Blocked URL: {url}")