@gapi/openai 1.8.140

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/.eslintrc.js ADDED
@@ -0,0 +1,27 @@
1
+ module.exports = {
2
+ // Specifies the ESLint parser
3
+ parser: "@typescript-eslint/parser",
4
+ extends: [
5
+ // Uses the recommended rules from the @typescript-eslint/eslint-plugin
6
+ "plugin:@typescript-eslint/recommended",
7
+ // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
8
+ "prettier/@typescript-eslint",
9
+ // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
10
+ "plugin:prettier/recommended"
11
+ ],
12
+ parserOptions: {
13
+ // Allows for the parsing of modern ECMAScript features
14
+ ecmaVersion: 2018,
15
+ // Allows for the use of imports
16
+ sourceType: "module"
17
+ },
18
+ rules: {
19
+ "@typescript-eslint/explicit-function-return-type": 0,
20
+ "simple-import-sort/sort": "error",
21
+ "sort-imports": "off",
22
+ "import/order": "off",
23
+ "@typescript-eslint/camelcase": 0,
24
+ "@typescript-eslint/interface-name-prefix": 0
25
+ },
26
+ plugins: ["simple-import-sort"]
27
+ };
@@ -0,0 +1,5 @@
1
+ semi: true
2
+ trailingComma: all
3
+ singleQuote: true
4
+ printWidth: 80
5
+ tabWidth: 2
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ ### @gapi/openai
2
+
3
+
4
+ #### Installation
5
+
6
+ ```bash
7
+ npm install @gapi/openai
8
+ ```
9
+
10
+ #### Usage
11
+
12
+ ```typescript
13
+ import { Module } from '@gapi/core';
14
+ import { OpenAIModue } from '@gapi/openai';
15
+
16
+ @Module({
17
+ imports: [
18
+ OpenAIModue.forRoot({
19
+ organization: 'org-ahnoIvsiO25Jq4hXeMxO3Fo8',
20
+ apiKey: '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',
21
+ })
22
+ ]
23
+ })
24
+ export class AppModule {}
25
+ ```
26
+
27
+
28
+ #### Exposed graphql mutation after importing
29
+
30
+ ```graphql
31
+ mutation {
32
+ createCompletion(
33
+ payload: {
34
+ model: "text-davinci-003"
35
+ prompt: "Can you write a description for a trello like card based on this text 'Create home landing page'"
36
+ max_tokens: 2048
37
+ }
38
+ ) {
39
+ id
40
+ object
41
+ created
42
+ model
43
+ choices {
44
+ text
45
+ index
46
+ logprobs
47
+ finish_reason
48
+ }
49
+ usage {
50
+ prompt_tokens
51
+ completion_tokens
52
+ total_tokens
53
+ }
54
+ }
55
+ }
56
+ ```
@@ -0,0 +1,5 @@
1
+ export * from './openai.controller';
2
+ export * from './openai.module';
3
+ export * from './openai.tokens';
4
+ export * from './types/create-completion-input.type';
5
+ export * from './types/create-completion.type';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./openai.controller"), exports);
14
+ __exportStar(require("./openai.module"), exports);
15
+ __exportStar(require("./openai.tokens"), exports);
16
+ __exportStar(require("./types/create-completion-input.type"), exports);
17
+ __exportStar(require("./types/create-completion.type"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sDAAoC;AACpC,kDAAgC;AAChC,kDAAgC;AAChC,uEAAqD;AACrD,iEAA+C"}
@@ -0,0 +1,9 @@
1
+ import { CreateCompletionRequest } from 'openai';
2
+ import { OpenAI } from './openai.tokens';
3
+ export declare class OpenAIController {
4
+ private openai;
5
+ constructor(openai: OpenAI);
6
+ createCompletion(root: any, { payload }: {
7
+ payload: CreateCompletionRequest;
8
+ }): import("rxjs").Observable<import("openai").CreateCompletionResponse>;
9
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OpenAIController = void 0;
16
+ const core_1 = require("@gapi/core");
17
+ const rxjs_1 = require("rxjs");
18
+ const operators_1 = require("rxjs/operators");
19
+ const openai_tokens_1 = require("./openai.tokens");
20
+ const create_completion_input_type_1 = require("./types/create-completion-input.type");
21
+ const create_completion_type_1 = require("./types/create-completion.type");
22
+ let OpenAIController = class OpenAIController {
23
+ constructor(openai) {
24
+ this.openai = openai;
25
+ }
26
+ createCompletion(root, { payload }) {
27
+ var _a, _b;
28
+ return rxjs_1.from(this.openai.createCompletion({
29
+ model: (_a = payload.model) !== null && _a !== void 0 ? _a : 'text-davinci-003',
30
+ prompt: payload.prompt,
31
+ max_tokens: (_b = payload.max_tokens) !== null && _b !== void 0 ? _b : 2048,
32
+ })).pipe(operators_1.map((res) => res.data));
33
+ }
34
+ };
35
+ __decorate([
36
+ core_1.Type(create_completion_type_1.CreateCompletionType),
37
+ core_1.Mutation({
38
+ payload: {
39
+ type: new core_1.GraphQLNonNull(create_completion_input_type_1.CreateCompletionInputType),
40
+ },
41
+ }),
42
+ __metadata("design:type", Function),
43
+ __metadata("design:paramtypes", [Object, Object]),
44
+ __metadata("design:returntype", void 0)
45
+ ], OpenAIController.prototype, "createCompletion", null);
46
+ OpenAIController = __decorate([
47
+ core_1.Controller(),
48
+ __param(0, core_1.Inject(openai_tokens_1.OpenAI)),
49
+ __metadata("design:paramtypes", [Object])
50
+ ], OpenAIController);
51
+ exports.OpenAIController = OpenAIController;
52
+ //# sourceMappingURL=openai.controller.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.controller.js","sourceRoot":"","sources":["../src/openai.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qCAAgF;AAEhF,+BAA4B;AAC5B,8CAAqC;AAErC,mDAAyC;AACzC,uFAAiF;AACjF,2EAAsE;AAGtE,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAC3B,YAAoC,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;IAQtD,gBAAgB,CAAC,IAAI,EAAE,EAAE,OAAO,EAAwC;;QACtE,OAAO,WAAI,CACT,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC;YAC3B,KAAK,QAAE,OAAO,CAAC,KAAK,mCAAI,kBAAkB;YAC1C,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,UAAU,QAAE,OAAO,CAAC,UAAU,mCAAI,IAAI;SACvC,CAAC,CACH,CAAC,IAAI,CAAC,eAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IACjC,CAAC;CACF,CAAA;AATC;IANC,WAAI,CAAC,6CAAoB,CAAC;IAC1B,eAAQ,CAAC;QACR,OAAO,EAAE;YACP,IAAI,EAAE,IAAI,qBAAc,CAAC,wDAAyB,CAAC;SACpD;KACF,CAAC;;;;wDASD;AAjBU,gBAAgB;IAD5B,iBAAU,EAAE;IAEE,WAAA,aAAM,CAAC,sBAAM,CAAC,CAAA;;GADhB,gBAAgB,CAkB5B;AAlBY,4CAAgB"}
@@ -0,0 +1,5 @@
1
+ import { ModuleWithProviders } from '@gapi/core';
2
+ import { ConfigurationParameters } from 'openai';
3
+ export declare class OpenAIModue {
4
+ static forRoot(config: ConfigurationParameters): ModuleWithProviders;
5
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var OpenAIModue_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.OpenAIModue = void 0;
11
+ const core_1 = require("@gapi/core");
12
+ const openai_1 = require("openai");
13
+ const openai_controller_1 = require("./openai.controller");
14
+ const openai_tokens_1 = require("./openai.tokens");
15
+ let OpenAIModue = OpenAIModue_1 = class OpenAIModue {
16
+ static forRoot(config) {
17
+ return {
18
+ module: OpenAIModue_1,
19
+ providers: [
20
+ {
21
+ provide: openai_tokens_1.OpenAI,
22
+ useFactory: () => new openai_1.OpenAIApi(new openai_1.Configuration(config)),
23
+ },
24
+ ],
25
+ };
26
+ }
27
+ };
28
+ OpenAIModue = OpenAIModue_1 = __decorate([
29
+ core_1.Module({
30
+ controllers: [openai_controller_1.OpenAIController],
31
+ })
32
+ ], OpenAIModue);
33
+ exports.OpenAIModue = OpenAIModue;
34
+ //# sourceMappingURL=openai.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.module.js","sourceRoot":"","sources":["../src/openai.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,qCAAyD;AACzD,mCAA2E;AAE3E,2DAAuD;AACvD,mDAAyC;AAKzC,IAAa,WAAW,mBAAxB,MAAa,WAAW;IACf,MAAM,CAAC,OAAO,CAAC,MAA+B;QACnD,OAAO;YACL,MAAM,EAAE,aAAW;YACnB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,sBAAM;oBACf,UAAU,EAAE,GAAG,EAAE,CAAC,IAAI,kBAAS,CAAC,IAAI,sBAAa,CAAC,MAAM,CAAC,CAAC;iBAC3D;aACF;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AAZY,WAAW;IAHvB,aAAM,CAAC;QACN,WAAW,EAAE,CAAC,oCAAgB,CAAC;KAChC,CAAC;GACW,WAAW,CAYvB;AAZY,kCAAW"}
@@ -0,0 +1,4 @@
1
+ import { InjectionToken } from '@gapi/core';
2
+ import { OpenAIApi } from 'openai';
3
+ export declare const OpenAI: InjectionToken<OpenAIApi>;
4
+ export declare type OpenAI = OpenAIApi;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpenAI = void 0;
4
+ const core_1 = require("@gapi/core");
5
+ exports.OpenAI = new core_1.InjectionToken('open-ai');
6
+ //# sourceMappingURL=openai.tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai.tokens.js","sourceRoot":"","sources":["../src/openai.tokens.ts"],"names":[],"mappings":";;;AAAA,qCAA4C;AAG/B,QAAA,MAAM,GAAG,IAAI,qBAAc,CAAS,SAAS,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import { GraphQLInputObjectType } from 'graphql';
2
+ export declare const CreateCompletionInputType: GraphQLInputObjectType;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CreateCompletionInputType = void 0;
4
+ const graphql_1 = require("graphql");
5
+ exports.CreateCompletionInputType = new graphql_1.GraphQLInputObjectType({
6
+ name: 'CreateCompletionInputType',
7
+ fields: () => ({
8
+ model: {
9
+ type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString),
10
+ },
11
+ prompt: {
12
+ type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString),
13
+ },
14
+ max_tokens: {
15
+ type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLInt),
16
+ },
17
+ }),
18
+ });
19
+ //# sourceMappingURL=create-completion-input.type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-completion-input.type.js","sourceRoot":"","sources":["../../src/types/create-completion-input.type.ts"],"names":[],"mappings":";;;AAAA,qCAKiB;AAEJ,QAAA,yBAAyB,GAAG,IAAI,gCAAsB,CAAC;IAClE,IAAI,EAAE,2BAA2B;IACjC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QACb,KAAK,EAAE;YACL,IAAI,EAAE,IAAI,wBAAc,CAAC,uBAAa,CAAC;SACxC;QACD,MAAM,EAAE;YACN,IAAI,EAAE,IAAI,wBAAc,CAAC,uBAAa,CAAC;SACxC;QACD,UAAU,EAAE;YACV,IAAI,EAAE,IAAI,wBAAc,CAAC,oBAAU,CAAC;SACrC;KACF,CAAC;CACH,CAAC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { GraphQLObjectType } from 'graphql';
2
+ export declare const CreateCompletionUsageType: GraphQLObjectType<any, any, {
3
+ [key: string]: any;
4
+ }>;
5
+ export declare const CreateCompletionChoicesType: GraphQLObjectType<any, any, {
6
+ [key: string]: any;
7
+ }>;
8
+ export declare const CreateCompletionType: GraphQLObjectType<any, any, {
9
+ [key: string]: any;
10
+ }>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CreateCompletionType = exports.CreateCompletionChoicesType = exports.CreateCompletionUsageType = void 0;
4
+ const graphql_1 = require("graphql");
5
+ exports.CreateCompletionUsageType = new graphql_1.GraphQLObjectType({
6
+ name: 'CreateCompletionUsageType',
7
+ fields: () => ({
8
+ prompt_tokens: {
9
+ type: graphql_1.GraphQLInt,
10
+ },
11
+ completion_tokens: {
12
+ type: graphql_1.GraphQLInt,
13
+ },
14
+ total_tokens: {
15
+ type: graphql_1.GraphQLInt,
16
+ },
17
+ }),
18
+ });
19
+ exports.CreateCompletionChoicesType = new graphql_1.GraphQLObjectType({
20
+ name: 'CreateCompletionChoicesType',
21
+ fields: () => ({
22
+ text: {
23
+ type: graphql_1.GraphQLString,
24
+ },
25
+ index: {
26
+ type: graphql_1.GraphQLString,
27
+ },
28
+ logprobs: {
29
+ type: graphql_1.GraphQLString,
30
+ },
31
+ finish_reason: {
32
+ type: graphql_1.GraphQLString,
33
+ },
34
+ }),
35
+ });
36
+ exports.CreateCompletionType = new graphql_1.GraphQLObjectType({
37
+ name: 'CreateCompletionType',
38
+ fields: () => ({
39
+ id: {
40
+ type: graphql_1.GraphQLString,
41
+ },
42
+ object: {
43
+ type: graphql_1.GraphQLString,
44
+ },
45
+ created: {
46
+ type: graphql_1.GraphQLInt,
47
+ },
48
+ model: {
49
+ type: graphql_1.GraphQLString,
50
+ },
51
+ choices: {
52
+ type: new graphql_1.GraphQLList(exports.CreateCompletionChoicesType),
53
+ },
54
+ usage: {
55
+ type: exports.CreateCompletionUsageType,
56
+ },
57
+ }),
58
+ });
59
+ //# sourceMappingURL=create-completion.type.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-completion.type.js","sourceRoot":"","sources":["../../src/types/create-completion.type.ts"],"names":[],"mappings":";;;AAAA,qCAKiB;AAEJ,QAAA,yBAAyB,GAAG,IAAI,2BAAiB,CAAC;IAC7D,IAAI,EAAE,2BAA2B;IACjC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QACb,aAAa,EAAE;YACb,IAAI,EAAE,oBAAU;SACjB;QACD,iBAAiB,EAAE;YACjB,IAAI,EAAE,oBAAU;SACjB;QACD,YAAY,EAAE;YACZ,IAAI,EAAE,oBAAU;SACjB;KACF,CAAC;CACH,CAAC,CAAC;AAEU,QAAA,2BAA2B,GAAG,IAAI,2BAAiB,CAAC;IAC/D,IAAI,EAAE,6BAA6B;IACnC,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QACb,IAAI,EAAE;YACJ,IAAI,EAAE,uBAAa;SACpB;QACD,KAAK,EAAE;YACL,IAAI,EAAE,uBAAa;SACpB;QACD,QAAQ,EAAE;YACR,IAAI,EAAE,uBAAa;SACpB;QACD,aAAa,EAAE;YACb,IAAI,EAAE,uBAAa;SACpB;KACF,CAAC;CACH,CAAC,CAAC;AAEU,QAAA,oBAAoB,GAAG,IAAI,2BAAiB,CAAC;IACxD,IAAI,EAAE,sBAAsB;IAC5B,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QACb,EAAE,EAAE;YACF,IAAI,EAAE,uBAAa;SACpB;QACD,MAAM,EAAE;YACN,IAAI,EAAE,uBAAa;SACpB;QACD,OAAO,EAAE;YACP,IAAI,EAAE,oBAAU;SACjB;QACD,KAAK,EAAE;YACL,IAAI,EAAE,uBAAa;SACpB;QACD,OAAO,EAAE;YACP,IAAI,EAAE,IAAI,qBAAW,CAAC,mCAA2B,CAAC;SACnD;QACD,KAAK,EAAE;YACL,IAAI,EAAE,iCAAyB;SAChC;KACF,CAAC;CACH,CAAC,CAAC"}
package/jest.config.js ADDED
@@ -0,0 +1,16 @@
1
+ module.exports = {
2
+ testEnvironment: 'node',
3
+ testPathIgnorePatterns: ['/node_modules/'],
4
+ coverageReporters: ['lcov', 'html'],
5
+ rootDir: './',
6
+ moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'node'],
7
+ globals: {
8
+ __DEV__: true,
9
+ },
10
+ transform: {
11
+ '\\.(ts|tsx)$': 'ts-jest',
12
+ },
13
+ testRegex: '/test/.*\\.spec.(ts|tsx|js)$',
14
+ verbose: true,
15
+ collectCoverage: true,
16
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@gapi/openai",
3
+ "version": "1.8.140",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/Stradivario/gapi.git"
7
+ },
8
+ "scripts": {
9
+ "patch": "npm run build && npm version patch && npm publish --update-readme --access public && npm run delete-dist",
10
+ "delete-dist": "rm -rf dist",
11
+ "clean": "git clean -dxf",
12
+ "lint": "npx eslint . --ext .ts",
13
+ "test": "echo missing-tests",
14
+ "lint-fix": "npx eslint . --fix --ext .ts",
15
+ "build": "rm -rf dist && tsc || true"
16
+ },
17
+ "author": {
18
+ "name": "Kristian Tachev(Stradivario)",
19
+ "email": "kristiqn.tachev@gmail.com"
20
+ },
21
+ "keywords": [
22
+ "graphql",
23
+ "gapi",
24
+ "node",
25
+ "access-control"
26
+ ],
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/Stradivario/gapi/issues"
30
+ },
31
+ "dependencies": {
32
+ "openai": "^3.1.0",
33
+ "@gapi/core": "^1.8.139"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^13.11.1",
37
+ "@types/jest": "^25.2.1",
38
+ "@typescript-eslint/eslint-plugin": "^2.10.0",
39
+ "@typescript-eslint/parser": "^2.10.0",
40
+ "eslint": "^6.7.2",
41
+ "prettier": "^2.0.4",
42
+ "typescript": "^3.8.3",
43
+ "eslint-config-prettier": "^6.7.0",
44
+ "eslint-plugin-prettier": "^3.1.1",
45
+ "eslint-plugin-simple-import-sort": "^5.0.0",
46
+ "jest": "^25.5.4",
47
+ "ts-jest": "^25.4.0"
48
+ },
49
+ "main": "./dist/index.js",
50
+ "types": "./dist/index.d.ts",
51
+ "module": "./dist/index.js",
52
+ "typings": "./dist/index.d.ts"
53
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './openai.controller';
2
+ export * from './openai.module';
3
+ export * from './openai.tokens';
4
+ export * from './types/create-completion-input.type';
5
+ export * from './types/create-completion.type';
@@ -0,0 +1,29 @@
1
+ import { Controller, GraphQLNonNull, Inject, Mutation, Type } from '@gapi/core';
2
+ import { CreateCompletionRequest } from 'openai';
3
+ import { from } from 'rxjs';
4
+ import { map } from 'rxjs/operators';
5
+
6
+ import { OpenAI } from './openai.tokens';
7
+ import { CreateCompletionInputType } from './types/create-completion-input.type';
8
+ import { CreateCompletionType } from './types/create-completion.type';
9
+
10
+ @Controller()
11
+ export class OpenAIController {
12
+ constructor(@Inject(OpenAI) private openai: OpenAI) {}
13
+
14
+ @Type(CreateCompletionType)
15
+ @Mutation({
16
+ payload: {
17
+ type: new GraphQLNonNull(CreateCompletionInputType),
18
+ },
19
+ })
20
+ createCompletion(root, { payload }: { payload: CreateCompletionRequest }) {
21
+ return from(
22
+ this.openai.createCompletion({
23
+ model: payload.model ?? 'text-davinci-003',
24
+ prompt: payload.prompt,
25
+ max_tokens: payload.max_tokens ?? 2048,
26
+ }),
27
+ ).pipe(map((res) => res.data));
28
+ }
29
+ }
@@ -0,0 +1,22 @@
1
+ import { Module, ModuleWithProviders } from '@gapi/core';
2
+ import { Configuration, ConfigurationParameters, OpenAIApi } from 'openai';
3
+
4
+ import { OpenAIController } from './openai.controller';
5
+ import { OpenAI } from './openai.tokens';
6
+
7
+ @Module({
8
+ controllers: [OpenAIController],
9
+ })
10
+ export class OpenAIModue {
11
+ public static forRoot(config: ConfigurationParameters): ModuleWithProviders {
12
+ return {
13
+ module: OpenAIModue,
14
+ providers: [
15
+ {
16
+ provide: OpenAI,
17
+ useFactory: () => new OpenAIApi(new Configuration(config)),
18
+ },
19
+ ],
20
+ };
21
+ }
22
+ }
@@ -0,0 +1,6 @@
1
+ import { InjectionToken } from '@gapi/core';
2
+ import { OpenAIApi } from 'openai';
3
+
4
+ export const OpenAI = new InjectionToken<OpenAI>('open-ai');
5
+
6
+ export type OpenAI = OpenAIApi;
@@ -0,0 +1,21 @@
1
+ import {
2
+ GraphQLInputObjectType,
3
+ GraphQLInt,
4
+ GraphQLNonNull,
5
+ GraphQLString,
6
+ } from 'graphql';
7
+
8
+ export const CreateCompletionInputType = new GraphQLInputObjectType({
9
+ name: 'CreateCompletionInputType',
10
+ fields: () => ({
11
+ model: {
12
+ type: new GraphQLNonNull(GraphQLString),
13
+ },
14
+ prompt: {
15
+ type: new GraphQLNonNull(GraphQLString),
16
+ },
17
+ max_tokens: {
18
+ type: new GraphQLNonNull(GraphQLInt),
19
+ },
20
+ }),
21
+ });
@@ -0,0 +1,63 @@
1
+ import {
2
+ GraphQLInt,
3
+ GraphQLList,
4
+ GraphQLObjectType,
5
+ GraphQLString,
6
+ } from 'graphql';
7
+
8
+ export const CreateCompletionUsageType = new GraphQLObjectType({
9
+ name: 'CreateCompletionUsageType',
10
+ fields: () => ({
11
+ prompt_tokens: {
12
+ type: GraphQLInt,
13
+ },
14
+ completion_tokens: {
15
+ type: GraphQLInt,
16
+ },
17
+ total_tokens: {
18
+ type: GraphQLInt,
19
+ },
20
+ }),
21
+ });
22
+
23
+ export const CreateCompletionChoicesType = new GraphQLObjectType({
24
+ name: 'CreateCompletionChoicesType',
25
+ fields: () => ({
26
+ text: {
27
+ type: GraphQLString,
28
+ },
29
+ index: {
30
+ type: GraphQLString,
31
+ },
32
+ logprobs: {
33
+ type: GraphQLString,
34
+ },
35
+ finish_reason: {
36
+ type: GraphQLString,
37
+ },
38
+ }),
39
+ });
40
+
41
+ export const CreateCompletionType = new GraphQLObjectType({
42
+ name: 'CreateCompletionType',
43
+ fields: () => ({
44
+ id: {
45
+ type: GraphQLString,
46
+ },
47
+ object: {
48
+ type: GraphQLString,
49
+ },
50
+ created: {
51
+ type: GraphQLInt,
52
+ },
53
+ model: {
54
+ type: GraphQLString,
55
+ },
56
+ choices: {
57
+ type: new GraphQLList(CreateCompletionChoicesType),
58
+ },
59
+ usage: {
60
+ type: CreateCompletionUsageType,
61
+ },
62
+ }),
63
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "module": "commonjs",
5
+ "target": "es6",
6
+ "baseUrl": ".",
7
+ "stripInternal": true,
8
+ "emitDecoratorMetadata": true,
9
+ "experimentalDecorators": true,
10
+ "moduleResolution": "node",
11
+ "outDir": "dist",
12
+ "removeComments": true,
13
+ "allowSyntheticDefaultImports": true,
14
+ "preserveConstEnums": true,
15
+ "sourceMap": true,
16
+ "strictNullChecks": false,
17
+ "forceConsistentCasingInFileNames": true,
18
+ "noFallthroughCasesInSwitch": true,
19
+ "noImplicitAny": false,
20
+ "noImplicitReturns": true,
21
+ "noImplicitThis": false,
22
+ "noUnusedLocals": true,
23
+ "noUnusedParameters": false,
24
+ "rootDir": "src",
25
+ "lib": ["es2017", "es2016", "es2015", "es6", "dom", "esnext.asynciterable"],
26
+ "skipLibCheck": true,
27
+ "typeRoots": ["node_modules/@types"]
28
+ },
29
+ "files": ["src/index.ts"]
30
+ }