-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Googlefan
committed
May 16, 2024
1 parent
f5158de
commit b1bd6ac
Showing
3 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { BaseGuildTextChannel, Collection } from "discord.js"; | ||
import { Chat, models } from "./chat"; | ||
import { client } from "."; | ||
|
||
const channelSpecificHistory = new Collection<string, Chat[]>(); | ||
const chatQueue = new Collection< | ||
string, | ||
{ queue: (Chat & { id: string })[]; processing: boolean } | ||
>(); | ||
|
||
function addHistory(channelId: string, chat: Chat) { | ||
channelSpecificHistory.set(channelId, [ | ||
...(channelSpecificHistory.get(channelId) || []), | ||
chat, | ||
]); | ||
} | ||
|
||
setInterval(() => { | ||
for (const [channelId, { queue, processing }] of chatQueue) { | ||
if (processing) continue; | ||
if (queue.length === 0) continue; | ||
const chat = queue.shift()!; | ||
chatQueue.set(channelId, { | ||
queue, | ||
processing: true, | ||
}); | ||
const model = models.find((x) => x.id === "gemini-1.0-pro")!; | ||
model | ||
.generate([...(channelSpecificHistory.get(channelId) || []), chat]) | ||
.then(async (res) => { | ||
let msg = undefined; | ||
try { | ||
const ch = client.channels.cache.get(channelId) as | ||
| BaseGuildTextChannel | ||
| undefined; | ||
msg = await ch?.send({ | ||
content: "Generating...", | ||
reply: { | ||
messageReference: chat.id, | ||
}, | ||
}); | ||
try { | ||
const msg = await ch?.messages.fetch(chat.id); | ||
msg?.reactions.removeAll(); | ||
} catch { | ||
console.error("Failed to fetch message"); | ||
} | ||
let tokens = 0; | ||
let lastTokens = 0; | ||
let contents = ""; | ||
for await (const { tokens: t, content: c } of res) { | ||
tokens += t; | ||
contents += c; | ||
if (tokens - lastTokens > 100) { | ||
await msg?.edit(contents); | ||
lastTokens = tokens; | ||
} | ||
} | ||
await msg?.edit(contents); | ||
addHistory(channelId, chat); | ||
addHistory(channelId, { | ||
text: contents, | ||
role: "assistant", | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
if (msg) { | ||
try { | ||
await msg.edit("Failed to generate message"); | ||
} catch {} | ||
} | ||
} | ||
chatQueue.set(channelId, { | ||
queue, | ||
processing: false, | ||
}); | ||
}); | ||
} | ||
}, 1000); | ||
|
||
export function addChatQueue(channelId: string, chat: Chat & { id: string }) { | ||
if (!chatQueue.has(channelId)) { | ||
chatQueue.set(channelId, { | ||
queue: [], | ||
processing: false, | ||
}); | ||
} | ||
chatQueue.get(channelId)!.queue.push(chat); | ||
} |