Skip to Content

OpenAI

Call AI from your app’s server code with manh/openai. Same calls as the official openai SDK, no API key needed.

Usage

app/tickets/repository.ts
import { OpenAI } from "manh/openai"; const openai = new OpenAI(); export async function suggestReply(ticket: { subject: string; body: string }) { try { const r = await openai.chat.completions.create({ model: "gpt-4o-mini", messages: [ { role: "system", content: "You are a support agent. Answer briefly and politely." }, { role: "user", content: `${ticket.subject}\n\n${ticket.body}` }, ], max_tokens: 400, }); return r.choices[0].message.content ?? null; } catch { return null; // AI only assists: the feature still works when AI fails } }

Use it in .ts files only.

Signature

manh/openai
class OpenAI { constructor(opts?: object); chat: { completions: { create(body: ChatCompletionCreateParams): Promise<ChatCompletion> } }; embeddings: { create(body: EmbeddingCreateParams): Promise<CreateEmbeddingResponse> }; }

OpenAI extends the official openai SDK. manh.run sets the key and the server address; you don’t pass apiKey.

Parameters

chat.completions.create and embeddings.create take the same parameters as the official SDK. Differences:

  • model: write the usual OpenAI names and manh.run maps them:
    • gpt-4o-mini, gpt-4.1-mini, gpt-5-mini, gpt-3.5*, o*-mini → gpt-oss-20b, fast and cheap, the default choice;
    • other gpt-* names (such as gpt-4o) → gpt-oss-120b, stronger reasoning;
    • text-embedding-* → bge-m3, multilingual, reads Vietnamese;
    • any other name → 400 BUILTIN_MODEL_NOT_ALLOWED.
  • max_tokens: at most 4,096; 4,096 when omitted.
  • n: always 1.
  • reasoning_effort: "low" unless you set it.
  • A request body over 256 KB fails with 413 BUILTIN_TOO_LARGE.

Returns

The same as the official SDK: chat.completions.create returns choices[0].message (with tool_calls when you pass tools) and can stream; embeddings.create returns data[i].embedding.

The models reason before answering. Always check finish_reason and treat empty content as a failure.

Examples

Function calling (tools): the model returns tool_calls, you run them through your repository and send the results back, for about 5 rounds at most.

app/assistant/repository.ts
const tools = [{ type: "function", function: { name: "lookupOrder", description: "Find an order by id", parameters: { type: "object", properties: { id: { type: "number" } }, required: ["id"] } } }]; const messages: any[] = [{ role: "user", content: "Summarise order 123" }]; for (let i = 0; i < 5; i++) { const r = await openai.chat.completions.create({ model: "gpt-4o-mini", messages, tools }); const msg = r.choices[0].message; messages.push(msg); if (!msg.tool_calls?.length) return { text: msg.content }; for (const call of msg.tool_calls) { const args = JSON.parse(call.function.arguments); const out = call.function.name === "lookupOrder" ? await getOrder(user, args.id) : null; messages.push({ role: "tool", tool_call_id: call.id, content: JSON.stringify(out) }); } }
  • Not available: images, audio, moderations, files (501 BUILTIN_UNSUPPORTED), and images inside messages. Don’t build features on them.
  • AI is metered in tokens, with a monthly budget per plan shared by every app of the account. Over the budget, calls fail with 429 BUILTIN_QUOTA_EXCEEDED until next month; the app keeps running. Send only the text the task needs and set a max_tokens that fits the answer.
  • Don’t use the Vercel AI SDK (ai, @ai-sdk/*): it needs your own provider key.

Next steps

  • Email, AI, file storage: the built-in services used together in one app.
  • enqueue: move a long AI call to a background job so the request answers at once.
Last updated