Intelligent conversation demo

更新时间:
复制 MD 格式

OpenSearch Industry Algorithm Edition supports the intelligent conversation feature. This feature calls a large model from the AI Search Development Platform to provide intelligent answers to user questions during a search. This topic provides a Java demo for this feature.

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.

Sample code

Important

The following example requires Java SDK 6.2.0 or later.

<dependency>
    <groupId>com.aliyun.opensearch</groupId>
    <artifactId>aliyun-sdk-opensearch</artifactId>
    <version>6.2.0</version>
</dependency>

package org.example;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SearcherClient;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.search.Chat;
import com.aliyun.opensearch.sdk.generated.search.Config;
import com.aliyun.opensearch.sdk.generated.search.SearchFormat;
import com.aliyun.opensearch.sdk.generated.search.SearchParams;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class testChat {

    private static String appName = "Replace with your OpenSearch application name";
    private static String host = "Replace with the API endpoint of your application";

    public static void main(String[] args) {

        // User identity information.
        // Read the AccessKey ID and AccessKey secret from the environment variables. You must configure the environment variables before you run this 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.setHost(host);
        opensearch.setTimeout(90000);
        OpenSearchClient openSearchClient = new OpenSearchClient(opensearch);
        SearcherClient searcherClient = new SearcherClient(openSearchClient);

        Config config = new Config(Lists.newArrayList(appName));
        config.setHits(10);
        config.setStart(0);
        config.setSearchFormat(SearchFormat.findByValue(1));
        config.setFetchFields(Arrays.asList("title", "description", "id"));
        SearchParams searchParams = new SearchParams(config);
        searchParams.setQuery("default:'OpenSearch'");
        searchParams.setRawQuery("OpenSearch");
        String chatName = "test_chat";

        Map<String, String> promptParameters = new HashMap();
        // promptParameters.put("rule_prompt", "");
        // promptParameters.put("attitude_prompt", "");
        // promptParameters.put("noanswer_prompt", "");
        Chat chat = new Chat(chatName, promptParameters);
        searchParams.setChat(chat);

        try {
            searcherClient.executeForServerSentEvents(searchParams, e -> {
                System.out.println(e.data());
            });
        } catch (OpenSearchClientException e) {
            System.out.println("ErrorMessage=" + e.getMessage());
        }
    }
}

Sample success response

{"result":{"searchtime":0.011841,"total":5,"num":5,"viewtotal":5,"compute_cost":[{"index_name":"rag_test","value":0.395}],"items":[{"content":"Many small and medium-sized enterprises (SMEs) need to retrieve various types of structured data. They typically use the search features provided by databases or build search systems with open source software. This approach not only consumes website resources and can easily cause performance issues, but also often provides poor relevance. Our product aims to provide SMEs with a low-cost, high-quality, high-performance, and customizable data search solution using Alibaba Cloud's advanced cloud computing and search technologies. The goal of this project differs slightly from the general cloud search solution. This project focuses on searching user structured data, while the general cloud search solution primarily handles web page data.","url":"Coo8 Shopping","id":"5","index_name":"rag_test"},{"content":"Specialized dictionary: Users can configure their own dedicated term dictionaries. Synonyms: User-defined synonym lists, typically for specific industries. Stop word configuration: Configure a dedicated stop word list in addition to the system's default list. Disabled word configuration: System filtering is enabled by default. Users can add their own filter lists to block search results upon a hit. Two options are available: Query filtering, which returns no results upon a match, and result filtering, which filters results based on matches.","url":"JD.com","id":"1","index_name":"rag_test"},{"content":"Many small and medium-sized enterprises (SMEs) need to retrieve various types of structured data. They typically use the search features provided by databases or build search systems with open source software. This approach not only consumes website resources and can easily cause performance issues, but also often provides poor relevance. Our product aims to provide SMEs with a low-cost, high-quality, high-performance, and customizable data search solution using Alibaba Cloud's advanced cloud computing and search technologies. The goal of this project differs slightly from the general cloud search solution. This project focuses on searching user structured data, while the general cloud search solution primarily handles web page data.","url":"Xiaomi Official Website","id":"4","index_name":"rag_test"},{"content":"A search engine is a system that uses specific computer programs and strategies to collect information from the Internet, organize and process it, and then provide retrieval services to display relevant information to users. Search engines include full-text indexes, directory indexes, metasearch engines, vertical search engines, federated search engines, portal search engines, and free link lists. Baidu and Google are examples of search engines.","url":"Amazon","id":"3","index_name":"rag_test"},{"content":"This is an extensible structured data search solution developed based on research into various types of typical sites. It can cover more than 95% of the data properties for that type. It supports custom dictionary configuration and sorting formula customization. It also provides good relevance, rich retrieval methods, and multi-dimensional filtering, sorting, and statistics. Users can save customization effort by selecting a template.","url":"Newegg Mall","id":"2","index_name":"rag_test"}],"facet":[]},"ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer the"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer the question"},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer the question."},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer the question."},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":"","ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer the question."},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
{"result":{"searchtime":0.011841,"total":5,"num":5,"viewtotal":5,"compute_cost":[{"index_name":"rag_test","value":0.395}],"items":[{"content":"Many small and medium-sized enterprises (SMEs) need to retrieve various types of structured data. They typically use the search features provided by databases or build search systems with open source software. This approach not only consumes website resources and can easily cause performance issues, but also often provides poor relevance. Our product aims to provide SMEs with a low-cost, high-quality, high-performance, and customizable data search solution using Alibaba Cloud's advanced cloud computing and search technologies. The goal of this project differs slightly from the general cloud search solution. This project focuses on searching user structured data, while the general cloud search solution primarily handles web page data.","url":"Coo8 Shopping","id":"5","index_name":"rag_test"},{"content":"Specialized dictionary: Users can configure their own dedicated term dictionaries. Synonyms: User-defined synonym lists, typically for specific industries. Stop word configuration: Configure a dedicated stop word list in addition to the system's default list. Disabled word configuration: System filtering is enabled by default. Users can add their own filter lists to block search results upon a hit. Two options are available: Query filtering, which returns no results upon a match, and result filtering, which filters results based on matches.","url":"JD.com","id":"1","index_name":"rag_test"},{"content":"Many small and medium-sized enterprises (SMEs) need to retrieve various types of structured data. They typically use the search features provided by databases or build search systems with open source software. This approach not only consumes website resources and can easily cause performance issues, but also often provides poor relevance. Our product aims to provide SMEs with a low-cost, high-quality, high-performance, and customizable data search solution using Alibaba Cloud's advanced cloud computing and search technologies. The goal of this project differs slightly from the general cloud search solution. This project focuses on searching user structured data, while the general cloud search solution primarily handles web page data.","url":"Xiaomi Official Website","id":"4","index_name":"rag_test"},{"content":"A search engine is a system that uses specific computer programs and strategies to collect information from the Internet, organize and process it, and then provide retrieval services to display relevant information to users. Search engines include full-text indexes, directory indexes, metasearch engines, vertical search engines, federated search engines, portal search engines, and free link lists. Baidu and Google are examples of search engines.","url":"Amazon","id":"3","index_name":"rag_test"},{"content":"This is an extensible structured data search solution developed based on research into various types of typical sites. It can cover more than 95% of the data properties for that type. It supports custom dictionary configuration and sorting formula customization. It also provides good relevance, rich retrieval methods, and multi-dimensional filtering, sorting, and statistics. Users can save customization effort by selecting a template.","url":"Newegg Mall","id":"2","index_name":"rag_test"}],"facet":[]},"ops_request_misc":"%7B%22request%5Fid%22%3A%222b352b22ce79cd53c4554204f400aff8%22%2C%22scm%22%3A%2220140713.150121380..%22%7D","chat":{"text":"Based on the known information, I cannot answer the question."},"tracer":"","request_id":"2b352b22ce79cd53c4554204f400aff8","errors":[],"status":"OK"}
[done]