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

Update spring ai lib #1

Merged
merged 2 commits into from
Aug 10, 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
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.7'
services:
postgres:
image: ankane/pgvector:v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions 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.2.2</version>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ai.springdemo</groupId>
Expand All @@ -15,7 +15,7 @@
<description>AISpringDemo</description>
<properties>
<java.version>21</java.version>
<spring-ai.version>0.8.0-SNAPSHOT</spring-ai.version>
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
<spring-retry.version>2.0.3</spring-retry.version>
</properties>
<dependencies>
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/ai/springdemo/aispringdemo/config/RagConfig.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.ai.springdemo.aispringdemo.config;

import org.springframework.ai.embedding.EmbeddingClient;
import org.springframework.ai.vectorstore.PgVectorStore;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.retry.annotation.EnableRetry;

@Configuration
@EnableRetry
public class RagConfig {

@Bean(name = "vectorStoreDB")
public VectorStore vectorStoreDB(EmbeddingModel embeddingModel) {
return new SimpleVectorStore(embeddingModel);
}

@Bean
public VectorStore vectorStore(EmbeddingClient embeddingClient, JdbcTemplate jdbcTemplate) {
return new PgVectorStore(jdbcTemplate, embeddingClient, 4);
public ChatMemory chatMemory() {
return new InMemoryChatMemory();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
public class DataLoadingService {
private static final Logger logger = LoggerFactory.getLogger(DataLoadingService.class);

@Value("classpath:/data/tr_technology_radar_vol_29_en.pdf")
@Value("classpath:/data/tw_report_looking_glass_2024.pdf")
// @Value("classpath:/data/tw_ebook_responsible_tech_playbook_united_nations.pdf")
// @Value("classpath:/data/tw_ebook_responsible_ai_2023.pdf")
// @Value("classpath:/data/tr_technology_radar_vol_29_en.pdf")
private Resource documentResource;

private final VectorStore vectorStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.document.Document;
Expand All @@ -31,13 +30,14 @@ public class QuestionService {
@Value("classpath:/prompts/system-chatbot.st")
private Resource chatbotSystemPromptResource;

private final ChatClient aiClient;
private final ChatClient chatClient;


private final VectorStore vectorStore;

@Autowired
public QuestionService(ChatClient aiClient, VectorStore vectorStore) {
this.aiClient = aiClient;
public QuestionService(ChatClient.Builder chatClientBuilder, VectorStore vectorStore) {
this.chatClient = chatClientBuilder.build();
this.vectorStore = vectorStore;
}

Expand All @@ -47,9 +47,9 @@ public String question(String message, boolean prompstuff) {
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));

logger.info("Asking AI model to reply to question.");
ChatResponse aiResponse = aiClient.call(prompt);
ChatResponse chatResponse = chatClient.prompt(prompt).call().chatResponse();
logger.info("AI responded.");
return aiResponse.getResults().get(0).getOutput().getContent().toString();
return chatResponse.getResults().getFirst().getOutput().getContent();
}

private Message getSystemMessage(String message, boolean prompstuff) {
Expand All @@ -68,6 +68,9 @@ private Message getSystemMessage(String message, boolean prompstuff) {


public String simpleQuestion(String question) {
return aiClient.call(question);
Message systemMessage = getSystemMessage(question, true);
UserMessage userMessage = new UserMessage(question);
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
return chatClient.prompt(prompt).call().chatResponse().getResults().getFirst().getOutput().getContent();
}
}
14 changes: 8 additions & 6 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ spring.datasource.password=admin

spring.threads.virtual.enabled=true

spring.ai.openai.temperature=0.3
spring.ai.openai.api-key= ${OPENAI_API_KEY}
spring.ai.openai.model=gpt-4-turbo
spring.ai.embedding-base-url=https://api.openai.com
spring.ai.openai.embedding-api-key= ${OPENAI_API_KEY}
spring.ai.openai.embedding-model=text-embedding-ada-002
spring.ai.openai.api-key={OPEN_AI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4o

spring.ai.openai.embedding.enabled=true
spring.ai.openai.chat.options.temperature=0.3
spring.ai.openai.embedding.base-url=https://api.openai.com
spring.ai.openai.embedding.api-key={OPEN_AI_API_KEY}
spring.ai.openai.embedding.options.model=text-embedding-ada-002

spring.management.endpoints.web.exposure.include=*
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading