Skip to content

Commit

Permalink
Merge pull request #80 from devoxx/issue-78
Browse files Browse the repository at this point in the history
Feat #78: Set chat memory size in Settings page
  • Loading branch information
stephanj authored May 27, 2024
2 parents 4febe75 + acb7d2a commit bb0b9cb
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.devoxx.genie"
version = "0.1.13"
version = "0.1.14"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/devoxx/genie/model/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private Constant() {
public static final Integer MAX_OUTPUT_TOKENS = 2500;
public static final Integer MAX_RETRIES = 3;
public static final Integer TIMEOUT = 60;
public static final Integer MAX_MEMORY = 6;
public static final Integer MAX_MEMORY = 10;

public static final Boolean STREAM_MODE = false;

Expand Down
41 changes: 34 additions & 7 deletions src/main/java/com/devoxx/genie/service/ChatMemoryService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.devoxx.genie.service;

import com.devoxx.genie.ui.listener.ChatMemorySizeListener;
import com.devoxx.genie.ui.topic.AppTopics;
import com.intellij.openapi.application.ApplicationManager;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
Expand All @@ -8,14 +10,26 @@

import java.util.List;

public class ChatMemoryService {
public class ChatMemoryService implements ChatMemorySizeListener {

private final MessageWindowChatMemory chatMemory =
MessageWindowChatMemory.builder()
.id("devoxxgenie")
.chatMemoryStore(new InMemoryChatMemoryStore())
.maxMessages(10) // TODO Make this configurable in the Settings page
.build();
private final InMemoryChatMemoryStore inMemoryChatMemoryStore = new InMemoryChatMemoryStore();

private MessageWindowChatMemory chatMemory;

/**
* Initialize the chat memory service triggered by PostStartupActivity
* @link PostStartupActivity
*/
public void init() {
createChatMemory(SettingsStateService.getInstance().getChatMemorySize());
createChangeListener();
}

private void createChangeListener() {
ApplicationManager.getApplication().getMessageBus()
.connect()
.subscribe(AppTopics.CHAT_MEMORY_SIZE_TOPIC, this);
}

@NotNull
public static ChatMemoryService getInstance() {
Expand All @@ -37,4 +51,17 @@ public List<ChatMessage> messages() {
public boolean isEmpty() {
return chatMemory.messages().isEmpty();
}

@Override
public void onChatMemorySizeChanged(int chatMemorySize) {
createChatMemory(chatMemorySize);
}

private void createChatMemory(int chatMemorySize) {
chatMemory = MessageWindowChatMemory.builder()
.id("devoxxgenie")
.chatMemoryStore(inMemoryChatMemoryStore)
.maxMessages(chatMemorySize)
.build();
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/devoxx/genie/service/PostStartupActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.devoxx.genie.service;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.ProjectActivity;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PostStartupActivity implements ProjectActivity {

@Nullable
@Override
public Object execute(@NotNull Project project, @NotNull Continuation<? super Unit> continuation) {
ChatMemoryService.getInstance().init();
return continuation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static SettingsStateService getInstance() {

private Integer timeout = Constant.TIMEOUT;
private Integer maxRetries = Constant.MAX_RETRIES;
private Integer maxMemory = Constant.MAX_MEMORY;
private Integer chatMemorySize = Constant.MAX_MEMORY;

// Was unable to make it work with Integer for some unknown reason
private String maxOutputTokens = Constant.MAX_OUTPUT_TOKENS.toString();
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/devoxx/genie/ui/DevoxxGenieSettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class DevoxxGenieSettingsManager implements Configurable {

private JFormattedTextField timeoutField;
private JFormattedTextField retryField;
private JFormattedTextField chatMemorySizeField;

private JCheckBox streamModeCheckBox;

Expand Down Expand Up @@ -103,6 +104,7 @@ public JComponent createComponent() {

setTitle("LLM Parameters", settingsPanel, gbc);

chatMemorySizeField = addFormattedFieldWithLabel(settingsPanel, gbc, "Chat memory size:", settings.getTemperature());
temperatureField = addFormattedFieldWithLabel(settingsPanel, gbc, "Temperature:", settings.getTemperature());
topPField = addFormattedFieldWithLabel(settingsPanel, gbc, "Top-P:", settings.getTopP());
maxOutputTokensField = addTextFieldWithLabel(settingsPanel, gbc, "Maximum output tokens :", settings.getMaxOutputTokens());
Expand Down Expand Up @@ -348,6 +350,7 @@ public boolean isModified() {
isModified |= isFieldModified(timeoutField, settings.getTimeout());
isModified |= isFieldModified(maxOutputTokensField, settings.getMaxOutputTokens());
isModified |= isFieldModified(retryField, settings.getMaxRetries());
isModified |= isFieldModified(chatMemorySizeField, settings.getChatMemorySize());
isModified |= isFieldModified(testPromptField, settings.getTestPrompt());
isModified |= isFieldModified(explainPromptField, settings.getExplainPrompt());
isModified |= isFieldModified(reviewPromptField, settings.getReviewPrompt());
Expand Down Expand Up @@ -383,6 +386,7 @@ public void apply() {
notifySettingsChanged();
}

boolean chatMemoryModified = false;
updateSettingIfModified(ollamaUrlField, settings.getOllamaModelUrl(), settings::setOllamaModelUrl);
updateSettingIfModified(lmstudioUrlField, settings.getLmstudioModelUrl(), settings::setLmstudioModelUrl);
updateSettingIfModified(gpt4allUrlField, settings.getGpt4allModelUrl(), settings::setGpt4allModelUrl);
Expand All @@ -391,6 +395,7 @@ public void apply() {
updateSettingIfModified(topPField, doubleConverter.toString(settings.getTopP()), value -> settings.setTopP(doubleConverter.fromString(value)));
updateSettingIfModified(timeoutField, settings.getTimeout(), value -> settings.setTimeout(safeCastToInteger(value)));
updateSettingIfModified(retryField, settings.getMaxRetries(), value -> settings.setMaxRetries(safeCastToInteger(value)));
chatMemoryModified = updateSettingIfModified(chatMemorySizeField, settings.getChatMemorySize(), value -> settings.setChatMemorySize(safeCastToInteger(value)));
updateSettingIfModified(maxOutputTokensField, settings.getMaxOutputTokens(), settings::setMaxOutputTokens);
updateSettingIfModified(testPromptField, settings.getTestPrompt(), settings::setTestPrompt);
updateSettingIfModified(explainPromptField, settings.getExplainPrompt(), settings::setExplainPrompt);
Expand All @@ -407,6 +412,10 @@ public void apply() {
updateSettingIfModified(astParentClassCheckBox, settings.getAstParentClass(), value -> settings.setAstParentClass(Boolean.parseBoolean(value)));
updateSettingIfModified(astReferenceClassesCheckBox, settings.getAstClassReference(), value -> settings.setAstClassReference(Boolean.parseBoolean(value)));
updateSettingIfModified(astReferenceFieldCheckBox, settings.getAstFieldReference(), value -> settings.setAstFieldReference(Boolean.parseBoolean(value)));

if (chatMemoryModified) {
notifyChatMemorySizeChangeListeners();
}
}

/**
Expand All @@ -427,6 +436,15 @@ public boolean updateSettingIfModified(JComponent field,
return false;
}

/**
* Notify the chat memory size change listeners
*/
public void notifyChatMemorySizeChangeListeners() {
ApplicationManager.getApplication().getMessageBus()
.syncPublisher(AppTopics.CHAT_MEMORY_SIZE_TOPIC)
.onChatMemorySizeChanged(SettingsStateService.getInstance().getChatMemorySize());
}

/**
* Extract the string value from the field
*
Expand Down Expand Up @@ -468,6 +486,7 @@ public void reset() {
setValue(topPField, settingsState.getTopP());
setValue(timeoutField, settingsState.getTimeout());
setValue(retryField, settingsState.getMaxRetries());
setValue(chatMemorySizeField, settingsState.getChatMemorySize());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devoxx.genie.ui.listener;

public interface ChatMemorySizeListener {
void onChatMemorySizeChanged(int chatMemorySize);
}
3 changes: 3 additions & 0 deletions src/main/java/com/devoxx/genie/ui/topic/AppTopics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.devoxx.genie.ui.topic;

import com.devoxx.genie.ui.listener.ChatMemorySizeListener;
import com.devoxx.genie.ui.listener.ChatMessageManagementService;
import com.devoxx.genie.ui.listener.SettingsChangeListener;
import com.intellij.util.messages.Topic;
Expand All @@ -12,4 +13,6 @@ public class AppTopics {
public static final Topic<ChatMessageManagementService> CHAT_MESSAGES_CHANGED_TOPIC =
Topic.create("chatChanged", ChatMessageManagementService.class);

public static final Topic<ChatMemorySizeListener> CHAT_MEMORY_SIZE_TOPIC =
new Topic<>("CHAT_MEMORY_SIZE_TOPIC", ChatMemorySizeListener.class);
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
]]></description>

<change-notes><![CDATA[
<h2>v0.1.14</h2>
<UL>
<LI>Feat #78: Set chat memory size in Settings page.</LI>
</UL>
<h2>v0.1.13</h2>
<UL>
<LI>Feat #71: Auto Abstract Syntax Tree (AST) context</span>: Automatically includes information about the superclass and class fields in the context for better code analysis and understanding.</LI>
Expand Down Expand Up @@ -189,6 +193,7 @@

<extensions defaultExtensionNs="com.intellij">
<notificationGroup id="com.devoxx.genie.notifications" displayType="BALLOON" />
<postStartupActivity implementation="com.devoxx.genie.service.PostStartupActivity"/>
</extensions>

<actions>
Expand Down

0 comments on commit bb0b9cb

Please sign in to comment.