modelstring必須呼び出すモデルの名前。モデル名については、「モデル概要」の表をご参照ください。inputarray<string> or string or file必須処理する入力テキスト。入力は、文字列、文字列の配列、またはファイルが可能です。 テキスト長とバッチサイズの制限は、モデルのバージョンによって異なります:
import osfrom openai import OpenAIclient = OpenAI( # 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。取得先: https://modelstudio.console.alibabacloud.com/?tab=model#/api-key api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数が設定されていない場合は、プレースホルダーをご利用の API キーに置き換えてください。 # これはシンガポールリージョンの URL です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1")completion = client.embeddings.create( model="qwen3.7-text-embedding", 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())
Copy
import 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 キーを使用する必要があります。取得先: https://modelstudio.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 です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 String baseUrl = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings"; HttpClient client = HttpClient.newHttpClient(); Map<String, Object> requestBody = new HashMap<>(); requestBody.put("model", "qwen3.7-text-embedding"); 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 --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings' \--header "Authorization: Bearer $DASHSCOPE_API_KEY" \--header 'Content-Type: application/json' \--data '{ "model": "qwen3.7-text-embedding", "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"}'
Python
Java
curl
Copy
import osfrom openai import OpenAIclient = OpenAI( # 中国 (北京) リージョンのモデルを使用する場合、そのリージョンの API キーを使用する必要があります。取得先: https://modelstudio.console.alibabacloud.com/?tab=model#/api-key api_key=os.getenv("DASHSCOPE_API_KEY"), # 環境変数が設定されていない場合は、プレースホルダーをご利用の API キーに置き換えてください。 # これはシンガポールリージョンの URL です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1")completion = client.embeddings.create( model="qwen3.7-text-embedding", 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())
Copy
import 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 キーを使用する必要があります。取得先: https://modelstudio.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 です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 String baseUrl = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings"; HttpClient client = HttpClient.newHttpClient(); Map<String, Object> requestBody = new HashMap<>(); requestBody.put("model", "qwen3.7-text-embedding"); 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 --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings' \--header "Authorization: Bearer $DASHSCOPE_API_KEY" \--header 'Content-Type: application/json' \--data '{ "model": "qwen3.7-text-embedding", "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"}'
import osimport dashscope# これはシンガポールリージョンの設定です。リージョンによって region の値は異なります。呼び出し時は、{WorkspaceId} をお使いのワークスペース ID に置き換えてください。client = dashscope.Client( # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key="sk-xxx" # API キーはリージョン固有です。API キーを取得するには、https://www.alibabacloud.com/help/model-studio/get-api-key をご参照ください api_key=os.getenv("DASHSCOPE_API_KEY"), workspace="{WorkspaceId}", region="ap-southeast-1",)resp = client.embeddings.create( model="qwen3.7-text-embedding", input='A swift wind, a high sky, and the gibbons cry mournfully. A clear islet, white sand, and the birds fly back. Boundless rustling woods shed their leaves. The endless Yangtze River comes rolling in.', dimension=1024, # ベクトルの次元を指定します(qwen3.7-text-embedding、text-embedding-v3、text-embedding-v4 のみサポート) output_type="dense&sparse")print(resp)
Copy
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;import com.alibaba.dashscope.utils.Constants;public final class Main { static { // これはシンガポールリージョンの URL です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"; } public static void basicCall() throws ApiException, NoApiKeyException{ TextEmbeddingParam param = TextEmbeddingParam .builder() .model("qwen3.7-text-embedding") .texts(Arrays.asList("A swift wind, a high sky, and the gibbons cry mournfully.", "A clear islet, white sand, and the birds fly back.", "Boundless rustling woods shed their leaves.", "The endless Yangtze River comes rolling in.")).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_V3) .texts(Arrays.asList("A swift wind, a high sky, and the gibbons cry mournfully.", "A clear islet, white sand, and the birds fly back.", "Boundless rustling woods shed their leaves.", "The endless Yangtze River comes rolling in.")).build(); TextEmbedding textEmbedding = new TextEmbedding(); Semaphore sem = new Semaphore(0); textEmbedding.call(param, new ResultCallback<TextEmbeddingResult>() { @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); }}
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \--header "Authorization: Bearer $DASHSCOPE_API_KEY" \--header 'Content-Type: application/json' \--data '{ "model": "qwen3.7-text-embedding", "input": { "texts": [ "A swift wind, a high sky, and the gibbons cry mournfully. A clear islet, white sand, and the birds fly back. Boundless rustling woods shed their leaves. The endless Yangtze River comes rolling in." ] }, "parameters": { "dimension": 1024, "output_type": "dense" }}'
Python
Java
curl
Copy
import osimport dashscopefrom dashscope.v2 import DashScopeError# これはシンガポールリージョンの設定です。リージョンによって region の値は異なります。呼び出し時は、{WorkspaceId} をお使いのワークスペース ID に置き換えてください。client = dashscope.Client( # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key="sk-xxx" # API キーはリージョン固有です。API キーを取得するには、https://www.alibabacloud.com/help/model-studio/get-api-key をご参照ください api_key=os.getenv("DASHSCOPE_API_KEY"), workspace="{WorkspaceId}", region="ap-southeast-1",)DASHSCOPE_MAX_BATCH_SIZE = 10inputs = ['A swift wind, a high sky, and the gibbons cry mournfully.', 'A clear islet, white sand, and the birds fly back.', 'Boundless rustling woods shed their leaves.', 'The endless Yangtze River comes rolling in.']result = Nonebatch_counter = 0try: for i in range(0, len(inputs), DASHSCOPE_MAX_BATCH_SIZE): batch = inputs[i:i + DASHSCOPE_MAX_BATCH_SIZE] resp = client.embeddings.create( model="qwen3.7-text-embedding", input=batch, dimension=1024 # ベクトルの次元を指定します(qwen3.7-text-embedding、text-embedding-v3、text-embedding-v4 のみサポート) ) if result is None: result = resp else: for emb in resp.embeddings: emb.index += batch_counter result.embeddings.append(emb) result.usage.total_tokens += resp.usage.total_tokens batch_counter += len(batch)except DashScopeError as e: print(f"HTTP status code: {e.status_code}") print(f"Error code: {e.code}") print(f"Error message: {e.message}") print("See: https://www.alibabacloud.com/help/ja/model-studio/developer-reference/error-code")print(result)
Copy
import java.util.Arrays;import java.util.List;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;import com.alibaba.dashscope.utils.Constants;public final class Main { static { // これはシンガポールリージョンの URL です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"; } private static final int DASHSCOPE_MAX_BATCH_SIZE = 10; public static void main(String[] args) { List<String> inputs = Arrays.asList( "A swift wind, a high sky, and the gibbons cry mournfully.", "A clear islet, white sand, and the birds fly back.", "Boundless rustling woods shed their leaves.", "The endless Yangtze River comes rolling in." ); TextEmbeddingResult result = null; int batchCounter = 0; for (int i = 0; i < inputs.size(); i += DASHSCOPE_MAX_BATCH_SIZE) { List<String> batch = inputs.subList(i, Math.min(i + DASHSCOPE_MAX_BATCH_SIZE, inputs.size())); TextEmbeddingParam param = TextEmbeddingParam.builder() .model("qwen3.7-text-embedding") .texts(batch) .build(); TextEmbedding textEmbedding = new TextEmbedding(); try { TextEmbeddingResult resp = textEmbedding.call(param); if (resp != null) { if (result == null) { result = resp; } else { for (var emb : resp.getOutput().getEmbeddings()) { emb.setTextIndex(emb.getTextIndex() + batchCounter); result.getOutput().getEmbeddings().add(emb); } result.getUsage().setTotalTokens(result.getUsage().getTotalTokens() + resp.getUsage().getTotalTokens()); } } else { System.out.println(resp); } } catch (ApiException | NoApiKeyException e) { e.printStackTrace(); } batchCounter += batch.size(); } System.out.println(result); }}
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \--header "Authorization: Bearer $DASHSCOPE_API_KEY" \--header 'Content-Type: application/json' \--data '{ "model": "qwen3.7-text-embedding", "input": { "texts": [ "A swift wind, a high sky, and the gibbons cry mournfully.", "A clear islet, white sand, and the birds fly back.", "Boundless rustling woods shed their leaves.", "The endless Yangtze River comes rolling in." ] }, "parameters": { "dimension": 1024, "output_type": "dense" }}'
Python
Java
curl
Copy
import osimport dashscope# これはシンガポールリージョンの設定です。リージョンによって region の値は異なります。呼び出し時は、{WorkspaceId} をお使いのワークスペース ID に置き換えてください。client = dashscope.Client( # 環境変数を設定していない場合は、次の行を Model Studio API キーに置き換えてください: api_key="sk-xxx" # API キーはリージョン固有です。API キーを取得するには、https://www.alibabacloud.com/help/model-studio/get-api-key をご参照ください api_key=os.getenv("DASHSCOPE_API_KEY"), workspace="{WorkspaceId}", region="ap-southeast-1",)# 'texts_to_embedding.txt' をご利用のファイル名またはパスに置き換えてください。with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f: resp = client.embeddings.create( model="qwen3.7-text-embedding", input=f, dimension=1024 # ベクトルの次元を指定します(qwen3.7-text-embedding、text-embedding-v3、text-embedding-v4 のみサポート) ) print(resp)
Copy
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;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;import com.alibaba.dashscope.utils.Constants;public final class Main { static { // これはシンガポールリージョンの URL です。ご利用の実際のワークスペース ID で {WorkspaceId} を置き換えてください。URL はリージョンによって異なります。 Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1"; } public static void main(String[] args) { // 'texts_to_embedding.txt' をご利用のファイルのパスに置き換えてください。 try (BufferedReader reader = new BufferedReader(new FileReader("texts_to_embedding.txt"))) { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } TextEmbeddingParam param = TextEmbeddingParam.builder() .model("qwen3.7-text-embedding") .text(content.toString()) .build(); TextEmbedding textEmbedding = new TextEmbedding(); TextEmbeddingResult result = textEmbedding.call(param); if (result != null) { System.out.println(result); } else { System.out.println("Failed to get embedding: " + result); } } catch (IOException | ApiException | NoApiKeyException e) { e.printStackTrace(); } }}