nustack
ReferenceFabrics

nu.llm

Chat-completions fabric: one OpenAI-compatible wire (/v1/chat/completions) over httpx, N providers behind convenience presets. One fabric equals one endpoint (base_url + api_key + default model); calls travel through a ChatRef on a Service and land as a Chat interaction. Prefer nu.arun — LLM calls are network-bound and block the event loop under sync.

Fabric

from nu.llm import LLMFabric

NameSortSignatureEffectMeaning
LLMFabricclassLLMFabric(*, base_url, api_key="", model="", timeout=120.0, headers=None)actionhttpx client for an OpenAI-compat chat/completions endpoint; sync chat + async achat

Refs

from nu.llm import ChatRef

NameSortSignatureEffectMeaning
ChatRefclassChatRef.method(**defaults)puredeclare a chat endpoint on a Service; defaults merge with per-call overrides

Call the ref with prompt="..." (sugar for a single user message) or messages=[{"role": ..., "content": ...}, ...]; extras (model, temperature, max_tokens, stop, ...) pass straight into the request body.

Interactions

from nu.llm import Chat

NameSortSignatureEffectMeaning
ChatcallableChat(ref, kwargs)actionone chat/completions call; yields dict with text + message + model + usage + finish_reason

Providers

from nu.llm import bind, ollama, openai, openrouter, groq, cerebras, xai, vllm

bind is the generic Provide factory; the rest fill base_url + api_key for a known endpoint. Each returns a Provide tagged by service_cls so multiple services can each get their own fabric.

NameSortSignatureEffectMeaning
bindfunctionbind(service_cls, **defaults)puregeneric Provide of an LLMFabric tagged by service_cls
ollamafunctionollama(service_cls, *, host="localhost", port=11434, model="", timeout=120.0)pureOllama on http://{host}:{port}/v1
openaifunctionopenai(service_cls, *, api_key, model="gpt-4o-mini")pureOpenAI api.openai.com
openrouterfunctionopenrouter(service_cls, *, api_key, model)pureOpenRouter openrouter.ai — thousands of models, one key
groqfunctiongroq(service_cls, *, api_key, model="llama-3.3-70b-versatile")pureGroq api.groq.com — fast inference
cerebrasfunctioncerebras(service_cls, *, api_key, model)pureCerebras api.cerebras.ai
xaifunctionxai(service_cls, *, api_key, model="grok-2-latest")purexAI api.x.ai (Grok)
vllmfunctionvllm(service_cls, *, base_url, model, api_key="")pureself-hosted vLLM at base_url (e.g. http://red:8000)

On this page