Skip to content

Commit

Permalink
upgrade to SB 3.4 and polish observability
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Dec 24, 2024
1 parent 4b4ac3a commit eb33e44
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 65 deletions.
48 changes: 34 additions & 14 deletions rag/rag-springai-ollama-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,8 @@

<properties>
<java.version>21</java.version>
<spring-ai.version>1.0.0-M4</spring-ai.version>
<spring-ai.version>1.0.0-M5</spring-ai.version>
<otelInstrumentation.version>2.10.0-alpha</otelInstrumentation.version>
<spotless.version>2.43.0</spotless.version>
</properties>

Expand All @@ -39,11 +40,7 @@
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
Expand All @@ -56,7 +53,14 @@
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
Expand All @@ -76,6 +80,17 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-otlp</artifactId>
</dependency>
<dependency>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -97,16 +112,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.redis.testcontainers</groupId>
<artifactId>testcontainers-redis</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>grafana</artifactId>
<scope>test</scope>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>grafana</artifactId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
<version>1.20.4</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
Expand All @@ -124,6 +137,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
<version>${otelInstrumentation.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -144,7 +164,7 @@
<configuration>
<java>
<palantirJavaFormat>
<version>2.47.0</version>
<version>2.50.0</version>
</palantirJavaFormat>
<importOrder/>
<removeUnusedImports/>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
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.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.prompt.AssistantPromptTemplate;
import org.springframework.ai.rag.generation.augmentation.ContextualQueryAugmenter;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

Expand All @@ -22,26 +22,35 @@ public class AIChatService {
isn't found in the DOCUMENTS section, simply state that you don't know the answer.
DOCUMENTS:
{question_answer_context}
{query}
""";

private final ChatClient aiClient;
private final VectorStore vectorStore;

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

var documentRetriever = VectorStoreDocumentRetriever.builder()
.vectorStore(vectorStore)
.similarityThreshold(0.50)
.build();

var queryAugmenter = ContextualQueryAugmenter.builder()
.promptTemplate(new AssistantPromptTemplate(template))
.allowEmptyContext(true)
.build();

RetrievalAugmentationAdvisor retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder()
.documentRetriever(documentRetriever)
.queryAugmenter(queryAugmenter)
.build();
this.aiClient =
builder.clone().defaultAdvisors(retrievalAugmentationAdvisor).build();
}

public String chat(String query) {
ChatResponse aiResponse = aiClient.prompt()
.advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.query(query), template))
.user(query)
.call()
.chatResponse();
String aiResponse = aiClient.prompt().user(query).call().content();
LOGGER.info("Response received from call :{}", aiResponse);
Generation generation = aiResponse.getResult();
return (generation != null) ? generation.getOutput().getContent() : "";
return aiResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@ spring.application.name=rag-springai-ollama-llm
spring.threads.virtual.enabled=true
spring.mvc.problemdetails.enabled=true

spring.ai.ollama.init.pull-model-strategy=when_missing
spring.ai.ollama.chat.options.model=mistral
spring.ai.ollama.chat.options.temperature=0.3
spring.ai.ollama.chat.options.top-k=2
spring.ai.ollama.chat.options.top-p=0.2

spring.ai.ollama.embedding.options.model=nomic-embed-text

spring.ai.vectorstore.redis.index=vector_store
spring.ai.vectorstore.redis.prefix=ai
spring.ai.vectorstore.redis.initialize-schema=true
#PgVector
spring.ai.vectorstore.observations.include-query-response=true
spring.ai.vectorstore.pgvector.initialize-schema=true

spring.ai.ollama.baseUrl=http://localhost:11434
spring.http.client.connect-timeout=PT1M
spring.http.client.read-timeout=PT5M

spring.testcontainers.beans.startup=parallel

##Observability
spring.ai.chat.observations.include-completion=true
spring.ai.chat.observations.include-prompt=true
spring.ai.chat.client.observations.include-input=true

management.endpoints.web.exposure.include=*
management.metrics.tags.service.name=${spring.application.name}
management.tracing.sampling.probability=1.0
management.otlp.tracing.endpoint=http://localhost:4318/v1/traces
management.otlp.logging.endpoint=http://localhost:4318/v1/logs

logging.level.org.springframework.ai.rag=debug

## only for development
spring.ai.vectorstore.pgvector.removeExistingVectorStoreTable=true
spring.ai.ollama.baseUrl=http://localhost:11434
16 changes: 16 additions & 0 deletions rag/rag-springai-ollama-llm/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />

<appender name="OpenTelemetry"
class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
<captureExperimentalAttributes>true</captureExperimentalAttributes>
<captureCodeAttributes>true</captureCodeAttributes>
<captureKeyValuePairAttributes>true</captureKeyValuePairAttributes>
</appender>

<root level="INFO">
<appender-ref ref="OpenTelemetry"/>
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.learning.ai.llmragwithspringai.config;

import com.redis.testcontainers.RedisStackContainer;
import java.io.IOException;
import java.time.Duration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.grafana.LgtmStackContainer;
import org.testcontainers.ollama.OllamaContainer;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -28,19 +26,15 @@ OllamaContainer ollama() throws IOException, InterruptedException {
}

@Bean
RedisStackContainer redisContainer(DynamicPropertyRegistry properties) {
RedisStackContainer redis = new RedisStackContainer(
RedisStackContainer.DEFAULT_IMAGE_NAME.withTag(RedisStackContainer.DEFAULT_TAG));
properties.add("spring.ai.vectorstore.redis.uri", () -> "redis://%s:%d"
.formatted(redis.getHost(), redis.getMappedPort(6379)));
return redis;
@ServiceConnection
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("pgvector/pgvector:pg17"));
}

@Bean
@Scope("singleton")
@ServiceConnection("otel/opentelemetry-collector-contrib")
@ServiceConnection
LgtmStackContainer lgtmStackContainer() {
return new LgtmStackContainer(DockerImageName.parse("grafana/otel-lgtm").withTag("0.7.1"))
return new LgtmStackContainer(DockerImageName.parse("grafana/otel-lgtm").withTag("0.8.1"))
.withStartupTimeout(Duration.ofMinutes(2));
}
}

0 comments on commit eb33e44

Please sign in to comment.