The assistant API supports the retrieval-augmented generation (RAG) tool. This tool enables an agent to retrieve external knowledge, such as private product information or customer preferences. This topic uses a simple "phone shopping guide" example to demonstrate how to use the RAG tool.
Function introduction
In traditional Large Language Model (LLM) applications, the LLM relies only on its pre-trained knowledge. It cannot dynamically retrieve external knowledge. As a result, it cannot answer questions about:
- Timeliness: Events that happened after the LLM was pre-trained. For example, an LLM pre-trained in 2023 cannot answer, "What honors did the Chinese national sports team win in 2024?"
- Private knowledge questions: These are questions about information shared within a limited scope. For example, "What are the company's rules and regulations?" and "Who is responsible for the development of AI products?".
- Long document processing: Retrieving precise information from many documents. For example, "Teach me how to deploy an LLM. Here are a few reference books."
Feature comparison with console RAG applications
Setting | Assistant API RAG application | RAG applications in the Model Studio console |
|---|---|---|
Knowledge base operations | Supported | Supported |
Recall count | Supported | Supported |
Other advanced settings supported by console RAG applications | Not supported | Supported |
Getting started
In this example, you will use a sample knowledge file to create a test knowledge base. You will also build an agent (Assistant) that helps users shop for a new phone.
This example only shows how to create a new knowledge base. For more information about adding files to an existing knowledge base or retrieving, updating, and deleting a knowledge base, see Knowledge Base API Guide.
Before you begin
Before you use the assistant RAG feature, make sure that you have:
- You have registered an Alibaba Cloud account and activated Model Studio.
- Installed the required Python libraries.
- Prepared the document that contains phone information, Model Studio Series Phone Product Introduction.docx, to create the knowledge base.
- In addition, set the following environment variables:
Complete code example
Complete code example
Create a knowledge base
To access knowledge documents, the RAG tool retrieves a knowledge index from an Alibaba Cloud Model Studio knowledge base. Upload your knowledge files to a Model Studio knowledge base and create a knowledge index. You can use one of the following two methods: Upload files and create an index using an API, or Upload files and create an index using the Model Studio console.
A code example is provided to help you quickly set up a knowledge base. You need to prepare:
-
The path of the document to upload (for example, ~/test.pdf).
The current API only supports uploading unstructured data, such as PDF and DOCX files. To upload structured data, use the console.
- The name of the knowledge base to create (for example, Test Knowledge Base).
Step 1: Create an Alibaba Cloud Model Studio client
Before you upload files and create a knowledge base, you must initialize the Alibaba Cloud Model Studio client to complete identity verification and endpoint configuration. You can configure your client as shown in the following example:
Step 2: Request a file upload lease
Before you upload a file, you must request a file upload lease. A lease is a temporary authorization that lets you upload a file to the Alibaba Cloud Model Studio service within a specific time frame. The following code shows an example of how to request a file upload lease:
Step 3: Upload the file to Alibaba Cloud Model Studio
After you obtain the upload lease, use the temporary request header and URL from the lease to upload the file to the Alibaba Cloud Model Studio server.
Step 4: Add the file to the default category
Model Studio uses categories to manage your uploaded files. Therefore, you must add the uploaded file to a category. Model Studio automatically creates a default category (category_id='default'). Use the following method to add the file to the default category.
Step 5: Query the parsing status of the file
You can query the parsing status of the file. After the file is parsed, you can create the knowledge base. The following code shows an example of how to query the parsing status of the file:
Step 6: Create a knowledge base index
You can create a knowledge base index for the file for use with the assistant API.
Step 7: Submit the index task
After you create the index, you must submit an index task so that Alibaba Cloud Model Studio can start building the knowledge base index.
Create an assistant
Step 8: Create an assistant
After the knowledge base is created, you can use it with the assistant API to create an intelligent assistant. The following code shows an example of how to create an assistant:
Manage conversations
Step 9: Submit a message to the assistant
After you create the assistant, you can submit a user message and receive the assistant's reply. The following code shows an example of how to send a message to the assistant:
Step 10: Create a conversation thread and interact
The assistant interacts with users through conversation threads. You can create a new thread for each conversation, send user input, and receive replies. The following code shows how to manage conversation threads and interact with the assistant:
FAQ
- Is there a visual way to create a knowledge base? Can I attach a knowledge base created in the Model Studio console? You can use the Model Studio console to create and use knowledge bases. Knowledge bases created in the console or using an API can be used to build the retrieval-augmented generation (RAG) feature of the assistant API.
- Does the assistant API support calling multiple tools? If an assistant is configured with both RAG and function call tools, will they conflict with each other? Yes, the assistant API supports calling multiple tools, and they will not conflict. The assistant decides whether to call a specific tool. If you configure multiple tools for an assistant, the assistant decides whether to call one or more tools based on the user's input.
- Does the RAG feature of the assistant API support setting assembly policies? Currently, the Assistant API for Retrieval-Augmented Generation (RAG) only supports specifying the number of segments to retrieve from a single knowledge base or the total number of segments to retrieve from multiple knowledge bases. Other advanced features are not yet supported. To use advanced RAG features, such as the "Assembly Strategy", you can use the Model Studio console to create an Alibaba Cloud Model Studio agent application.