Skip to content

Commit

Permalink
add edits and images api
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Mar 8, 2023
1 parent 9df914f commit 74c8e83
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 3 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

An elegant Node.js library written in TypeScript for the OpenAI API.

- [Installation](#installation)
- [Example](#example)
- [V1 API](#v1-api)
- [Models](#models)
- [Completions](#completions)
- [Chat](#chat)
- [Edits](#edits)
- [Images](#images)

## Installation

```bash
Expand Down Expand Up @@ -46,6 +55,8 @@ To use the OpenAI V1 API, you must call the `v1()` method on the client instance
const v1 = client.v1();
```

Check out the [OpenAI V1 API docs](https://platform.openai.com/docs/api-reference/introduction) for more information.

### Models

List all available models:
Expand Down Expand Up @@ -88,3 +99,46 @@ const chat = await v1.chat.create({
]
});
```

### Edits

Create an edit:

```javascript
const edit = await v1.edits.create({
model: 'text-davinci-edit-001',
input: 'I am a test',
instruction: 'Make this text funny',
});
```

### Images

Create an image:

```javascript
const image = await v1.images.create({
prompt: 'A cute baby sea otter',
n: 1,
size: '512x512',
});
```

Create image edit:

```javascript
const imageEdit = await v1.images.edit({
prompt: 'Make this image funny',
n: 1,
size: '512x512',
}, '/path/to/image.png');
```

Create image variation:

```javascript
const imageVariation = await v1.images.variation({
n: 1,
size: '512x512',
}, '/path/to/image.png');
```
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
"test": "npx mocha"
},
"devDependencies": {
"@types/node": "latest",
"@types/mocha": "latest",
"@types/node": "latest",
"assert": "latest",
"mocha": "latest",
"ts-node": "latest",
"tsc-esm-fix": "latest",
"typescript": "latest"
},
"dependencies": {
"axios": "^1.3.4"
"axios": "^1.3.4",
"form-data": "^4.0.0"
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export class OpenAI {
},
chat: {
create: v1.createChat(client)
},
edits: {
create: v1.createEdit(client)
},
images: {
create: v1.createImage(client),
edit: v1.editImage(client),
createVariation: v1.createImageVariation(client)
}
};
}
Expand Down
29 changes: 29 additions & 0 deletions src/v1/edits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Usage } from ".";
import { ApiClient } from "..";

type EditRequest = {
model: string;
input?: string;
instruction: string;
n?: number;
temperature?: number;
top_p?: number;
};

type Choice = {
index: number;
text: string;
}

type Edit = {
object: string;
created: number;
choices: Choice[];
usage: Usage;
}

export function createEdit(client: ApiClient) {
return async (request: EditRequest): Promise<Edit> => {
return await client("edits", { method: "POST", data: request });
}
}
83 changes: 83 additions & 0 deletions src/v1/images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { createReadStream } from "fs";
import { ApiClient } from "..";
import FormData from "form-data";

type ImageSize = '256x256' | '512x512' | '1024x1024';

type ImageResponseFormat = 'url' | 'b64_json';

type CreateImageRequest = {
prompt: string;
n?: number;
size?: ImageSize;
response_format?: ImageResponseFormat;
user?: string;
};

type EditImageRequest = {
prompt: string;
n?: number;
size?: ImageSize;
response_format?: ImageResponseFormat;
user?: string;
};

type CreateImageVariationRequest = {
n?: number;
size?: ImageSize;
response_format?: ImageResponseFormat;
user?: string;
};

type UrlImage = {
url: string;
};

type B64JsonImage = {
b64_json: string;
};

type ImageData = UrlImage | B64JsonImage;

type Image = {
created: number;
data: ImageData[];
};

export function createImage(client: ApiClient) {
return async (request: CreateImageRequest): Promise<Image> => {
return await client("images/generations", { method: "POST", data: request });
}
}

export function editImage(client: ApiClient) {
return async (request: EditImageRequest, image: string, mask?: string): Promise<Image> => {
const form = new FormData();

for (const key in request) {
form.append(key, '' + request[key as keyof EditImageRequest]);
}

form.append('image', createReadStream(image));

if (mask) {
form.append('mask', createReadStream(mask));
}

return await client("images/edits", { method: "POST", data: form });
}
}

export function createImageVariation(client: ApiClient) {
return async (request: CreateImageVariationRequest, image: string): Promise<Image> => {
const form = new FormData();

for (const key in request) {
form.append(key, '' + request[key as keyof CreateImageVariationRequest]);
}

form.append('image', createReadStream(image));

return await client("images/variations", { method: "POST", data: form });
}
}
4 changes: 3 additions & 1 deletion src/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export type Usage = {

export * from "./models";
export * from "./completions";
export * from "./chat";
export * from "./chat";
export * from "./edits";
export * from "./images";

0 comments on commit 74c8e83

Please sign in to comment.