Retrieve an Alibaba Cloud Model Studio knowledge base using Spring AI Alibaba

更新时间:
复制 MD 格式

Scope

  • JDK version: JDK 17 or later

  • Spring Boot version: Spring Boot 3 GA or later

Procedure

1. Get the project code

For the complete sample code, see the bailian-rag-knowledge project in the Spring AI Alibaba project examples. Download the entire examples folder to your local machine to ensure the project structure and dependencies are complete.

2. Configure the environment

Obtain the API key for Alibaba Cloud Model Studio and configure it as an environment variable named AI_DASHSCOPE_API_KEY. Using an environment variable helps you avoid the security risks of hard coding.

To perform operations on a knowledge base in a sub-workspace, you also need to obtain the workspace ID and configure it as an environment variable named AI_DASHSCOPE_WORKSPACE_ID.
# Configuration file path for bailian-rag-knowledge: bailian-rag-knowledge/src/main/resources/application.yml
spring:
  ai:
    dashscope:
      api-key: ${AI_DASHSCOPE_API_KEY} # API key for Alibaba Cloud Model Studio
      # workspace-id: ${AI_DASHSCOPE_WORKSPACE_ID} # Workspace ID (optional). You do not need to configure this when retrieving the default workspace's knowledge base.

3. Sample code

You can use DashScopeApi to retrieve the Alibaba Cloud Model Studio knowledge base.

Controller sample code

import org.springframework.web.bind.annotation.*;

import com.alibaba.cloud.ai.example.rag.knowledge.service.RagService;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
public class CloudRagController {
    
    /** Inject CloudRagService */
    private final RagService cloudRagService;
    
    public CloudRagController(RagService cloudRagService) {
        this.cloudRagService = cloudRagService;
    }
    
    @GetMapping(value="/bailian/knowledge/generate", produces="text/event-stream")
    public Flux<String> generate(@RequestParam(value = "message",
                    defaultValue = "Hello, what are the main topics of your knowledge base documents?") String message) {
        return cloudRagService.retrieve(message).map(x -> x.getResult().getOutput().getContent());
    }
}

When a user asks a question, DashScopeDocumentRetriever retrieves the most relevant text segments. It then submits these segments along with the original question to the large language model (LLM), which is qwen-max by default, to generate an answer.

Service sample code

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.*;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service()
public class CloudRagService implements RagService {
    /** The value of INDEX_NAME here is the name of the knowledge base to retrieve. The knowledge base must be created in advance. */
    private static final String INDEX_NAME = "TestKnowledgeBase";
    
    /** Prompt template */
    private static final String retrievalSystemTemplate = """
        Here is some context information. 
        --------------------- 
        {question_answer_context} 
        --------------------- 
        Use only the context provided, not your prior knowledge, to answer the user's question. If the context does not contain the answer, state that you cannot answer the question.
        """; 

    private final ChatClient chatClient;
    
    private final DashScopeApi dashscopeApi;
    
    public CloudRagService(ChatClient.Builder builder, DashScopeApi dashscopeApi) {
            // Create a DocumentRetriever to retrieve the knowledge base.
            DocumentRetriever retriever = new DashScopeDocumentRetriever(dashscopeApi,
            DashScopeDocumentRetrieverOptions.builder().withIndexName(INDEX_NAME).build());
        
            this.dashscopeApi = dashscopeApi;
            // Initialize ChatClient. Set the knowledge base to retrieve and the model to call.
            this.chatClient = builder
                .defaultAdvisors(new DocumentRetrievalAdvisor(retriever, retrievalSystemTemplate))
                // The model defaults to qwen-max. You can set a different model using the following code.
                //.defaultOptions(DashScopeChatOptions.builder().withModel("qwen-plus").build())
                .build();
    }

    @Override
    public Flux<ChatResponse> retrieve(String message) {
        return chatClient.prompt().user(message).stream().chatResponse();
    }
}

Learn more

  • Spring AI Alibaba: Provides documentation, tutorials, hands-on blogs, and a developer community to help you quickly develop generative AI applications in Java.

  • Create and use a knowledge base: Learn more about the core features and best practices of Alibaba Cloud Model Studio knowledge bases.