ashscope-sdk-java.2.16.9.source-code.TextEmbeddingUsage Maven / Gradle / Ivy
The newest version!
import java.util.Arrays;
import java.util.concurrent.Semaphore;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.embeddings.TextEmbeddingParam;
import com.alibaba.dashscope.embeddings.TextEmbeddingResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public final class TextEmbeddingUsage {
public static void basicCall() throws ApiException, NoApiKeyException{
TextEmbeddingParam param = TextEmbeddingParam
.builder()
.model(TextEmbedding.Models.TEXT_EMBEDDING_V1)
.texts(Arrays.asList("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来")).build();
TextEmbedding textEmbedding = new TextEmbedding();
TextEmbeddingResult result = textEmbedding.call(param);
System.out.println(result);
}
public static void callWithCallback() throws ApiException, NoApiKeyException, InterruptedException{
TextEmbeddingParam param = TextEmbeddingParam
.builder()
.model(TextEmbedding.Models.TEXT_EMBEDDING_V1)
.texts(Arrays.asList("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来")).build();
TextEmbedding textEmbedding = new TextEmbedding();
Semaphore sem = new Semaphore(0);
textEmbedding.call(param, new ResultCallback() {
@Override
public void onEvent(TextEmbeddingResult message) {
System.out.println(message);
}
@Override
public void onComplete(){
sem.release();
}
@Override
public void onError(Exception err){
System.out.println(err.getMessage());
err.printStackTrace();
sem.release();
}
});
sem.acquire();
}
public static void main(String[] args){
try{
callWithCallback();
}catch(ApiException|NoApiKeyException|InterruptedException e){
e.printStackTrace();
System.out.println(e);
}
try {
basicCall();
} catch (ApiException | NoApiKeyException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}