1、准备Midjourney账号,我们给大家准备公共账号了,可以免费用一个月
公共账号群里找 米开朗基杨 拿
2、注册laf账号,并新建云函数
注册地址:https://laf.dev
API云函数
import cloud from '@lafjs/cloud'
import { Midjourney, MidjourneyMessage } from 'midjourney'
const SERVER_ID = ''
const CHANNEL_ID = ''
const SALAI_TOKEN = ''
const Limit = 100
const MaxWait = 3
const 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.body
switch (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 } = param
const msg = await client.Imagine(
`[${msg_Id}] ${question}`,
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log("imagine success ", msg)
return true
}
async function upscale(param) {
console.log("upscale", param)
const { question, index, id, url } = param
const 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
}
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 } = param
const 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
}
3、获取并修改SERVER_ID、CHANNEL_ID、SALAI_TOKEN
公共账号,无需自己手动获取。如果是自己的账号,获取方法如下
- 新建频道,并拉midjourney机器人进群
- 从URL中获取SERVER_ID、CHANNEL_ID
- 从浏览器控制台获取SALAI_TOKEN
打开浏览器控制台,并切换到NERWORK,进入Fetch标签页,刷新网页,选择任意一个请求,找到authorization,这个就是SALAI_TOKEN
修改云函数中的SERVER_ID、CHANNEL_ID、SALAI_TOKEN并发布云函数
4、测试请求
用POST请求去请求这个云函数
以下是请求参数的示例
查询全部历史消息
{
"type":"RetrieveMessages"
}
生图,msg_Id
由前端生成,后续查询生成结果用
{
"type":"imagine",
"param":{
"question": "a super man",
"msg_Id":"1"
}
}
放大图片,id
url
来自查询全部历史消息,index
是图片序号,question
是提示词
{
"type":"upscale",
"param":{
"index":2,
"question": "a dog and a cat",
"id":"1111579111815659551",
"url":"https://cdn.discordapp.com/attachments/1110206663958466611/1111579111287164988/johnsonmaureen_2023_a_dog_and_a_cat_5927bc5d-5d80-423c-bf89-c2357d5aaf6b.png"
}
}
变换图片,id
url
来自查询全部历史消息,index
是图片序号,question
是提示词
{
"type":"variation",
"param":{
"index":2,
"question": "a dog and a cat",
"id":"1111579111815659551",
"url":"https://cdn.discordapp.com/attachments/1110206663958466611/1111579111287164988/johnsonmaureen_2023_a_dog_and_a_cat_5927bc5d-5d80-423c-bf89-c2357d5aaf6b.png"
}
}