Text vectorization and chunk vectorization

Updated at:

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.

  • For information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.

    2. Restart Windows for the AccessKey pair to take effect.

Dependencies

The following dependencies are required to upload files by using the SDK.

<dependency>
    <groupId>com.aliyun.opensearch</groupId>
    <artifactId>aliyun-sdk-opensearch</artifactId>
    <version>6.0.0</version>
</dependency>
pip install alibabacloud_tea_util 
pip install alibabacloud_opensearch_util
pip install alibabacloud_credentials
V3.4.1 (2021-05-11)
Download URL: https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20230719/mxik/opensearch-sdk-php-release-v3.4.1.zip

Sample code for EmbeddingDoc

For more information about BaseRequest, see Sample code for the Python client

package com.aliyun.opensearch;

import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;

import java.util.HashMap;
import java.util.Map;

public class LLMSearch {
    private static String appName = "Replace with the application name";
    private static String host = "Replace with the API endpoint of the application";
    private static String path = "/apps/AppName/actions/knowledge-embedding";

    public static void main(String[] args) {
      //User identification information
      //Read the AccessKey ID and AccessKey secret from environment variables. You must configure the environment variables before you run the sample code.
      String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
      String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        //ApiReadTimeOut
        OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
        openSearch.setTimeout(62000);

        OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);

        Map<String, String> params = new HashMap<String, String>() {{
            put("format", "full_json");
            put("_POST_BODY", "{\"content\":\"Test text\",\"query\":false}");
        }};
      	try {
            OpenSearchResult openSearchResult = openSearchClient
            .callAndDecodeResult(path, params, "POST");
            System.out.println("RequestID=" + openSearchResult.getTraceInfo().getRequestId());
            System.out.println(openSearchResult.getResult());
        } catch (
            OpenSearchException e) {
            System.out.println("RequestID=" + e.getRequestId());
            System.out.println("ErrorCode=" + e.getCode());
            System.out.println("ErrorMessage=" + e.getMessage());
        } catch (
            OpenSearchClientException e) {
            System.out.println("ErrorMessage=" + e.getMessage());
        }
    }
}
# -*- coding: utf-8 -*-

import time, os
from typing import Dict, Any

from Tea.exceptions import TeaException
from Tea.request import TeaRequest
from alibabacloud_tea_util import models as util_models
from BaseRequest import Config, Client


class LLMSearch:
    def __init__(self, config: Config):
        self.Clients = Client(config=config)
        self.runtime = util_models.RuntimeOptions(
            connect_timeout=10000,
            read_timeout=10000,
            autoretry=False,
            ignore_ssl=False,
            max_idle_conns=50,
            max_attempts=3
        )
        self.header = {}


    def searchDoc(self, app_name: str,body:Dict, query_params: dict={}) -> Dict[str, Any]:
        try:
            response = self.Clients._request(method="POST", pathname=f'/v3/openapi/apps/{app_name}/actions/knowledge-embedding',
                                             query=query_params, headers=self.header, body=body, runtime=self.runtime)
            return response
        except TeaException as e:
            print(e)


if __name__ == "__main__":
    # Configure the unified request endpoint. Remove http:// from the endpoint.
    endpoint = "<endpoint>"

    # The protocol can be set to HTTPS or HTTP.
    endpoint_protocol = "HTTP"

    # User identification information
    # Read the AccessKey ID and AccessKey secret from environment variables.
    # You must configure the environment variables before you run the sample code. For more information, see the "Configure environment variables" step earlier in this document.
    access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID")
    access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

    # The type parameter can be set to sts or access_key for authentication. Default value: access_key. You can use sts to configure RAM-STS authentication.
    # Valid values: sts and access_key
    auth_type = "access_key"

    # If you use RAM-STS authentication, configure security_token. You can call the Alibaba Cloud AssumeRole operation to obtain the required STS authentication information.
    security_token =  "<security_token>"

    # Configure the common information used by requests.
    # If you do not use a RAM user, omit the type and security_token parameters.
    Configs = Config(endpoint=endpoint, access_key_id=access_key_id, access_key_secret=access_key_secret,
                     security_token=security_token, type=auth_type, protocol=endpoint_protocol)

    # Create an OpenSearch instance.
    # Replace <app_name> with the name of the OpenSearch LLM-Based Conversational Search Edition instance that you created.
    ops = LLMSearch(Configs)
    app_name = "<app_name>"

    # --------------- Document search ---------------

    docQuery = {"content":"Test text","query":false}

    res1 = ops.searchDoc(app_name=app_name, body=docQuery)
    print(res1)
<?php
require_once($path . "/OpenSearch/Autoloader/Autoloader.php");

use OpenSearch\Client\OpenSearchClient;

// User identification information
// Read the AccessKey ID and AccessKey secret from environment variables.
// You must configure the environment variables before you run the sample code. For more information, see the "Configure environment variables" step earlier in this document.
// Replace with your AccessKey ID.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
//Replace with your AccessKey secret.
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$endPoint = '<Replace with the endpoint>';
$appName = '<Replace with the application name>';
$options = array('debug' => true);
$requestBody = "{"content":"Test text","query":false}";

$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

$uri = "/apps/{$appName}/actions/knowledge-embedding";

try{
    $ret = $client->post($uri, $requestBody);
    print_r(json_decode($ret->result, true));
}catch (\Throwable $e) {
    print_r($e);
}

Sample code for SplitDoc

package com.aliyun.opensearch;

import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;

import java.util.HashMap;
import java.util.Map;

public class LLMSearch {
    private static String appName = "Replace with the application name";
    private static String host = "Replace with the API endpoint of the application";
    private static String path = "/apps/AppName/actions/knowledge-split";

    public static void main(String[] args) {
      //User identification information
      //Read the AccessKey ID and AccessKey secret from environment variables. You must configure the environment variables before you run the sample code.
      String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
      String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        //ApiReadTimeOut
        OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
        openSearch.setTimeout(62000);

        OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);

        Map<String, String> params = new HashMap<String, String>() {{
            put("format", "full_json");
            put("_POST_BODY", "{"title":"Test title","content":"Test text","use_embedding":true}");
        }};
      	try {
            OpenSearchResult openSearchResult = openSearchClient
            .callAndDecodeResult(path, params, "POST");
            System.out.println("RequestID=" + openSearchResult.getTraceInfo().getRequestId());
            System.out.println(openSearchResult.getResult());
        } catch (
            OpenSearchException e) {
            System.out.println("RequestID=" + e.getRequestId());
            System.out.println("ErrorCode=" + e.getCode());
            System.out.println("ErrorMessage=" + e.getMessage());
        } catch (
            OpenSearchClientException e) {
            System.out.println("ErrorMessage=" + e.getMessage());
        }
    }
}
# -*- coding: utf-8 -*-

import time, os
from typing import Dict, Any

from Tea.exceptions import TeaException
from Tea.request import TeaRequest
from alibabacloud_tea_util import models as util_models
from BaseRequest import Config, Client


class LLMSearch:
    def __init__(self, config: Config):
        self.Clients = Client(config=config)
        self.runtime = util_models.RuntimeOptions(
            connect_timeout=10000,
            read_timeout=10000,
            autoretry=False,
            ignore_ssl=False,
            max_idle_conns=50,
            max_attempts=3
        )
        self.header = {}


    def searchDoc(self, app_name: str,body:Dict, query_params: dict={}) -> Dict[str, Any]:
        try:
            response = self.Clients._request(method="POST", pathname=f'/v3/openapi/apps/{app_name}/actions/knowledge-split',
                                             query=query_params, headers=self.header, body=body, runtime=self.runtime)
            return response
        except TeaException as e:
            print(e)


if __name__ == "__main__":
    # Configure the unified request endpoint. Remove http:// from the endpoint.
    endpoint = "<endpoint>"

    # The protocol can be set to HTTPS or HTTP.
    endpoint_protocol = "HTTP"

    # User identification information
    # Read the AccessKey ID and AccessKey secret from environment variables.
    # You must configure the environment variables before you run the sample code. For more information, see the "Configure environment variables" step earlier in this document.
    access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID")
    access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

    # The type parameter can be set to sts or access_key for authentication. Default value: access_key. You can use sts to configure RAM-STS authentication.
    # Valid values: sts and access_key
    auth_type = "access_key"

    # If you use RAM-STS authentication, configure security_token. You can call the Alibaba Cloud AssumeRole operation to obtain the required STS authentication information.
    security_token =  "<security_token>"

    # Configure the common information used by requests.
    # If you do not use a RAM user, omit the type and security_token parameters.
    Configs = Config(endpoint=endpoint, access_key_id=access_key_id, access_key_secret=access_key_secret,
                     security_token=security_token, type=auth_type, protocol=endpoint_protocol)

    # Create an OpenSearch instance.
    # Replace <app_name> with the name of the OpenSearch LLM-Based Conversational Search Edition instance that you created.
    ops = LLMSearch(Configs)
    app_name = "<app_name>"

    # --------------- Document search ---------------

    docQuery = {"title":"Test title","content":"Test text","use_embedding":true}

    res1 = ops.searchDoc(app_name=app_name, body=docQuery)
    print(res1)
<?php
require_once($path . "/OpenSearch/Autoloader/Autoloader.php");

use OpenSearch\Client\OpenSearchClient;

// User identification information
// Read the AccessKey ID and AccessKey secret from environment variables.
// You must configure the environment variables before you run the sample code. For more information, see the "Configure environment variables" step earlier in this document.
// Replace with your AccessKey ID.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
//Replace with your AccessKey secret.
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$endPoint = '<Replace with the endpoint>';
$appName = '<Replace with the application name>';
$options = array('debug' => true);
$requestBody = "{"title":"Test title","content":"Test text","use_embedding":true}";

$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

$uri = "/apps/{$appName}/actions/knowledge-split";

try{
    $ret = $client->post($uri, $requestBody);
    print_r(json_decode($ret->result, true));
}catch (\Throwable $e) {
    print_r($e);
}