首先感谢laf提供免费使用Midjourney API接口和云函数,需要详细了解的可以访问他们的官网论坛。
感谢论坛前面几位的分享,我做了参考。都有参考就不列啦哈!!!
直接开始:
第一步
复制MJ-SEND云函数到laf云平台,并发布获得云函数访问地址“https://xxx.laf.dev/mj-sent”
import cloud from '@lafjs/cloud'
import { Midjourney, MidjourneyMessage } from 'midjourney'
const SERVER_ID = '' // Midjourney 服务 ID
const CHANNEL_ID = '' // Midjourney 频道 ID
const SALAI_TOKEN = '' // Midjourney 服务 Tokenconst Limit = 100
const MaxWait = 3const client = new Midjourney({ServerId: SERVER_ID,ChannelId: CHANNEL_ID,SalaiToken: SALAI_TOKEN,Debug: true,SessionId: SALAI_TOKEN,Limit: Limit,MaxWait: MaxWait
});export default async function (ctx: FunctionContext) {const { type, param } = ctx.bodyswitch (type) {case 'RetrieveMessages':return await RetrieveMessages(param)case 'imagine':return await imagine(param)case 'upscale':return await upscale(param)case 'variation':return await variation(param)}}// 查询最近消息
async function RetrieveMessages(param) {console.log("RetrieveMessages")const client = new MidjourneyMessage({ChannelId: CHANNEL_ID,SalaiToken: SALAI_TOKEN,});const msg = await client.RetrieveMessages();console.log("RetrieveMessages success ", msg)return msg
}// 创建生图任务
async function imagine(param) {console.log("imagine", param)const { question, msg_Id } = paramconst msg = await client.Imagine(`[${msg_Id}] ${question}`,(uri: string, progress: string) => {console.log("loading", uri, "progress", progress);});console.log("imagine success ", msg)return true
}// upscale 放大图片
async function upscale(param) {console.log("upscale", param)const { question, index, id, url } = paramconst hash = url.split("_").pop()?.split(".")[0] ?? ""console.log(hash)const msg = await client.Upscale(question,index,id,hash,(uri: string, progress: string) => {console.log("loading", uri, "progress", progress);});console.log("upscale success ", msg)return msg
}// variation 变换图片
async function variation(param) {console.log("variation", param)const client = new Midjourney({ServerId: SERVER_ID,ChannelId: CHANNEL_ID,SalaiToken: SALAI_TOKEN,Debug: true,SessionId: SALAI_TOKEN,Limit: Limit,MaxWait: 100});const { question, index, id, url } = paramconst hash = url.split("_").pop()?.split(".")[0] ?? ""const msg = await client.Variation(question,index,id,hash,(uri: string, progress: string) => {console.log("loading", uri, "progress", progress);});console.log("variation success ", msg)return msg
}
第二步
到我的huggingface空间复制到自己的服务器,查找“https://xxx.laf.dev/mj-sent”替换成你自己的云函数地址“
3、五分钟不到就可以免费拥有CHATGPT+Midjourney啦,加油!!去赢得AI第一桶金!!!