@bantai-dev/openai
v1.0.0
Published
OpenAI provider for @bantai-dev/llm
Maintainers
Readme
@bantai-dev/openai
OpenAI provider for @bantai-dev/llm. Uses the OpenAI Responses API with optional Zod-based structured output.
Features
- OpenAI Responses API – Uses
openai.responses.create()for chat and structured output. - Zod structured output – Pass
outputSchemain the LLM input; responses are validated and typed. - Policy & token quotas – Use with
generateText()from@bantai-dev/llmand policies/context from core.
Installation
pnpm add @bantai-dev/openai @bantai-dev/llm openai zodPeer dependencies
zod^4.3.5
Usage
Basic
import { openai } from "@bantai-dev/openai";
import { generateText, withLLMContext } from "@bantai-dev/llm";
import { defineContext, definePolicy } from "@bantai-dev/core";
import { z } from "zod";
const appContext = defineContext(
z.object({
userId: z.string().optional(),
tier: z.enum(["free", "premium"]),
})
);
const llmContext = withLLMContext(appContext, { storage }); // storage: see @bantai-dev/llm README
const policy = definePolicy(llmContext, "My Policy", [/* rules */]);
const provider = openai("gpt-4o");
const result = await generateText({
provider,
policies: [policy],
input: {
llm: {
prompt: [{ role: "user", content: "Hello" }],
maxTokensPerRequest: 512,
outputSchema: z.object({ answer: z.string() }),
},
tier: "free",
userId: "user-1",
},
});
console.log(result.output); // { answer: "..." }
console.log(result.usage); // { inputTokens, outputTokens, totalTokens }With client options
import { openai } from "@bantai-dev/openai";
const provider = openai("gpt-4o", {
providerOptions: {
apiKey: process.env.OPENAI_API_KEY,
// other OpenAI ClientOptions
},
});Prompt formats
- String – Converted to a single user message:
prompt: "Hello". - Messages – Array of
{ role: "user" | "system" | "assistant", content: string }passed to the Responses API.
API
openai(model, options?)
Creates an LLMProvider for the OpenAI Responses API.
- model – OpenAI chat model id (e.g.
"gpt-4o","gpt-4o-mini"). See OpenAI models. - options
- providerOptions – Optional OpenAI ClientOptions (e.g.
apiKey,baseURL).
- providerOptions – Optional OpenAI ClientOptions (e.g.
Returns an adapter with:
providerName:"openai"defaultModel: the givenmodelgenerateText(input, options)– Callsopenai.responses.create()with:inputfromconvertPromptToOpenAIMessages(input.llm.prompt)- Optional
text: { format: zodTextFormat(outputSchema, "output") }wheninput.llm.outputSchemais set
convertPromptToOpenAIMessages(prompt)
Utility to convert the LLM prompt (string or message array) to the format expected by openai.responses.create() input. Re-exported for use in custom wrappers if needed.
Environment
OPENAI_API_KEY– Set for the OpenAI client (or pass viaproviderOptions.apiKey).
Related
- @bantai-dev/llm – Context, token quota rules,
generateText/streamText. - OpenAI Node SDK – Underlying client.
License
MIT
