Skip to content

Commit 516f3f4

Browse files
authored
support stable diffusion plugin (mlc-ai#49)
1 parent 3454ea3 commit 516f3f4

File tree

14 files changed

+269
-47
lines changed

14 files changed

+269
-47
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@
4141
- [SerpAPI](https://js.langchain.com/docs/api/tools/classes/SerpAPI)
4242
- [BingSerpAPI](https://js.langchain.com/docs/api/tools/classes/BingSerpAPI)
4343
- DuckDuckGo
44-
4544
- 计算
4645
- [Calculator](https://js.langchain.com/docs/api/tools_calculator/classes/Calculator)
47-
4846
- 网络请求
4947
- [WebBrowser](https://js.langchain.com/docs/api/tools_webbrowser/classes/WebBrowser)
50-
5148
- 其它
5249
- [Wiki](https://js.langchain.com/docs/api/tools/classes/WikipediaQueryRun)
5350
- DALL-E
5451
- DALL-E 插件需要配置 R2 存储,请参考 [Cloudflare R2 服务配置指南](./docs/cloudflare-r2-cn.md) 配置
55-
- ~只支持非 Cloudflare 环境的部署方式,在 Cloudflare 下该插件会失效 https://github.com/Hk-Gosuto/ChatGPT-Next-Web-LangChain/issues/43~
52+
- StableDiffusion
53+
- 本插件目前为测试版本,后续可能会有较大的变更,请谨慎使用
54+
- 使用本插件需要一定的专业知识,Stable Diffusion 本身的相关问题不在本项目的解答范围内,如果您确定要使用本插件请参考 [Stable Diffusion 插件配置指南](./docs/stable-diffusion-plugin-cn.md) 文档进行配置
55+
- StableDiffusion 插件需要配置 R2 存储,请参考 [Cloudflare R2 服务配置指南](./docs/cloudflare-r2-cn.md) 配置
5656

5757

5858

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Tool } from "langchain/tools";
2+
import S3FileStorage from "../../utils/r2_file_storage";
3+
4+
export class StableDiffusionWrapper extends Tool {
5+
name = "stable_diffusion_image_generator";
6+
7+
constructor() {
8+
super();
9+
}
10+
11+
/** @ignore */
12+
async _call(prompt: string) {
13+
let url = process.env.STABLE_DIFFUSION_API_URL;
14+
const data = {
15+
prompt: prompt,
16+
negative_prompt:
17+
process.env.STABLE_DIFFUSION_NEGATIVE_PROMPT ??
18+
"longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
19+
seed: -1,
20+
subseed: -1,
21+
subseed_strength: 0,
22+
batch_size: 1,
23+
n_iter: 1,
24+
steps: process.env.STABLE_DIFFUSION_STEPS ?? 20,
25+
cfg_scale: process.env.STABLE_DIFFUSION_CFG_SCALE ?? 7,
26+
width: process.env.STABLE_DIFFUSION_WIDTH ?? 720,
27+
height: process.env.STABLE_DIFFUSION_HEIGHT ?? 720,
28+
restore_faces: process.env.STABLE_DIFFUSION_RESTORE_FACES ?? false,
29+
eta: 0,
30+
sampler_index: process.env.STABLE_DIFFUSION_SAMPLER_INDEX ?? "Euler a",
31+
};
32+
console.log(`[${this.name}]`, data);
33+
const response = await fetch(`${url}/sdapi/v1/txt2img`, {
34+
method: "POST",
35+
headers: {
36+
"Content-Type": "application/json",
37+
},
38+
body: JSON.stringify(data),
39+
});
40+
const json = await response.json();
41+
let imageBase64 = json.images[0];
42+
if (!imageBase64) return "No image was generated";
43+
const buffer = Buffer.from(imageBase64, "base64");
44+
const filePath = await S3FileStorage.put(`${Date.now()}.png`, buffer);
45+
console.log(`[${this.name}]`, filePath);
46+
return filePath;
47+
}
48+
49+
description = `stable diffusion is an ai art generation model similar to dalle-2.
50+
input requires english.
51+
output will be the image link url.
52+
use markdown to display images. like: ![img](/api/file/xxx.png)`;
53+
}

app/api/langchain/tool/agent/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { DynamicTool, Tool } from "langchain/tools";
2121
import { DallEAPIWrapper } from "@/app/api/langchain-tools/dalle_image_generator";
2222
import { BaiduSearch } from "@/app/api/langchain-tools/baidu_search";
2323
import { GoogleSearch } from "@/app/api/langchain-tools/google_search";
24+
import { StableDiffusionWrapper } from "@/app/api/langchain-tools/stable_diffusion_image_generator";
2425

2526
const serverConfig = getServerSideConfig();
2627

@@ -228,10 +229,13 @@ async function handle(req: NextRequest) {
228229
const webBrowserTool = new WebBrowser({ model, embeddings });
229230
const calculatorTool = new Calculator();
230231
const dallEAPITool = new DallEAPIWrapper(apiKey, baseUrl);
232+
const stableDiffusionTool = new StableDiffusionWrapper();
231233
if (useTools.includes("web-search")) tools.push(searchTool);
232234
if (useTools.includes(webBrowserTool.name)) tools.push(webBrowserTool);
233235
if (useTools.includes(calculatorTool.name)) tools.push(calculatorTool);
234236
if (useTools.includes(dallEAPITool.name)) tools.push(dallEAPITool);
237+
if (useTools.includes(stableDiffusionTool.name))
238+
tools.push(stableDiffusionTool);
235239

236240
useTools.forEach((toolName) => {
237241
if (toolName) {

app/masks/cn.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import { BuiltinMask } from "./typing";
22

33
export const CN_MASKS: BuiltinMask[] = [
4+
{
5+
avatar: "1f3a8",
6+
name: "Stable Diffusion",
7+
context: [
8+
{
9+
id: "SVx3ybvohJAKXDQ1KKQcs",
10+
date: "",
11+
role: "system",
12+
content:
13+
"Stable Diffusion is an AI art generation model similar to DALLE-2.\nHere are some prompts for generating art with Stable Diffusion.\n\nPrompt Example:\n\n- A ghostly apparition drifting through a haunted mansion's grand ballroom, illuminated by flickering candlelight. Eerie, ethereal, moody lighting.\n- portait of a homer simpson archer shooting arrow at forest monster, front game card, drark, marvel comics, dark, smooth\n- pirate, deep focus, fantasy, matte, sharp focus\n- red dead redemption 2, cinematic view, epic sky, detailed, low angle, high detail, warm lighting, volumetric, godrays, vivid, beautiful\n- a fantasy style portrait painting of rachel lane / alison brie hybrid in the style of francois boucher oil painting, rpg portrait\n- athena, greek goddess, claudia black, bronze greek armor, owl crown, d & d, fantasy, portrait, headshot, sharp focus\n- closeup portrait shot of a large strong female biomechanic woman in a scenic scifi environment, elegant, smooth, sharp focus, warframe\n- ultra realistic illustration of steve urkle as the hulk, elegant, smooth, sharp focus\n- portrait of beautiful happy young ana de armas, ethereal, realistic anime, clean lines, sharp lines, crisp lines, vibrant color scheme\n- A highly detailed and hyper realistic portrait of a gorgeous young ana de armas, lisa frank, butterflies, floral, sharp focus\n- lots of delicious tropical fruits with drops of moisture on table, floating colorful water, mysterious expression, in a modern and abstract setting, with bold and colorful abstract art, blurred background, bright lighting\n- 1girl, The most beautiful form of chaos, Fauvist design, Flowing colors, Vivid colors, dynamic angle, fantasy world\n- solo, sitting, close-up, girl in the hourglass, Sand is spilling out of the broken hourglass, flowing sand, huge hourglass art, hologram, particles, nebula, magic circle\n- geometric abstract background, 1girl, depth of field, zentangle, mandala, tangle, entangle, beautiful and aesthetic, dynamic angle, glowing skin, floating colorful sparkles the most beautiful form of chaos, elegant, a brutalist designed, vivid colours, romanticism\n\nFollow the structure of the example prompts. This means a very short description of the scene, followed by modifiers divided by commas to alter the mood, style, lighting, and more.\nIf the user input is in English, directly use the user input as a parameter to call the stable_diffusion_image_generator plugin. If the user input is not in English, generate an English prompt word based on the example and then call the stable_diffusion_image_generator plugin.",
14+
},
15+
],
16+
modelConfig: {
17+
model: "gpt-3.5-turbo",
18+
temperature: 1,
19+
top_p: 1,
20+
max_tokens: 2000,
21+
presence_penalty: 0,
22+
frequency_penalty: 0,
23+
sendMemory: false,
24+
historyMessageCount: 0,
25+
compressMessageLengthThreshold: 1000,
26+
},
27+
lang: "cn",
28+
builtin: false,
29+
createdAt: 1697205441045,
30+
usePlugins: true,
31+
hideContext: true,
32+
},
433
{
534
avatar: "1f5bc-fe0f",
635
name: "以文搜图",

app/masks/en.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
import { BuiltinMask } from "./typing";
22

33
export const EN_MASKS: BuiltinMask[] = [
4+
{
5+
avatar: "1f3a8",
6+
name: "Stable Diffusion",
7+
context: [
8+
{
9+
id: "SVx3ybvohJAKXDQ1KKQcs",
10+
date: "",
11+
role: "system",
12+
content:
13+
"Stable Diffusion is an AI art generation model similar to DALLE-2.\nHere are some prompts for generating art with Stable Diffusion.\n\nPrompt Example:\n\n- A ghostly apparition drifting through a haunted mansion's grand ballroom, illuminated by flickering candlelight. Eerie, ethereal, moody lighting.\n- portait of a homer simpson archer shooting arrow at forest monster, front game card, drark, marvel comics, dark, smooth\n- pirate, deep focus, fantasy, matte, sharp focus\n- red dead redemption 2, cinematic view, epic sky, detailed, low angle, high detail, warm lighting, volumetric, godrays, vivid, beautiful\n- a fantasy style portrait painting of rachel lane / alison brie hybrid in the style of francois boucher oil painting, rpg portrait\n- athena, greek goddess, claudia black, bronze greek armor, owl crown, d & d, fantasy, portrait, headshot, sharp focus\n- closeup portrait shot of a large strong female biomechanic woman in a scenic scifi environment, elegant, smooth, sharp focus, warframe\n- ultra realistic illustration of steve urkle as the hulk, elegant, smooth, sharp focus\n- portrait of beautiful happy young ana de armas, ethereal, realistic anime, clean lines, sharp lines, crisp lines, vibrant color scheme\n- A highly detailed and hyper realistic portrait of a gorgeous young ana de armas, lisa frank, butterflies, floral, sharp focus\n- lots of delicious tropical fruits with drops of moisture on table, floating colorful water, mysterious expression, in a modern and abstract setting, with bold and colorful abstract art, blurred background, bright lighting\n- 1girl, The most beautiful form of chaos, Fauvist design, Flowing colors, Vivid colors, dynamic angle, fantasy world\n- solo, sitting, close-up, girl in the hourglass, Sand is spilling out of the broken hourglass, flowing sand, huge hourglass art, hologram, particles, nebula, magic circle\n- geometric abstract background, 1girl, depth of field, zentangle, mandala, tangle, entangle, beautiful and aesthetic, dynamic angle, glowing skin, floating colorful sparkles the most beautiful form of chaos, elegant, a brutalist designed, vivid colours, romanticism\n\nFollow the structure of the example prompts. This means a very short description of the scene, followed by modifiers divided by commas to alter the mood, style, lighting, and more.\nIf the user input is in English, directly use the user input as a parameter to call the stable_diffusion_image_generator plugin. If the user input is not in English, generate an English prompt word based on the example and then call the stable_diffusion_image_generator plugin.",
14+
},
15+
],
16+
modelConfig: {
17+
model: "gpt-3.5-turbo",
18+
temperature: 1,
19+
top_p: 1,
20+
max_tokens: 2000,
21+
presence_penalty: 0,
22+
frequency_penalty: 0,
23+
sendMemory: false,
24+
historyMessageCount: 0,
25+
compressMessageLengthThreshold: 1000,
26+
},
27+
lang: "en",
28+
builtin: false,
29+
createdAt: 1697205441045,
30+
usePlugins: true,
31+
hideContext: true,
32+
},
433
{
534
avatar: "1f47e",
635
name: "GitHub Copilot",

app/plugins/cn.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ export const CN_PLUGINS: BuiltinPlugin[] = [
4848
createdAt: 1694703673000,
4949
enable: false,
5050
},
51+
{
52+
name: "Stable Diffusion",
53+
toolName: "stable_diffusion_image_generator",
54+
lang: "cn",
55+
description:
56+
"Stable Diffusion 图像生成模型。使用本插件需要配置 Cloudflare R2 对象存储服务以及 stable-diffusion-webui 接口。",
57+
builtin: true,
58+
createdAt: 1688899480510,
59+
enable: false,
60+
},
5161
];

app/plugins/en.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ export const EN_PLUGINS: BuiltinPlugin[] = [
5050
createdAt: 1694703673000,
5151
enable: false,
5252
},
53+
{
54+
name: "Stable Diffusion",
55+
toolName: "stable_diffusion_image_generator",
56+
lang: "en",
57+
description:
58+
"Stable Diffusion text-to-image model. Using this plugin requires configuring Cloudflare R2 object storage service and stable-diffusion-webui API.",
59+
builtin: true,
60+
createdAt: 1688899480510,
61+
enable: false,
62+
},
5363
];

app/utils/r2_file_storage.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ export default class S3FileStorage {
5151

5252
console.log(signedUrl);
5353

54-
await fetch(signedUrl, {
55-
method: "PUT",
56-
body: data,
57-
});
54+
try {
55+
await fetch(signedUrl, {
56+
method: "PUT",
57+
body: data,
58+
});
5859

59-
return `/api/file/${fileName}`;
60+
return `/api/file/${fileName}`;
61+
} catch (e) {
62+
console.error("[R2]", e);
63+
throw e;
64+
}
6065
}
6166
}
860 KB
Loading
45.8 KB
Loading

0 commit comments

Comments
 (0)