-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from devoxx/issue-74
Issue 74
- Loading branch information
Showing
22 changed files
with
309 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/devoxx/genie/chatmodel/jan/JanChatModelFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.devoxx.genie.chatmodel.jan; | ||
|
||
import com.devoxx.genie.chatmodel.ChatModelFactory; | ||
import com.devoxx.genie.model.ChatModel; | ||
import com.devoxx.genie.model.jan.Data; | ||
import com.devoxx.genie.service.JanService; | ||
import com.devoxx.genie.ui.SettingsState; | ||
import com.devoxx.genie.ui.util.NotificationUtil; | ||
import com.intellij.openapi.project.ProjectManager; | ||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import dev.langchain4j.model.localai.LocalAiChatModel; | ||
import okhttp3.OkHttpClient; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JanChatModelFactory implements ChatModelFactory { | ||
|
||
// Moved client instance here for the sake of better performance | ||
private final OkHttpClient client = new OkHttpClient(); | ||
|
||
@Override | ||
public ChatLanguageModel createChatModel(@NotNull ChatModel chatModel) { | ||
return LocalAiChatModel.builder() | ||
.baseUrl(SettingsState.getInstance().getJanModelUrl()) | ||
.modelName(chatModel.getModelName()) | ||
.maxRetries(chatModel.getMaxRetries()) | ||
.temperature(chatModel.getTemperature()) | ||
.maxTokens(chatModel.getMaxTokens()) | ||
.timeout(Duration.ofSeconds(chatModel.getTimeout())) | ||
.topP(chatModel.getTopP()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Get the model names from the Jan service. | ||
* @return List of model names | ||
*/ | ||
@Override | ||
public List<String> getModelNames() { | ||
List<String> modelNames = new ArrayList<>(); | ||
try { | ||
List<Data> models = new JanService(client).getModels(); | ||
for (Data model : models) { | ||
modelNames.add(model.getId()); | ||
} | ||
} catch (IOException e) { | ||
NotificationUtil.sendNotification(ProjectManager.getInstance().getDefaultProject(), | ||
"Jan is not running or model not installed."); | ||
} | ||
return modelNames; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.devoxx.genie.model.jan; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class Data { | ||
@JsonProperty("id") | ||
private String id; | ||
|
||
@JsonProperty("object") | ||
private String object; | ||
|
||
@JsonProperty("name") | ||
private String name; | ||
|
||
@JsonProperty("version") | ||
private String version; | ||
|
||
@JsonProperty("description") | ||
private String description; | ||
|
||
@JsonProperty("format") | ||
private String format; | ||
|
||
@JsonProperty("settings") | ||
private Settings settings; | ||
|
||
@JsonProperty("parameters") | ||
private Parameters parameters; | ||
|
||
@JsonProperty("metadata") | ||
private Metadata metadata; | ||
|
||
@JsonProperty("engine") | ||
private String engine; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.devoxx.genie.model.jan; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class Metadata { | ||
@JsonProperty("author") | ||
private String author; | ||
|
||
@JsonProperty("tags") | ||
private List<String> tags; | ||
|
||
@JsonProperty("size") | ||
private long size; | ||
|
||
@JsonProperty("cover") | ||
private String cover; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.devoxx.genie.model.jan; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class Parameters { | ||
@JsonProperty("temperature") | ||
private double temperature; | ||
|
||
@JsonProperty("top_p") | ||
private double topP; | ||
|
||
@JsonProperty("stream") | ||
private boolean stream; | ||
|
||
@JsonProperty("max_tokens") | ||
private int maxTokens; | ||
|
||
@JsonProperty("stop") | ||
private List<String> stop; | ||
|
||
@JsonProperty("frequency_penalty") | ||
private double frequencyPenalty; | ||
|
||
@JsonProperty("presence_penalty") | ||
private double presencePenalty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.devoxx.genie.model.jan; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Setter | ||
@Getter | ||
public class ResponseDTO { | ||
|
||
@JsonProperty("object") | ||
private String object; | ||
|
||
@JsonProperty("data") | ||
private List<Data> data; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.devoxx.genie.model.jan; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class Settings { | ||
@JsonProperty("ctx_len") | ||
private int ctxLen; | ||
|
||
@JsonProperty("prompt_template") | ||
private String promptTemplate; | ||
|
||
@JsonProperty("llama_model_path") | ||
private String llamaModelPath; | ||
|
||
@JsonProperty("ngl") | ||
private int ngl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.devoxx.genie.service; | ||
|
||
import com.devoxx.genie.model.jan.Data; | ||
import com.devoxx.genie.model.jan.ResponseDTO; | ||
import com.devoxx.genie.ui.SettingsState; | ||
import com.google.gson.Gson; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class JanService { | ||
private final OkHttpClient client; | ||
|
||
public JanService(OkHttpClient client) { | ||
this.client = client; | ||
} | ||
|
||
public List<Data> getModels() throws IOException { | ||
String baseUrl = ensureEndsWithSlash(SettingsState.getInstance().getJanModelUrl()); | ||
|
||
Request request = new Request.Builder() | ||
.url(baseUrl + "models") | ||
.build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
if (!response.isSuccessful()) { | ||
throw new UnsuccessfulRequestException("Unexpected code " + response); | ||
} | ||
|
||
assert response.body() != null; | ||
|
||
ResponseDTO responseDTO = new Gson().fromJson(response.body().string(), ResponseDTO.class); | ||
return responseDTO != null && responseDTO.getData() != null ? responseDTO.getData() : List.of(); | ||
} | ||
} | ||
|
||
@Contract(pure = true) | ||
private String ensureEndsWithSlash(@NotNull String url) { | ||
return url.endsWith("/") ? url : url + "/"; | ||
} | ||
|
||
public static class UnsuccessfulRequestException extends IOException { | ||
public UnsuccessfulRequestException(String message) { | ||
super(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.