| 329 | spec = datamodel_pb2.SandboxSpec(policy=_baseline_policy()) |
| 330 | |
| 331 | def call_external_openai_endpoint() -> str: |
| 332 | import json |
| 333 | import ssl |
| 334 | import urllib.error |
| 335 | import urllib.request |
| 336 | |
| 337 | body = json.dumps( |
| 338 | { |
| 339 | "model": "test-model", |
| 340 | "messages": [{"role": "user", "content": "hello"}], |
| 341 | } |
| 342 | ).encode() |
| 343 | |
| 344 | req = urllib.request.Request( |
| 345 | "https://api.openai.com/v1/chat/completions", |
| 346 | data=body, |
| 347 | headers={ |
| 348 | "Content-Type": "application/json", |
| 349 | "Authorization": "Bearer dummy", |
| 350 | }, |
| 351 | method="POST", |
| 352 | ) |
| 353 | ctx = ssl.create_default_context() |
| 354 | ctx.check_hostname = False |
| 355 | ctx.verify_mode = ssl.CERT_NONE |
| 356 | |
| 357 | try: |
| 358 | resp = urllib.request.urlopen(req, timeout=30, context=ctx) |
| 359 | return resp.read().decode() |
| 360 | except urllib.error.URLError as exc: |
| 361 | return f"url_error:{exc}" |
| 362 | except Exception as exc: |
| 363 | return f"error:{type(exc).__name__}:{exc}" |
| 364 | |
| 365 | with sandbox(spec=spec, delete_on_exit=True) as sb: |
| 366 | result = sb.exec_python(call_external_openai_endpoint, timeout_seconds=60) |