#OpenAI Key
openai.api.key = <YOUR_GENERATED_OPEN_API_KEY>
#OpenAI Completion API
openai.api.completions.url = https://api.openai.com/v1/completions
openai.api.model = text-davinci-003
openai.api.maxtoken = 600
openai.api.temperature = 0.8
#OpenAI Image API
openai.api.image.url = https://api.openai.com/v1/images/generations
openai.api.number = 2
openai.api.size = 1024x1024
#OpenAI Prompt
openai.prompt.product.description = Write the Product description for
openai.prompt.product.summary = Write the Product Summary for
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
-->
<action-definition id="com.openai.backoffice.actions.openaieventaction"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.hybris.com/schema/cockpitng/action-definition.xsd">
<name>OpenAI Generate Product Information and Images</name>
<description>Action to trigger OpenAI API to generate Product Details</description>
<author>hybris</author>
<version>1.0</version>
<actionClassName>com.openai.backoffice.actions.OpenAIEventAction</actionClassName>
<inputType>de.hybris.platform.core.model.product.ProductModel</inputType>
<outputType>java.lang.String</outputType>
<iconUri>icons/openai.png</iconUri>
</action-definition>
curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "text-davinci-003",
"prompt": "Write the Product description for NP03ZL Middle Zoom Lens",
"max_tokens": 600,
"temperature": 0.8
}'
String requestBody = String.format("{\"model\": \"%s\", \"prompt\": \"%s\", \"max_tokens\": %d, \"temperature\": %f}", model, prompt, maxTokens, temperature);
curl https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "The NP03ZL Middle Zoom Lens is the perfect choice for capturing stunning photos and videos in any environment. This lens has an impressive focal length of 16-50mm and an aperture range of f/3.5-5.6, allowing it to capture sharp, clear images even in low light. Its advanced optical design includes two aspherical elements and two extra-low dispersion elements for excellent sharpness, clarity and color accuracy. Its advanced image stabilization system helps to minimize blur and camera shake, while the silent and fast autofocus system keeps the subject in focus in any situation. With its durable construction and weather-resistant design, the NP03ZL Middle Zoom Lens is a great choice for professionals who demand quality and performance.",
"n": 2,
"size": "1024x1024"
}'
String requestBody = String.format("{\"prompt\": \"%s\", \"n\": %d, \"size\": \"%s\"}", prompt, n, size);
private JsonObject sendOpenAIRequest(String apiKey, String url, String requestBody) {
JsonObject result = null;
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Authorization", "Bearer " + apiKey);
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
con.setDoOutput(true);
con.getOutputStream().write(postData);
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
result = JsonParser.parseString(response.toString()).getAsJsonObject();
}
} else {
LOG.error("OpenAI API returned an error: " + responseCode);
}
} catch (Exception e) {
LOG.error("Error calling OpenAI API", e);
}
return result;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 |