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.
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.
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.