Streamlit

Updated at:
Copy as MD

This topic describes how to build an application using Streamlit and the AnalyticDB for PostgreSQL RAG service. This application demonstrates the retrieval-augmented generation (RAG) capabilities of the RAG service in a web interface.

Version requirements

An AnalyticDB for PostgreSQL V6.0 instance.

Prerequisites

  • You have an Alibaba Cloud account or a RAM user with the AliyunGPDBFullAccess and AliyunECSFullAccess policies attached. You have created an AccessKey ID and an AccessKey secret for the account or RAM user. For more information, see RAM users and Create an AccessKey pair.

  • You have created an AnalyticDB for PostgreSQL instance, and an initial account in the instance. For more information, see Create an instance and Create an initial account.

    Important

    You must enable vector engine optimization when you create the instance.

  • You have obtained an API key for Alibaba Cloud Model Studio. For more information about how to obtain an API key, see API keys.

  • You have created an ECS instance with Python 3.9 or a later version installed.

Step 1: Deploy service dependencies

  1. To help you get started, AnalyticDB for PostgreSQL provides sample code. Download and deploy the code to use the features of the knowledge base.

    Remotely connect to the ECS instance and download the code.

    git clone https://github.com/aliyun/adbpg-chat-demo.git  # Obtain the latest adbpg-chat-demo code.
    cd adbpg-chat-demo
  2. Use the venv module of Python to create and activate a virtual environment.

    python -m venv $(pwd)/venv
    source $(pwd)/venv/bin/activate    
  3. Install the dependencies.

    pip install -r requirements.txt

Step 2: Start the service

  1. Before you start the service, configure the environment variables.

    export ADBPG_INSTANCE_ID=gp-bp1q203vj895g**** 
    export ADBPG_INSTANCE_REGION=cn-hangzhou    
    export DASHSCOPE_API_KEY='****************************'      
    export ALI_CLOUD_ACCESS_KEY_ID='LTAI****************'          
    export ALI_CLOUD_ACCESS_KEY_SECRET='yourAccessKeySecret' 
    export DASHSCOPE_LLM_NAME=qwen-max 

    Parameters

    Parameter

    Description

    ADBPG_INSTANCE_ID

    The ID of the AnalyticDB for PostgreSQL instance.

    ADBPG_INSTANCE_REGION

    The region of the AnalyticDB for PostgreSQL instance.

    DASHSCOPE_API_KEY

    The API key for Alibaba Cloud Model Studio.

    ALI_CLOUD_ACCESS_KEY_ID

    The AccessKey ID of your Alibaba Cloud account or RAM user.

    ALI_CLOUD_ACCESS_KEY_SECRET

    The AccessKey secret of your Alibaba Cloud account or RAM user.

    DASHSCOPE_LLM_NAME

    The name of the large language model (LLM). Valid values:

    • qwen-max

    • qwen-plus

    • qwen-turbo

    • qwen-long

  2. Start the service. By default, the service starts on port 8501.

    streamlit run Home.py

Step 3: Access the service

  1. In a local browser, enter http://<ECS Public IP Address>:8501 to access the service.

    Note

    On the details page of the ECS instance, you can find the public IP address under Instance Details > Configuration Information > Elastic IP Address.

  2. Initialize the instance.

    On the Home page, enter the username and password for the initial account of your AnalyticDB for PostgreSQL instance to initialize it. You must initialize the instance before each use. If the initialization is successful, the "Initialization successful" message is displayed.

    After a successful initialization, click Namespace or KnowledgeBase in the sidebar. The following page appears.

    The page is divided into three sections.

    1. The leftmost section is the sidebar, which contains Home, Namespace, KnowledgeBase, and Chat.

      • Namespace: A namespace provides multi-tenant data isolation. You must set a password when you create a namespace to enhance data security. Namespaces support create, read, update, and delete (CRUD) operations.

      • KnowledgeBase: A knowledge base stores documents and belongs to a specific namespace. Knowledge bases support CRUD operations. After you enter a knowledge base, you can upload, download, and view documents, run recall tests, and ask the LLM questions.

      • Chat: The chat interface. This feature uses natural language processing (NLP) to understand user input and generate relevant answers.

    2. The central section displays the details of each feature in the sidebar.

    3. The rightmost section provides sample code for quickly writing and debugging the backend code of the RAG service.

Step 4: Try the chat feature

  1. In the sidebar, click Namespace. In the central section, click Create Namespace.

  2. Enter a Project Name, enter and confirm a Namespace Password, and click OK.

  3. Below the "Creation successful" message, click Enter Knowledge Base. Then, click Create Knowledge Base, enter a Name, and select text-embedding-v2 for EmbeddingModel. Configure the other parameters as needed.

  4. Below the "Creation successful" message, find the knowledge base that you created (foo in this example) and click Go to . In the sidebar, click Upload Document, select the document to upload, and click Upload Document.

    Note
    • The sample document, Connect to Dataiku.pdf, is in PDF format. You must select PDFWithImageRefLoader. If you upload a document in another format, you do not need to select a value for this parameter.

    • The document name must include a file extension, such as .pdf.

  5. After the document is uploaded, click Q & A in the sidebar. In the text box at the bottom, enter your question, such as "How do I connect to AnalyticDB for PostgreSQL in Dataiku?". A relevant answer appears.