ofa_server¶
OpenAI-compatible HTTP server for ofa (BYOK shim for VS Code, etc.).
- Exposes:
GET /v1/models — advertises the ofa-* model IDs POST /v1/chat/completions — OpenAI-format chat with SSE streaming GET /healthz — liveness probe (no auth)
- What each /v1/chat/completions request does:
Authenticate via Authorization: Bearer <token> (token read from a keyfile created on first –serve run).
- Pick the ofa “mode” from the model field:
ofa-openfoam, ofa-hpc, ofa-code, ofa-amrex, ofa-marbles, ofa-quantum-computing, ofa-vasp, ofa-reframe
Build the system prompt via
ofa_main.load_system_prompt(mode)so long-term prefs + lessons are injected exactly the same way the CLI does.Run RAG on the user’s last message via the same retriever the CLI uses for that mode, fence the result via
_fence_rag, and prepend it to the user message.Stream the assistant response via
ofa_main.chat_streamand repackage each chunk as an OpenAI SSE data: { … } line.
- What we deliberately do NOT do in v1:
Tool-calling. We advertise toolCalling: false in the BYOK config so VS Code uses ofa as a smart chat backend, not as an agent that expects OpenAI function-call JSON.
/v1/embeddings. Out of scope; Copilot or another provider can handle embeddings if VS Code needs them.
Concurrency: ThreadingHTTPServer with a per-request handler. Ollama itself serialises generation under the hood; our handler is mostly I/O forwarding, so threads are cheap and prevent one slow request from blocking the readiness probe.
Functions
|
Return the bearer token stored at |
|
Start the BYOK server. |
- ofa_server.load_or_create_api_key(path)[source]¶
Return the bearer token stored at
path, creating it (0o600) if the file doesn’t exist. A missing parent directory is created too.
- ofa_server.serve(host='0.0.0.0', port=None, api_key_file=None, no_auth=False, local_port=None, enable_tools=False, quiet=False)[source]¶
Start the BYOK server. Blocks until Ctrl+C.
- Parameters:
host (str) – Address to bind. Default
0.0.0.0so that anssh -Lthrough Kestrel’s login node can reach the compute-node socket (127.0.0.1is unreachable across that hop). The bearer token keeps this safe on Kestrel’s internal network. Pass127.0.0.1only when client + server run on the same machine.port (int | None) – TCP port on the server side.
None(default) uses a per-user-stable random port in 40000-49999 (persisted to$OFA_SCRATCH/.ofa_serve_portso the BYOK URL stays valid across restarts). Kestrel compute nodes can host up to 4 users (quarter-node GPU allocations), so picking from a 10000-port range keeps the collision probability near-zero. Pass0to let the OS pick (different port each restart) or a specific integer to pin.api_key_file (str | None) – Path to the bearer-token file. Created with mode 0o600 on first run. Defaults to
$OFA_SCRATCH/.ofa_api_key.no_auth (bool) – Skip the Authorization check. ONLY for local testing.
local_port (int | None) – Suggested laptop-side port for the printed
ssh -Lline and BYOK URL.None(default) uses a per-user-stable random port in the 49200-64200 range (persisted to scratch so the VS Code config doesn’t have to change between runs).enable_tools (bool) – Forward OpenAI-format
tools/tool_choicefrom incoming requests to Ollama and translatetool_callsresponses back to OpenAI SSE format. Lets VS Code’s Agent mode chain file edits / terminal commands through one approval gate instead of click-per-block. Off by default because local 31B models can emit malformed JSON for VS Code’s complex tool schemas.quiet (bool)
- Return type:
None