Skip to content

Commit

Permalink
Added Info Command, Added Minor Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeyoYT committed Jun 18, 2024
1 parent b34fe24 commit 935cbf3
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main/java/me/ailama/commands/AiCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@
public class AiCommand implements AiLamaSlashCommand {

public SlashCommandData getCommandData() {
return Commands.slash("ai","ask ai (ollama)")
SlashCommandData slashCommandData = Commands.slash("ai", "ask ai (ollama)")
.setIntegrationTypes(IntegrationType.USER_INSTALL)
.setContexts(InteractionContextType.ALL)
.setDefaultPermissions(DefaultMemberPermissions.ENABLED)

.addOption(OptionType.STRING, "ask", "The query you want to ask", true)
.addOption(OptionType.BOOLEAN, "web", "If you want the response based on web search", false)
.addOption(OptionType.STRING, "model", "Example (gemma:2b)", false)
.addOption(OptionType.BOOLEAN, "ephemeral", "If you want the response to be ephemeral", false)
.addOption(OptionType.STRING, "url", "The Url of website for RAG", false)
.addOption(OptionType.BOOLEAN, "reset-session", "If you want reset chat memory", false)

.setNSFW(false);

if(SearXNGManager.getInstance().isSearXNGEnabled()) {
slashCommandData.addOption(OptionType.BOOLEAN, "web", "If you want the response based on web search", false);
}

return slashCommandData;
}

public void handleCommand(SlashCommandInteractionEvent event) {
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/me/ailama/commands/InfoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package me.ailama.commands;

import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.model.ollama.OllamaModel;
import dev.langchain4j.model.ollama.OllamaModelDetails;
import dev.langchain4j.model.ollama.OllamaModels;
import me.ailama.config.Config;
import me.ailama.handler.commandhandler.Automatic1111Manager;
import me.ailama.handler.commandhandler.OllamaManager;
import me.ailama.handler.commandhandler.SearXNGManager;
import me.ailama.handler.interfaces.AiLamaSlashCommand;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;

public class InfoCommand implements AiLamaSlashCommand {
@Override
public SlashCommandData getCommandData() {
return Commands.slash("info","get information related to ollama")
.setIntegrationTypes(IntegrationType.USER_INSTALL)
.setContexts(InteractionContextType.ALL)
.setDefaultPermissions(DefaultMemberPermissions.ENABLED)

.setNSFW(false);
}

@Override
public void handleCommand(SlashCommandInteractionEvent event) {

event.deferReply().setEphemeral(true).queue();

String ollamaUrl = Config.get("OLLAMA_URL");
String ollamaPort = Config.get("OLLAMA_PORT");

String isOllamaServerRunning = OllamaManager.getInstance().checkOllamaServer() ? "Yes" : "No";
String currentModel = OllamaManager.getInstance().getCurrentModel();
String defaultModel = Config.get("OLLAMA_MODEL");

String hasChatMemory = OllamaManager.getInstance().hasMemoryEnabled() ? "Yes" : "No";
int chatMemorySize = OllamaManager.getInstance().getChatMemorySize();

String isWebSearchEnabled = SearXNGManager.getInstance().isSearXNGEnabled() ? "Yes" : "No";
String isImageGenerationEnabled = Automatic1111Manager.getInstance().isAutomatic1111Enabled() ? "Yes" : "No";

/*
* Url: URL
* Port: PORT
*
* Ollama Server Running: Yes/No
* Current Model: MODEL
* Default Model: MODEL
* */

String ollamaInformationBuilder = "Url: " + ollamaUrl + "\n" +
"Port: " + ollamaPort + "\n\n" +
"**Ollama Server Running :-** \n" + isOllamaServerRunning + "\n\n" +
"**Current Model :-**\n" + currentModel + "\n\n" +
"**Default Model :-**\n" + defaultModel;

String chatMemoryBuilder = "Chat Memory :- \n" + hasChatMemory + "\n" +
"Chat Memory Size :- \n" + chatMemorySize;

String dependentCommandsEnabled = "Web Search :- \n" + isWebSearchEnabled + "\n" +
"Image Generation :- \n" + isImageGenerationEnabled;


EmbedBuilder embed = new EmbedBuilder()
.setTitle( event.getJDA().getSelfUser().getEffectiveName() + " Connection & Data Information :- ")
.setDescription("Information related to Ollama, Chat Memory, and Dependent Commands")
.addBlankField(false)
.addField("Ollama Information :-", ollamaInformationBuilder,true)
.addBlankField(false)
.addField("Chat Memory Information :- ", chatMemoryBuilder,true)
.addField("Dependent Commands Enabled :- ", dependentCommandsEnabled,true)
.setColor(0x001440);

event.getHook().sendMessageEmbeds(embed.build()).setEphemeral(true).queue();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public CommandRegister() {
addCommand(new DocumentCommand(), true);
addCommand(new ImageCommand(), Automatic1111Manager.getInstance().isAutomatic1111Enabled());
addCommand(new ModelCommand(), true);
addCommand(new InfoCommand(), true);
}

public AiLamaSlashCommand getCommand(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.jsoup.Jsoup;

import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -604,11 +607,44 @@ public List<String> getModels() {
return models.availableModels().content().stream().map(OllamaModel::getName).toList();
}

public boolean checkOllamaServer() {

String ollamaUrl = Config.get("OLLAMA_URL").replaceFirst("^(http://|https://)", "").replaceAll("/$", "");

try {
InetAddress address = InetAddress.getByName(ollamaUrl);
int port = Integer.parseInt(Config.get("OLLAMA_PORT"));

Socket socket = new Socket(address, port);
boolean state = socket.isConnected();

// Close the socket connection after checking
socket.close();

return state;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}

public String getCurrentModel() {
return model;
}

public boolean hasMemoryEnabled() {
return chatMemoryLimit > 1;
}

public int getChatMemorySize() {
return chatMemoryLimit;
}

public static OllamaManager getInstance() {
if (OllamaManager.ollama == null) {
OllamaManager.ollama = new OllamaManager();
}
return OllamaManager.ollama;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static String fixEngineString(String engines) {
- If the URL is in the forbidden list, it will be removed
*/
public SearXNGResult bestMatch(SearXNG searXNG) {
SearXNGResult bestResult = searXNG.results.get(0);
SearXNGResult bestResult = searXNG.results.getFirst();

double bestMatchScore = bestResult.score;

Expand Down

0 comments on commit 935cbf3

Please sign in to comment.