fix(43): fix dall-e plugin (#46)

This commit is contained in:
Hk-Gosuto 2023-10-08 02:53:38 -05:00 committed by GitHub
parent 33d4f3d603
commit 8b769714ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 12 deletions

View File

@ -1,5 +1,4 @@
import { Tool } from "langchain/tools";
import OpenAI from "openai";
import S3FileStorage from "../../utils/r2_file_storage";
export class DallEAPIWrapper extends Tool {
@ -27,18 +26,29 @@ export class DallEAPIWrapper extends Tool {
/** @ignore */
async _call(prompt: string) {
const openai = new OpenAI({
apiKey: this.apiKey,
baseURL: this.baseURL,
});
const response = await openai.images.generate({
prompt: prompt,
n: this.n,
size: this.size,
});
let image_url;
const apiUrl = `${this.baseURL}/images/generations`;
let image_url = response.data[0].url;
console.log(image_url);
try {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
prompt: prompt,
n: this.n,
size: this.size
})
};
const response = await fetch(apiUrl, requestOptions);
const json = await response.json();
console.log("[DALL-E]", json);
image_url = json.data[0].url;
} catch (e) {
console.error("[DALL-E]", e);
}
if (!image_url) return "No image was generated";
let filePath = await this.saveImageFromUrl(image_url);
console.log(filePath);