请求体 | 输入字符串Pythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
base_url="https://dashscope-intl.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-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", "风急天高猿啸哀,渚清沙白鸟飞回,无边落木萧萧下,不尽长江滚滚来");
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": "风急天高猿啸哀,渚清沙白鸟飞回,无边落木萧萧下,不尽长江滚滚来",
"dimension": "1024",
"encoding_format": "float"
}'
输入字符串列表Pythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
base_url="https://dashscope-intl.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) {
/** 从环境变量中获取 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-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("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来");
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());
}
}
}
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": [
"风急天高猿啸哀",
"渚清沙白鸟飞回",
"无边落木萧萧下",
"不尽长江滚滚来"
],
"dimension": 1024,
"encoding_format": "float"
}'
输入文件Pythonimport os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
base_url="https://dashscope-intl.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) {
/** 从环境变量中获取 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-intl.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-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"
}'
|