Skip to content

Commit 412fa90

Browse files
committed
actions: add Sentiment command
Signed-off-by: TRACTION <[email protected]>
1 parent 16cf016 commit 412fa90

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

src/actions/Sentiment.ts

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*!
2+
* @author TRACTION (iamtraction)
3+
* @copyright 2024
4+
*/
5+
import { ApplicationCommandType, MessageContextMenuCommandInteraction } from "discord.js";
6+
import { Client, Command } from "@bastion/tesseract";
7+
import Anthropic from "@anthropic-ai/sdk";
8+
import OpenAI from "openai";
9+
import { GoogleGenerativeAI } from "@google/generative-ai";
10+
11+
import Settings from "../utils/settings.js";
12+
13+
class SentimentCommand extends Command {
14+
constructor() {
15+
super({
16+
type: ApplicationCommandType.Message,
17+
name: "Sentiment",
18+
description: "",
19+
owner: true,
20+
});
21+
}
22+
23+
public async exec(interaction: MessageContextMenuCommandInteraction<"cached">): Promise<unknown> {
24+
if (!interaction.targetMessage.content) return;
25+
26+
const systemPrompt = "You are a helpful and informative AI assistant. You will analyze the given text and provide an assessment of its sentiment. Consider the overall tone, specific words and phrases used, and any contextual clues. Your response should be short, concise, objective, and informative.";
27+
const sentimentPrompt = `Please analyze the following text and provide a assessment of its sentiment: ${ interaction.targetMessage.content }`;
28+
29+
// use ChatGPT if OpenAI API key is present
30+
if (((interaction.client as Client).settings as Settings).get("openai").apiKey) {
31+
const openai = new OpenAI({
32+
apiKey: ((interaction.client as Client).settings as Settings).get("openai").apiKey,
33+
});
34+
35+
const response = await openai.chat.completions.create({
36+
model: ((interaction.client as Client).settings as Settings).get("openai").model,
37+
messages: [
38+
{
39+
role: "system",
40+
content: systemPrompt,
41+
},
42+
{
43+
role: "user",
44+
content: sentimentPrompt,
45+
},
46+
],
47+
max_tokens: ((interaction.client as Client).settings as Settings).get("openai").maxTokens,
48+
user: interaction.member.id,
49+
});
50+
51+
return await interaction.reply({
52+
content: response.choices[0].message.content,
53+
ephemeral: true,
54+
});
55+
}
56+
57+
// use Gemini if Gemini API key is present
58+
if (((interaction.client as Client).settings as Settings).get("gemini").apiKey) {
59+
const gemini = new GoogleGenerativeAI(((interaction.client as Client).settings as Settings).get("gemini").apiKey);
60+
const geminiModel = gemini.getGenerativeModel({
61+
model: ((interaction.client as Client).settings as Settings).get("gemini").model,
62+
systemInstruction: systemPrompt,
63+
generationConfig: {
64+
maxOutputTokens: ((interaction.client as Client).settings as Settings).get("gemini").maxOutputTokens,
65+
},
66+
});
67+
68+
const result = await geminiModel.generateContent(sentimentPrompt);
69+
70+
return await interaction.reply({
71+
content: result.response.text(),
72+
ephemeral: true,
73+
});
74+
}
75+
76+
// use Claude if Anthropic API key is present
77+
if (((interaction.client as Client).settings as Settings).get("anthropic").apiKey) {
78+
const anthropic = new Anthropic({
79+
apiKey: ((interaction.client as Client).settings as Settings).get("anthropic").apiKey,
80+
});
81+
82+
const result = await anthropic.messages.create({
83+
model: ((interaction.client as Client).settings as Settings).get("anthropic").model,
84+
system: systemPrompt,
85+
max_tokens: ((interaction.client as Client).settings as Settings).get("anthropic").maxTokens,
86+
messages: [
87+
{
88+
role: "user",
89+
content: [
90+
{
91+
type: "text",
92+
text: sentimentPrompt,
93+
},
94+
],
95+
},
96+
],
97+
metadata: {
98+
user_id: interaction.member.id,
99+
},
100+
});
101+
102+
return await interaction.reply({
103+
content: result.content[0].type === "text" && result.content[0].text,
104+
ephemeral: true,
105+
});
106+
}
107+
}
108+
}
109+
110+
export { SentimentCommand as Command };

0 commit comments

Comments
 (0)