| 200 | spec = datamodel_pb2.SandboxSpec(policy=_baseline_policy()) |
| 201 | |
| 202 | def call_chat_completions() -> str: |
| 203 | import json |
| 204 | import ssl |
| 205 | import urllib.request |
| 206 | |
| 207 | body = json.dumps( |
| 208 | { |
| 209 | "model": "test-model", |
| 210 | "messages": [{"role": "user", "content": "hello"}], |
| 211 | } |
| 212 | ).encode() |
| 213 | |
| 214 | req = urllib.request.Request( |
| 215 | "https://inference.local/v1/chat/completions", |
| 216 | data=body, |
| 217 | headers={ |
| 218 | "Content-Type": "application/json", |
| 219 | "Authorization": "Bearer dummy-key", |
| 220 | }, |
| 221 | method="POST", |
| 222 | ) |
| 223 | # The proxy will TLS-terminate, so we need to accept its cert. |
| 224 | ctx = ssl.create_default_context() |
| 225 | ctx.check_hostname = False |
| 226 | ctx.verify_mode = ssl.CERT_NONE |
| 227 | |
| 228 | resp = urllib.request.urlopen(req, timeout=30, context=ctx) |
| 229 | return resp.read().decode() |
| 230 | |
| 231 | with sandbox(spec=spec, delete_on_exit=True) as sb: |
| 232 | result = sb.exec_python(call_chat_completions, timeout_seconds=60) |