feat: open-ai
This commit is contained in:
@@ -24,10 +24,10 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.ai</groupId>-->
|
||||
<!-- <artifactId>spring-ai-openai-spring-boot-starter</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
|
||||
@@ -4,6 +4,8 @@ import org.springframework.ai.ollama.OllamaChatClient;
|
||||
import org.springframework.ai.ollama.OllamaEmbeddingClient;
|
||||
import org.springframework.ai.ollama.api.OllamaApi;
|
||||
import org.springframework.ai.ollama.api.OllamaOptions;
|
||||
import org.springframework.ai.openai.OpenAiEmbeddingClient;
|
||||
import org.springframework.ai.openai.api.OpenAiApi;
|
||||
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
|
||||
import org.springframework.ai.vectorstore.PgVectorStore;
|
||||
import org.springframework.ai.vectorstore.SimpleVectorStore;
|
||||
@@ -25,6 +27,11 @@ public class OllamaConfig {
|
||||
return new OllamaApi(baseUrl);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OpenAiApi openAiApi(@Value("${spring.ai.openai.base-url}") String baseUrl, @Value("${spring.ai.openai.api-key}") String apikey) {
|
||||
return new OpenAiApi(baseUrl, apikey);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OllamaChatClient ollamaChatClient(OllamaApi ollamaApi) {
|
||||
return new OllamaChatClient(ollamaApi);
|
||||
@@ -36,17 +43,27 @@ public class OllamaConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleVectorStore simpleVectorStore(OllamaApi ollamaApi) {
|
||||
OllamaEmbeddingClient embeddingClient = new OllamaEmbeddingClient(ollamaApi);
|
||||
embeddingClient.withDefaultOptions(OllamaOptions.create().withModel("nomic-embed-text"));
|
||||
return new SimpleVectorStore(embeddingClient);
|
||||
public SimpleVectorStore vectorStore(@Value("${spring.ai.rag.embed}") String model, OllamaApi ollamaApi, OpenAiApi openAiApi) {
|
||||
if ("nomic-embed-text".equalsIgnoreCase(model)) {
|
||||
OllamaEmbeddingClient embeddingClient = new OllamaEmbeddingClient(ollamaApi);
|
||||
embeddingClient.withDefaultOptions(OllamaOptions.create().withModel("nomic-embed-text"));
|
||||
return new SimpleVectorStore(embeddingClient);
|
||||
} else {
|
||||
OpenAiEmbeddingClient embeddingClient = new OpenAiEmbeddingClient(openAiApi);
|
||||
return new SimpleVectorStore(embeddingClient);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PgVectorStore pgVectorStore(OllamaApi ollamaApi, JdbcTemplate jdbcTemplate) {
|
||||
OllamaEmbeddingClient embeddingClient = new OllamaEmbeddingClient(ollamaApi);
|
||||
embeddingClient.withDefaultOptions(OllamaOptions.create().withModel("nomic-embed-text"));
|
||||
return new PgVectorStore(jdbcTemplate, embeddingClient);
|
||||
public PgVectorStore pgVectorStore(@Value("${spring.ai.rag.embed}") String model, OllamaApi ollamaApi, OpenAiApi openAiApi, JdbcTemplate jdbcTemplate) {
|
||||
if ("nomic-embed-text".equalsIgnoreCase(model)) {
|
||||
OllamaEmbeddingClient embeddingClient = new OllamaEmbeddingClient(ollamaApi);
|
||||
embeddingClient.withDefaultOptions(OllamaOptions.create().withModel("nomic-embed-text"));
|
||||
return new PgVectorStore(jdbcTemplate, embeddingClient);
|
||||
} else {
|
||||
OpenAiEmbeddingClient embeddingClient = new OpenAiEmbeddingClient(openAiApi);
|
||||
return new PgVectorStore(jdbcTemplate, embeddingClient);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,6 +15,12 @@ spring:
|
||||
options:
|
||||
num-batch: 512
|
||||
model: nomic-embed-text
|
||||
openai:
|
||||
base-url: xxx
|
||||
api-key: xxx
|
||||
embedding-model: text-embedding-ada-002
|
||||
rag:
|
||||
embed: nomic-embed-text #nomic-embed-text、text-embedding-ada-002
|
||||
# Redis
|
||||
redis:
|
||||
sdk:
|
||||
|
||||
Reference in New Issue
Block a user