All Products
Search
Document Center

OpenSearch:Demo code for implementing the search feature

Last Updated:Nov 28, 2024

This topic describes how to run a Q&A test by using OpenSearch SDK.

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

To use OpenSearch SDK to upload files, you must specify the following dependencies:

<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

Demo code

For 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 your application name";
    private static String host = "Replace with your API endpoint";
    private static String path = "/apps/AppName/actions/knowledge-search";

    public static void main(String[] args) {
      // User identification information
      // Obtain the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running 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(90000);

        OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);

        Map<String, String> params = new HashMap<String, String>() {{
            put("format", "full_json");
            put("_POST_BODY", "{\"question\":{\"text\":\"How to charge\",\"type\":\"TEXT\",\"session\":\"\"},\"options\":{\"retrieve\":{\"doc\":{\"filter\":\"\",\"top_n\":5,\"sf\":\"\",\"dense_weight\":\"0.7\",\"formula\":\"\",\"operator\":\"AND\"},\"entry\":{\"sf\":\"\"},\"image\":{\"sf\":\"\",\"dense_weight\":\"0.7\"},\"qp\":{\"query_extend\":false,\"query_extend_num\":5},\"return_hits\":false,\"rerank\":{\"enable\":true,\"model\":\"ops-bge-reranker-larger\"}},\"chat\":{\"stream\":true,\"prompt_config\":{\"attitude\":\"normal\",\"rule\":\"detailed\",\"noanswer\":\"sorry\",\"language\":\"Chinese\",\"role\":false,\"role_name\":\"AI Assistant\",\"out_format\":\"text\"},\"agent\":{\"tools\":[]},\"csi_level\":\"strict\",\"history_max\":\"\",\"link\":\"false\",\"model\":\"qwen-plus\",\"model_generation\":\"\"}}}");

        }};
      	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=90000,
            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-search',
                                             query=query_params, headers=self.header, body=body, runtime=self.runtime)
            return response
        except TeaException as e:
            print(e)


if __name__ == "__main__":
    # Specify the unified request endpoint without the http:// prefix
    endpoint = "<endpoint>"

    # Specify the protocol. Valid values: HTTPS/HTTP
    endpoint_protocol = "HTTP"

    # User identification information
    # Obtain the AccessKey ID and AccessKey Secret from environment variables.
    # You must configure the environment variables before running the sample code. For more information, see the "Configure environment variables" section of this topic.
    access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID")
    access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

    # Specify the authentication type. Valid values: sts/access_key. The default value is access_key. You can configure RAM-STS authentication by using sts.
    # Valid values: sts or access_key
    auth_type = "access_key"

    # If you use RAM-STS authentication, specify the security_token parameter. You can use Alibaba Cloud AssumeRole to obtain the relevant STS authentication structure.
    security_token =  "<security_token>"

    # Specify the common request information.
    # The type and security_token parameters are required only if you use the SDK as a RAM user.
    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 <application name> with the name of the intelligent Q&A version instance you created
    ops = LLMSearch(Configs)
    app_name = "<application name>"

    # --------------- Document Search ---------------

    docQuery = {"question": {"text": "search", "type": "TEXT"}}

    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
// Obtain the AccessKey ID and AccessKey Secret from environment variables.
// You must configure the environment variables before running the sample code. For more information, see the "Configure environment variables" section of this topic.
// Replace with the corresponding access key id
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
// Replace with the corresponding access secret
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$endPoint = '<Replace with endpoint>';
$appName = '<Replace with application name>';
$options = array('timeout' => 90);
$requestBody = "{\"question\":{\"text\":\"Based on the survey of various types of typical sites\",\"type\":\"TEXT\"}}";

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

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

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

For more information about other parameters in POST_BODY, see SearchKnowledge.

Sample code for Java SDK streaming output

Sample request

  • Using AccessKey

    Note

    Java SDK version 6.0.0 or later is required for the following sample.

    package org.example;
    
    import com.aliyun.opensearch.OpenSearchClient;
    import com.aliyun.opensearch.sdk.dependencies.org.apache.http.HttpResponse;
    import com.aliyun.opensearch.sdk.generated.OpenSearch;
    import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.nio.charset.StandardCharsets;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Main {
        private static String host = "Replace with your access address";
    
        public static void main(String[] args) {
          // Obtain the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
            String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
            String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
            OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
            openSearch.setTimeout(90000);
            OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);
            Map<String, String> params = new HashMap<String, String>() {{
                put("_POST_BODY", "{\"question\": {\"text\": \"Beijing\",\"type\": \"TEXT\"},\"options\": {\"chat\": {\"stream\": true}}}");
            }};
    
            String path = "/apps/AppName/actions/knowledge-search";
            try {
                HttpResponse httpResponse = openSearchClient
                        .callForHttpResponse(path, params, "POST");
                InputStream responseBodyStream  = httpResponse.getEntity().getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
    
                br.close();
                // The request is received.
                openSearchClient.getHttpClientManager().getClientTracer().success(httpResponse, "");
            } catch (OpenSearchClientException e) {
                System.out.println("ErrorMessage=" + e.getMessage());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

  • Using API Key

    Note

    Java SDK version 6.0.2 or higher is required.

    package org.example;
    
    import com.aliyun.opensearch.OpenSearchClient;
    import com.aliyun.opensearch.sdk.dependencies.org.apache.http.HttpResponse;
    import com.aliyun.opensearch.sdk.generated.OpenSearch;
    import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.nio.charset.StandardCharsets;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Main {
        private static String host = "Replace with your access address";
    
        public static void main(String[] args) {
            // Access using API Key, SDK version must be 6.0.2 or later
            OpenSearch openSearch = new OpenSearch();
            openSearch.setBearerToken("OS-xxxx");
            openSearch.setHost(host);
            openSearch.setTimeout(90000);
            OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);
            Map<String, String> params = new HashMap<String, String>() {{
                put("_POST_BODY", "{\"question\": {\"text\": \"Beijing\",\"type\": \"TEXT\"},\"options\": {\"chat\": {\"stream\": true}}}");
            }};
    
            String path = "/apps/AppName/actions/knowledge-search";
            try {
                HttpResponse httpResponse = openSearchClient
                        .callForHttpResponse(path, params, "POST");
                InputStream responseBodyStream  = httpResponse.getEntity().getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
    
                br.close();
                // The request is received.
                openSearchClient.getHttpClientManager().getClientTracer().success(httpResponse, "");
            } catch (OpenSearchClientException e) {
                System.out.println("ErrorMessage=" + e.getMessage());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

Sample output

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":391.178295,"result":{"data":[{"answer":"Beijing","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":409.279769,"result":{"data":[{"answer":"Beijing is in China","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":429.260018,"result":{"data":[{"answer":"Beijing is the","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":447.107968,"result":{"data":[{"answer":"Beijing is the capital of China","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":465.897194,"result":{"data":[{"answer":"Beijing is the capital of China,","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":485.54058,"result":{"data":[{"answer":"Beijing is the capital of China, with","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":504.273681,"result":{"data":[{"answer":"Beijing is the capital of China, with a long","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":523.287943,"result":{"data":[{"answer":"Beijing is the capital of China, with a long history","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":542.027658,"result":{"data":[{"answer":"Beijing is the capital of China, with a long history.","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":559.790625,"result":{"data":[{"answer":"Beijing is the capital of China, with a long history.","type":"TEXT"}]}}

data:{"request_id":"AF429C69-2D36-4328-A9BA-5ECDEC716176","status":"OK","latency":689.980221,"result":{"data":[{"answer":"Beijing is the capital of China, with a long history.","type":"TEXT","reference":[{"tokenNum":12,"id":"6004483b75f98deb48198e3543a90c0c","title":"Beijing.txt"},{"tokenNum":607,"id":"770f7cb52fec7afb1be65d637063a8cb","title":"Leading New China, Striving for a New Era.docx"}]}]}}

data:[done]