AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


【dify工具源码】手把手教你将midjourney v6接入到dify中
发布日期:2024-09-25 13:13:45 浏览次数: 1722 来源:提示日记


 先说好,midjourney本身是没有公开的api接口的,所以今天说的是一个叫mazeguru平台接入了midjourney,然后从mazeguru平台平台的api做成dify的自定义工具,实现将midjourney v6接入到dify中调用。


https://maze.guru/


我们都知道,在github有midjourney的套壳项目,其本质还是用你的midjourney账户的token来抓数据的,这种行为对你的midjourney账户是不安全的,并且很不稳定,搞不好你的账户就嘎了。mazeguru这个平台就好用一些,因为它集成度已经很高了。


本次分享主要分两个方面:

  1. dify自定义工具源码分享

  2. 使用细节


01

dify自定义工具源码分享


{"openapi": "3.1.0","info": {"title": "Mazeguru","description": "提供文生图和图生图功能,以及查询生成结果的API。","version": "1.0.0"},"tags": [{"name": "Image Generation","description": "文生图相关操作"},{"name": "Image-to-Image Generation","description": "图生图相关操作"},{"name": "Result Query","description": "查询生成结果"}],"paths": {"/api/v1/txt2img": {"post": {"summary": "生成图像","operationId": "文生图","tags": ["Image Generation"],"parameters": [{"name": "Authorization","in": "header","required": true,"description": "这里填写的是mazeguru平台的App Key。","schema": {"type": "string"}}],"requestBody": {"content": {"application/json": {"schema": {"type": "object","properties": {"num": {"type": "integer","description": "生成图像数量,默认为1","default": 1},"mj_stylize": {"type": "integer","description": "仅选择MJ型号生效,如果你确定使用midjourney,这里必须填写:100.如果填写的不是100,表示不调用midjourney模型。","default": 100},"seed": {"type": "string","description": "随机种子,这里建议填写不超过8位数的正整数。这个就不谈了,大家都懂。"},"art_genre": {"type": "string","description": "艺术风格,当你使用midjourney任何模型的时候,这里不填即可。当你使用非midjourney模型时,这里可以填,具体能填什么,看我公众号文章分享【公众号:提示日记】","example": "animal"},"width": {"type": "integer","description": "图像宽度,与高度参数搭配使用的。宽高取值只能是如下几个:1024x1024 、 768x1536 、 928x1232 、 1232x928 、 1456x816 、 816x1456","default": 1024},"height": {"type": "integer","description": "图像高度,与宽度参数搭配使用的。宽高取值只能是如下几个:1024x1024 、 768x1536 、 928x1232 、 1232x928 、 1456x816 、 816x1456","default": 1024},"style_id": {"type": "integer","description": "样式ID。请注意,这里不能乱填,如果你使用midjourney的模型,这里只有4个参数可以填写,任填一个:153 、 154 、20003 、 20006。【153对应MJv5】【154对应Niji】【20003对应MJv6】【20006对应Nijiv6】","default": 20003},"prompt": {"type": "string","description": "图像生成提示词。最大不能超过1000个单词。"}},"required": ["prompt"]}}}},"responses": {"200": {"description": "成功生成任务ID","content": {"application/json": {"schema": {"type": "object","properties": {"code": { "type": "string" },"data": {"type": "object","properties": {"jobs": {"type": "array","items": { "type": "string" }}}},"msg": { "type": "string" }}}}}}}}},"/api/v1/img2img": {"post": {"summary": "图生图生成图像","operationId": "图生图","tags": ["Image-to-Image Generation"],"parameters": [{"name": "Authorization","in": "header","required": true,"description": "这里填写的是mazeguru平台的App Key。","schema": {"type": "string"}}],"requestBody": {"content": {"application/json": {"schema": {"type": "object","properties": {"num": {"type": "integer","description": "生成图像数量","default": 1},"mj_stylize": {"type": "integer","description": "仅选择MJ型号生效,如果你确定使用midjourney,这里必须填写:100.如果填写的不是100,表示不调用midjourney模型。","default": 100},"style_id": {"type": "integer","description": "样式ID。请注意,这里不能乱填,如果你使用midjourney的模型,这里只有4个参数可以填写,任填一个:153 、 154 、20003 、 20006。【153对应MJv5】【154对应Niji】【20003对应MJv6】【20006对应Nijiv6】","default": 20003},"prompt": {"type": "string","description": "图像生成提示词。最大不能超过1000个单词。"},"width": {"type": "integer","description": "图像宽度,与高度参数搭配使用的。宽高取值只能是如下几个:1024x1024 、 768x1536 、 928x1232 、 1232x928 、 1456x816 、 816x1456","default": 1024},"height": {"type": "integer","description": "图像高度,与宽度参数搭配使用的。宽高取值只能是如下几个:1024x1024 、 768x1536 、 928x1232 、 1232x928 、 1456x816 、 816x1456","default": 1024},"art_genre": {"type": "string","description": "艺术风格,当你使用midjourney任何模型的时候,这里不填即可。当你使用非midjourney模型时,这里可以填,具体能填什么,看我公众号文章分享【公众号:提示日记】","example": "写真"},"seed": {"type": "string","description": "随机种子,这里建议填写不超过8位数的正整数。这个就不谈了,大家都懂。"},"init_image_url": {"type": "string","description": "种子图像URL,请注意填写完整的URL,并且确保mazeguru能访问并下载你的种子图像。"},"init_image_similarity": {"type": "integer","description": "与种子图像的相似度,值0-100的整数,数值越大相似度越高","default": 75}},"required": ["prompt", "init_image_url"]}}}},"responses": {"200": {"description": "成功生成任务ID","content": {"application/json": {"schema": {"type": "object","properties": {"code": { "type": "string" },"data": {"type": "object","properties": {"jobs": {"type": "array","items": { "type": "string" }}}},"msg": { "type": "string" }}}}}}}}},"/api/v1/generate-result": {"post": {"summary": "查询生成结果","operationId": "查询结果","tags": ["Result Query"],"parameters": [{"name": "Authorization","in": "header","required": true,"description": "这里填写的是mazeguru平台的App Key。","schema": {"type": "string"}}],"requestBody": {"content": {"application/json": {"schema": {"type": "object","properties": {"jobs": {"type": "array","items": { "type": "string" },"description": "任务ID列表"}},"required": ["jobs"]}}}},"responses": {"200": {"description": "成功查询结果","content": {"application/json": {"schema": { "type": "object","properties": {"code": { "type": "string" },"data": {"type": "array","items": {"type": "object","properties": {"job": { "type": "string" },"status": { "type": "integer" },"picture_url": { "type": "string" },"complete_percent": { "type": "string" },"audio_info": { "type": "string" }}}}}}}}}}}}},"components": {},"servers": [{"url": "https://open.maze.guru"}]}



添加好了之后,就如上图所示。从mazeguru接入了3个工具,分别是文生图、图生图、查询结果。这三个工具都能在dify的工作流中使用。





请注意,无论是文生图还是图生图工具,后面都需要先提取任务ID,也就是返回数据中的jobid。然后将jobid传给查询结果的工具获取生成的图片URL。


为了方便大家理解不同工具的返回结果,下面我将各个工具的返回数据示例写出来,大家自行参考。


文生图:

{"code": "200","data": {"jobs": ["C5B5FE8DBF3679D9049342C130341BD9"]},"msg": ""}

图生图:

{"code": "200","data": {"jobs": ["C5B5FE8DBF3679D9049342C130341BD9"]},"msg": ""}

由此可见,文生图和图生图的返回结构是一样的,你需要提取jobs列表中的参数即可。请注意,如果你生成的是多张图,jobs列表中会出现多个参数哦。如果你是新手,我个人建议你生成图片的数量设置为1,这样方便你提取参数。如果你是编程老手,我个人强烈推荐大家使用dify v0.7.1以上的版本,搭配会话变量这个功能临时存储,然后分别查询结果,这样可以锻炼你使用会话变量这个功能的习惯。说实话,我认为这个功能真的很不错。


查询结果:

{"code": "200","data": [{"job": "1746803472390426624","status": 4,"picture_url": "https://cdn.maze.guru/ai/13tpaqf/8E76E92A9BF2B64ECE3F12690DA6FE43-01.jpg","complete_percent": "1","audio_info": "[{\"rate\":99.9,\"suggestion\":\"pass\",\"label\":\"normal\",\"scene\":\"porn\"},{\"rate\":99.91,\"suggestion\":\"pass\",\"label\":\"normal\",\"scene\":\"terrorism\"}]"}]}

其中picture_url就是生成的图片地址。

当然,这个返回结果存在几个你没见过的参数,分别给大家解释一下,如下图所示:

一般来说,异步任务有这样的返回结果,显得还是很专业的,mazeguru跟fal平台生成图片是不一样的哈,大家要注意,fal平台是线性任务,获取图片的过程中是需要“在线等待”的,而mazeguru是异步生成,图片的结果是需要单独请求(也就是使用查询结果这个工具)才能获得。



02


使用细节


关于工具的使用细节,我上面基本上讲清楚了,但是如果你需要获得mazeguru平台更多的样式信息,那么你就需要看看这个,下面我提供一个样式对照表,方便你灵活使用。


Stable Style(适用于基础样式)

style idstyle name
29Girl Realistic
31Real Photo
43Fantasy
52Universal
71Scifi
90C4D
100Asian Portrait
111Any Portait
1562.5D XL
192Interior Design XL
193Universal XL
20004Dalle3

Anime Style(适用于基础样式)

style idstyle name
3Default V1
46Semi Impasto
84TV002
97Everything
112Lineless Art
1132.2D Impasto
155Anime XL

MJ&NIJI(mj模型专用,开会员才能用)

style idstyle name
153MJv5
154Niji
20003MJv6
20006Nijiv6

Exclusive(平台特殊样式,开会员才能用)

style idstyle name
115Emoji Mixed
116SD Figure
117Geometri c
1181990s
119Chubby
120Flat Graphics
121Fairy
122Bokeh
123Childbook
124Papercut
125Sharpie
126Tshirt Printing
127Vector Graphics
128Hand Draw
129Minimal Logo
130Watercolor
131Felt Craft
132Hologram
133Vector Design
134Kawayi
140Glass Crack
141Genevieve Book
142Cute Animals
143Cobblestone
144Oriental Ink
145Mexican Sticker
146Zen Illustration
147Japan Minimalist
1482D Chibi
149Star Warframe
150Low Poly
151Splatoon
152Fresh Punk
157Ukiyoe
158Yoshitaka Amano
160Goro Fujita
161Kintsugi
162Wabi-sabi
163Ikebana
164Trendy Toys
165Temari
166Surrealism
167Line Art
168Floral Art
169Gothic Fairy Tale
170Superhero Poster
171Superhero Comic
173Superhero Comic III
174Superhero Comic II
175Romantic Goth
176Pop Art
177Kawanabe Kyosai
178Japanese Vintage Poster
179Hina Doll
180Healing Cartoon
181Hasui Kawase
182Harajuku Fashion
183Future Punk
184Cement Design
185Broken Design
186Brazilian Modernism II
187Brazilian Modernism
188Balloon Art
189B&W Goth


好了,以上就是这个工具的使用全部教程了,请留意,这个平台调用mj的成本比mj官方是高的,好处就是不会封你的mj账户,起码比你直接使用mj代理平台抓数据要安全一些。


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询