-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
178 additions
and
92 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: 4 additions & 1 deletion
5
...dapter/application/ChatConfiguration.java → ...application/ApplicationConfiguration.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
12 changes: 0 additions & 12 deletions
12
...main/java/io/github/kszapsza/springairag/adapter/application/SystemMessageProperties.java
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/main/java/io/github/kszapsza/springairag/adapter/llm/ChatProperties.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,19 @@ | ||
package io.github.kszapsza.springairag.adapter.llm; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "app.chat") | ||
public record ChatProperties( | ||
Embedding embedding, | ||
SystemPrompt systemPrompt) { | ||
|
||
public record Embedding(String document) { | ||
} | ||
|
||
public record SystemPrompt( | ||
String resource, | ||
Map<String, Object> placeholders) { | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...io/github/kszapsza/springairag/adapter/llm/classpath/ClasspathResourcesConfiguration.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.github.kszapsza.springairag.adapter.llm.classpath; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
|
||
import io.github.kszapsza.springairag.adapter.llm.EmbeddingDocumentsProvider; | ||
import io.github.kszapsza.springairag.adapter.llm.SystemPromptTemplateProvider; | ||
|
||
@Configuration | ||
class ClasspathResourcesConfiguration { | ||
|
||
@Bean | ||
EmbeddingDocumentsProvider embeddingDocumentsProvider( | ||
@Value("${app.chat.embedding.document}") Resource documentResource) { | ||
return new ClasspathEmbeddingDocumentsProvider(documentResource); | ||
} | ||
|
||
@Bean | ||
SystemPromptTemplateProvider systemPromptTemplateProvider( | ||
@Value("${app.chat.system-prompt.resource}") Resource systemPromptResource) { | ||
return new ClasspathSystemPromptTemplateProvider(systemPromptResource); | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
src/main/java/io/github/kszapsza/springairag/adapter/llm/openai/OpenAiChatConfiguration.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,50 @@ | ||
package io.github.kszapsza.springairag.adapter.llm.openai; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor; | ||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; | ||
import org.springframework.ai.chat.client.advisor.api.Advisor; | ||
import org.springframework.ai.chat.messages.Message; | ||
import org.springframework.ai.chat.prompt.ChatOptions; | ||
import org.springframework.ai.openai.OpenAiChatModel; | ||
import org.springframework.ai.openai.OpenAiChatOptions; | ||
import org.springframework.ai.vectorstore.SearchRequest; | ||
import org.springframework.ai.vectorstore.VectorStore; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.github.kszapsza.springairag.adapter.llm.ChatProperties; | ||
import io.github.kszapsza.springairag.adapter.llm.SystemPromptTemplateProvider; | ||
|
||
@Configuration | ||
class OpenAiChatConfiguration { | ||
|
||
@Bean | ||
ChatClient chatClient(OpenAiChatModel chatModel) { | ||
return ChatClient.create(chatModel); | ||
} | ||
|
||
@Bean | ||
ChatOptions chatOptions() { | ||
return OpenAiChatOptions.builder() | ||
.withFunction("realEstateSearchFunction") | ||
.build(); | ||
} | ||
|
||
@Bean | ||
List<Advisor> chatAdvisors(VectorStore vectorStore) { | ||
return List.of( | ||
new SimpleLoggerAdvisor(), | ||
new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults())); | ||
} | ||
|
||
@Bean | ||
Message systemMessage( | ||
SystemPromptTemplateProvider systemPromptTemplateProvider, | ||
ChatProperties chatProperties) { | ||
return systemPromptTemplateProvider.getSystemPromptTemplate() | ||
.createMessage(chatProperties.systemPrompt().placeholders()); | ||
} | ||
} |
87 changes: 38 additions & 49 deletions
87
src/main/java/io/github/kszapsza/springairag/adapter/llm/openai/OpenAiChatProvider.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 |
---|---|---|
@@ -1,78 +1,67 @@ | ||
package io.github.kszapsza.springairag.adapter.llm.openai; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor; | ||
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; | ||
import org.springframework.ai.chat.client.advisor.api.Advisor; | ||
import org.springframework.ai.chat.messages.Message; | ||
import org.springframework.ai.chat.model.Generation; | ||
import org.springframework.ai.chat.prompt.ChatOptions; | ||
import org.springframework.ai.chat.prompt.SystemPromptTemplate; | ||
import org.springframework.ai.openai.OpenAiChatModel; | ||
import org.springframework.ai.openai.OpenAiChatOptions; | ||
import org.springframework.ai.vectorstore.SearchRequest; | ||
import org.springframework.ai.vectorstore.VectorStore; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.github.kszapsza.springairag.adapter.application.SystemMessageProperties; | ||
import io.github.kszapsza.springairag.adapter.llm.SystemPromptTemplateProvider; | ||
import io.github.kszapsza.springairag.domain.chat.ChatProvider; | ||
import io.github.kszapsza.springairag.domain.chat.ChatRequest; | ||
import io.github.kszapsza.springairag.domain.chat.ChatResponse; | ||
|
||
@Component | ||
@Configuration | ||
public class OpenAiChatProvider implements ChatProvider { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(OpenAiChatProvider.class); | ||
|
||
private final OpenAiChatModel chatModel; | ||
private final VectorStore vectorStore; | ||
private final SystemMessageProperties systemMessageProperties; | ||
private final SystemPromptTemplate systemPromptTemplate; | ||
private final ChatClient chatClient; | ||
private final ChatOptions chatOptions; | ||
private final List<Advisor> advisors; | ||
private final Message systemMessage; | ||
|
||
public OpenAiChatProvider( | ||
OpenAiChatModel chatModel, | ||
VectorStore vectorStore, | ||
SystemMessageProperties systemMessageProperties, | ||
SystemPromptTemplateProvider systemPromptTemplateProvider) { | ||
this.chatModel = chatModel; | ||
this.vectorStore = vectorStore; | ||
this.systemMessageProperties = systemMessageProperties; | ||
this.systemPromptTemplate = systemPromptTemplateProvider.getSystemPromptTemplate(); | ||
ChatClient chatClient, | ||
ChatOptions chatOptions, | ||
List<Advisor> advisors, | ||
Message systemMessage) { | ||
this.chatClient = chatClient; | ||
this.chatOptions = chatOptions; | ||
this.advisors = advisors; | ||
this.systemMessage = systemMessage; | ||
} | ||
|
||
@Override | ||
public ChatResponse chat(ChatRequest request) { | ||
if (request.message() == null || request.message().trim().isEmpty()) { | ||
logger.warn("Received a null or empty request message"); | ||
return new ChatResponse.Failure("Request message cannot be null or empty"); | ||
} | ||
try { | ||
var content = callChatModel(request.message()).getContent(); | ||
return new ChatResponse.Success(content); | ||
return callChatModel(request.message()) | ||
.map((generation) -> generation.getOutput().getContent()) | ||
.map((content) -> (ChatResponse) new ChatResponse.Success(content)) | ||
.orElseGet(() -> { | ||
logger.warn("Received null generation result"); | ||
return new ChatResponse.Error.ServerError("Received null generation result"); | ||
}); | ||
} catch (Exception ex) { | ||
return new ChatResponse.Failure(ex.getMessage()); | ||
logger.error("Error during chat model call", ex); | ||
return new ChatResponse.Error.ServerError("An internal error occurred during processing."); | ||
} | ||
} | ||
|
||
private Message callChatModel(String userMessageContent) { | ||
return ChatClient.create(chatModel) | ||
.prompt() | ||
.options(buildOptions()) | ||
.advisors( | ||
new SimpleLoggerAdvisor(), | ||
new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults())) | ||
.system(systemPromptTemplate.createMessage(systemMessageProperties.placeholders()).getContent()) | ||
.user(userMessageContent) | ||
.call() | ||
.chatResponse() | ||
.getResult() | ||
.getOutput(); | ||
} | ||
|
||
private ChatOptions buildOptions() { | ||
return OpenAiChatOptions.builder() | ||
.withFunction("realEstateSearchFunction") | ||
.build(); | ||
private Optional<Generation> callChatModel(String userMessageContent) { | ||
return Optional.ofNullable( | ||
chatClient.prompt() | ||
.options(chatOptions) | ||
.advisors(advisors) | ||
.system(systemMessage.getContent()) | ||
.user(userMessageContent) | ||
.call() | ||
.chatResponse() | ||
.getResult()); | ||
} | ||
} |
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.