Request body | Input stringPythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If you have not configured environment variables, replace this with your API Key
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1" # base_url of Alibaba Cloud Model Studio
)
completion = client.embeddings.create(
model="text-embedding-v3",
input='The clothes are of good quality and look good, definitely worth the wait. I love them.',
dimensions=1024,
encoding_format="float"
)
print(completion.model_dump_json())
Javaimport java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.dashscope.utils.JsonUtils;
public final class Main {
public static void main(String[] args) {
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "text-embedding-v3");
requestBody.put("input", "The wind is strong and the sky is high, the apes wail sadly. The islet is clear, the sand is white, and birds fly back. Endless falling leaves descend rustling, the mighty Yangtze River flows on endlessly");
requestBody.put("dimensions", 1024);
requestBody.put("encoding_format", "float");
try {
String requestBodyString = JsonUtils.toJson(requestBody);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Response: " + response.body());
} else {
System.out.printf("Failed to retrieve response, status code: %d, response: %s%n", response.statusCode(), response.body());
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
curlcurl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "text-embedding-v3",
"input": "The wind howls fiercely in the high sky as monkeys cry in sorrow, the clear sandbank shines white as birds fly back, endless falling leaves descend rustling everywhere, the mighty Yangtze River flows on endlessly",
"dimension": "1024",
"encoding_format": "float"
}'
Input string listPythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If you haven't configured environment variables, replace this with your API Key
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1" # base_url for Alibaba Cloud Model Studio service
)
completion = client.embeddings.create(
model="text-embedding-v3",
input=['The wind is strong and the sky is high, the apes wail sadly', 'The sandbar is clear and white, birds fly back', 'Endless falling leaves descend rustling', 'The endless Yangtze River rolls on'],
dimensions=1024,
encoding_format="float"
)
print(completion.model_dump_json())
Javaimport java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Arrays;
import com.alibaba.dashscope.utils.JsonUtils;
public final class Main {
public static void main(String[] args) {
/** Get API Key from environment variables, if not configured, please replace it with your API Key*/
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "text-embedding-v3");
List<String> inputList = Arrays.asList("The wind is strong and the sky is high, the monkey's cry is sad", "The sandbar is clear and the sand is white, birds fly back", "Endless falling leaves come down rustling", "The endless Yangtze River flows on and on");
requestBody.put("input", inputList);
requestBody.put("encoding_format", "float");
try {
/** Convert the request body to JSON string*/
String requestBodyString = JsonUtils.toJson(requestBody);
/**Build HTTP request*/
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
.build();
/**Send request and receive response*/
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Response: " + response.body());
} else {
System.out.printf("Failed to retrieve response, status code: %d, response: %s%n", response.statusCode(), response.body());
}
} catch (Exception e) {
/** Catch and print exception*/
System.err.println("Error: " + e.getMessage());
}
}
}
curlcurl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "text-embedding-v3",
"input": [
"The wind is strong, the sky is high, and the apes wail sadly",
"The sandbar is clear, the sand is white, and birds fly back",
"Endless falling leaves descend rustling",
"The endless Yangtze River flows rolling on"
],
"dimension": 1024,
"encoding_format": "float"
}'
Input filePythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # If you have not configured environment variables, replace this with your API Key
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1" # base_url of Alibaba Cloud Model Studio
)
# Make sure to replace 'texts_to_embedding.txt' with your own file name or path
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
completion = client.embeddings.create(
model="text-embedding-v3",
input=f,
encoding_format="float"
)
print(completion.model_dump_json())
Javaimport java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.alibaba.dashscope.utils.JsonUtils;
public class Main {
public static void main(String[] args) {
/** Get API Key from environment variables. If not configured, replace it directly with your API Key*/
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
/** Read input file*/
StringBuilder inputText = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader("<full path to the content root>"))) {
String line;
while ((line = reader.readLine()) != null) {
inputText.append(line).append("\n");
}
} catch (IOException e) {
System.err.println("Error reading input file: " + e.getMessage());
return;
}
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "text-embedding-v3");
requestBody.put("input", inputText.toString().trim());
requestBody.put("dimensions", 1024);
requestBody.put("encoding_format", "float");
try {
String requestBodyString = JsonUtils.toJson(requestBody);
/**Build HTTP request*/
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Response: " + response.body());
} else {
System.out.printf("Failed to retrieve response, status code: %d, response: %s%n", response.statusCode(), response.body());
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
curlMake sure to replace 'texts_to_embedding.txt' with your own file name or path FILE_CONTENT=$(cat texts_to_embedding.txt | jq -Rs .)
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "text-embedding-v3",
"input": ['"$FILE_CONTENT"'],
"dimension": 1024,
"encoding_format": "float"
}'
|