-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate open ai to provide the ai summary and conversation health e…
…valuation (#60)
- Loading branch information
1 parent
ceb8894
commit 9884cef
Showing
34 changed files
with
735 additions
and
47 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
5 changes: 0 additions & 5 deletions
5
buildSrc/src/main/groovy/flowinquiry.spring-cache-conventions.gradle
This file was deleted.
Oops, something went wrong.
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
26 changes: 26 additions & 0 deletions
26
commons/src/main/java/io/flowinquiry/config/CacheConfig.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,26 @@ | ||
package io.flowinquiry.config; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import java.util.concurrent.TimeUnit; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.cache.caffeine.CaffeineCacheManager; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableCaching | ||
public class CacheConfig { | ||
|
||
@Bean | ||
public CacheManager cacheManager() { | ||
Caffeine<Object, Object> caffeineBuilder = | ||
Caffeine.newBuilder() | ||
.expireAfterWrite(10, TimeUnit.MINUTES) // Set cache expiration | ||
.maximumSize(1000); // Set maximum cache size | ||
|
||
CaffeineCacheManager cacheManager = new CaffeineCacheManager(); | ||
cacheManager.setCaffeine(caffeineBuilder); | ||
return cacheManager; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
commons/src/main/java/io/flowinquiry/modules/ai/config/ChatModelConfiguration.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,30 @@ | ||
package io.flowinquiry.modules.ai.config; | ||
|
||
import io.flowinquiry.modules.ai.service.ChatModelService; | ||
import io.flowinquiry.modules.ai.service.OllamaChatModelService; | ||
import io.flowinquiry.modules.ai.service.OpenAiChatModelService; | ||
import java.util.Optional; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
@Configuration | ||
public class ChatModelConfiguration { | ||
|
||
@Bean | ||
@Primary | ||
@ConditionalOnBean({OllamaChatModelService.class, OpenAiChatModelService.class}) | ||
public ChatModelService chatModel( | ||
Optional<OllamaChatModelService> ollamaChatModelService, | ||
Optional<OpenAiChatModelService> openAiChatModelService) { | ||
if (ollamaChatModelService.isPresent()) { | ||
return ollamaChatModelService.get(); | ||
} else if (openAiChatModelService.isPresent()) { | ||
return openAiChatModelService.get(); | ||
} | ||
|
||
// If no chat models are present, this block won't execute due to @ConditionalOnBean | ||
return null; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
commons/src/main/java/io/flowinquiry/modules/ai/service/ChatModelService.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,46 @@ | ||
package io.flowinquiry.modules.ai.service; | ||
|
||
/** | ||
* Interface representing a generic chat model service. | ||
* | ||
* <p>This interface defines the contract for interacting with different chat models, such as OpenAI | ||
* or Ollama, by providing a method to process input and return a response. Implementations of this | ||
* interface should encapsulate the logic specific to each chat model's API or processing. | ||
* | ||
* <h3>Usage Example</h3> | ||
* | ||
* <pre>{@code | ||
* ChatModelService chatModelService = new OpenAiChatModelService(); | ||
* String response = chatModelService.call("Summarize this text."); | ||
* System.out.println(response); | ||
* }</pre> | ||
* | ||
* <h3>Implementations</h3> | ||
* | ||
* <ul> | ||
* <li>{@link OpenAiChatModelService} | ||
* <li>{@link OllamaChatModelService} | ||
* </ul> | ||
* | ||
* <h3>Thread-Safety</h3> | ||
* | ||
* Implementations of this interface should ensure thread-safety if they are intended to be used in | ||
* concurrent environments. | ||
* | ||
* @author Hai Nguyen | ||
* @version 1.0 | ||
*/ | ||
public interface ChatModelService { | ||
|
||
/** | ||
* Processes the given input using the chat model and returns the model's response. | ||
* | ||
* <p>The input string can be any text to be processed by the chat model, such as a question, a | ||
* request for summarization, or any prompt supported by the model. | ||
* | ||
* @param input the text input to be processed by the chat model | ||
* @return the response generated by the chat model | ||
* @throws IllegalArgumentException if the input is null or empty | ||
*/ | ||
String call(String input); | ||
} |
16 changes: 16 additions & 0 deletions
16
commons/src/main/java/io/flowinquiry/modules/ai/service/OllamaChatModelService.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,16 @@ | ||
package io.flowinquiry.modules.ai.service; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@ConditionalOnProperty( | ||
name = {"OLLAMA_CHAT_MODEL", "OLLAMA_API_KEY"}, | ||
matchIfMissing = false) | ||
public class OllamaChatModelService implements ChatModelService { | ||
|
||
@Override | ||
public String call(String input) { | ||
return "Response from Ollama: " + input; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
commons/src/main/java/io/flowinquiry/modules/ai/service/OpenAiChatModelService.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,25 @@ | ||
package io.flowinquiry.modules.ai.service; | ||
|
||
import org.springframework.ai.openai.OpenAiChatModel; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@ConditionalOnProperty( | ||
name = {"OPEN_AI_CHAT_MODEL", "OPEN_AI_API_KEY"}, | ||
matchIfMissing = false) | ||
@ConditionalOnBean(OpenAiChatModel.class) | ||
public class OpenAiChatModelService implements ChatModelService { | ||
|
||
private final OpenAiChatModel openAiChatModel; | ||
|
||
public OpenAiChatModelService(OpenAiChatModel openAiChatModel) { | ||
this.openAiChatModel = openAiChatModel; | ||
} | ||
|
||
@Override | ||
public String call(String input) { | ||
return openAiChatModel.call(input); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
commons/src/main/java/io/flowinquiry/modules/teams/controller/AiChatController.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,26 @@ | ||
package io.flowinquiry.modules.teams.controller; | ||
|
||
import io.flowinquiry.modules.teams.service.TeamRequestHealthEvalService; | ||
import org.springframework.ai.openai.OpenAiChatModel; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/ai") | ||
@ConditionalOnBean(OpenAiChatModel.class) | ||
public class AiChatController { | ||
|
||
private final TeamRequestHealthEvalService teamRequestHealthEvalService; | ||
|
||
public AiChatController(TeamRequestHealthEvalService teamRequestHealthEvalService) { | ||
this.teamRequestHealthEvalService = teamRequestHealthEvalService; | ||
} | ||
|
||
@PostMapping | ||
public String createRequestSummary(@RequestBody String requestDescription) { | ||
return teamRequestHealthEvalService.summarizeTeamRequest(requestDescription); | ||
} | ||
} |
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.