請求體 | 輸入字符串Pythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您沒有配置環(huán)境變量,請在此處用您的API Key進行替換
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" # 填寫百煉服務的base_url
)
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.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", "風急天高猿嘯哀,渚清沙白鳥飛回,無邊落木蕭蕭下,不盡長江滾滾來");
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.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "text-embedding-v3",
"input": "風急天高猿嘯哀,渚清沙白鳥飛回,無邊落木蕭蕭下,不盡長江滾滾來",
"dimension": "1024",
"encoding_format": "float"
}'
輸入字符串列表Pythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您沒有配置環(huán)境變量,請在此處用您的API Key進行替換
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" # 填寫百煉服務的base_url
)
completion = client.embeddings.create(
model="text-embedding-v1",
input=['風急天高猿嘯哀', '渚清沙白鳥飛回', '無邊落木蕭蕭下', '不盡長江滾滾來'],
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) {
/** 從環(huán)境變量中獲取 API Key,如果未配置,請直接替換為您的 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.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("風急天高猿嘯哀", "渚清沙白鳥飛回", "無邊落木蕭蕭下", "不盡長江滾滾來");
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();
/**發(fā)送請求并接收響應*/
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.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "text-embedding-v3",
"input": [
"風急天高猿嘯哀",
"渚清沙白鳥飛回",
"無邊落木蕭蕭下",
"不盡長江滾滾來"
],
"dimension": 1024,
"encoding_format": "float"
}'
輸入文件Pythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您沒有配置環(huán)境變量,請在此處用您的API Key進行替換
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" # 填寫百煉服務的base_url
)
# 確保將 'texts_to_embedding.txt' 替換為您自己的文件名或路徑
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
completion = client.embeddings.create(
model="text-embedding-v3",
input=f,
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.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) {
/** 從環(huán)境變量中獲取 API Key,如果未配置,請直接替換為您的 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.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
/** 讀取輸入文件*/
StringBuilder inputText = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader("<文件所來自的內容根的路徑>"))) {
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);
/**構建 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確保將 'texts_to_embedding.txt' 替換為您自己的文件名或路徑 FILE_CONTENT=$(cat texts_to_embedding.txt | jq -Rs .)
curl --location 'https://dashscope.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"
}'
|