Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : upgrade to spring boot 3.4.1 & polish #134

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions rag/rag-springai-openai-llm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.6</version>
<version>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learning.ai</groupId>
Expand All @@ -16,7 +16,7 @@

<properties>
<java.version>21</java.version>
<spring-ai.version>0.8.1</spring-ai.version>
<spring-ai.version>1.0.0-M5</spring-ai.version>
<spotless.version>2.43.0</spotless.version>
</properties>

Expand Down Expand Up @@ -56,7 +56,7 @@
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
<version>2.7.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -192,5 +192,15 @@
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ApplicationRunner runner(VectorStore vectorStore, JdbcTemplate template, TokenTe
ExtractedTextFormatter textFormatter = ExtractedTextFormatter.builder()
.withNumberOfBottomTextLinesToDelete(3)
.withNumberOfTopPagesToSkipBeforeDelete(1)
.withNumberOfTopTextLinesToDelete(1)
.build();
TikaDocumentReader documentReader = new TikaDocumentReader(resource, textFormatter);
template.update("delete from vector_store");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
Expand Down Expand Up @@ -40,26 +38,24 @@ with one player from the fielding team (the bowler) bowling the ball towards the
private final ChatClient aiClient;
private final VectorStore vectorStore;

public AIChatService(ChatClient aiClient, VectorStore vectorStore) {
this.aiClient = aiClient;
public AIChatService(ChatClient.Builder chatClientBuilder, VectorStore vectorStore) {
this.aiClient = chatClientBuilder.clone().build();
this.vectorStore = vectorStore;
}

public String chat(String searchQuery) {
// Querying the VectorStore using natural language looking for the information about info asked.
List<Document> listOfSimilarDocuments = this.vectorStore.similaritySearch(searchQuery);
String documents = listOfSimilarDocuments.stream()
.map(Document::getContent)
.map(Document::getText)
.collect(Collectors.joining(System.lineSeparator()));
// Constructing the systemMessage to indicate the AI model to use the passed information
// to answer the question.
Message systemMessage = new SystemPromptTemplate(template).createMessage(Map.of("documents", documents));
UserMessage userMessage = new UserMessage(searchQuery);
OpenAiChatOptions chatOptions =
OpenAiChatOptions.builder().withFunction("currentDateFunction").build();
OpenAiChatOptions.builder().function("currentDateFunction").build();
Prompt prompt = new Prompt(List.of(systemMessage, userMessage), chatOptions);
ChatResponse aiResponse = aiClient.call(prompt);
Generation generation = aiResponse.getResult();
return (generation != null) ? generation.getOutput().getContent() : "";
return aiClient.prompt(prompt).call().content();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ spring.ai.openai.api-key=demo
spring.ai.openai.base-url=http://langchain4j.dev/demo/openai
spring.ai.openai.chat.options.model=gpt-4o-mini
spring.ai.openai.chat.options.temperature=0.2
spring.ai.openai.chat.options.responseFormat=json_object
#spring.ai.openai.chat.options.responseFormat=json_object

spring.ai.openai.embedding.enabled=true
spring.ai.openai.embedding.options.model=text-embedding-3-small

#spring.ai.openai.image.model=dall-e-3

#PgVector
spring.ai.vectorstore.pgvector.initialize-schema=true
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TestLlmRagWithSpringAiApplication {
@Bean
@ServiceConnection
PostgreSQLContainer<?> pgvectorContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("pgvector/pgvector:pg16"));
return new PostgreSQLContainer<>(DockerImageName.parse("pgvector/pgvector:pg17"));
}

public static void main(String[] args) {
Expand Down
Loading