Skip to content

Commit

Permalink
add simple huggingface service
Browse files Browse the repository at this point in the history
with a static model for now
  • Loading branch information
Techbot121 authored and Meta Construct committed Dec 5, 2023
1 parent b37c019 commit 841926a
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 133 deletions.
39 changes: 39 additions & 0 deletions app/services/Huggingface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Container } from "../Container";
import { HfInference } from "@huggingface/inference";
import { Service } from ".";
import config from "@/config/huggingface.json";

const hf = new HfInference(config.accessToken);

const Model = "mistralai/Mistral-7B-Instruct-v0.1";

export class Huggingface extends Service {
name = "Huggingface";

async textGeneration(input: string, limit: number, temperature?: number) {
return await hf.textGeneration({
model: Model,
inputs: `<s>[INST] ${config.systemPrompt} Hi [/INST] ${
config.systemAnswer
}</s>[INST] ${input.replaceAll(/\[\/?INST\]|<\/?s>/g, "")} [/INST]`,
parameters: {
max_new_tokens: limit,
temperature: temperature,
return_full_text: false,
},
});
}

// async textGenerationStream(input: string, limit?: number) {
// for await (const output of hf.textGenerationStream({
// model: Model,
// inputs: input,
// parameters: { max_new_tokens: limit },
// })) {
// return output.generated_text;
// }
// }
}
export default (container: Container): Service => {
return new Huggingface(container);
};
4 changes: 2 additions & 2 deletions app/services/Markov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class MarkovChain extends MarkovChainBase {
});
}
}
export class MarkovService extends Service {
export class Markov extends Service {
name = "Markov";
markov = new MarkovChain("./metaconcord.db");

Expand Down Expand Up @@ -266,5 +266,5 @@ export class MarkovService extends Service {
}

export default (container: Container): Service => {
return new MarkovService(container);
return new Markov(container);
};
35 changes: 15 additions & 20 deletions app/services/discord/modules/shitposting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const ALLOWED_IMG_PROVIDERS = ["tenor", "imgur", "discordapp", "tumblr"];

const IGNORE_LIST = ["437294613976449024"];

function getWord(msg: string) {
function getWord(msg?: string) {
if (!msg) return undefined;
let search: string;
const words = msg.replaceAll(`<@${DiscordConfig.bot.userId}> `, "").split(" ");
const index = (Math.random() * words.length) | 0;
Expand Down Expand Up @@ -68,28 +69,22 @@ export const Shat = async (options?: {
: options.forceMessage;
const rng = Math.random();
if (rng > TENOR_IMAGE_FREQ && !options?.forceImage) {
const message = options?.msg?.replaceAll(`<@${DiscordConfig.bot.userId}> `, "");
let search: string | undefined;
if (
options?.msg &&
!options.msg.startsWith("http") &&
(rng <= REPLY_FREQ || options.forceReply)
) {
search = getWord(options.msg);
let shat: string | undefined;
if (message && !message.startsWith("http")) {
if (rng <= REPLY_FREQ || options?.forceReply) search = getWord(options?.msg);
const response = await globalThis.MetaConcord.container
.getService("Huggingface")
?.textGeneration(message, 100);
shat = response.generated_text;
}
let mk = await globalThis.MetaConcord.container
.getService("Markov")
?.generate(search, DefaultMarkovConfig);

if ((!mk || mk === options?.msg) && options?.fallback)
mk = await globalThis.MetaConcord.container
if (!shat || shat === options?.msg)
shat = await globalThis.MetaConcord.container
.getService("Markov")
?.generate(getWord(options.fallback), DefaultMarkovConfig);
if (!mk || mk === options?.msg)
mk = await globalThis.MetaConcord.container
.getService("Markov")
?.generate(undefined, DefaultMarkovConfig);

return mk ? { content: mk.replaceAll(`<@${DiscordConfig.bot.userId}> `, "") } : undefined;
?.generate(getWord(search ?? options?.fallback), DefaultMarkovConfig);
return shat ? { content: shat } : undefined;
} else {
const images = globalThis.MetaConcord.container.getService("Motd")?.images;
let word =
Expand Down Expand Up @@ -423,7 +418,7 @@ export default async (bot: DiscordBot) => {
).catch(console.error);
if (isChatChannel) replied = true;
} else {
msg.react(getRandomEmoji());
msg.react(getRandomEmoji()).catch();
}
}

Expand Down
9 changes: 7 additions & 2 deletions app/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import DataProvider, { Data } from "./Data";
import DiscordBotProvider, { DiscordBot } from "./discord";
import DiscordMetadataProvider, { DiscordMetadata } from "./DiscordMetadata";
import GameBridgeProvider, { GameBridge } from "./gamebridge";
import HuggingfaceProvider, { Huggingface } from "./Huggingface";
import IRCProvider, { IRC } from "./IRC";
import MarkovProvider, { MarkovService } from "./Markov";
import MarkovProvider, { Markov } from "./Markov";
import MotdProvider, { Motd } from "./Motd";
import SQLProvider, { SQL } from "./SQL";
import StarboardProvider, { Starboard } from "./Starboard";
Expand All @@ -25,6 +26,7 @@ import WebAppProvider, { WebApp } from "./webapp";
export default [
SQLProvider,
MarkovProvider,
HuggingfaceProvider,
SteamProvider,
DataProvider,
BanProvider,
Expand All @@ -40,6 +42,8 @@ export default [

export {
SQL,
Markov,
Huggingface,
Data,
DiscordBot,
GameBridge,
Expand All @@ -57,12 +61,13 @@ export type ServiceMap = {
Data?: Data;
DiscordBot?: DiscordBot;
GameBridge?: GameBridge;
Huggingface?: Huggingface;
Bans?: Bans;
Steam?: Steam;
DiscordMetadata?: DiscordMetadata;
WebApp?: WebApp;
Motd?: Motd;
Markov?: MarkovService;
Markov?: Markov;
Starboard?: Starboard;
Tenor?: Tenor;
SQL?: SQL;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@ffmpeg.wasm/core-mt": "^0.12.0",
"@ffmpeg.wasm/main": "^0.12.0",
"@huggingface/inference": "^2.6.4",
"@napi-rs/canvas": "^0.1.37",
"@octokit/webhooks": "^12.0.3",
"ajv": "^8.11.2",
Expand All @@ -27,7 +28,7 @@
"cookie-parser": "^1.4.6",
"dayjs": "^1.10.6",
"diff": "^5.1.0",
"discord.js": "^14.13.0",
"discord.js": "^14.14.1",
"dotenv": "^16.0.2",
"express": "^4.17.1",
"express-rate-limit": "^6.7.0",
Expand Down
Loading

0 comments on commit 841926a

Please sign in to comment.