Skip to content

Commit

Permalink
add support for XEP-0301: In-Band Real Time Text
Browse files Browse the repository at this point in the history
  • Loading branch information
deleolajide committed Nov 22, 2023
1 parent 2462ae4 commit 412f31d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This plugin is a wrapper to llama.cpp server binary. It uses the HTTP API to cre

## Overview
<img src="https://igniterealtime.github.io/openfire-llama-plugin/llama-chat.png" />
https://igniterealtime.github.io/openfire-llama-plugin/llama-chat.webm

## Known Issues

Expand Down
Binary file added docs/llama-chat.webm
Binary file not shown.
4 changes: 2 additions & 2 deletions src/java/org/ifsoft/llama/openfire/LLaMA.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ private void startLLaMAProcess(String filename) {

if (llamaExePath != null && llamaEnabled && !isNull(modelUrl)) {
createLLaMAUser();
loginLLaMAUser(false);

try {
final String llamaHost = JiveGlobals.getProperty("llama.host", getIpAddress());
Expand All @@ -292,8 +293,7 @@ private void startLLaMAProcess(String filename) {
llamaThread = Spawn.startProcess(llamaExePath + " " + params, new File(llamaHomePath), this);

Thread.sleep(1000);

loginLLaMAUser(false);

Log.info("LLaMA enabled " + llamaExePath + " " + params);
}
catch (Exception e)
Expand Down
34 changes: 30 additions & 4 deletions src/java/org/ifsoft/llama/openfire/LLaMAConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public LLaMAConnection(String username, String remoteUrl) {
router = new SessionPacketRouter( session );
route("<presence />");

Log.error("xmpp session created for " + username);
Log.info("xmpp session created for " + username);
} catch (Exception e) {
Log.error("XMPPConnection error", e);
}
Expand All @@ -95,6 +95,7 @@ public void handlePrediction(final String prompt, final JID requestor, final Mes
exec.execute(new Runnable() {
public void run() {
long threadId = Thread.currentThread().getId()% LLaMA.numThreads;

final String alias = JiveGlobals.getProperty("llama.alias", "LLaMA");
/*
"system_prompt": {
Expand All @@ -119,7 +120,7 @@ public void run() {

JSONObject testData = new JSONObject();
testData.put("system_prompt", systemPrompt);
testData.put("prompt", "[INST]" + prompt + "[/INST]");
testData.put("prompt", "<s>[INST]" + prompt + "[/INST]</s>");
testData.put("n_predict", JiveGlobals.getIntProperty("llama.predictions", 256));
testData.put("stream", true);
testData.put("cache_prompt", JiveGlobals.getBooleanProperty("llama.cache.prompt", true));
Expand Down Expand Up @@ -303,7 +304,8 @@ private void getJson(String urlToRead, JSONObject data, JID requestor, Message.T

rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

if (requestor != null) replyState("active", requestor, chatType);
if (requestor != null) replyState("active", requestor, chatType);
long seq = 0;

while ((line = rd.readLine()) != null) {
Log.debug("getJson - stream\n" + line);
Expand All @@ -325,7 +327,12 @@ private void getJson(String urlToRead, JSONObject data, JID requestor, Message.T

} else {
result.append(content);
if (requestor != null) replyState("composing", requestor, chatType);

if (requestor != null) {
replyState("composing", requestor, chatType);
replyRtt(content, requestor, chatType, seq);
seq++;
}
}
} else { // end of text stream
String msg = result.toString();
Expand Down Expand Up @@ -359,6 +366,25 @@ private void replyState(String state, JID requestor, Message.Type chatType) {
XMPPServer.getInstance().getRoutingTable().routePacket(requestor, newMessage, true);
}

private void replyRtt(String msg, JID requestor, Message.Type chatType, long seq) {
Log.debug("replyRtt from LLaMA " + requestor + " " + msg);

Message newMessage = new Message();
newMessage.setFrom(username + "@" + domain + "/" + remoteAddr);
newMessage.setTo(requestor);
newMessage.setType(chatType);

Element rtt = DocumentHelper.createElement(QName.get("rtt", "urn:xmpp:rtt:0"));
rtt.addAttribute("seq", String.valueOf(seq));
if (seq == 0) rtt.addAttribute("event", "new");

Element t = rtt.addElement("t");
t.setText(msg);
newMessage.addExtension(new PacketExtension(rtt));

XMPPServer.getInstance().getRoutingTable().routePacket(requestor, newMessage, true);
}

private void replyChat(String msg, JID requestor, Message.Type chatType) {
Log.debug("replyChat from LLaMA " + requestor + "\n" + msg);

Expand Down

0 comments on commit 412f31d

Please sign in to comment.