リクエストボディ | 入力文字列Pythonimport os
from openai import OpenAI
client = OpenAI(
# 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。API キーは https://bailian.console.alibabacloud.com/?tab=model#/api-key で取得できます。
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、プレースホルダーをご自身の API キーに置き換えてください。
# 中国 (北京) リージョンのモデルを使用する場合、URL を https://dashscope.aliyuncs.com/compatible-mode/v1 に置き換えてください。
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)
completion = client.embeddings.create(
model="text-embedding-v4",
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) {
// 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。API キーは https://bailian.console.alibabacloud.com/?tab=model#/api-key で取得できます。
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
// 中国 (北京) リージョンのモデルを使用する場合、URL を https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings に置き換えてください。
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-v4");
requestBody.put("input", "The wind is strong, the sky is high, and the apes cry mournfully. The islet is clear, the sand is white, and the birds fly back. The boundless forest sheds its leaves shower by shower. The endless river rolls on wave after wave.");
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());
}
}
}
curl中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キー を使用し、URL を https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings に置き換えてください。 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-v4",
"input": "The wind is strong, the sky is high, and the apes cry mournfully. The islet is clear, the sand is white, and the birds fly back. The boundless forest sheds its leaves shower by shower. The endless river rolls on wave after wave.",
"dimensions": 1024,
"encoding_format": "float"
}'
入力文字列リストPythonimport os
from openai import OpenAI
client = OpenAI(
# 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。API キーは https://bailian.console.alibabacloud.com/?tab=model#/api-key で取得できます。
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、プレースホルダーをご自身の API キーに置き換えてください。
# 中国 (北京) リージョンのモデルを使用する場合、URL を https://dashscope.aliyuncs.com/compatible-mode/v1 に置き換えてください。
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)
completion = client.embeddings.create(
model="text-embedding-v4",
input=['The wind is strong, the sky is high, and the apes cry mournfully.', 'The islet is clear, the sand is white, and the birds fly back.', 'The boundless forest sheds its leaves shower by shower.', 'The endless river rolls on wave after wave.'],
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) {
/** 環境変数から API キーを取得します。設定されていない場合は、プレースホルダーをご自身の API キーに置き換えてください。 */
// 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。API キーは https://bailian.console.alibabacloud.com/?tab=model#/api-key で取得できます。
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
// 中国 (北京) リージョンのモデルを使用する場合、URL を https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings に置き換えてください。
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-v4");
List<String> inputList = Arrays.asList("The wind is strong, the sky is high, and the apes cry mournfully.", "The islet is clear, the sand is white, and the birds fly back.", "The boundless forest sheds its leaves shower by shower.", "The endless river rolls on wave after wave.");
requestBody.put("input", inputList);
requestBody.put("encoding_format", "float");
try {
/** リクエストボディを JSON 文字列に変換します。 */
String requestBodyString = JsonUtils.toJson(requestBody);
/** HTTP リクエストを構築します。 */
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());
}
}
}
curl中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キー を使用し、URL を https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings に置き換えてください。 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-v4",
"input": [
"The wind is strong, the sky is high, and the apes cry mournfully.",
"The islet is clear, the sand is white, and the birds fly back.",
"The boundless forest sheds its leaves shower by shower.",
"The endless river rolls on wave after wave."
],
"dimensions": 1024,
"encoding_format": "float"
}'
入力ファイルPythonimport os
from openai import OpenAI
client = OpenAI(
# 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。API キーは https://bailian.console.alibabacloud.com/?tab=model#/api-key で取得できます。
api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数を設定していない場合は、プレースホルダーをご自身の API キーに置き換えてください。
# 中国 (北京) リージョンのモデルを使用する場合、URL を https://dashscope.aliyuncs.com/compatible-mode/v1 に置き換えてください。
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)
# 'texts_to_embedding.txt' をご自身のファイル名またはパスに置き換えてください。
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
completion = client.embeddings.create(
model="text-embedding-v4",
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) {
/** 環境変数から API キーを取得します。設定されていない場合は、プレースホルダーをご自身の API キーに置き換えてください。 */
// 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。API キーは https://bailian.console.alibabacloud.com/?tab=model#/api-key で取得できます。
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
// 中国 (北京) リージョンのモデルを使用する場合、URL を https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings に置き換えてください。
String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
/** 入力ファイルを読み込みます。 */
StringBuilder inputText = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader("<path_to_your_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-v4");
requestBody.put("input", inputText.toString().trim());
requestBody.put("dimensions", 1024);
requestBody.put("encoding_format", "float");
try {
String requestBodyString = JsonUtils.toJson(requestBody);
/** HTTP リクエストを構築します。 */
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());
}
}
}
curl中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キー を使用し、URL を https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings に置き換えてください。 'texts_to_embedding.txt' をご自身のファイル名またはパスに置き換えてください。 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-v4",
"input": ['"$FILE_CONTENT"'],
"dimensions": 1024,
"encoding_format": "float"
}'
|