Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit 57733e9

Browse files
refactor
- index to mod - 処理をcoreに分離
1 parent acbcbbf commit 57733e9

21 files changed

+198
-206
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# gpt
22

3+
OpenAI API made easy !!
4+
now deno support !
5+
6+
<https://beta.openai.com/docs>
7+
38
## install
49

510
```bash
@@ -37,3 +42,9 @@ async function main() {
3742
}
3843
main();
3944
```
45+
46+
## deno
47+
48+
```ts
49+
import { openai } from 'https://cdn.skypack.dev/@hikae/gpt?dts'
50+
```

example/classification.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { openai, App } from "../src/fp";
1+
import { openai, App } from "@hikae/gpt";
22

33
export const classification = async (app: App) => {
44
const result = await openai.classification({

example/completion.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { App, openai } from "../src/index";
2-
import { CompletionOpts } from "../src/types/index";
1+
import { App, openai, CompletionOpts } from "@hikae/gpt";
32

43
export const completion = async (app: App) => {
54
const completionOpts: CompletionOpts = {
File renamed without changes.

deno/main.ts example/deno/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { App, openai } from "https://cdn.skypack.dev/@hikae/gpt?dts";
2+
import "https://deno.land/x/dotenv/load.ts";
23

34
const key = Deno.env.get("OPENAI_API_KEY");
45
if (!key) {
@@ -18,5 +19,5 @@ const result = await openai.classification({
1819
model: "ada",
1920
})(app);
2021

21-
console.log(result.selected_examples);
22+
console.log(result.label);
2223
// result = 'Positive'

example/engine.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, openai } from "../src/index";
1+
import { App, openai } from "@hikae/gpt";
22

33
export const engine = async (app: App) => {
44
const result = await openai.engine()(app);

example/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { openai } from "../src/index";
1+
import { openai } from "@hikae/gpt";
22
import { classification } from "./classification";
33
import { completion } from "./completion";
44
import { engine } from "./engine";

example/search.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { App, openai } from "../src/index";
2-
import { SearchOpts } from "../src/types/index";
1+
import { App, openai, SearchOpts } from "@hikae/gpt";
32

43
export const search = async (app: App) => {
54
const searchOpts: SearchOpts = {

package.json

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"scripts": {
33
"test": "jest",
44
"dev": "esmo example/main.ts",
5-
"build": "rimraf dist && tsup src/index.ts --dts --format cjs,esm",
5+
"dev:deno": "deno run --unstable -A example/deno/main.ts",
6+
"build": "rimraf dist && tsup src/mod.ts --dts --format cjs,esm",
67
"package-check": "npx @skypack/package-check"
78
},
89
"dependencies": {
@@ -23,18 +24,18 @@
2324
"typescript": "^4.4.2"
2425
},
2526
"name": "@hikae/gpt",
26-
"version": "0.0.9",
27+
"version": "0.0.10",
2728
"description": "A framework to build OpenAPI apps using nodejs",
2829
"keywords": [
2930
"gpt3"
3031
],
31-
"main": "dist/index.js",
32-
"module": "dist/index.mjs",
33-
"types": "dist/index.d.ts",
32+
"main": "dist/mod.js",
33+
"module": "dist/mod.mjs",
34+
"types": "dist/mod.d.ts",
3435
"exports": {
35-
"require": "./dist/index.js",
36-
"import": "./dist/index.mjs",
37-
"default": "./dist/index.mjs"
36+
"require": "./dist/mod.js",
37+
"import": "./dist/mod.mjs",
38+
"default": "./dist/mod.mjs"
3839
},
3940
"files": [
4041
"dist"

src/class.ts

+14-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
engineListURL,
77
engineURL,
88
searchURL,
9-
} from "./lib/index";
9+
} from "./lib/mod";
1010
import {
1111
Completion,
1212
CompletionOpts,
@@ -16,7 +16,14 @@ import {
1616
ListEngine,
1717
Search,
1818
SearchOpts,
19-
} from "./types";
19+
} from "./types/mod";
20+
import {
21+
classification,
22+
completion,
23+
engine,
24+
listEngines,
25+
search,
26+
} from "./core/mod";
2027
import { Options } from "ky";
2128

2229
/** App initialization options */
@@ -54,11 +61,7 @@ export class App {
5461
* @link https://beta.openai.com/docs/guides/completion/completion
5562
*/
5663
async completion(opts: CompletionOpts) {
57-
const url = completionURL(this.engineId);
58-
const options: Options = {
59-
json: opts,
60-
};
61-
return await this.api.post(url, options).json<Completion>();
64+
return completion(opts)({ api: this.api, engineId: this.engineId });
6265
}
6366

6467
// search
@@ -68,11 +71,7 @@ export class App {
6871
* @link https://beta.openai.com/docs/guides/search/search
6972
*/
7073
async search(opts: SearchOpts) {
71-
const url = searchURL(this.engineId);
72-
const options: Options = {
73-
json: opts,
74-
};
75-
return await this.api.post(url, options).json<Search>();
74+
return search(opts)({ api: this.api, engineId: this.engineId });
7675
}
7776

7877
// Classification
@@ -82,11 +81,7 @@ export class App {
8281
* @link https://beta.openai.com/docs/api-reference/classifications/create
8382
*/
8483
async classification(opts: ClassificationOpts) {
85-
const url = classificationsURL();
86-
const options: Options = {
87-
json: opts,
88-
};
89-
return await this.api.post(url, options).json<Classification>();
84+
return classification(opts)({ api: this.api, engineId: this.engineId });
9085
}
9186

9287
// Engines
@@ -96,16 +91,14 @@ export class App {
9691
* @link https://beta.openai.com/docs/api-reference/engines/list
9792
*/
9893
async listEngines() {
99-
const url = engineListURL();
100-
return await this.api.get(url).json<ListEngine>();
94+
return listEngines()({ api: this.api, engineId: this.engineId });
10195
}
10296

10397
/**
10498
* Retrieve engine OR get current engine
10599
* @link https://beta.openai.com/docs/api-reference/engines/retrieve
106100
*/
107101
async engine(engineId?: string) {
108-
const url = engineURL(engineId ?? this.engineId);
109-
return await this.api.get(url).json<Engine>();
102+
return engine()({ api: this.api, engineId: engineId ?? this.engineId });
110103
}
111104
}

src/core/classification.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ClassificationOpts, App, Classification } from "dist";
2+
import { Options } from "ky";
3+
import { classificationsURL } from "~/lib/mod";
4+
5+
/**
6+
* Create Classification
7+
* @link https://beta.openai.com/docs/api-reference/classifications/create
8+
*/
9+
export const classification =
10+
(opts: ClassificationOpts) =>
11+
async ({ api }: App) => {
12+
const url = classificationsURL();
13+
const options: Options = {
14+
json: opts,
15+
};
16+
const res = await api.post(url, options).json<Classification>();
17+
return res;
18+
};

src/core/completion.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CompletionOpts, App, Completion } from "dist";
2+
import { Options } from "ky";
3+
import { completionURL } from "~/lib/mod";
4+
5+
/**
6+
* Completion
7+
* @link https://beta.openai.com/docs/guides/completion/completion
8+
*/
9+
export const completion =
10+
(opts: CompletionOpts) =>
11+
async ({ api, engineId }: App) => {
12+
const url = completionURL(engineId);
13+
const options: Options = {
14+
json: opts,
15+
};
16+
const data = await api.post(url, options).json<Completion>();
17+
return data;
18+
};

src/core/engine.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { App, ListEngine, Engine } from "~/mod";
2+
import { engineListURL, engineURL } from "~/lib/mod";
3+
4+
/**
5+
* List engines
6+
* @link https://beta.openai.com/docs/api-reference/engines/list
7+
*/
8+
export const listEngines =
9+
() =>
10+
async ({ api }: App) => {
11+
const url = engineListURL();
12+
const res = await api.get(url).json<ListEngine>();
13+
return res;
14+
};
15+
16+
/**
17+
* Retrieve engine
18+
* @link https://beta.openai.com/docs/api-reference/engines/retrieve
19+
*/
20+
export const engine =
21+
() =>
22+
async ({ api, engineId }: App) => {
23+
const url = engineURL(engineId);
24+
const res = api.get(url).json<Engine>();
25+
return res;
26+
};

src/core/mod.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./classification";
2+
export * from "./completion";
3+
export * from "./engine";
4+
export * from "./search";

src/core/search.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { SearchOpts, App, Search } from "dist";
2+
import { Options } from "ky";
3+
import { searchURL } from "~/lib/mod";
4+
5+
/**
6+
* Search
7+
* @link https://beta.openai.com/docs/guides/search/search
8+
*/
9+
export const search =
10+
(opts: SearchOpts) =>
11+
async ({ api, engineId }: App) => {
12+
const url = searchURL(engineId);
13+
const options: Options = {
14+
json: opts,
15+
};
16+
const res = await api.post(url, options).json<Search>();
17+
return res;
18+
};

src/fp.ts

+7-88
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1+
import { createApi, DEFAULT_ENGINE } from "./lib/mod";
12
import {
2-
createApi,
3-
classificationsURL,
4-
completionURL,
5-
DEFAULT_ENGINE,
6-
engineListURL,
7-
engineURL,
8-
searchURL,
9-
} from "./lib/index";
10-
import {
11-
Completion,
12-
CompletionOpts,
13-
Classification,
14-
ClassificationOpts,
15-
Engine,
16-
ListEngine,
17-
Search,
18-
SearchOpts,
19-
} from "./types/index";
20-
import { Options } from "ky";
3+
classification,
4+
completion,
5+
engine,
6+
listEngines,
7+
search,
8+
} from "./core/mod";
219

2210
/**
2311
* A OpenAPI App
@@ -31,75 +19,6 @@ const app = (apiKey: string, engineId = DEFAULT_ENGINE): App => ({
3119
engineId,
3220
});
3321

34-
/**
35-
* Completion
36-
* @link https://beta.openai.com/docs/guides/completion/completion
37-
*/
38-
const completion =
39-
(opts: CompletionOpts) =>
40-
async ({ api, engineId }: App) => {
41-
const url = completionURL(engineId);
42-
const options: Options = {
43-
json: opts,
44-
};
45-
const data = await api.post(url, options).json<Completion>();
46-
return data;
47-
};
48-
49-
/**
50-
* Search
51-
* @link https://beta.openai.com/docs/guides/search/search
52-
*/
53-
const search =
54-
(opts: SearchOpts) =>
55-
async ({ api, engineId }: App) => {
56-
const url = searchURL(engineId);
57-
const options: Options = {
58-
json: opts,
59-
};
60-
const res = await api.post(url, options).json<Search>();
61-
return res;
62-
};
63-
64-
/**
65-
* Create Classification
66-
* @link https://beta.openai.com/docs/api-reference/classifications/create
67-
*/
68-
const classification =
69-
(opts: ClassificationOpts) =>
70-
async ({ api }: App) => {
71-
const url = classificationsURL();
72-
const options: Options = {
73-
json: opts,
74-
};
75-
const res = await api.post(url, options).json<Classification>();
76-
return res;
77-
};
78-
79-
/**
80-
* List engines
81-
* @link https://beta.openai.com/docs/api-reference/engines/list
82-
*/
83-
const listEngines =
84-
() =>
85-
async ({ api }: App) => {
86-
const url = engineListURL();
87-
const res = await api.get(url).json<ListEngine>();
88-
return res;
89-
};
90-
91-
/**
92-
* Retrieve engine
93-
* @link https://beta.openai.com/docs/api-reference/engines/retrieve
94-
*/
95-
const engine =
96-
() =>
97-
async ({ api, engineId }: App) => {
98-
const url = engineURL(engineId);
99-
const res = api.get(url).json<Engine>();
100-
return res;
101-
};
102-
10322
export const openai = {
10423
app,
10524
completion,

0 commit comments

Comments
 (0)