ofa_client

ofa Python client — call ofa –serve from user code.

Zero external dependencies. Pure Python stdlib. Copy this file into any Python environment on Kestrel (or symlink from $OFA_ROOT/src/ofa_client.py) and use:

from ofa_client import ask, Session

# One-shot, stateless
text = ask("summarise this plot", image="output/step_0100.png")

# Multi-turn, client-side history
sess = Session(model="ofa-code")
sess.ask("what turbulence model for cavity flow at Re=1e4?")
sess.ask("show me a controlDict for that")   # sees the previous turn

Auto-detects the running ofa –serve via, in order:

  1. Explicit url= / token= kwargs.

  2. $OFA_BYOK_URL / $OFA_BYOK_TOKEN environment variables.

  3. $OFA_SCRATCH/.ofa_serve_port and $OFA_SCRATCH/.ofa_api_key.

  4. /scratch/$USER/.ofa_serve_port and /scratch/$USER/.ofa_api_key.

Raises RuntimeError with a clear message if no server can be located. Import works in any Python 3.8+ interpreter regardless of what other packages are installed — the client’s only imports are stdlib.

Functions

ask(prompt, *[, image, images, context, ...])

Send a one-shot prompt (with optional images and file/text context) to ofa --serve and return the reply text.

Classes

Session(*[, model, url, token, timeout, ...])

Multi-turn chat session with client-side history.

ofa_client.ask(prompt, *, image=None, images=None, context=None, file=None, files=None, model='ofa-code', url=None, token=None, timeout=120.0, full_file=False)[source]

Send a one-shot prompt (with optional images and file/text context) to ofa --serve and return the reply text. Stateless — each call is a fresh session.

Parameters:
  • prompt (str) – The main question (required).

  • images (list | None) – Path (or list of paths) to local image files to attach.

  • context (str | None) – Inline text context to include verbatim in the user message.

  • files (list | None) – Path (or list of paths) to local text files whose contents get inlined (fenced with the filename). Only the last 32 KB of each file is included by default; pass full_file=True to override.

  • model (str) – One of MODEL_IDS. Default "ofa-code".

  • timeout (float) – Overrides for auto-detection (see module docstring).

  • full_file (bool) – If True, read entire file= / files= contents instead of only the last 32 KB.

  • image (str | PathLike | None)

  • images

  • file (str | PathLike | None)

  • files

  • url (str | None)

  • token (str | None)

  • timeout

Return type:

str

class ofa_client.Session(*, model='ofa-code', url=None, token=None, timeout=120.0, full_file=False)[source]

Bases: object

Multi-turn chat session with client-side history.

Each .ask() call appends to the internal messages list and sends the whole thing on the next request, so the model sees prior context:

sess = Session(model="ofa-code")
sess.ask("what turbulence model for cavity flow?")
sess.ask("show me a controlDict for that")   # sees turn 1

Server-side state is deliberately NOT used: keeping the history client-side means it survives ofa --serve restarts, has no server memory footprint, and matches the OpenAI protocol shape.

Parameters:
ask(prompt, *, image=None, images=None, context=None, file=None, files=None)[source]

Same shape as ofa_client.ask, but appends to and consults this session’s message history.

Parameters:
Return type:

str

clear()[source]

Forget all prior turns (start a fresh conversation).

Return type:

None