-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
dalle.ts
162 lines (143 loc) Β· 4.28 KB
/
dalle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { OpenAI as OpenAIClient } from "openai";
import { Tool, ToolParams } from "@langchain/core/tools";
/**
* An interface for the Dall-E API Wrapper.
*/
export interface DallEAPIWrapperParams extends ToolParams {
/**
* The OpenAI API key
* Alias for `apiKey`
*/
openAIApiKey?: string;
/**
* The OpenAI API key
*/
apiKey?: string;
/**
* The model to use.
* Alias for `model`
* @params "dall-e-2" | "dall-e-3"
* @default "dall-e-3"
*/
modelName?: string;
/**
* The model to use.
* @params "dall-e-2" | "dall-e-3"
* @default "dall-e-3"
*/
model?: string;
/**
* The style of the generated images. Must be one of vivid or natural.
* Vivid causes the model to lean towards generating hyper-real and dramatic images.
* Natural causes the model to produce more natural, less hyper-real looking images.
* @default "vivid"
*/
style?: "natural" | "vivid";
/**
* The quality of the image that will be generated. βhdβ creates images with finer
* details and greater consistency across the image.
* @default "standard"
*/
quality?: "standard" | "hd";
/**
* The number of images to generate.
* Must be between 1 and 10.
* For dall-e-3, only `n: 1` is supported.
* @default 1
*/
n?: number;
/**
* The size of the generated images.
* Must be one of 256x256, 512x512, or 1024x1024 for DALLΒ·E-2 models.
* Must be one of 1024x1024, 1792x1024, or 1024x1792 for DALLΒ·E-3 models.
* @default "1024x1024"
*/
size?: "256x256" | "512x512" | "1024x1024" | "1792x1024" | "1024x1792";
/**
* The format in which the generated images are returned.
* Must be one of "url" or "b64_json".
* @default "url"
*/
responseFormat?: "url" | "b64_json";
/**
* A unique identifier representing your end-user, which will help
* OpenAI to monitor and detect abuse.
*/
user?: string;
/**
* The organization to use
*/
organization?: string;
}
/**
* A tool for generating images with Open AIs Dall-E 2 or 3 API.
*/
export class DallEAPIWrapper extends Tool {
static lc_name() {
return "DallEAPIWrapper";
}
name = "dalle_api_wrapper";
description =
"A wrapper around OpenAI DALL-E API. Useful for when you need to generate images from a text description. Input should be an image description.";
protected client: OpenAIClient;
static readonly toolName = "dalle_api_wrapper";
private model = "dall-e-3";
private style: "natural" | "vivid" = "vivid";
private quality: "standard" | "hd" = "standard";
private n = 1;
private size:
| "256x256"
| "512x512"
| "1024x1024"
| "1792x1024"
| "1024x1792" = "1024x1024";
private responseFormat: "url" | "b64_json" = "url";
private user?: string;
constructor(fields?: DallEAPIWrapperParams) {
super(fields);
const openAIApiKey =
fields?.apiKey ??
fields?.openAIApiKey ??
getEnvironmentVariable("OPENAI_API_KEY");
const organization =
fields?.organization ?? getEnvironmentVariable("OPENAI_ORGANIZATION");
const clientConfig = {
apiKey: openAIApiKey,
organization,
dangerouslyAllowBrowser: true,
};
this.client = new OpenAIClient(clientConfig);
this.model = fields?.model ?? fields?.modelName ?? this.model;
this.style = fields?.style ?? this.style;
this.quality = fields?.quality ?? this.quality;
this.n = fields?.n ?? this.n;
this.size = fields?.size ?? this.size;
this.responseFormat = fields?.responseFormat ?? this.responseFormat;
this.user = fields?.user;
}
/** @ignore */
async _call(input: string): Promise<string> {
const response = await this.client.images.generate({
model: this.model,
prompt: input,
n: this.n,
size: this.size,
response_format: this.responseFormat,
style: this.style,
quality: this.quality,
user: this.user,
});
let data = "";
if (this.responseFormat === "url") {
[data] = response.data
.map((item) => item.url)
.filter((url): url is string => url !== "undefined");
} else {
[data] = response.data
.map((item) => item.b64_json)
.filter((b64_json): b64_json is string => b64_json !== "undefined");
}
return data;
}
}