You need to enable JavaScript to run this app.
导航
Embeddings
最近更新时间:2024.12.31 10:48:28首次发布时间:2024.08.12 20:39:07

前提条件

示例代码

说明

示例代码中 <YOUR_ENDPOINT_ID> 需要替换为您在平台上创建的推理接入点 ID。

向量化

package com.volcengine.ark.runtime;

import com.volcengine.ark.runtime.model.embeddings.EmbeddingRequest;
import com.volcengine.ark.runtime.model.embeddings.EmbeddingResult;
import com.volcengine.ark.runtime.service.ArkService;

import java.util.ArrayList;
import java.util.List;

public class EmbeddingsExample {
    public static void main(String[] args) {

        String apiKey = System.getenv("ARK_API_KEY");
        ArkService service = ArkService.builder().apiKey(apiKey).build();

        System.out.println("\n----- embeddings request -----");

        List<String> inputs = new ArrayList<>();
        inputs.add("花椰菜又称菜花、花菜,是一种常见的蔬菜。");
        EmbeddingRequest embeddingRequest = EmbeddingRequest.builder()
                .model("<YOUR_ENDPOINT_ID>")
                .input(inputs)
                .build();

        EmbeddingResult res = service.createEmbeddings(embeddingRequest);
        System.out.println(res);

        // shutdown service
        service.shutdownExecutor();
    }
}

设置自定义 header

package com.volcengine.ark.runtime;

import com.volcengine.ark.runtime.model.embeddings.EmbeddingRequest;
import com.volcengine.ark.runtime.model.embeddings.EmbeddingResult;
import com.volcengine.ark.runtime.service.ArkService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EmbeddingsExample {
    public static void main(String[] args) {
        String apiKey = System.getenv("ARK_API_KEY");
        ArkService service = ArkService.builder().apiKey(apiKey).build();

        System.out.println("\n----- embeddings request -----");

        List<String> inputs = new ArrayList<>();
        inputs.add("花椰菜又称菜花、花菜,是一种常见的蔬菜。");
        EmbeddingRequest embeddingRequest = EmbeddingRequest.builder()
                .model("<YOUR_ENDPOINT_ID>")
                .input(inputs)
                .build();

        EmbeddingResult res = service.createEmbeddings(embeddingRequest, new HashMap<String, String>(){{put(Const.CLIENT_REQUEST_HEADER, "20240627115839147D61D8875537A133C1");}});
        System.out.println(res);

        // shutdown service
        service.shutdownExecutor();
    }
}

异常处理

package com.volcengine.ark.runtime;

import com.volcengine.ark.runtime.exception.ArkHttpException;
import com.volcengine.ark.runtime.model.embeddings.EmbeddingRequest;
import com.volcengine.ark.runtime.model.embeddings.EmbeddingResult;
import com.volcengine.ark.runtime.service.ArkService;

import java.util.ArrayList;
import java.util.List;

public class EmbeddingsExample {
    public static void main(String[] args) {

        String apiKey = System.getenv("ARK_API_KEY");
        ArkService service = ArkService.builder().apiKey(apiKey).build();

        System.out.println("\n----- embeddings request -----");

        List<String> inputs = new ArrayList<>();
        inputs.add("花椰菜又称菜花、花菜,是一种常见的蔬菜。");
        EmbeddingRequest embeddingRequest = EmbeddingRequest.builder()
                .model("<YOUR_ENDPOINT_ID>")
                .input(inputs)
                .build();

        try {
            EmbeddingResult res = service.createEmbeddings(embeddingRequest);
            System.out.println(res);
        } catch (ArkHttpException e) {
            System.out.print(e.toString());
        }
        
        // shutdown service
        service.shutdownExecutor();
    }
}

并发调用

当您的业务有较高的请求量,您可以通过单例模式结合线程池和连接池来调整并发调用,以应对不同的并发需求。

建议:

  • 每个进程仅初始化ArkService一次,作为单例使用。多次初始化会构建多个线程池以及连接池。
  • 进程退出前需要使用 service.shutdownExecutor() 关闭线程池。
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.embeddings.EmbeddingRequest;
import com.volcengine.ark.runtime.model.embeddings.EmbeddingResult;
import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class EmbeddingsExample {
    static String apiKey = System.getenv("ARK_API_KEY");
    static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
    static Dispatcher dispatcher = new Dispatcher();
    static ArkService service = ArkService.builder()
            .dispatcher(dispatcher)
            .connectionPool(connectionPool)
            .apiKey(apiKey)
            .build();

    public static void main(String[] args) {
        System.out.println("\n----- embeddings request -----");


        List<String> inputs = new ArrayList<>();
        inputs.add("花椰菜又称菜花、花菜,是一种常见的蔬菜。");
        EmbeddingRequest embeddingRequest = EmbeddingRequest.builder()
                .model("<YOUR_ENDPOINT_ID>")
                .input(inputs)
                .build();


        EmbeddingResult res = service.createEmbeddings(embeddingRequest);
        System.out.println(res);


        // shutdown service after all requests are finished
        service.shutdownExecutor();
    }
}