fix(43): fix dall-e plugin (#46)
This commit is contained in:
parent
33d4f3d603
commit
8b769714ce
|
@ -1,5 +1,4 @@
|
||||||
import { Tool } from "langchain/tools";
|
import { Tool } from "langchain/tools";
|
||||||
import OpenAI from "openai";
|
|
||||||
import S3FileStorage from "../../utils/r2_file_storage";
|
import S3FileStorage from "../../utils/r2_file_storage";
|
||||||
|
|
||||||
export class DallEAPIWrapper extends Tool {
|
export class DallEAPIWrapper extends Tool {
|
||||||
|
@ -27,18 +26,29 @@ export class DallEAPIWrapper extends Tool {
|
||||||
|
|
||||||
/** @ignore */
|
/** @ignore */
|
||||||
async _call(prompt: string) {
|
async _call(prompt: string) {
|
||||||
const openai = new OpenAI({
|
let image_url;
|
||||||
apiKey: this.apiKey,
|
const apiUrl = `${this.baseURL}/images/generations`;
|
||||||
baseURL: this.baseURL,
|
|
||||||
});
|
|
||||||
const response = await openai.images.generate({
|
|
||||||
prompt: prompt,
|
|
||||||
n: this.n,
|
|
||||||
size: this.size,
|
|
||||||
});
|
|
||||||
|
|
||||||
let image_url = response.data[0].url;
|
try {
|
||||||
console.log(image_url);
|
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";
|
if (!image_url) return "No image was generated";
|
||||||
let filePath = await this.saveImageFromUrl(image_url);
|
let filePath = await this.saveImageFromUrl(image_url);
|
||||||
console.log(filePath);
|
console.log(filePath);
|
||||||
|
|
Loading…
Reference in New Issue