@bantai-dev/openai 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +112 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# @bantai-dev/openai
|
|
2
|
+
|
|
3
|
+
OpenAI provider for [@bantai-dev/llm](../llm). Uses the OpenAI Responses API with optional Zod-based structured output.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **OpenAI Responses API** – Uses `openai.responses.create()` for chat and structured output.
|
|
8
|
+
- **Zod structured output** – Pass `outputSchema` in the LLM input; responses are validated and typed.
|
|
9
|
+
- **Policy & token quotas** – Use with `generateText()` from `@bantai-dev/llm` and policies/context from core.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add @bantai-dev/openai @bantai-dev/llm openai zod
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Peer dependencies
|
|
18
|
+
|
|
19
|
+
- `zod` ^4.3.5
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Basic
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { openai } from "@bantai-dev/openai";
|
|
27
|
+
import { generateText, withLLMContext } from "@bantai-dev/llm";
|
|
28
|
+
import { defineContext, definePolicy } from "@bantai-dev/core";
|
|
29
|
+
import { z } from "zod";
|
|
30
|
+
|
|
31
|
+
const appContext = defineContext(
|
|
32
|
+
z.object({
|
|
33
|
+
userId: z.string().optional(),
|
|
34
|
+
tier: z.enum(["free", "premium"]),
|
|
35
|
+
})
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const llmContext = withLLMContext(appContext, { storage }); // storage: see @bantai-dev/llm README
|
|
39
|
+
const policy = definePolicy(llmContext, "My Policy", [/* rules */]);
|
|
40
|
+
|
|
41
|
+
const provider = openai("gpt-4o");
|
|
42
|
+
|
|
43
|
+
const result = await generateText({
|
|
44
|
+
provider,
|
|
45
|
+
policies: [policy],
|
|
46
|
+
input: {
|
|
47
|
+
llm: {
|
|
48
|
+
prompt: [{ role: "user", content: "Hello" }],
|
|
49
|
+
maxTokensPerRequest: 512,
|
|
50
|
+
outputSchema: z.object({ answer: z.string() }),
|
|
51
|
+
},
|
|
52
|
+
tier: "free",
|
|
53
|
+
userId: "user-1",
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
console.log(result.output); // { answer: "..." }
|
|
58
|
+
console.log(result.usage); // { inputTokens, outputTokens, totalTokens }
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### With client options
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { openai } from "@bantai-dev/openai";
|
|
65
|
+
|
|
66
|
+
const provider = openai("gpt-4o", {
|
|
67
|
+
providerOptions: {
|
|
68
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
69
|
+
// other OpenAI ClientOptions
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Prompt formats
|
|
75
|
+
|
|
76
|
+
- **String** – Converted to a single user message: `prompt: "Hello"`.
|
|
77
|
+
- **Messages** – Array of `{ role: "user" | "system" | "assistant", content: string }` passed to the Responses API.
|
|
78
|
+
|
|
79
|
+
## API
|
|
80
|
+
|
|
81
|
+
### `openai(model, options?)`
|
|
82
|
+
|
|
83
|
+
Creates an `LLMProvider` for the OpenAI Responses API.
|
|
84
|
+
|
|
85
|
+
- **model** – OpenAI chat model id (e.g. `"gpt-4o"`, `"gpt-4o-mini"`). See [OpenAI models](https://platform.openai.com/docs/models).
|
|
86
|
+
- **options**
|
|
87
|
+
- **providerOptions** – Optional [OpenAI ClientOptions](https://github.com/openai/openai-node#configuration) (e.g. `apiKey`, `baseURL`).
|
|
88
|
+
|
|
89
|
+
Returns an adapter with:
|
|
90
|
+
|
|
91
|
+
- `providerName`: `"openai"`
|
|
92
|
+
- `defaultModel`: the given `model`
|
|
93
|
+
- `generateText(input, options)` – Calls `openai.responses.create()` with:
|
|
94
|
+
- `input` from `convertPromptToOpenAIMessages(input.llm.prompt)`
|
|
95
|
+
- Optional `text: { format: zodTextFormat(outputSchema, "output") }` when `input.llm.outputSchema` is set
|
|
96
|
+
|
|
97
|
+
### `convertPromptToOpenAIMessages(prompt)`
|
|
98
|
+
|
|
99
|
+
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.
|
|
100
|
+
|
|
101
|
+
## Environment
|
|
102
|
+
|
|
103
|
+
- `OPENAI_API_KEY` – Set for the OpenAI client (or pass via `providerOptions.apiKey`).
|
|
104
|
+
|
|
105
|
+
## Related
|
|
106
|
+
|
|
107
|
+
- [@bantai-dev/llm](../llm) – Context, token quota rules, `generateText`/`streamText`.
|
|
108
|
+
- [OpenAI Node SDK](https://github.com/openai/openai-node) – Underlying client.
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as zod from 'zod';
|
|
2
|
+
import * as zod_v4_core from 'zod/v4/core';
|
|
3
|
+
import { LLMProvider, WithLLMContext } from '@bantai-dev/llm';
|
|
4
|
+
import { ClientOptions } from 'openai';
|
|
5
|
+
import { ChatModel } from 'openai/resources/shared';
|
|
6
|
+
|
|
7
|
+
type OpenAiOptions = {
|
|
8
|
+
providerOptions?: ClientOptions;
|
|
9
|
+
};
|
|
10
|
+
declare const openai: (model: ChatModel, options?: OpenAiOptions) => LLMProvider<WithLLMContext, unknown, zod.ZodObject<Readonly<{
|
|
11
|
+
[k: string]: zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>;
|
|
12
|
+
}>, zod_v4_core.$strip>, unknown>;
|
|
13
|
+
|
|
14
|
+
export { type OpenAiOptions, openai };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { OpenAI } from 'openai';
|
|
2
|
+
import { zodTextFormat } from 'openai/helpers/zod.mjs';
|
|
3
|
+
|
|
4
|
+
// src/provider.ts
|
|
5
|
+
|
|
6
|
+
// src/utils.ts
|
|
7
|
+
function convertPromptToOpenAIMessages(prompt) {
|
|
8
|
+
return typeof prompt === "string" ? [{ role: "user", content: prompt }] : prompt.map((p) => ({
|
|
9
|
+
role: p.role,
|
|
10
|
+
content: p.content
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/provider.ts
|
|
15
|
+
var openai = (model, options) => {
|
|
16
|
+
const openaiClient = new OpenAI({
|
|
17
|
+
...options?.providerOptions || {}
|
|
18
|
+
});
|
|
19
|
+
const adapter = {
|
|
20
|
+
providerName: "openai",
|
|
21
|
+
defaultModel: model,
|
|
22
|
+
generateText: async (input) => {
|
|
23
|
+
const messages = convertPromptToOpenAIMessages(input.llm.prompt);
|
|
24
|
+
const response = await openaiClient.responses.create({
|
|
25
|
+
model: input.llm.model || model,
|
|
26
|
+
input: messages,
|
|
27
|
+
...input.llm.outputSchema && {
|
|
28
|
+
text: {
|
|
29
|
+
format: zodTextFormat(input.llm.outputSchema, "output")
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
output: response.output_text,
|
|
35
|
+
usage: {
|
|
36
|
+
inputTokens: response.usage?.input_tokens || 0,
|
|
37
|
+
outputTokens: response.usage?.output_tokens || 0,
|
|
38
|
+
totalTokens: response.usage?.total_tokens || 0
|
|
39
|
+
},
|
|
40
|
+
providerResponse: response
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return adapter;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { openai };
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/provider.ts"],"names":[],"mappings":";;;;;;AAGO,SAAS,8BACZ,MAAA,EACa;AACb,EAAA,OAAO,OAAO,MAAA,KAAW,QAAA,GACnB,CAAC,EAAE,IAAA,EAAM,MAAA,EAAQ,OAAA,EAAS,MAAA,EAAQ,CAAA,GAClC,MAAA,CAAO,G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","file":"index.js","sourcesContent":["import { LLMGenerateTextInput } from \"@bantai-dev/llm\";\nimport { ResponseInput } from \"openai/resources/responses/responses.js\";\n\nexport function convertPromptToOpenAIMessages(\n prompt: LLMGenerateTextInput[\"prompt\"]\n): ResponseInput {\n return typeof prompt === \"string\"\n ? [{ role: \"user\", content: prompt }]\n : prompt.map((p) => ({\n role: p.role,\n content: p.content,\n }));\n}\n","import { LLMProvider, WithLLMContext } from \"@bantai-dev/llm\";\nimport { ClientOptions, OpenAI } from \"openai\";\nimport { zodTextFormat } from \"openai/helpers/zod.mjs\";\nimport { ResponseInput } from \"openai/resources/responses/responses.js\";\nimport { ChatModel } from \"openai/resources/shared\";\nimport { convertPromptToOpenAIMessages } from \"./utils.js\";\n\nexport type OpenAiOptions = {\n providerOptions?: ClientOptions;\n};\nexport const openai = (model: ChatModel, options?: OpenAiOptions) => {\n const openaiClient = new OpenAI({\n ...(options?.providerOptions || {}),\n });\n\n const adapter: LLMProvider<WithLLMContext> = {\n providerName: \"openai\",\n defaultModel: model,\n generateText: async (input) => {\n const messages: ResponseInput = convertPromptToOpenAIMessages(input.llm.prompt);\n\n const response = await openaiClient.responses.create({\n model: input.llm.model || model,\n input: messages,\n ...(input.llm.outputSchema && {\n text: {\n format: zodTextFormat(input.llm.outputSchema, \"output\"),\n },\n }),\n });\n\n return {\n output: response.output_text,\n usage: {\n inputTokens: response.usage?.input_tokens || 0,\n outputTokens: response.usage?.output_tokens || 0,\n totalTokens: response.usage?.total_tokens || 0,\n },\n providerResponse: response,\n };\n },\n };\n return adapter;\n};\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bantai-dev/openai",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "OpenAI provider for @bantai-dev/llm",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/bosquejun/bantai.git",
|
|
22
|
+
"directory": "packages/ai/openai"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://bantai.vercel.app/",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/bosquejun/bantai/issues"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"openai",
|
|
30
|
+
"openai-extension",
|
|
31
|
+
"ai",
|
|
32
|
+
"ai-extension",
|
|
33
|
+
"llm",
|
|
34
|
+
"policy-engine",
|
|
35
|
+
"policy-library",
|
|
36
|
+
"token-control",
|
|
37
|
+
"token-quota",
|
|
38
|
+
"bantai"
|
|
39
|
+
],
|
|
40
|
+
"author": {
|
|
41
|
+
"name": "Jun",
|
|
42
|
+
"email": "bosquejun@gmail.com"
|
|
43
|
+
},
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"registry": "https://registry.npmjs.org/"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"tsconfig-paths": "^4.2.0",
|
|
51
|
+
"tsup": "^8.5.1",
|
|
52
|
+
"typescript": "5.9.2",
|
|
53
|
+
"vitest": "^2.1.8",
|
|
54
|
+
"@bantai-dev/typescript-config": "0.0.0",
|
|
55
|
+
"@bantai-dev/storage-redis": "1.2.0"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"openai": "^6.18.0",
|
|
59
|
+
"zod": "^4.3.5",
|
|
60
|
+
"@bantai-dev/core": "1.2.0",
|
|
61
|
+
"@bantai-dev/llm": "1.0.0",
|
|
62
|
+
"@bantai-dev/with-rate-limit": "1.2.0"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"zod": "^4.3.5"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "tsup",
|
|
69
|
+
"check-types": "tsc --noEmit",
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"test:watch": "vitest",
|
|
72
|
+
"test:coverage": "vitest --coverage"
|
|
73
|
+
}
|
|
74
|
+
}
|