Skip to content

Commit

Permalink
added model selection
Browse files Browse the repository at this point in the history
  • Loading branch information
mjfelis committed Nov 11, 2024
1 parent 57966ed commit 1855e4b
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class OllamaMsg implements Serializable {

private UUID uuid;
private String content;
private String model;

public UUID getUuid() {
return uuid;
}
Expand All @@ -19,5 +21,11 @@ public String getContent() {
public void setContent(String content) {
this.content = content;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void receiveMessage(OllamaMsg msg) throws Exception {
m.setRole(OllamaChatMessageRole.USER);
messages.add(m);

String response = service.getChatResponse(messages);
String response = service.getChatResponse(messages, msg.getModel());
service.historize(msg.getUuid(), LlamaRole.USER, msg.getContent());
service.historize(msg.getUuid(), LlamaRole.ASSISTANT, response);
TextMessage tm = TextMessage.build(msg.getUuid(), null, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class Session implements Serializable {
@Column(columnDefinition="text")
private String avatarName;

@Column(columnDefinition="text")
private String model;



@Column(columnDefinition="text")
private String address;

Expand Down Expand Up @@ -152,6 +157,14 @@ public void setAddress(String address) {
this.address = address;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,17 @@ public List<OllamaChatMessage> getMessagesFromHistory(UUID connectionId) {
return messages;
}

public String getChatResponse(List<OllamaChatMessage> messages) throws OllamaBaseException, IOException, InterruptedException {
public String getChatResponse(List<OllamaChatMessage> messages, String model) throws OllamaBaseException, IOException, InterruptedException {

this.checkModels();
this.checkModels(model);

OllamaAPI ollamaAPI = new OllamaAPI(serviceUrl);
ollamaAPI.setRequestTimeoutSeconds(timeout);

OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance("llama3.2");
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(model);
OllamaChatRequest requestModel = builder.withMessages(messages).build();
logger.info("");
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);

//"llama3.2"
return chatResult.getResponse();
}

Expand All @@ -124,15 +123,15 @@ public void historize(UUID connectionId, LlamaRole role, String data) {
em.persist(h);
}

private void checkModels() {
private void checkModels(String model) {
OllamaAPI ollamaAPI = new OllamaAPI(serviceUrl);
ollamaAPI.setRequestTimeoutSeconds(300);
boolean modelLoaded = false;
List<Model> models;
try {
models = ollamaAPI.listModels();
for (Model m: models) {
if (m.getName().startsWith("llama3.2")) {
if (m.getName().startsWith(model)) {
modelLoaded = true;
}
logger.info(m.getName());
Expand All @@ -145,7 +144,7 @@ private void checkModels() {
if (!modelLoaded) {
logger.info("loading model...");
try {
ollamaAPI.pullModel("llama3.2");
ollamaAPI.pullModel(model);
} catch (Exception e) {
logger.error("", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,22 @@ public class Service {
@ConfigProperty(name = "io.twentysixty.demos.auth.vision.redirdomain.d")
Optional<String> dRedirDomain;


@ConfigProperty(name = "io.twentysixty.ollama.hologram.chatbot.ollamaserver.models")
String modelsString;

private static String[] models = null;
private static String defaultModel = null;


private static String CMD_ROOT_MENU_AUTHENTICATE = "/auth";
private static String CMD_ROOT_MENU_NO_CRED = "/nocred";
private static String CMD_ROOT_MENU_OPTION1 = "/option1";
private static String CMD_ROOT_MENU_LOGOUT = "/logout";
private static String CMD_ROOT_MENU_TO = "/to";

private static String CMD_ROOT_MENU_SET_MODEL = "/set";




Expand Down Expand Up @@ -170,6 +180,24 @@ public class Service {
private static Object lockObj = new Object();


private String[] getModels() {
synchronized(lockObj) {
if (models == null) {
models = modelsString.split(",");

}
if ((models == null) || (models.length == 0)) {
models = new String[1];
models[0] = "llama3.2:3b";
}
defaultModel = models[0];
}


return models;
}



public void newConnection(UUID connectionId) throws Exception {
UUID threadId = UUID.randomUUID();
Expand Down Expand Up @@ -226,6 +254,8 @@ private Session getSession(UUID connectionId) {
if (session == null) {
session = new Session();
session.setConnectionId(connectionId);
this.getModels();
session.setModel(defaultModel);
em.persist(session);

}
Expand Down Expand Up @@ -338,7 +368,7 @@ public void userInput(BaseMessage message) throws Exception {


String content = null;

boolean contextual = false;
MediaMessage mm = null;

if (message instanceof TextMessage) {
Expand All @@ -350,7 +380,7 @@ public void userInput(BaseMessage message) throws Exception {

ContextualMenuSelect menuSelect = (ContextualMenuSelect) message;
content = menuSelect.getSelectionId();

contextual = true;
} else if ((message instanceof MenuSelectMessage)) {

MenuSelectMessage menuSelect = (MenuSelectMessage) message;
Expand Down Expand Up @@ -471,13 +501,24 @@ public void userInput(BaseMessage message) throws Exception {
}
mtProducer.sendMessage(TextMessage.build(message.getConnectionId(), message.getThreadId() , this.getMessage("UNAUTHENTICATED")));

} else if (contextual && (content.startsWith(CMD_ROOT_MENU_SET_MODEL.toString()))) {

content = content.replaceAll(CMD_ROOT_MENU_SET_MODEL.toString(), "").strip();

if (session != null) {
session.setModel(content);
session = em.merge(session);
}
mtProducer.sendMessage(TextMessage.build(message.getConnectionId(), message.getThreadId() , this.getMessage("SET_MODEL").replaceAll("MODEL", content)));

}

else if ((session != null) && (session.getAuthTs() != null)) {

OllamaMsg msg = new OllamaMsg();
msg.setContent(content);
msg.setUuid(session.getConnectionId());
msg.setModel(session.getModel());

ollamaProducer.sendMessage(msg);

Expand Down Expand Up @@ -572,7 +613,13 @@ public BaseMessage getRootMenu(UUID connectionId, Session session) {

}

options.add(ContextualMenuItem.build(CMD_ROOT_MENU_OPTION1, ROOT_MENU_OPTION1, null));
//options.add(ContextualMenuItem.build(CMD_ROOT_MENU_OPTION1, ROOT_MENU_OPTION1, null));

for (String s: this.getModels()) {
options.add(ContextualMenuItem.build(CMD_ROOT_MENU_SET_MODEL + " " + s, this.getMessage("CMD_ROOT_MENU_SET_MODEL_TEXT").replaceAll("MODEL", session.getModel()), null));

}

options.add(ContextualMenuItem.build(CMD_ROOT_MENU_LOGOUT, this.getMessage("ROOT_MENU_LOGOUT"), null));

}
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/META-INF/resources/Messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ ROOT_MENU_AUTHENTICATE=Authenticate
ROOT_MENU_NO_CRED=Get an Identity Card
ROOT_MENU_LOGOUT=Logout
ROOT_MENU_DEFAULT_DESCRIPTION=You are not authenticated
ROOT_MENU_AUTHENTICATED_DESCRIPTION=Welcome NAME!
ROOT_MENU_AUTHENTICATED_DESCRIPTION=Welcome NAME! Selected model: MODEL
ERROR=Ask me something.
FACE_VERIFICATION_HEADER=Face Verification
FACE_VERIFICATION_DESC=Start Face Verification to Authenticate yourself
UNAUTHENTICATED=You are now unauthenticated.
SUCCESS=Transaction successful. TX
WAIT=MESSAGE: Service unavailable, or to many requests to this address. Please wait.
USAGE=Ask me something.
ERROR_NOT_AUTHENTICATED=You need to prove you're human to use this service. Check the contextual menu and authenticate yourself.
ERROR_NOT_AUTHENTICATED=You need to prove you're human to use this service. Check the contextual menu and authenticate yourself.
CMD_ROOT_MENU_SET_MODEL_TEXT=Switch to MODEL model
SET_MODEL=Model set to MODEL.
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/resources/Messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ SUCCESS=Transaction successful. TX
WAIT=MESSAGE: Service unavailable, or to many requests to this address. Please wait.
USAGE=Pregúntame algo.
ERROR_NOT_AUTHENTICATED=Debes demostrar eres hunamo para usar este servicio. Accede al menú contextual para ver las opciones disponibles.
CMD_ROOT_MENU_SET_MODEL_TEXT=Usar el modelo MODEL
SET_MODEL=Modelo seleccionado: MODEL.
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/resources/Messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ UNAUTHENTICATED=Vous êtes déconnecté.
SUCCESS=Transaction OK. TX
WAIT=MESSAGE: Service indisponible, ou trop d'envois à la même adresse. Reessayer avec une autre adresse ou dans un moment.
USAGE=Pose moi une question.
ERROR_NOT_AUTHENTICATED=Vous devez prouver que vous êtes humain pour utiliser ce service. Veuillez lancer l'autentication depuis le menu contextuel.
ERROR_NOT_AUTHENTICATED=Vous devez prouver que vous êtes humain pour utiliser ce service. Veuillez lancer l'autentication depuis le menu contextuel.
CMD_ROOT_MENU_SET_MODEL_TEXT=Utiliser le modèle MODEL
SET_MODEL=Modèle selectionné: MODEL.
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ io.twentysixty.demos.auth.messages.nocred=If you do not have an AvatarID (requir
io.twentysixty.demos.auth.messages.auth_success=You're actually human.
%dev.io.twentysixty.demos.auth.vision.redirdomain=p2604.ovpndev.2060.io
%dev.io.twentysixty.demos.auth.vision.redirdomain.q=p2603.ovpndev.2060.io
%dev.io.twentysixty.demos.auth.vision.redirdomain.d=p2604.ovpndev.2060.io
Expand Down Expand Up @@ -61,6 +62,10 @@ io.twentysixty.ollama.hologram.chatbot.ollamaserver.url=http://localhost:11434/
io.twentysixty.ollama.hologram.chatbot.ollamaserver.maxhistorysize=100
io.twentysixty.ollama.hologram.chatbot.ollamaserver.timeoutseconds=60
io.twentysixty.ollama.hologram.chatbot.ollamaserver.models=llama3.2:3b,llama3.1:8b,gemma2:9b,gemma2:27b,qwen2.5:7b,phi3.5
quarkus.http.host=0.0.0.0
quarkus.http.port=2903
Expand Down
6 changes: 4 additions & 2 deletions target/classes/META-INF/resources/Messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ ROOT_MENU_AUTHENTICATE=Authenticate
ROOT_MENU_NO_CRED=Get an Identity Card
ROOT_MENU_LOGOUT=Logout
ROOT_MENU_DEFAULT_DESCRIPTION=You are not authenticated
ROOT_MENU_AUTHENTICATED_DESCRIPTION=Welcome NAME!
ROOT_MENU_AUTHENTICATED_DESCRIPTION=Welcome NAME! Selected model: MODEL
ERROR=Ask me something.
FACE_VERIFICATION_HEADER=Face Verification
FACE_VERIFICATION_DESC=Start Face Verification to Authenticate yourself
UNAUTHENTICATED=You are now unauthenticated.
SUCCESS=Transaction successful. TX
WAIT=MESSAGE: Service unavailable, or to many requests to this address. Please wait.
USAGE=Ask me something.
ERROR_NOT_AUTHENTICATED=You need to prove you're human to use this service. Check the contextual menu and authenticate yourself.
ERROR_NOT_AUTHENTICATED=You need to prove you're human to use this service. Check the contextual menu and authenticate yourself.
CMD_ROOT_MENU_SET_MODEL_TEXT=Switch to MODEL model
SET_MODEL=Model set to MODEL.
2 changes: 2 additions & 0 deletions target/classes/META-INF/resources/Messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ SUCCESS=Transaction successful. TX
WAIT=MESSAGE: Service unavailable, or to many requests to this address. Please wait.
USAGE=Pregúntame algo.
ERROR_NOT_AUTHENTICATED=Debes demostrar eres hunamo para usar este servicio. Accede al menú contextual para ver las opciones disponibles.
CMD_ROOT_MENU_SET_MODEL_TEXT=Usar el modelo MODEL
SET_MODEL=Modelo seleccionado: MODEL.
4 changes: 3 additions & 1 deletion target/classes/META-INF/resources/Messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ UNAUTHENTICATED=Vous êtes déconnecté.
SUCCESS=Transaction OK. TX
WAIT=MESSAGE: Service indisponible, ou trop d'envois à la même adresse. Reessayer avec une autre adresse ou dans un moment.
USAGE=Pose moi une question.
ERROR_NOT_AUTHENTICATED=Vous devez prouver que vous êtes humain pour utiliser ce service. Veuillez lancer l'autentication depuis le menu contextuel.
ERROR_NOT_AUTHENTICATED=Vous devez prouver que vous êtes humain pour utiliser ce service. Veuillez lancer l'autentication depuis le menu contextuel.
CMD_ROOT_MENU_SET_MODEL_TEXT=Utiliser le modèle MODEL
SET_MODEL=Modèle selectionné: MODEL.
5 changes: 5 additions & 0 deletions target/classes/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ io.twentysixty.demos.auth.messages.nocred=If you do not have an AvatarID (requir
io.twentysixty.demos.auth.messages.auth_success=You're actually human.
%dev.io.twentysixty.demos.auth.vision.redirdomain=p2604.ovpndev.2060.io
%dev.io.twentysixty.demos.auth.vision.redirdomain.q=p2603.ovpndev.2060.io
%dev.io.twentysixty.demos.auth.vision.redirdomain.d=p2604.ovpndev.2060.io
Expand Down Expand Up @@ -61,6 +62,10 @@ io.twentysixty.ollama.hologram.chatbot.ollamaserver.url=http://localhost:11434/
io.twentysixty.ollama.hologram.chatbot.ollamaserver.maxhistorysize=100
io.twentysixty.ollama.hologram.chatbot.ollamaserver.timeoutseconds=60
io.twentysixty.ollama.hologram.chatbot.ollamaserver.models=llama3.2:3b,llama3.1:8b,gemma2:9b,gemma2:27b,qwen2.5:7b,phi3.5
quarkus.http.host=0.0.0.0
quarkus.http.port=2903
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 1855e4b

Please sign in to comment.