Skip to content

Commit

Permalink
Merge pull request #385 from devoxx/issue-384
Browse files Browse the repository at this point in the history
Fix #384: Tavily and Google Search fixed
  • Loading branch information
stephanj authored Dec 12, 2024
2 parents 9098fad + 4653ba4 commit 079f62a
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface DevoxxGenieSettingsService {

String getOpenRouterKey();

Boolean getEnableWebSearch();
Boolean getIsWebSearchEnabled();

String getGoogleSearchKey();

Expand Down Expand Up @@ -128,7 +128,7 @@ public interface DevoxxGenieSettingsService {

void setOpenRouterKey(String key);

void setEnableWebSearch(Boolean flag);
void setIsWebSearchEnabled(Boolean flag);

void setGoogleSearchKey(String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void execute(ChatMessageContext chatMessageContext,
isCancelled = false;

if (FIND_COMMAND.equals(chatMessageContext.getCommandName())) {
semanticSearch(chatMessageContext, promptOutputPanel, enableButtons);
semanticSearch(chatMessageContext, promptOutputPanel);
enableButtons.run();
return;
}
Expand Down Expand Up @@ -101,8 +101,7 @@ private void prompt(ChatMessageContext chatMessageContext,
* @param promptOutputPanel the prompt output panel
*/
private static void semanticSearch(ChatMessageContext chatMessageContext,
@NotNull PromptOutputPanel promptOutputPanel,
Runnable enableButtons) {
@NotNull PromptOutputPanel promptOutputPanel) {
try {
SemanticSearchService semanticSearchService = SemanticSearchService.getInstance();
Map<String, SearchResult> searchResults = semanticSearchService.search(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public void execute(ChatMessageContext chatMessageContext,
@NotNull PromptOutputPanel promptOutputPanel,
Runnable enableButtons) {
promptOutputPanel.addUserPrompt(chatMessageContext);
long startTime = System.currentTimeMillis();
WebSearchService.getInstance().searchWeb(chatMessageContext)
.ifPresent(aiMessage -> {
chatMessageContext.setAiMessage(aiMessage);
chatMessageContext.setExecutionTimeMs(System.currentTimeMillis() - startTime);
promptOutputPanel.addChatResponse(chatMessageContext);
enableButtons.run();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.devoxx.genie.service.websearch;

import com.devoxx.genie.model.request.ChatMessageContext;
import com.devoxx.genie.service.DevoxxGenieSettingsService;
import com.devoxx.genie.ui.settings.DevoxxGenieStateService;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.WebSearchContentRetriever;
Expand All @@ -17,11 +17,10 @@

import java.util.Optional;

import static com.devoxx.genie.model.Constant.GOOGLE_SEARCH_ACTION;
import static com.devoxx.genie.model.Constant.TAVILY_SEARCH_ACTION;

public class WebSearchService {

private static final Logger LOG = Logger.getInstance(WebSearchService.class);

public static WebSearchService getInstance() {
return ApplicationManager.getApplication().getService(WebSearchService.class);
}
Expand All @@ -41,7 +40,10 @@ interface SearchWebsite {
* @return the AI message
*/
public @NotNull Optional<AiMessage> searchWeb(@NotNull ChatMessageContext chatMessageContext) {
WebSearchEngine engine = createWebSearchEngine(chatMessageContext.getContext());
LOG.debug("Searching the web for: " + chatMessageContext.getUserPrompt());

WebSearchEngine engine = createWebSearchEngine();

return Optional.ofNullable(engine)
.flatMap(webSearchEngine -> executeSearchCommand(webSearchEngine, chatMessageContext));
}
Expand All @@ -55,6 +57,8 @@ interface SearchWebsite {
*/
private @NotNull Optional<AiMessage> executeSearchCommand(WebSearchEngine webSearchEngine,
@NotNull ChatMessageContext chatMessageContext) {
LOG.debug("Executing search command for: " + chatMessageContext.getUserPrompt());

ContentRetriever contentRetriever = WebSearchContentRetriever.builder()
.webSearchEngine(webSearchEngine)
.maxResults(DevoxxGenieStateService.getInstance().getMaxSearchResults())
Expand All @@ -70,25 +74,25 @@ interface SearchWebsite {

/**
* Get the web search engine.
*
* @param searchType the search type
* @return the web search engine
*/
private @Nullable WebSearchEngine createWebSearchEngine(@NotNull String searchType) {
DevoxxGenieSettingsService settings = DevoxxGenieStateService.getInstance();
private @Nullable WebSearchEngine createWebSearchEngine() {
LOG.debug("Creating web search engine");
DevoxxGenieStateService stateService = DevoxxGenieStateService.getInstance();

if (searchType.equals(TAVILY_SEARCH_ACTION) && settings.getTavilySearchKey() != null) {
if (stateService.isTavilySearchEnabled()) {
return TavilyWebSearchEngine.builder()
.apiKey(settings.getTavilySearchKey())
.apiKey(stateService.getTavilySearchKey())
.build();
} else if (searchType.equals(GOOGLE_SEARCH_ACTION) &&
settings.getGoogleSearchKey() != null &&
settings.getGoogleCSIKey() != null) {
} else if (stateService.isGoogleSearchEnabled() &&
stateService.getGoogleSearchKey() != null &&
stateService.getGoogleCSIKey() != null) {
return GoogleCustomWebSearchEngine.builder()
.apiKey(settings.getGoogleSearchKey())
.csi(settings.getGoogleCSIKey())
.apiKey(stateService.getGoogleSearchKey())
.csi(stateService.getGoogleCSIKey())
.build();
}
LOG.info("Web search engine not found or all disabled");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void updateInitialVisibility(@NotNull DevoxxGenieStateService stateServi
// Set initial visibility based on state service
switches.get(0).setVisible(stateService.getRagEnabled());
switches.get(1).setVisible(stateService.getGitDiffEnabled());
switches.get(2).setVisible(stateService.getEnableWebSearch());
switches.get(2).setVisible(stateService.getIsWebSearchEnabled());

// Update panel visibility
updatePanelVisibility();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static DevoxxGenieStateService getInstance() {
private String jlamaUrl = JLAMA_MODEL_URL;
private String customOpenAIUrl = "";

//
// Local LLM Providers
private boolean isOllamaEnabled = true;
private boolean isLmStudioEnabled = true;
private boolean isGpt4AllEnabled = true;
Expand All @@ -79,6 +79,7 @@ public static DevoxxGenieStateService getInstance() {
private boolean isJlamaEnabled = true;
private boolean isCustomOpenAIEnabled = false;

// Remote LLM Providers
private boolean isOpenAIEnabled = false;
private boolean isMistralEnabled = false;
private boolean isAnthropicEnabled = false;
Expand All @@ -103,7 +104,10 @@ public static DevoxxGenieStateService getInstance() {
private String azureOpenAIKey = "";

// Search API Keys
private Boolean enableWebSearch = ENABLE_WEB_SEARCH;
private Boolean isWebSearchEnabled = ENABLE_WEB_SEARCH;

private boolean tavilySearchEnabled = false;
private boolean googleSearchEnabled = false;

private String googleSearchKey = "";
private String googleCSIKey = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,53 +48,53 @@ public JComponent createComponent() {
*/
@Override
public boolean isModified() {
DevoxxGenieStateService settings = DevoxxGenieStateService.getInstance();
DevoxxGenieStateService stateService = DevoxxGenieStateService.getInstance();

boolean isModified = false;

isModified |= !settings.getStreamMode().equals(llmSettingsComponent.getStreamModeCheckBox().isSelected());

isModified |= isFieldModified(llmSettingsComponent.getOpenAIKeyField(), settings.getOpenAIKey());
isModified |= isFieldModified(llmSettingsComponent.getMistralApiKeyField(), settings.getMistralKey());
isModified |= isFieldModified(llmSettingsComponent.getAnthropicApiKeyField(), settings.getAnthropicKey());
isModified |= isFieldModified(llmSettingsComponent.getGroqApiKeyField(), settings.getGroqKey());
isModified |= isFieldModified(llmSettingsComponent.getDeepInfraApiKeyField(), settings.getDeepInfraKey());
isModified |= isFieldModified(llmSettingsComponent.getGeminiApiKeyField(), settings.getGeminiKey());
isModified |= isFieldModified(llmSettingsComponent.getDeepSeekApiKeyField(), settings.getDeepSeekKey());
isModified |= isFieldModified(llmSettingsComponent.getLlamaCPPModelUrlField(), settings.getLlamaCPPUrl());
isModified |= isFieldModified(llmSettingsComponent.getJlamaModelUrlField(), settings.getJlamaUrl());
isModified |= isFieldModified(llmSettingsComponent.getOpenRouterApiKeyField(), settings.getOpenRouterKey());

isModified |= isFieldModified(llmSettingsComponent.getOllamaModelUrlField(), settings.getOllamaModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getLmStudioModelUrlField(), settings.getLmstudioModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getGpt4AllModelUrlField(), settings.getGpt4allModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getJanModelUrlField(), settings.getJanModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getExoModelUrlField(), settings.getExoModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getCustomOpenAIUrlField(), settings.getCustomOpenAIUrl());

isModified |= !settings.getShowAzureOpenAIFields().equals(llmSettingsComponent.getEnableAzureOpenAICheckBox().isSelected());
isModified |= isFieldModified(llmSettingsComponent.getAzureOpenAIEndpointField(), settings.getAzureOpenAIEndpoint());
isModified |= isFieldModified(llmSettingsComponent.getAzureOpenAIDeploymentField(), settings.getAzureOpenAIDeployment());
isModified |= isFieldModified(llmSettingsComponent.getAzureOpenAIKeyField(), settings.getAzureOpenAIKey());

isModified |= settings.isOllamaEnabled() != llmSettingsComponent.getOllamaEnabledCheckBox().isSelected();
isModified |= settings.isLmStudioEnabled() != llmSettingsComponent.getLmStudioEnabledCheckBox().isSelected();
isModified |= settings.isGpt4AllEnabled() != llmSettingsComponent.getGpt4AllEnabledCheckBox().isSelected();
isModified |= settings.isJanEnabled() != llmSettingsComponent.getJanEnabledCheckBox().isSelected();
isModified |= settings.isExoEnabled() != llmSettingsComponent.getExoEnabledCheckBox().isSelected();
isModified |= settings.isLlamaCPPEnabled() != llmSettingsComponent.getLlamaCPPEnabledCheckBox().isSelected();
isModified |= settings.isJlamaEnabled() != llmSettingsComponent.getJlamaEnabledCheckBox().isSelected();
isModified |= settings.isCustomOpenAIEnabled() != llmSettingsComponent.getCustomOpenAIEnabledCheckBox().isSelected();

isModified |= settings.isOpenAIEnabled() != llmSettingsComponent.getOpenAIEnabledCheckBox().isSelected();
isModified |= settings.isMistralEnabled() != llmSettingsComponent.getMistralEnabledCheckBox().isSelected();
isModified |= settings.isAnthropicEnabled() != llmSettingsComponent.getAnthropicEnabledCheckBox().isSelected();
isModified |= settings.isGroqEnabled() != llmSettingsComponent.getGroqEnabledCheckBox().isSelected();
isModified |= settings.isDeepInfraEnabled() != llmSettingsComponent.getDeepInfraEnabledCheckBox().isSelected();
isModified |= settings.isGoogleEnabled() != llmSettingsComponent.getGeminiEnabledCheckBox().isSelected();
isModified |= settings.isDeepSeekEnabled() != llmSettingsComponent.getDeepSeekEnabledCheckBox().isSelected();
isModified |= settings.isOpenRouterEnabled() != llmSettingsComponent.getOpenRouterEnabledCheckBox().isSelected();
isModified |= settings.getShowAzureOpenAIFields() != llmSettingsComponent.getEnableAzureOpenAICheckBox().isSelected();
isModified |= !stateService.getStreamMode().equals(llmSettingsComponent.getStreamModeCheckBox().isSelected());

isModified |= isFieldModified(llmSettingsComponent.getOpenAIKeyField(), stateService.getOpenAIKey());
isModified |= isFieldModified(llmSettingsComponent.getMistralApiKeyField(), stateService.getMistralKey());
isModified |= isFieldModified(llmSettingsComponent.getAnthropicApiKeyField(), stateService.getAnthropicKey());
isModified |= isFieldModified(llmSettingsComponent.getGroqApiKeyField(), stateService.getGroqKey());
isModified |= isFieldModified(llmSettingsComponent.getDeepInfraApiKeyField(), stateService.getDeepInfraKey());
isModified |= isFieldModified(llmSettingsComponent.getGeminiApiKeyField(), stateService.getGeminiKey());
isModified |= isFieldModified(llmSettingsComponent.getDeepSeekApiKeyField(), stateService.getDeepSeekKey());
isModified |= isFieldModified(llmSettingsComponent.getLlamaCPPModelUrlField(), stateService.getLlamaCPPUrl());
isModified |= isFieldModified(llmSettingsComponent.getJlamaModelUrlField(), stateService.getJlamaUrl());
isModified |= isFieldModified(llmSettingsComponent.getOpenRouterApiKeyField(), stateService.getOpenRouterKey());

isModified |= isFieldModified(llmSettingsComponent.getOllamaModelUrlField(), stateService.getOllamaModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getLmStudioModelUrlField(), stateService.getLmstudioModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getGpt4AllModelUrlField(), stateService.getGpt4allModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getJanModelUrlField(), stateService.getJanModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getExoModelUrlField(), stateService.getExoModelUrl());
isModified |= isFieldModified(llmSettingsComponent.getCustomOpenAIUrlField(), stateService.getCustomOpenAIUrl());

isModified |= !stateService.getShowAzureOpenAIFields().equals(llmSettingsComponent.getEnableAzureOpenAICheckBox().isSelected());
isModified |= isFieldModified(llmSettingsComponent.getAzureOpenAIEndpointField(), stateService.getAzureOpenAIEndpoint());
isModified |= isFieldModified(llmSettingsComponent.getAzureOpenAIDeploymentField(), stateService.getAzureOpenAIDeployment());
isModified |= isFieldModified(llmSettingsComponent.getAzureOpenAIKeyField(), stateService.getAzureOpenAIKey());

isModified |= stateService.isOllamaEnabled() != llmSettingsComponent.getOllamaEnabledCheckBox().isSelected();
isModified |= stateService.isLmStudioEnabled() != llmSettingsComponent.getLmStudioEnabledCheckBox().isSelected();
isModified |= stateService.isGpt4AllEnabled() != llmSettingsComponent.getGpt4AllEnabledCheckBox().isSelected();
isModified |= stateService.isJanEnabled() != llmSettingsComponent.getJanEnabledCheckBox().isSelected();
isModified |= stateService.isExoEnabled() != llmSettingsComponent.getExoEnabledCheckBox().isSelected();
isModified |= stateService.isLlamaCPPEnabled() != llmSettingsComponent.getLlamaCPPEnabledCheckBox().isSelected();
isModified |= stateService.isJlamaEnabled() != llmSettingsComponent.getJlamaEnabledCheckBox().isSelected();
isModified |= stateService.isCustomOpenAIEnabled() != llmSettingsComponent.getCustomOpenAIEnabledCheckBox().isSelected();

isModified |= stateService.isOpenAIEnabled() != llmSettingsComponent.getOpenAIEnabledCheckBox().isSelected();
isModified |= stateService.isMistralEnabled() != llmSettingsComponent.getMistralEnabledCheckBox().isSelected();
isModified |= stateService.isAnthropicEnabled() != llmSettingsComponent.getAnthropicEnabledCheckBox().isSelected();
isModified |= stateService.isGroqEnabled() != llmSettingsComponent.getGroqEnabledCheckBox().isSelected();
isModified |= stateService.isDeepInfraEnabled() != llmSettingsComponent.getDeepInfraEnabledCheckBox().isSelected();
isModified |= stateService.isGoogleEnabled() != llmSettingsComponent.getGeminiEnabledCheckBox().isSelected();
isModified |= stateService.isDeepSeekEnabled() != llmSettingsComponent.getDeepSeekEnabledCheckBox().isSelected();
isModified |= stateService.isOpenRouterEnabled() != llmSettingsComponent.getOpenRouterEnabledCheckBox().isSelected();
isModified |= stateService.getShowAzureOpenAIFields() != llmSettingsComponent.getEnableAzureOpenAICheckBox().isSelected();

return isModified;
}
Expand Down
Loading

0 comments on commit 079f62a

Please sign in to comment.