feat: support dall-e image not storage

This commit is contained in:
Hk-Gosuto 2023-12-14 20:02:28 +08:00
parent 22f43e5564
commit 789de39b2c
3 changed files with 16 additions and 7 deletions

View File

@ -38,7 +38,6 @@
- 除插件工具外,与原项目保持一致 [ChatGPT-Next-Web 主要功能](https://github.com/Yidadaa/ChatGPT-Next-Web#主要功能)
- 支持 GPT-4V(视觉) 模型
- 需要配置对象存储服务,请参考 [对象存储服务配置指南](./docs/s3-oss.md) 配置
- 基于 [LangChain](https://github.com/hwchase17/langchainjs) 实现的插件功能,目前支持以下插件,未来会添加更多
- 搜索
- [SerpAPI](https://js.langchain.com/docs/api/tools/classes/SerpAPI)
@ -54,6 +53,7 @@
- [Wiki](https://js.langchain.com/docs/api/tools/classes/WikipediaQueryRun)
- DALL-E 3
- DALL-E 3 插件需要配置对象存储服务,请参考 [对象存储服务配置指南](./docs/s3-oss.md) 配置
- 如无需图像转存则可以配置 `DALLE_NO_IMAGE_STORAGE=1` ,此时将直接将 DALL-E 服务返回的临时 URL 用于图像显示,注意:该链接具有时效性
- StableDiffusion
- 本插件目前为测试版本,后续可能会有较大的变更,请谨慎使用
- 使用本插件需要一定的专业知识Stable Diffusion 本身的相关问题不在本项目的解答范围内,如果您确定要使用本插件请参考 [Stable Diffusion 插件配置指南](./docs/stable-diffusion-plugin-cn.md) 文档进行配置

View File

@ -64,6 +64,8 @@ export class BaiduSearch extends Tool {
async _call(input: string) {
const searchResults = await search(input, this.maxResults);
console.log(searchResults);
if (searchResults.results.length === 0) {
return "No good search result found";
}

View File

@ -8,6 +8,8 @@ export class DallEAPIWrapper extends StructuredTool {
apiKey: string;
baseURL?: string;
noStorage: boolean;
callback?: (data: string) => Promise<void>;
constructor(
@ -22,6 +24,8 @@ export class DallEAPIWrapper extends StructuredTool {
this.apiKey = apiKey;
this.baseURL = baseURL;
this.callback = callback;
this.noStorage = !!process.env.DALLE_NO_IMAGE_STORAGE;
}
async saveImageFromUrl(url: string) {
@ -45,7 +49,7 @@ export class DallEAPIWrapper extends StructuredTool {
/** @ignore */
async _call({ prompt, size }: z.infer<typeof this.schema>) {
let image_url;
let imageUrl;
const apiUrl = `${this.baseURL}/images/generations`;
try {
const requestOptions = {
@ -64,21 +68,24 @@ export class DallEAPIWrapper extends StructuredTool {
const response = await fetch(apiUrl, requestOptions);
const json = await response.json();
console.log("[DALL-E]", json);
image_url = json.data[0].url;
imageUrl = json.data[0].url;
} catch (e) {
console.error("[DALL-E]", e);
}
if (!image_url) return "No image was generated";
if (!imageUrl) return "No image was generated";
try {
let filePath = await this.saveImageFromUrl(image_url);
let filePath = imageUrl;
if (!this.noStorage) {
filePath = await this.saveImageFromUrl(imageUrl);
}
console.log("[DALL-E]", filePath);
var imageMarkdown = `![img](${filePath})`;
if (this.callback != null) await this.callback(imageMarkdown);
return imageMarkdown;
} catch (e) {
if (this.callback != null)
await this.callback("Image upload to R2 storage failed");
return "Image upload to R2 storage failed";
await this.callback("Image upload to OSS failed");
return "Image upload to OSS failed";
}
}