diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/README-en.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/README-en.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/README-en.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/README-en.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/images/sca-ai-example-front.gif b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/images/sca-ai-example-front.gif similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/images/sca-ai-example-front.gif rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/images/sca-ai-example-front.gif diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/pom.xml b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/pom.xml similarity index 98% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/pom.xml rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/pom.xml index b6e3651fc..a490f5b3c 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-ai-example/pom.xml +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/pom.xml @@ -24,7 +24,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud ${revision} - ../pom.xml + ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/TongYiApplication.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/TongYiApplication.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/TongYiApplication.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/TongYiApplication.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/controller/TongYiController.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/controller/TongYiController.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/controller/TongYiController.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/controller/TongYiController.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/ActorsFilms.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/ActorsFilms.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/ActorsFilms.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/ActorsFilms.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/Completion.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/Completion.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/Completion.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/models/Completion.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/AbstractTongYiServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/AbstractTongYiServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/AbstractTongYiServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/AbstractTongYiServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/TongYiService.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/TongYiService.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/TongYiService.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/TongYiService.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/TongYiAudioSimpleServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/TongYiAudioSimpleServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/TongYiAudioSimpleServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/audio/TongYiAudioSimpleServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/TongYiSimpleServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/TongYiSimpleServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/TongYiSimpleServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/helloworld/TongYiSimpleServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/TongYiImagesServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/TongYiImagesServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/TongYiImagesServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/images/TongYiImagesServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/TongYiOutputParseServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/TongYiOutputParseServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/TongYiOutputParseServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/output/TongYiOutputParseServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/TongYiPromptTemplateServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/TongYiPromptTemplateServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/TongYiPromptTemplateServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/prompttemplate/TongYiPromptTemplateServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/TongYiRolesServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/TongYiRolesServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/TongYiRolesServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/roles/TongYiRolesServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/README.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/README.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/README.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/TongYiStuffServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/TongYiStuffServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/TongYiStuffServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/stuff/TongYiStuffServiceImpl.java diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/README.md new file mode 100644 index 000000000..0fc5e74da --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/README.md @@ -0,0 +1,22 @@ +# Spring Cloud Alibaba AI Text Embedding + +`TongYiController` 接受一个 HTTP GET 请求 `http://localhost:8080/ai/audio`。 +`controller` 将会调用 `TongYiService` 中的 `genAudio` 方法,完成服务请求得到响应。 + +有一个可选的 `text` 参数,其默认值为“Spring Cloud Alibaba AI 框架!”。 请求响应来自 Alibaba TongYi Text Embedding 服务。 + +## 构建和运行 + +1. 修改配置文件 `application.yml` 中的 apikey 为有效的 apikey; +2. 通过 IDE 或者 `./mvnw spring-boot:run` 运行应用程序。 + +## 访问接口 + +使用 curl 工具或者使用浏览器对接口发起请求: + +```shell +$ curl http://localhost:8080/ai/textEmbedding + +# Response: +为一组向量集合 +``` diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/TongYiTextEmbeddingServiceImpl.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/TongYiTextEmbeddingServiceImpl.java similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/TongYiTextEmbeddingServiceImpl.java rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/java/com/alibaba/cloud/ai/example/tongyi/service/impl/textembedding/TongYiTextEmbeddingServiceImpl.java diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/application.yml similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/application.yml rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/application.yml diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/docs/wikipedia-curling.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/docs/wikipedia-curling.md similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/docs/wikipedia-curling.md rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/docs/wikipedia-curling.md diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/prompts/assistant-message.st b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/prompts/assistant-message.st similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/prompts/assistant-message.st rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/prompts/assistant-message.st diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/prompts/joke-prompt.st b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/prompts/joke-prompt.st similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/prompts/joke-prompt.st rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/prompts/joke-prompt.st diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/prompts/qa-prompt.st b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/prompts/qa-prompt.st similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/prompts/qa-prompt.st rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/prompts/qa-prompt.st diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/static/index.html b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/static/index.html similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/static/index.html rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/static/index.html diff --git a/spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/static/js/marked.min.js b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/static/js/marked.min.js similarity index 100% rename from spring-cloud-alibaba-examples/spring-cloud-ai-example/src/main/resources/static/js/marked.min.js rename to spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-example/src/main/resources/static/js/marked.min.js diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/README-en.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/README-en.md new file mode 100644 index 000000000..19e7b1684 --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/README-en.md @@ -0,0 +1,126 @@ +# Spring Cloud Alibaba AI RAG Example + +This sample describes how to implement a RAG (Retrieval Augmented Generation) application using SCA AI and Spring AI RedisVector Store. + +> RAG is a generative model based on retrieval, which combines retrieval and generation to produce more accurate and diverse texts. +> SCA AI: Spring Cloud Alibaba AI, adapting TongYi LLM big model through Spring AI API. +> Spring AI: The Spring AI project aims to simplify the development of applications that include artificial intelligence features and avoid unnecessary complexity. +> Spring AI RedisVector Store: Redis extends the core functionality of Redis OSS to allow Redis to be used as a vector database. Spring AI provides the RedisVector Store adapter. +> Project Code Address: [spring-cloud-ai-rag-example](https://github.com/alibaba/spring-cloud-alibaba/tree/2023.x/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example) + +## 1. Environmental preparation + +Use Docker Compose to deploy a Redis service to store vector data. + +```yaml +version: '3.8' + +services: + redis: + image: redis/redis-stack-server + container_name: redis + hostname: redis + ports: + - 6379:6379 +``` + +Start with `docker compose up -d`, and then you can `docker ps | grep redis` check to see if the container is running properly. + +## 2. Project dependency + +> This project introduces `spring-cloud-alibaba-ai-starter` and `spring-ai-redis-spring-boot-starter` realizes RAG application. + +You need to introduce the following dependencies in the POM. XML: + +```xml + + com.alibaba.cloud + spring-cloud-starter-alibaba-ai + + + + org.springframework.ai + spring-ai-redis-spring-boot-starter + ${spring.ai.version} + +``` + +## 3. Configuration + +Configure the following information in the application. Yml: + +```yaml +spring: + ai: + vectorstore: + redis: + index: peer + prefix: peer +``` + +## 4. Write the code + +The `loader` classes are as follows: + +```java +@Override +public void run(ApplicationArguments args) throws Exception { + + Map indexInfo = vectorStore.getJedis().ftInfo(properties.getIndex()); + int numDocs = Integer.parseInt((String) indexInfo.getOrDefault("num_docs", "0")); + if (numDocs > 20000) { + logger.info("Embeddings already loaded. Skipping"); + return; + } + + Resource file = data; + if (Objects.requireNonNull(data.getFilename()).endsWith(".gz")) { + GZIPInputStream inputStream = new GZIPInputStream(data.getInputStream()); + file = new InputStreamResource(inputStream, "beers.json.gz"); + } + + logger.info("Creating Embeddings..."); + JsonReader loader = new JsonReader(file, KEYS); + vectorStore.add(loader.get()); + logger.info("Embeddings created."); +} +``` + +The `Service` classes are as follows: + +```java +public Generation retrieve(String message) { + + SearchRequest request = SearchRequest.query(message).withTopK(topK); + List docs = store.similaritySearch(request); + + Message systemMessage = getSystemMessage(docs); + UserMessage userMessage = new UserMessage(message); + + Prompt prompt = new Prompt(List.of(systemMessage, userMessage)); + ChatResponse response = client.call(prompt); + + return response.getResult(); +} + +private Message getSystemMessage(List similarDocuments) { + + String documents = similarDocuments.stream() + .map(Document::getContent) + .collect(Collectors.joining("\n")); + SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemBeerPrompt); + + return systemPromptTemplate.createMessage(Map.of("documents", documents)); +} +``` + +## 5. Run and verify + +You can start the SprigBoot main class and then use a browser to access: + +```shell +# request params is prompt,the default value:What ber pairs well with smoked meats?" +http://localhost:8080/rag/chat +``` + +To experience the RAG application. diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/README.md b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/README.md new file mode 100644 index 000000000..b5e45d4a8 --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/README.md @@ -0,0 +1,126 @@ +# Spring Cloud Alibaba AI RAG Example + +本示例介绍如何使用 SCA AI 和 Spring AI RedisVectorStore 实现 RAG(Retrieval Augmented Generation)应用。 + +> RAG 是一个基于检索的生成模型,它将检索和生成结合在一起,以生成更加准确和多样化的文本。 +> SCA AI: Spring Cloud Alibaba AI, 通过 Spring AI API 适配 TongYi LLM 大模型。 +> Spring AI: Spring AI项目旨在简化包含人工智能功能的应用程序的开发,避免不必要的复杂性。 +> Spring AI RedisVectorStore: Redis 扩展了 Redis OSS 的核心功能,允许将 Redis 用作矢量数据库,Spring AI 提供了 RedisVectorStore 适配器。 +> 项目代码地址:[spring-cloud-ai-rag-example](https://github.com/alibaba/spring-cloud-alibaba/tree/2023.x/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example) + +## 1. 环境准备 + +使用 Docker Compose 部署一个 Redis 服务,用于存储向量数据。 + +```yaml +version: '3.8' + +services: + redis: + image: redis/redis-stack-server + container_name: redis + hostname: redis + ports: + - 6379:6379 +``` + +使用 `docker compose up -d` 启动,然后您可以通过 `docker ps | grep redis` 查看容器是否正常运行。 + +## 2. 项目依赖 + +> 本项目通过引入 `spring-cloud-alibaba-ai-starter` 和 `spring-ai-redis-spring-boot-starter` 实现 RAG 应用。 + +您需要在 pom.xml 中引入如下依赖: + +```xml + + com.alibaba.cloud + spring-cloud-starter-alibaba-ai + + + + org.springframework.ai + spring-ai-redis-spring-boot-starter + ${spring.ai.version} + +``` + +## 3. 配置 + +在 application.yml 中配置如下信息: + +```yaml +spring: + ai: + vectorstore: + redis: + index: peer + prefix: peer +``` + +## 4. 编写代码 + +`loader` 类如下所示: + +```java +@Override +public void run(ApplicationArguments args) throws Exception { + + Map indexInfo = vectorStore.getJedis().ftInfo(properties.getIndex()); + int numDocs = Integer.parseInt((String) indexInfo.getOrDefault("num_docs", "0")); + if (numDocs > 20000) { + logger.info("Embeddings already loaded. Skipping"); + return; + } + + Resource file = data; + if (Objects.requireNonNull(data.getFilename()).endsWith(".gz")) { + GZIPInputStream inputStream = new GZIPInputStream(data.getInputStream()); + file = new InputStreamResource(inputStream, "beers.json.gz"); + } + + logger.info("Creating Embeddings..."); + JsonReader loader = new JsonReader(file, KEYS); + vectorStore.add(loader.get()); + logger.info("Embeddings created."); +} +``` + +`Service` 类如下所示: + +```java +public Generation retrieve(String message) { + + SearchRequest request = SearchRequest.query(message).withTopK(topK); + List docs = store.similaritySearch(request); + + Message systemMessage = getSystemMessage(docs); + UserMessage userMessage = new UserMessage(message); + + Prompt prompt = new Prompt(List.of(systemMessage, userMessage)); + ChatResponse response = client.call(prompt); + + return response.getResult(); +} + +private Message getSystemMessage(List similarDocuments) { + + String documents = similarDocuments.stream() + .map(Document::getContent) + .collect(Collectors.joining("\n")); + SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemBeerPrompt); + + return systemPromptTemplate.createMessage(Map.of("documents", documents)); +} +``` + +## 5. 运行并验证 + +您可以通过启动 SprigBoot 主类,之后使用浏览器访问: + +```shell +# 参数为 prompt,默认值为:What ber pairs well with smoked meats?" +http://localhost:8080/rag/chat +``` + +来体验 RAG 应用。 diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/docker/docker-compose.yml b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/docker/docker-compose.yml new file mode 100644 index 000000000..3dbd05225 --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/docker/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.8' + +services: + redis: + image: redis/redis-stack-server + container_name: redis + hostname: redis + ports: + - 6379:6379 diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/pom.xml b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/pom.xml new file mode 100644 index 000000000..486968440 --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/pom.xml @@ -0,0 +1,104 @@ + + + + + + + + spring-cloud-alibaba-examples + com.alibaba.cloud + ${revision} + ../../pom.xml + + 4.0.0 + + spring-cloud-ai-rag-example + Spring Cloud Starter Alibaba AI RAG Example + Example build RAG Application By Spring Cloud Alibaba AI + jar + + + 0.8.1 + 5.1.0 + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-ai + + + + org.springframework.ai + spring-ai-redis-spring-boot-starter + ${spring.ai.version} + + + logback-classic + ch.qos.logback + + + + + + redis.clients + jedis + ${redis.jedis.version} + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + + + diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/RAGApplication.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/RAGApplication.java new file mode 100644 index 000000000..90a64268b --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/RAGApplication.java @@ -0,0 +1,41 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.example.ai.rag; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author yuluo + * @author yuluo + */ + +@SpringBootApplication +public class RAGApplication { + + private static final Logger logger = LoggerFactory.getLogger(RAGApplication.class); + + public static void main(String[] args) { + + SpringApplication.run(RAGApplication.class, args); + logger.info("RAGApplication started successfully."); + } + +} diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/controller/RAGController.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/controller/RAGController.java new file mode 100644 index 000000000..aed67ecba --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/controller/RAGController.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.example.ai.rag.controller; + +import com.alibaba.cloud.example.ai.rag.service.RAGService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author yuluo + * @author yuluo + */ + +@CrossOrigin +@RestController +@RequestMapping("/rag") +public class RAGController { + + @Autowired + private RAGService ragService; + + @GetMapping("/chat") + public String chatMessage(@RequestParam(value = "prompt", + defaultValue = "What ber pairs well with smoked meats?") String prompt) { + + return ragService.retrieve(prompt) + .getOutput() + .getContent(); + } + +} diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/loader/RAGDataLoader.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/loader/RAGDataLoader.java new file mode 100644 index 000000000..034af9628 --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/loader/RAGDataLoader.java @@ -0,0 +1,83 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.example.ai.rag.loader; + +import java.util.Map; +import java.util.Objects; +import java.util.zip.GZIPInputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.ai.autoconfigure.vectorstore.redis.RedisVectorStoreProperties; +import org.springframework.ai.reader.JsonReader; +import org.springframework.ai.vectorstore.RedisVectorStore; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Component; + +/** + * @author yuluo + * @author yuluo + */ + +@Component +public class RAGDataLoader implements ApplicationRunner { + + private static final Logger logger = LoggerFactory.getLogger(RAGDataLoader.class); + + private static final String[] KEYS = { "name", "abv", "ibu", "description" }; + + @Value("classpath:/data/beers.json.gz") + private Resource data; + + private final RedisVectorStore vectorStore; + + private final RedisVectorStoreProperties properties; + + public RAGDataLoader(RedisVectorStore vectorStore, RedisVectorStoreProperties properties) { + + this.vectorStore = vectorStore; + this.properties = properties; + } + + @Override + public void run(ApplicationArguments args) throws Exception { + + Map indexInfo = vectorStore.getJedis().ftInfo(properties.getIndex()); + int numDocs = Integer.parseInt((String) indexInfo.getOrDefault("num_docs", "0")); + if (numDocs > 20000) { + logger.info("Embeddings already loaded. Skipping"); + return; + } + + Resource file = data; + if (Objects.requireNonNull(data.getFilename()).endsWith(".gz")) { + GZIPInputStream inputStream = new GZIPInputStream(data.getInputStream()); + file = new InputStreamResource(inputStream, "beers.json.gz"); + } + + logger.info("Creating Embeddings..."); + JsonReader loader = new JsonReader(file, KEYS); + vectorStore.add(loader.get()); + logger.info("Embeddings created."); + } + +} diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/service/RAGService.java b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/service/RAGService.java new file mode 100644 index 000000000..93bfb9d6c --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/java/com/alibaba/cloud/example/ai/rag/service/RAGService.java @@ -0,0 +1,85 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.example.ai.rag.service; + +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.messages.Message; +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.chat.prompt.SystemPromptTemplate; +import org.springframework.ai.document.Document; +import org.springframework.ai.vectorstore.SearchRequest; +import org.springframework.ai.vectorstore.VectorStore; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Service; + +/** + * @author yuluo + * @author yuluo + */ + +@Service +public class RAGService { + + @Value("classpath:/prompts/system-qa.st") + private Resource systemBeerPrompt; + + @Value("${topk:10}") + private int topK; + + private final ChatClient client; + + private final VectorStore store; + + public RAGService(ChatClient client, VectorStore store) { + + this.client = client; + this.store = store; + } + + public Generation retrieve(String message) { + + SearchRequest request = SearchRequest.query(message).withTopK(topK); + List docs = store.similaritySearch(request); + + Message systemMessage = getSystemMessage(docs); + UserMessage userMessage = new UserMessage(message); + + Prompt prompt = new Prompt(List.of(systemMessage, userMessage)); + ChatResponse response = client.call(prompt); + + return response.getResult(); + } + + private Message getSystemMessage(List similarDocuments) { + + String documents = similarDocuments.stream() + .map(Document::getContent) + .collect(Collectors.joining("\n")); + SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemBeerPrompt); + + return systemPromptTemplate.createMessage(Map.of("documents", documents)); + } + +} diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/application.yml new file mode 100644 index 000000000..b47055776 --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/application.yml @@ -0,0 +1,28 @@ +# +# Copyright 2023-2024 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +server: + port: 8081 + +spring: + application: + name: sca-ai-rag-example + + ai: + vectorstore: + redis: + index: peer + prefix: peer diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/data/beers.json.gz b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/data/beers.json.gz new file mode 100644 index 000000000..e32d6b028 Binary files /dev/null and b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/data/beers.json.gz differ diff --git a/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/prompts/system-qa.st b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/prompts/system-qa.st new file mode 100644 index 000000000..323908d7c --- /dev/null +++ b/spring-cloud-alibaba-examples/ai-example/spring-cloud-ai-rag-example/src/main/resources/prompts/system-qa.st @@ -0,0 +1,7 @@ +You're assisting with questions about products in a beer catalog. +Use the information from the DOCUMENTS section to provide accurate answers. +The answer involves referring to the ABV or IBU of the beer, include the beer name in the response. +If unsure, simply state that you don't know. + +DOCUMENTS: +{documents} diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 37b7e9c1f..8f39d79c6 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -52,7 +52,8 @@ integrated-example/integrated-praise-consumer integrated-example/integrated-common integrated-example/integrated-frontend - spring-cloud-ai-example + ai-example/spring-cloud-ai-example + ai-example/spring-cloud-ai-rag-example diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-ai/src/main/java/com/alibaba/cloud/ai/tongyi/TongYiAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-ai/src/main/java/com/alibaba/cloud/ai/tongyi/TongYiAutoConfiguration.java index 43075efc9..ab4cd7e0f 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-ai/src/main/java/com/alibaba/cloud/ai/tongyi/TongYiAutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-ai/src/main/java/com/alibaba/cloud/ai/tongyi/TongYiAutoConfiguration.java @@ -64,7 +64,6 @@ TongYiImagesProperties.class, TongYiAudioSpeechProperties.class, TongYiConnectionProperties.class, - TongYiConnectionProperties.class, TongYiTextEmbeddingProperties.class }) public class TongYiAutoConfiguration {