Skip to content

Commit

Permalink
add support for hosted
Browse files Browse the repository at this point in the history
  • Loading branch information
deleolajide committed Nov 22, 2023
1 parent c864b84 commit 43ae80f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/i18n/llama_i18n.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ config.page.settings.description=Edit LLaMA settings
config.page.test=LLaMA Test
config.page.test.description=Test LLaMA Application
config.page.configuration.host=Bind IP Address/Hostname
config.page.configuration.hosted=Use Hosted LLaMA server
config.page.configuration.hosted.url=Hosted URL
config.page.configuration.port=Port
config.page.configuration.top.k.sampling=Top K Sampling
config.page.configuration.top.p.sampling=Top P Sampling
Expand Down
36 changes: 31 additions & 5 deletions src/java/org/ifsoft/llama/openfire/LLaMA.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void destroyPlugin() {

public void initializePlugin(final PluginManager manager, final File pluginDirectory) {
boolean llamaEnabled = JiveGlobals.getBooleanProperty("llama.enabled", true);
boolean llamaHosted = JiveGlobals.getBooleanProperty("llama.hosted", true);

if (llamaEnabled) {
Log.info("llama enabled");
Expand All @@ -114,7 +115,14 @@ public void initializePlugin(final PluginManager manager, final File pluginDirec
checkNatives(pluginDirectory);
executor = Executors.newCachedThreadPool();
startJSP(pluginDirectory);
setupLLaMA(pluginDirectory);

if (llamaHosted) {
createLLaMAUser();
loginLLaMAUser(true);
llamaConnection.handlePrediction("what is your name?", null, null);
} else {
setupLLaMA(pluginDirectory);
}
self = this;

Log.info("llama initiated");
Expand All @@ -130,6 +138,10 @@ public static String getPort() {
public static String getModelPath() {
return (JiveGlobals.getHomeDirectory() + File.separator + "llama").replace("\\", "/");
}

public static String getHostedUrl() {
return "https://chatgpt.free-solutions.ch";
}

public static String getModelUrl() {
return "https://huggingface.co/TheBloke/Llama-2-7b-Chat-GGUF/blob/main/llama-2-7b-chat.Q5_K_M.gguf";
Expand Down Expand Up @@ -280,10 +292,8 @@ private void startLLaMAProcess(String filename) {
llamaThread = Spawn.startProcess(llamaExePath + " " + params, new File(llamaHomePath), this);

Thread.sleep(1000);

final String llamaUser = JiveGlobals.getProperty("llama.username", "llama");
llamaConnection = new LLaMAConnection(llamaUser, llamaPort);


loginLLaMAUser(false);
Log.info("LLaMA enabled " + llamaExePath + " " + params);
}
catch (Exception e)
Expand All @@ -296,6 +306,22 @@ private void startLLaMAProcess(String filename) {
}
}

private void loginLLaMAUser(boolean hosted) {
Log.info("LLaMA user login");

final String llamaUser = JiveGlobals.getProperty("llama.username", "llama");
String llamaUrl = JiveGlobals.getProperty("llama.hosted.url", getHostedUrl());

if (!hosted) {
final String llamaHost = JiveGlobals.getProperty("llama.host", getIpAddress());
final String llamaPort = JiveGlobals.getProperty("llama.port", LLaMA.self.getPort());

llamaUrl = "http://" + llamaHost + ":" + llamaPort;
}

llamaConnection = new LLaMAConnection(llamaUser, llamaUrl);
}

private void checkNatives(File pluginDirectory) {
try
{
Expand Down
9 changes: 4 additions & 5 deletions src/java/org/ifsoft/llama/openfire/LLaMAConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class LLaMAConnection extends VirtualConnection
private ConnectionConfiguration configuration;
private ConnectionType connectionType;
private String username;
private String port;
private AuthToken authToken = null;
private LocalClientSession session;
private ExecutorService exec = Executors.newFixedThreadPool(LLaMA.numThreads);
Expand All @@ -63,11 +62,11 @@ public class LLaMAConnection extends VirtualConnection
private final String hostname = XMPPServer.getInstance().getServerInfo().getHostname();

public String remoteAddr;

public String remoteUrl;

public LLaMAConnection(String username, String port) {
public LLaMAConnection(String username, String remoteUrl) {
this.username = username;
this.port = port;
this.remoteUrl = remoteUrl;
this.remoteAddr = "llama-" + System.currentTimeMillis();

try {
Expand Down Expand Up @@ -282,7 +281,7 @@ private void getJson(String urlToRead, JSONObject data, JID requestor, Message.T
String password = JiveGlobals.getProperty("llama.password", "llama");
String auth = username + ":" + password;
String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
String uri = "http://" + llamaHost + ":" + port + urlToRead;
String uri = remoteUrl + urlToRead;

Log.info("getJson from LLaMA " + requestor + " " + uri + "\n" + data);

Expand Down
14 changes: 10 additions & 4 deletions src/web/llama-chat.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@
<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager" />
<%
webManager.init(request, response, session, application, out );
String port = JiveGlobals.getProperty("llama.port", "8080");
String host = JiveGlobals.getProperty("llama.host", LLaMA.getIpAddress());
String url = "http://" + host + ":" + port;
boolean llamaHosted = JiveGlobals.getBooleanProperty("llama.hosted", true);
String url = JiveGlobals.getProperty("llama.hosted.url", LLaMA.getHostedUrl());
if (!llamaHosted) {
final String llamaHost = JiveGlobals.getProperty("llama.host", LLaMA.getIpAddress());
final String llamaPort = JiveGlobals.getProperty("llama.port", LLaMA.self.getPort());
url = "http://" + llamaHost + ":" + llamaPort;
}
%>

<html>
Expand Down
24 changes: 22 additions & 2 deletions src/web/llama-settings.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
String alias = request.getParameter("alias");
JiveGlobals.setProperty("llama.alias", alias);
String hosted_url = request.getParameter("hosted_url");
JiveGlobals.setProperty("llama.hosted.url", hosted_url);
String model_path = request.getParameter("model_path");
JiveGlobals.setProperty("llama.model.path", model_path);
Expand All @@ -54,6 +57,9 @@
String cache_prompt = request.getParameter("cache_prompt");
JiveGlobals.setProperty("llama.cache.prompt", (cache_prompt != null && cache_prompt.equals("on")) ? "true": "false");
String hosted = request.getParameter("hosted");
JiveGlobals.setProperty("llama.hosted", (hosted != null && hosted.equals("on")) ? "true": "false");
String enabled = request.getParameter("enabled");
JiveGlobals.setProperty("llama.enabled", (enabled != null && enabled.equals("on")) ? "true": "false");
}
Expand Down Expand Up @@ -83,19 +89,33 @@
<th colspan="2"><fmt:message key="config.page.settings.description"/></th>
</tr>
</thead>
<tbody>
<tbody>
<tr>
<td nowrap colspan="2">
<input type="checkbox" name="enabled"<%= (JiveGlobals.getProperty("llama.enabled", "true").equals("true")) ? " checked" : "" %>>
<fmt:message key="config.page.configuration.enabled" />
</td>
</tr>
<tr>
<td nowrap colspan="2">
<input type="checkbox" name="hosted"<%= (JiveGlobals.getProperty("llama.hosted", "true").equals("true")) ? " checked" : "" %>>
<fmt:message key="config.page.configuration.hosted" />
</td>
</tr>
<tr>
<td nowrap colspan="2">
<input type="checkbox" name="cache_prompt"<%= (JiveGlobals.getProperty("llama.cache.prompt", "true").equals("true")) ? " checked" : "" %>>
<fmt:message key="config.page.configuration.cache.prompt" />
</td>
</tr>
<tr>
<td align="left" width="150">
<fmt:message key="config.page.configuration.hosted.url"/>
</td>
<td><input type="text" size="100" maxlength="256" name="hosted_url" required
value="<%= JiveGlobals.getProperty("llama.hosted.url", plugin.getHostedUrl()) %>">
</td>
</tr>
<tr>
<td align="left" width="150">
<fmt:message key="config.page.configuration.model.path"/>
Expand Down

0 comments on commit 43ae80f

Please sign in to comment.