这是indexloc提供的服务,不要输入任何密码
Skip to content
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
100 changes: 4 additions & 96 deletions spring-ai-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-markdown-document-reader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-chroma-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-anthropic-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-converse-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -84,10 +56,6 @@
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
Expand Down Expand Up @@ -122,80 +90,20 @@
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-mongodb-atlas</artifactId>
<artifactId>spring-ai-starter-vector-store-mongodb-atlas</artifactId>
<version>${spring-ai-mongodb-atlas.version}</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>chromadb</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.boot.mainclass>com.baeldung.springai.chromadb.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>assistant</id>
<properties>
<spring.boot.mainclass>com.baeldung.spring.ai.om.OrderManagementApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>anthropic</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.anthropic.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>deepseek</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.deepseek.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>evaluator</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.evaluator.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>hugging-face</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.huggingface.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>mcp-server</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.mcp.server.ServerApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>mcp-client</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.mcp.client.ClientApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>amazon-nova</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.nova.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>pgvector</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.semanticsearch.Application</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>transcribe</id>
<properties>
<spring.boot.mainclass>com.baeldung.springai.transcribe.Application</spring.boot.mainclass>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.springai.embeddings;

import org.springframework.ai.autoconfigure.chat.client.ChatClientAutoConfiguration;
import org.springframework.ai.model.openai.autoconfigure.OpenAiAudioSpeechAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

@SpringBootApplication(exclude = {
ChatClientAutoConfiguration.class,
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class,
org.springframework.ai.autoconfigure.vectorstore.mongo.MongoDBAtlasVectorStoreAutoConfiguration.class,
org.springframework.ai.vectorstore.mongodb.autoconfigure.MongoDBAtlasVectorStoreAutoConfiguration.class,
OpenAiAudioSpeechAutoConfiguration.class})
class Application {

public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setAdditionalProfiles("embeddings");
app.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.springai.embeddings;

import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.openai.OpenAiEmbeddingOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmbeddingConfig {

@Bean
public OpenAiApi openAiApi(@Value("${spring.ai.openai.api-key}") String apiKey) {
return OpenAiApi.builder()
.apiKey(apiKey)
.build();
}

@Bean
public OpenAiEmbeddingModel openAiEmbeddingModel(OpenAiApi openAiApi) {
OpenAiEmbeddingOptions options = OpenAiEmbeddingOptions.builder()
.model("text-embedding-3-small")
.build();
return new OpenAiEmbeddingModel(openAiApi, MetadataMode.EMBED, options);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.springai.embeddings;

import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmbeddingController {

private final EmbeddingService embeddingService;
private final ManualEmbeddingService manualEmbeddingService;

public EmbeddingController(EmbeddingService embeddingService, ManualEmbeddingService manualEmbeddingService) {
this.embeddingService = embeddingService;
this.manualEmbeddingService = manualEmbeddingService;
}

@PostMapping("/embeddings")
public ResponseEntity<EmbeddingResponse> getEmbeddings(@RequestBody String text) {
EmbeddingResponse response = embeddingService.getEmbeddings(text);
return ResponseEntity.ok(response);
}

@PostMapping("/manual-embeddings")
public ResponseEntity<EmbeddingResponse> getManualEmbeddings(@RequestBody String text) {
EmbeddingResponse response = manualEmbeddingService.getEmbeddings(text);
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.springai.embeddings;

import java.util.Arrays;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.stereotype.Service;

@Service
public class EmbeddingService {

private final EmbeddingModel embeddingModel;

public EmbeddingService(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}

public EmbeddingResponse getEmbeddings(String... texts) {
EmbeddingRequest request = new EmbeddingRequest(Arrays.asList(texts), null);
return embeddingModel.call(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baeldung.springai.embeddings;

import java.util.Arrays;

import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.stereotype.Service;

@Service
public class ManualEmbeddingService {

private final OpenAiEmbeddingModel openAiEmbeddingModel;

public ManualEmbeddingService(OpenAiEmbeddingModel openAiEmbeddingModel) {
this.openAiEmbeddingModel = openAiEmbeddingModel;
}

public EmbeddingResponse getEmbeddings(String... texts) {
EmbeddingRequest request = new EmbeddingRequest(Arrays.asList(texts), null);
return openAiEmbeddingModel.call(request);
}

}
12 changes: 12 additions & 0 deletions spring-ai-3/src/main/resources/application-embeddings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spring:
ai:
openai:
api-key: "<YOUR-API-KEY>"
embedding:
options:
model: "text-embedding-3-small"

# Avoid starting docker from the shared codebase
docker:
compose:
enabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.springai.embeddings;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("embeddings")
class EmbeddingServiceLiveTest {

@Autowired
private EmbeddingService embeddingService;

@Test
void whenGetEmbeddings_thenReturnEmbeddingResponse() {
String text = "This is a test string for embedding.";
EmbeddingResponse response = embeddingService.getEmbeddings(text);
assertThat(response).isNotNull();
assertThat(response.getResults()).isNotNull();
assertThat(response.getResults().isEmpty()).isFalse();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.springai.embeddings;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("embeddings")
class ManualEmbeddingServiceLiveTest {

@Autowired
private ManualEmbeddingService embeddingService;

@Test
void whenGetEmbeddings_thenReturnEmbeddingResponse() {
String text = "This is a test string for embedding.";
EmbeddingResponse response = embeddingService.getEmbeddings(text);
assertThat(response).isNotNull();
assertThat(response.getResults()).isNotNull();
assertThat(response.getResults().isEmpty()).isFalse();
}

}