import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.volcengine.auth.ISignerV4;
import com.volcengine.auth.impl.SignerV4Impl;
import com.volcengine.model.Credentials;
import com.volcengine.service.SignableRequest;
private static final String AK = "xxx";
private static final String SK = "xxx";
private static final String HOST = "aisearch.cn-beijing.volces.com";
private static final String SERVICE = "aisearch";
private static final String REGION = "cn-beijing";
private static final String SCHEMA = "https";
public static SignableRequest prepareRequest(String host, String path, String method, List<NameValuePair> params, String body, String ak, String sk) throws Exception {
SignableRequest request = new SignableRequest();
request.setMethod(method);
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "application/json");
request.setHeader("Host", HOST);
request.setEntity(new StringEntity(body, "utf-8"));
URIBuilder builder = request.getUriBuilder();
builder.setScheme(SCHEMA);
builder.setParameters(params);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(2000).build();
request.setConfig(requestConfig);
Credentials credentials = new Credentials(REGION, SERVICE);
credentials.setAccessKeyID(ak);
credentials.setSecretAccessKey(sk);
ISignerV4 ISigner = new SignerV4Impl();
ISigner.sign(request, credentials);
public static void search_demo() {
String path = String.format("/api/v1/application/%s/search", "应用id");
Map<String, Object> bodyData = new HashMap<>();
bodyData.put("dataset_id", "实际数据集id");
bodyData.put("page_number", 1);
bodyData.put("page_size", 10);
Map<String, String> query = new HashMap<>();
query.put("text", "测试");
bodyData.put("query", query);
Map<String, String> user = new HashMap<>();
user.put("_user_id", "mock_user_id");
bodyData.put("user", user);
String body = gson.toJson(bodyData);
SignableRequest signableRequest = prepareRequest(HOST, path, "POST", null, body, AK, SK);
URI uri = new URIBuilder().setScheme(SCHEMA).setHost(HOST).setPath(path).build();
HttpPost httpPost = new HttpPost(uri);
httpPost.setConfig(signableRequest.getConfig());
httpPost.setEntity(signableRequest.getEntity());
for (Header header : signableRequest.getAllHeaders()) {
httpPost.setHeader(header.getName(), header.getValue());
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(statusCode);
System.out.println(responseBody);
} catch (IOException e) {
System.out.println("请求出错: " + e.getMessage());
throw new RuntimeException(e);
public static void chat_search_demo() {
String path = String.format("/api/v1/application/%s/chat_search", "应用id");
Map<String, Object> bodyData = new HashMap<>();
bodyData.put("session_id", "mock_session_id");
Map<String, Object> inputMessage = new HashMap<>();
Map<String, String> content = new HashMap<>();
content.put("type", "text");
content.put("text", "帮我推荐几部喜剧?");
inputMessage.put("content", Collections.singletonList(content));
bodyData.put("input_message", inputMessage);
Map<String, String> user = new HashMap<>();
user.put("_user_id", "mock_user_id");
bodyData.put("user", user);
String body = gson.toJson(bodyData);
SignableRequest signableRequest = prepareRequest(HOST, path, "POST", null, body, AK, SK);
URI uri = new URIBuilder().setScheme(SCHEMA).setHost(HOST).setPath(path).build();
HttpPost httpPost = new HttpPost(uri);
httpPost.setConfig(signableRequest.getConfig());
httpPost.setEntity(signableRequest.getEntity());
for (Header header : signableRequest.getAllHeaders()) {
httpPost.setHeader(header.getName(), header.getValue());
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
processStreamResponse(response.getEntity());
System.err.println("HTTP请求失败,状态码: " + statusCode);
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.err.println("响应内容: " + responseBody);
} catch (IOException e) {
System.out.println("请求出错: " + e.getMessage());
throw new RuntimeException(e);
private static void processStreamResponse(HttpEntity entity) throws IOException {
try (InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), 128)) {
while ((line = reader.readLine()) != null) {
System.out.println(line);
EntityUtils.consumeQuietly(entity);
public static void main(String[] args) {