Bu eğitimde, vektör veritabanı olarak Platform for AI (PAI) Elastic Algorithm Service (EAS) ve ApsaraDB RDS for PostgreSQL kullanarak Alım Artırılmış Üretim (RAG) tabanlı büyük dil modeli (LLM) sohbet botunun nasıl oluşturulacağı gösterilmektedir.
Bu eğitimde şunları yaparsınız:
ApsaraDB RDS for PostgreSQL örneğinde bir vektör veritabanı ayarlayın
EAS konsolu aracılığıyla RAG tabanlı bir sohbet botu dağıtın
Alım, LLM ve RAG sorgu modlarıyla web kullanıcı arayüzünü kullanarak sohbet botunu test edin
cURL ve Python kullanarak sohbet botu API'sini çağırın
Nasıl çalışır?
RAG, alımı üretimle birleştirerek bağımsız LLM uygulamalarının doğruluk sınırlamalarını ele alır. Bir sorgu geldiğinde sistem, vektör veritabanından ilgili belgeleri alır, bunları isteme ekler ve zenginleştirilmiş istemi LLM'ye gönderir; böylece yalnızca modelin eğitim verilerine değil, özel bilgi tabanınıza dayalı yanıtlar üretilir. Modelin yeniden eğitilmesine gerek yoktur.
Bu mimaride ApsaraDB RDS for PostgreSQL, vektör veritabanı olarak hizmet verir. pg_jieba uzantısı, Çince metinler için anahtar kelime tabanlı alım ve geri çağırma özelliğini etkinleştirir.
Ön koşullar
Başlamadan önce şunlara sahip olduğunuzdan emin olun:
Bir Virtual Private Cloud (VPC), vSwitch ve güvenlik grubu. Daha fazla bilgi için bkz. VPC oluşturma ve yönetme ve Güvenlik grubu oluşturma
(İsteğe bağlı) Özel ince ayarlı bir model kullanmayı planlıyorsanız bir Object Storage Service (OSS) paketi veya Apsara File Storage NAS (NAS) dosya sistemi. Daha fazla bilgi için bkz. OSS konsolunu kullanarak başlayın veya Dosya sistemi oluşturma
Vektör veritabanını oluşturmak için Faiss kullanıyorsanız bir OSS paketi gereklidir.
Sınırlamalar
ApsaraDB RDS for PostgreSQL örneği ve EAS aynı bölgede bulunmalıdır.
Sohbet botu, LLM hizmetinin belirteç sınırıyla kısıtlıdır. Uzun konuşmalar bu sınıra ulaşabilir. Tek turlu senaryolarda sınıra ulaşma olasılığını azaltmak için web kullanıcı arayüzünde Chat history seçeneğini devre dışı bırakın. Ayrıntılar için bkz. Sohbet geçmişini devre dışı bırakma.
1. Adım: Vektör veritabanını ayarlama
Bir ApsaraDB RDS for PostgreSQL örneği oluşturun. VPC dahili bağlantıyı etkinleştirmek için bunu planlanan EAS dağıtımıyla aynı bölgeye yerleştirin. Daha fazla bilgi için bkz. Örnek oluşturma.
-
Örnek için yetkili bir hesap ve bir veritabanı oluşturun. Daha fazla bilgi için bkz. Veritabanı ve hesap oluşturma.
Account Type değerini Privileged Account olarak ayarlayın.
Veritabanını oluştururken Authorized By açılır listesinden yetkili hesabı seçin.
-
Veritabanı bağlantı ayrıntılarını alın.
Go to the Instances page. In the top navigation bar, select the region where your instance resides, then click the instance ID.
Sol gezinme bölmesinde Database Connection seçeneğine tıklayın.
Uç noktayı ve bağlantı noktası numarasını not edin. Sohbet botunu dağıtırken bunlara ihtiyacınız olacaktır.
-
Add
pg_jiebato theshared_preload_librariesparameter. In the instance parameters page, findshared_preload_librariesand addpg_jiebato Running Parameter Value — for example:'pg_stat_statements,auto_explain,pg_cron'. For more information, see Modify the parameters of an ApsaraDB RDS for PostgreSQL instance.The pg_jieba extension segments Chinese text for keyword-based retrieval and recall. For more information, see Use the pg_jieba extension .
2. Adım: RAG tabanlı sohbet botunu dağıtma
Log on to the Platform for AI (PAI) console.
In the left-side navigation pane, click Workspaces. On the Workspaces page, find your workspace and click its name. If no workspace exists, create one. For more information, see Create a workspace.
In the left-side navigation pane, choose Model Deployment > Elastic Algorithm Service (EAS).
On the Elastic Algorithm Service (EAS) page, click Deploy Service. In the Scenario-based Model Deployment section, click RAG-based Smart Dialogue Deployment.
-
On the RAG-based LLM Chatbot Deployment page, configure the parameters.
Basic information
Parameter Description Service Name The name of the service. Model Source The model source. Valid values: Open Source Model and Custom Fine-tuned Model. Model Type The model type. Select based on your requirements. If Model Source is Custom Fine-tuned Model, also configure the parameter quantity and precision. Model Settings Required when Model Source is Custom Fine-tuned Model. Specify where the fine-tuned model file is stored. The model file format must be compatible with Hugging Face Transformers. Valid values: Mount OSS (select the OSS path) and Mount NAS (select the NAS file system and source path). Resource configuration
Parameter Description Resource Configuration If Model Source is Open Source Model, the system selects an instance type automatically. If Model Source is Custom Fine-tuned Model, select an instance type that matches your model. For more information, see Deploy LLM applications in EAS. Inference Acceleration Available for the Qwen, Llama2, ChatGLM, or Baichuan2 model on A10 or GU30 instances. Options: BladeLLM Inference Acceleration (high concurrency and low latency) and Open-source vLLM Inference Acceleration. Vektör veritabanı ayarları
Parameter Description Vector Database Type Select RDS PostgreSQL. Host Address The internal or public endpoint of the RDS instance. Use the internal endpoint when the RAG application and the database are in the same region. If they are in different regions, use the public endpoint — see Apply for or release a public endpoint. Port Default: 5432. Database The name of the database you created in Step 1. Table Name A new or existing table name. If you use an existing table, its schema must be compatible with the RAG-based LLM chatbot format. Account The privileged account of the RDS instance. Password The password of the privileged account. VPC yapılandırması
Parameter Description VPC If Host Address is an internal endpoint, select the VPC of the RDS instance. If Host Address is a public endpoint, configure a VPC and vSwitch, then create a NAT gateway and an elastic IP address (EIP) for internet access. Add the EIP to the IP address whitelist of the RDS instance. For more information, see Use the SNAT feature of an Internet NAT gateway to access the Internet and Configure an IP address whitelist. vSwitch Security Group Name The security group. Do not use the security group named created_by_rds— it is reserved for system access control. Click Deploy. When the Service Status column shows Running, the chatbot is deployed.
3. Adım: Web kullanıcı arayüzü ile test etme
Uygulamanıza entegre etmeden önce sohbet botunun performansını doğrulamak için yerleşik web kullanıcı arayüzünü kullanın.
Sohbet botunu yapılandırma
On the EAS page, click View Web App in the Service Type column.
-
Set the embedding model:
Embedding Model Name: Four models are available. The optimal model is selected by default.
Embedding Dimension: Auto-configured after you select an Embedding Model Name.
Click Connect PostgreSQL to verify the connection to the RDS vector database. The connection settings come from the deployment configuration and cannot be modified here.
Bilgi tabanı dosyalarını yükleme
On the Upload tab, upload your business data files.
Desteklenen dosya biçimleri: TXT, PDF, XLSX, XLS, CSV, DOCX, DOC, Markdown ve HTML.
-
Belgelerin nasıl bölüneceğini kontrol etmek için parçalama parametrelerini yapılandırın.
Parameter Description Default Chunk Size The size of each chunk, in bytes. 500 Chunk Overlap The overlap between adjacent chunks. 10 Process with QA Extraction Model When set to Yes, the system extracts question-answer pairs from uploaded files, improving retrieval precision. — Upload files on the Files tab, or upload a directory on the Directory tab.
-
The system runs data cleansing (text extraction and hyperlink replacement) and semantic-based chunking before storing the data.
After the files are uploaded,
completedis displayed in the Status column of the Upload Status Info table andUpload success.is displayed in the Upload Status section, indicating that the files are uploaded successfully.
Çıkarım parametrelerini yapılandırma
On the Chat tab, configure retrieval and generation parameters.
Alım tabanlı sorgu ayarları
At the top of the configuration panel on the left side of the Chat tab, you can select Retrieval, LLM, or RAG (Retrieval + LLM) as the query mode.
| Parameter | Description |
|---|---|
| Streaming Output | Return results in streaming mode. |
| Retrieval Model | The retrieval method. Embedding Only: vector database-based retrieval. Keyword Only: keyword-based retrieval. Hybrid: combines both methods. In most scenarios, vector-based retrieval delivers better results. For corpora where precise keyword matching matters, Keyword Only or Hybrid may perform better. ApsaraDB RDS for PostgreSQL uses pg_jieba for Chinese text segmentation. For more information, see Use the pg_jieba extension. |
| Reranker Type | Apply a second-pass ranking model to improve result precision. You can use the simple-weighted-reranker or model-based-reranker to perform a higher-precision re-rank operation on the top K results. Not
If you use a model for the first time, you may need to wait for a period of time before the model is loaded. |
| Top K | The number of top results to retrieve from the vector database. |
| Similarity Score Threshold | The minimum similarity score for a result to be returned. A higher value returns fewer but more relevant results. |
RAG sorgu ayarları (alım + LLM)
The predefined prompt template types include Simple, General, Extract URL, Accurate Content, and Custom.
Select a predefined prompt template or define a custom one. You can configure Streaming Output, Retrieval Model, and Reranker Type in this mode as well. For more information on available prompt policies, see RAG-based LLM chatbot.
Sorguları çalıştırma
Sohbet botu üç sorgu modunu destekler:
Retrieval — Vektör veritabanından en alakalı K sonucu döndürür.
On the Chat tab, select Retrieval as the query mode in the left-side panel, enter a question in the input box (for example, What is Machine Learning Platform for AI?), and then click Submit.
LLM — Yanıt oluşturmak için yalnızca LLM uygulamasını kullanır.
On the Chat tab, select LLM as the query mode, enter a question in the input box at the bottom (for example, What is Machine Learning Platform for AI?), and then click Submit to send the request.
RAG (alım + LLM) — İlgili belgeleri alır, seçilen istem şablonuna ekler ve birleştirilmiş girişi LLM'ye gönderir.
On the Chat tab, select RAG (Retrieval + LLM) as the query mode in the left-side panel, select a prompt template type such as Custom, enter a question in the input box below (for example, "What is Machine Learning Platform for AI?"), and then click Submit.
4. Adım: API'yi çağırma
Uç noktayı ve belirteci alma
Click the chatbot service name to open the Service Details page.
In the Basic Information section, click View Endpoint Information.
On the Public Endpoint tab of the Invocation Method dialog, copy the service endpoint and token.
Bilgi tabanı dosyalarını yükleme
Connect to the vector database and upload knowledge base files before calling the API. Alternatively, populate the vector database directly using a table that conforms to the PAI-RAG schema.
API'yi çağırma
Sohbet botu, her bir sorgu modu için bir tane olmak üzere üç uç nokta sunar:
| Mode | Endpoint |
|---|---|
| Retrieval | service/query/retrieval |
| LLM | service/query/llm |
| RAG (retrieval + LLM) | service/query |
Replace <service_url> and <service_token> with the values from the previous step. Remove the trailing slash (/) from the service URL.
cURL
Tek turlu istekler
# Retrieval mode
curl -X 'POST' '<service_url>service/query/retrieval' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What is PAI?"}'
# LLM mode (supports optional parameters such as temperature)
curl -X 'POST' '<service_url>service/query/llm' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What is PAI?", "temperature": 0.9}'
# RAG mode (retrieval + LLM)
curl -X 'POST' '<service_url>service/query' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What is PAI?"}'
Çok turlu istekler
Multi-turn conversations are supported in RAG and LLM modes. Use session_id to maintain conversation state across requests, or pass chat_history explicitly.
# Round 1: send the first question and get a session_id in the response
curl -X 'POST' '<service_url>service/query' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What is PAI?"}'
# Round 2: include the session_id to continue the conversation
curl -X 'POST' '<service_url>service/query' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What are the benefits of PAI?", "session_id": "ed7a80e2e20442eab****"}'
# Alternative: pass chat_history directly as a list of {user, bot} pairs
curl -X 'POST' '<service_url>service/query' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What are the features of PAI?", "chat_history": [{"user": "What is PAI", "bot": "PAI is an AI platform provided by Alibaba Cloud..."}]}'
# When both session_id and chat_history are provided, the chat_history is appended to the session
curl -X 'POST' '<service_url>service/query' \
-H 'Authorization: <service_token>' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"question": "What are the features of PAI?", "chat_history": [{"user": "What is PAI", "bot": "PAI is an AI platform provided by Alibaba Cloud..."}], "session_id": "1702ffxxad3xxx6fxxx97daf7c"}'
Python
Tek turlu istekler
import requests
EAS_URL = 'http://xxxx.****.cn-beijing.pai-eas.aliyuncs.com' # Remove trailing /
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'MDA5NmJkNzkyMGM1Zj****YzM4M2YwMDUzZTdiZmI5YzljYjZmNA==',
}
def test_post_api_query_llm():
url = EAS_URL + '/service/query/llm'
data = {"question": "What is PAI?"}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response.status_code}')
ans = dict(response.json())
print(f"======= Question =======\n {data['question']}")
print(f"======= Answer =======\n {ans['answer']} \n\n")
def test_post_api_query_retrieval():
url = EAS_URL + '/service/query/retrieval'
data = {"question": "What is PAI?"}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response.status_code}')
ans = dict(response.json())
print(f"======= Question =======\n {data['question']}")
print(f"======= Answer =======\n {ans['docs']}\n\n")
def test_post_api_query_rag():
url = EAS_URL + '/service/query'
data = {"question": "What is PAI?"}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response.status_code}')
ans = dict(response.json())
print(f"======= Question =======\n {data['question']}")
print(f"======= Answer =======\n {ans['answer']}")
print(f"======= Retrieved Docs =======\n {ans['docs']}\n\n")
# LLM mode
test_post_api_query_llm()
# Retrieval mode
test_post_api_query_retrieval()
# RAG mode (retrieval + LLM)
test_post_api_query_rag()
Çok turlu istekler
Multi-turn conversations are supported in LLM and RAG modes. Pass the session_id from the previous response to continue a conversation.
import requests
EAS_URL = 'http://xxxx.****.cn-beijing.pai-eas.aliyuncs.com' # Remove trailing /
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'MDA5NmJkN****jNlMDgzYzM4M2YwMDUzZTdiZmI5YzljYjZmNA==',
}
def test_post_api_query_llm_with_chat_history():
url = EAS_URL + '/service/query/llm'
# Round 1
data = {"question": "What is PAI?"}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response.status_code}')
ans = dict(response.json())
print(f"=======Round 1: Question =======\n {data['question']}")
print(f"=======Round 1: Answer =======\n {ans['answer']} session_id: {ans['session_id']} \n")
# Round 2: use the session_id from Round 1
data_2 = {
"question": "What are the benefits of PAI?",
"session_id": ans['session_id']
}
response_2 = requests.post(url, headers=headers, json=data_2)
if response_2.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response_2.status_code}')
ans_2 = dict(response_2.json())
print(f"=======Round 2: Question =======\n {data_2['question']}")
print(f"=======Round 2: Answer =======\n {ans_2['answer']} session_id: {ans_2['session_id']} \n\n")
def test_post_api_query_rag_with_chat_history():
url = EAS_URL + '/service/query'
# Round 1
data = {"question": "What is PAI?"}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response.status_code}')
ans = dict(response.json())
print(f"=======Round 1: Question =======\n {data['question']}")
print(f"=======Round 1: Answer =======\n {ans['answer']} session_id: {ans['session_id']}")
print(f"=======Round 1: Retrieved Docs =======\n {ans['docs']}\n")
# Round 2: use the session_id from Round 1
data = {
"question": "What are the features of PAI?",
"session_id": ans['session_id']
}
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
raise ValueError(f'Error post to {url}, code: {response.status_code}')
ans = dict(response.json())
print(f"=======Round 2: Question =======\n {data['question']}")
print(f"=======Round 2: Answer =======\n {ans['answer']} session_id: {ans['session_id']}")
print(f"=======Round 2: Retrieved Docs =======\n {ans['docs']}")
# LLM mode with chat history
test_post_api_query_llm_with_chat_history()
# RAG mode with chat history
test_post_api_query_rag_with_chat_history()
Bilgi tabanı içeriğini görüntüleme
After the chatbot is running, connect to the RDS PostgreSQL database to inspect the imported knowledge base content directly. For connection instructions, see Connect to an ApsaraDB RDS for PostgreSQL instance.
Vektör veritabanındaki dizin tablosu dört sütun içerir: id, text, metadata_ ve node_id. id sütunu otomatik artan bir tam sayıdır ve node_id sütunu uzun bir karma dizesidir.
SSS
Sohbet geçmişini devre dışı bırakma
To disable multi-turn conversation history on the web UI, clear the Chat history checkbox.
Sonraki adımlar
Deploy a standalone LLM: Deploy an LLM application callable via web UI or API, then integrate your enterprise knowledge base using the LangChain framework. See Quickly deploy LLMs in EAS.
Generate AI video: Deploy an AI video generation service using ComfyUI and Stable Video Diffusion. See Use ComfyUI to deploy an AI video generation model service.