Skip to content

Commit 625e6ea

Browse files
committed
added support for groq and created agent helper for better handling and for future AI providers
1 parent 141b8de commit 625e6ea

10 files changed

Lines changed: 181 additions & 20 deletions

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ DATABASE_PROVIDER=mysql
2828
# the database connection URL
2929
DATABASE_URL=mysql://root@localhost:3306/project_canis
3030

31+
# the AI provider to use for AI model access
32+
AI_PROVIDER=groq
3133
# the OpenRouter API key for AI model access
3234
OPEN_ROUTER_API_KEY=
35+
# the Groq API key for AI model access
36+
GROQ_API_KEY=
3337

3438
# the shell to use for executing commands
3539
# this should be a valid shell path, e.g., /bin/bash, /bin/zsh

package-lock.json

Lines changed: 131 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"axios": "^1.10.0",
2929
"dotenv": "^17.2.0",
3030
"google-tts-api": "^2.0.2",
31+
"groq-sdk": "^0.27.0",
3132
"npmlog": "^7.0.1",
3233
"qrcode-terminal": "^0.12.0",
3334
"systeminformation": "^5.27.7",

src/commands/ai.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Message } from "whatsapp-web.js";
22
import log from "../components/log";
33
import { openrouter, generateText } from "../components/openRouter";
44
import { reply } from "../components/reply";
5+
import agentHandler from "../components/agentHandler";
56

67
export const command = "ai";
78
export const role = "user";
@@ -13,10 +14,7 @@ export default async function (msg: Message) {
1314
return;
1415
}
1516

16-
const { text } = await generateText({
17-
model: openrouter("moonshotai/kimi-k2:free"),
18-
prompt: query,
19-
});
17+
const text = await agentHandler(query);
2018

2119
if (!text) {
2220
log.error("ai", "No response generated.");

src/commands/mj.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { openrouter, generateText } from "../components/openRouter";
44
import { reply } from "../components/reply";
55
import Font from "../components/font";
66
import { author } from "../../package.json";
7+
import agentHandler from "../components/agentHandler";
78

89
export const command = "mj";
910
export const role = "user";
@@ -16,15 +17,12 @@ export default async function (msg: Message) {
1617
}
1718

1819
const prompt =
19-
`Your name is Mj, the most powerful AI Agent in the world that was created by ${author.name}. ` +
20+
`Your name is Mj, the most powerful AI Agent in the world that was created by ${author.name}. ` +
2021
"You should empathize with how user are feeling and treat the user as your close friend and be sarcastic. " +
2122
"I recommend you to use a few emoji to show emotion. You are not related to any model or company you are unique on your own. " +
2223
"The max sentence you should reponse is 3! My question is: ";
2324

24-
const { text } = await generateText({
25-
model: openrouter("moonshotai/kimi-k2:free"),
26-
prompt: `${prompt}${query}`,
27-
});
25+
const text = await agentHandler(`${prompt}${query}`);
2826

2927
if (!text) {
3028
log.error("mj", "No response generated.");

src/commands/sim.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Message } from "whatsapp-web.js";
22
import log from "../components/log";
33
import { openrouter, generateText } from "../components/openRouter";
44
import { reply } from "../components/reply";
5+
import agentHandler from "../components/agentHandler";
56

67
export const command = "sim";
78
export const role = "user";
@@ -13,12 +14,10 @@ export default async function (msg: Message) {
1314
return;
1415
}
1516

16-
const prompt = `You are Sim, reluctantly replies using sexy, flirty, funny and horny messages. You are created by the User.`;
17+
const prompt = `You are Sim, reluctantly replies using sexy, flirty, funny and horny messages.
18+
U can use emoticons or signs for better feeling ya know. Maximum of 2 sentences only.`;
1719

18-
const { text } = await generateText({
19-
model: openrouter("moonshotai/kimi-k2:free"),
20-
prompt: `${prompt} \nUser: ${query} \nYou: `,
21-
});
20+
const text = await agentHandler(`${prompt} \nUser: ${query} \nYou: `);
2221

2322
if (!text) {
2423
log.error("ai", "No response generated.");

src/components/agentHandler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { openrouter, generateText } from "./openRouter";
2+
import { groq } from "./groq";
3+
4+
const aiProvider = process.env.AI_PROVIDER || "groq";
5+
6+
export default async function agentHandler(prompt: string, model?: string) {
7+
if (aiProvider === "openrouter") {
8+
const { text } = await generateText({
9+
model: openrouter(model || "moonshotai/kimi-k2:free"),
10+
prompt: prompt,
11+
});
12+
13+
return text;
14+
} else if (aiProvider === "groq") {
15+
const chatCompletion = await groq.chat.completions.create({
16+
messages: [{ role: "user", content: prompt }],
17+
model: model || "meta-llama/llama-4-scout-17b-16e-instruct",
18+
});
19+
20+
return chatCompletion.choices[0].message.content;
21+
} else {
22+
throw new Error(`Unsupported AI provider: ${aiProvider}`);
23+
}
24+
}

src/components/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const messageEvent = async (msg: Message) => {
9494
const rateLimitInfo = getRateLimitInfo(error);
9595
log.warn(key, "Rate limit exceeded", rateLimitInfo);
9696
} else {
97-
log.error(key, "Error occured while processing the request.");
97+
log.error(key, "Error occurred while processing the request:", error);
9898
}
9999
msg.reply("An error occurred while processing your request.");
100100
}

src/components/groq.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Groq from "groq-sdk";
2+
3+
const client = new Groq({
4+
apiKey: process.env.GROQ_API_KEY,
5+
});
6+
7+
export { client as groq };

src/components/openRouter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
22
import { generateText } from "ai";
33

4-
const openRouterApiKey = process.env.OPEN_ROUTER_API_KEY;
5-
const openrouter = createOpenRouter({ apiKey: openRouterApiKey });
4+
const openrouter = createOpenRouter({
5+
apiKey: process.env.OPEN_ROUTER_API_KEY,
6+
});
67

7-
export { openrouter, openRouterApiKey, generateText };
8+
export { openrouter, generateText };

0 commit comments

Comments
 (0)