Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# OCI OpenAI SDK for java
# [DEPRECATED] OCI OpenAI SDK for java

This library is no longer maintained. You should use official [OpenAI SDK](https://developers.openai.com/api/docs/libraries), not the [OCI SDK](https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdks.htm) nor the legacy [OCI OpenAI SDK](https://github.com/oracle-samples/oci-openai), to invoke the Responses API. Ensure you have the latest version of the OpenAI SDK installed.

If you need IAM Auth, use the below in conjunction with OpenAI SDK.
- Python [https://github.com/oracle-samples/oci-genai-auth-python](https://github.com/oracle-samples/oci-genai-auth-python)
- Java [https://github.com/oracle-samples/oci-genai-auth-java](https://github.com/oracle-samples/oci-genai-auth-java)

If you have been using the legacy OCI OpenAI SDK below prior to our GA release, we will continue to support that.
- [https://github.com/oracle-samples/oci-openai](https://github.com/oracle-samples/oci-openai)
- [https://github.com/oracle/oci-openai-java](https://github.com/oracle/oci-openai-java)

## About

Expand Down
31 changes: 31 additions & 0 deletions examples/sample-files/AcceptAccessReview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta property="og:site_name" content="Oracle Help Center">
<meta name="generator" content="Oracle Markdown Generation 1.0: Jekyll 3.9.0">
<link rel="stylesheet" href="/en/cloud/paas/access-governance/agebr/sp_common/book-template/ohc-book-template/css/book.css">

<link rel="schema.dcterms" href="http://purl.org/dc/terms/">
<meta name="dcterms.created" content="2023-09-08T11:32:47+00:00">
<meta name="dcterms.title" content="Implement Event-Based Access Reviews with Oracle Access Governance">
<meta name="dcterms.category" content="cloud">
<meta name="dcterms.isVersionOf" content="AGEBR">
<meta name="dcterms.product" content="en/cloud/paas/access-governance">
<meta name="dcterms.identifier" content="F72104-03">
<meta name="dcterms.release" content="Latest">
<script id="ssot-metadata" type="application/json"> {"primary":{"category":{"short_name":"cloud","element_name":"Cloud","display_in_url":true},"suite":{"short_name":"paas","element_name":"Cloud Platform","display_in_url":true},"product_group":{"short_name":"security","element_name":"Security","display_in_url":false},"product":{"short_name":"access-governance","element_name":"Access Governance","display_in_url":true},"release":{"short_name":"latest","element_name":"Latest","display_in_url":false}}} </script>
<script type="application/ld+json"> {"@context":"https://schema.org","@type":"WebPage","name":"","datePublished":"2022-11-09 12:26:52 GMT","dateModified":"2023-09-08 11:32:47 GMT"} </script>
</head><body>
<article>
<h1 id="description-of-the-illustration-acceptaccessreviewpng">Description of the illustration AcceptAccessReview.png</h1>
<p>This image shows how Access Governance raised an accept recommendation based on its Identity Intelligence system.</p>

<div class="footer copyrightlogo">
<p><a href="https://docs.oracle.com/pls/topic/lookup?ctx=en/legal&amp;id=cpyr" target="_blank" class="new-window">Copyright ©</a> 2023, Oracle and/or its affiliates.</p>
</div>
</article>
</body>
</html>
31 changes: 31 additions & 0 deletions examples/sample-files/AcceptHighRisk.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta property="og:site_name" content="Oracle Help Center">
<meta name="generator" content="Oracle Markdown Generation 1.0: Jekyll 3.9.0">
<link rel="stylesheet" href="/en/cloud/paas/access-governance/agpar/sp_common/book-template/ohc-book-template/css/book.css">

<link rel="schema.dcterms" href="http://purl.org/dc/terms/">
<meta name="dcterms.created" content="2023-08-31T08:48:43+00:00">
<meta name="dcterms.title" content="Perform User Access Reviews with Oracle Access Governance">
<meta name="dcterms.category" content="cloud">
<meta name="dcterms.isVersionOf" content="AGPAR">
<meta name="dcterms.product" content="en/cloud/paas/access-governance">
<meta name="dcterms.identifier" content="F70244-06">
<meta name="dcterms.release" content="Latest">
<script id="ssot-metadata" type="application/json"> {"primary":{"category":{"short_name":"cloud","element_name":"Cloud","display_in_url":true},"suite":{"short_name":"paas","element_name":"Cloud Platform","display_in_url":true},"product_group":{"short_name":"security","element_name":"Security","display_in_url":false},"product":{"short_name":"access-governance","element_name":"Access Governance","display_in_url":true},"release":{"short_name":"latest","element_name":"Latest","display_in_url":false}}} </script>
<script type="application/ld+json"> {"@context":"https://schema.org","@type":"WebPage","name":"","datePublished":"2022-09-08 15:33:12 GMT","dateModified":"2023-08-31 08:48:43 GMT"} </script>
</head><body>
<article>
<h1 id="description-of-the-illustration-accepthighriskpng">Description of the illustration AcceptHighRisk.png</h1>
<p>This image shows the decision made to accept a high-risk review item.</p>

<div class="footer copyrightlogo">
<p><a href="https://docs.oracle.com/pls/topic/lookup?ctx=en/legal&amp;id=cpyr" target="_blank" class="new-window">Copyright ©</a> 2023, Oracle and/or its affiliates.</p>
</div>
</article>
</body>
</html>
31 changes: 31 additions & 0 deletions examples/sample-files/AcceptRecommendation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta property="og:site_name" content="Oracle Help Center">
<meta name="generator" content="Oracle Markdown Generation 1.0: Jekyll 3.9.0">
<link rel="stylesheet" href="/en/cloud/paas/access-governance/agebr/sp_common/book-template/ohc-book-template/css/book.css">

<link rel="schema.dcterms" href="http://purl.org/dc/terms/">
<meta name="dcterms.created" content="2023-09-08T11:32:47+00:00">
<meta name="dcterms.title" content="Implement Event-Based Access Reviews with Oracle Access Governance">
<meta name="dcterms.category" content="cloud">
<meta name="dcterms.isVersionOf" content="AGEBR">
<meta name="dcterms.product" content="en/cloud/paas/access-governance">
<meta name="dcterms.identifier" content="F72104-03">
<meta name="dcterms.release" content="Latest">
<script id="ssot-metadata" type="application/json"> {"primary":{"category":{"short_name":"cloud","element_name":"Cloud","display_in_url":true},"suite":{"short_name":"paas","element_name":"Cloud Platform","display_in_url":true},"product_group":{"short_name":"security","element_name":"Security","display_in_url":false},"product":{"short_name":"access-governance","element_name":"Access Governance","display_in_url":true},"release":{"short_name":"latest","element_name":"Latest","display_in_url":false}}} </script>
<script type="application/ld+json"> {"@context":"https://schema.org","@type":"WebPage","name":"","datePublished":"2022-11-09 12:26:52 GMT","dateModified":"2023-09-08 11:32:47 GMT"} </script>
</head><body>
<article>
<h1 id="description-of-the-illustration-acceptrecommendationpng">Description of the illustration AcceptRecommendation.png</h1>
<p>This image shows that this permission is recommended for acceptance by Access Governance.</p>

<div class="footer copyrightlogo">
<p><a href="https://docs.oracle.com/pls/topic/lookup?ctx=en/legal&amp;id=cpyr" target="_blank" class="new-window">Copyright ©</a> 2023, Oracle and/or its affiliates.</p>
</div>
</article>
</body>
</html>
208 changes: 208 additions & 0 deletions examples/src/main/java/com/examples/demo/FileUploadExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package com.examples.demo;

import com.openai.client.OpenAIClient;
import com.openai.core.MultipartField;
import com.openai.models.files.FileCreateParams;
import com.openai.models.files.FileListPage;
import com.openai.models.files.FileListParams;
import com.openai.models.files.FileObject;
import com.openai.models.files.FilePurpose;
import com.oracle.genai.openai.OciOpenAI;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Example client that uploads a file to the OpenAI-compatible OCI Generative AI "Files" endpoint
* (and then lists files) using {@link com.oracle.genai.openai.OciOpenAI}.
*
* How to run:
*
* 1) Build the SDK once from the repo root (required because the examples depend on it):
* {@code mvn -DskipTests package}
*
* 2) Build the examples module:
* {@code cd examples}
* {@code mvn -DskipTests package}
*
* 3) Set the required environment variables:
* {@code export OCI_GENAI_DP_BASE_URL="https://<region-endpoint>/20231130/openai/v1"}
* {@code export OCI_COMPARTMENT_ID="ocid1.compartment.oc1...."}
* {@code export OCI_GENAI_PROJECT_ID="ocid1.generativeaiproject.oc1...."}
*
* Optional environment variables:
* {@code export OCI_AUTH_PROFILE="DEFAULT"} (or your local OCI CLI profile)
* {@code export OCI_OPENAI_AUTH_TYPE="security_token"}
*
* 4) Run from your IDE by invoking {@link #main(String[])}.
*
* Note: This example expects you have valid local OCI credentials (e.g. via {@code ~/.oci/config}
* and the selected profile) consistent with the auth type.
*/
public class FileUploadExample {
// Configuration is read from environment variables to keep this example portable.
//
// Required:
// OCI_GENAI_DP_BASE_URL e.g. https://dev.inference.generativeai.us-phoenix-1.oci.oraclecloud.com/20231130/openai/v1
// OCI_COMPARTMENT_ID ocid1.compartment.oc1....
// OCI_GENAI_PROJECT_ID ocid1.generativeaiproject.oc1....
//
// Optional:
// OCI_AUTH_PROFILE (default: DEFAULT)
// OCI_OPENAI_AUTH_TYPE (default: security_token)
private static final String ENV_BASE_URL = "OCI_GENAI_DP_BASE_URL";
private static final String ENV_COMPARTMENT_ID = "OCI_COMPARTMENT_ID";
private static final String ENV_PROJECT_ID = "OCI_GENAI_PROJECT_ID";
private static final String ENV_PROFILE = "OCI_AUTH_PROFILE";
private static final String ENV_AUTH_TYPE = "OCI_OPENAI_AUTH_TYPE";

private final OpenAIClient ociOpenAI;

public FileUploadExample() throws IOException {
String baseUrl = requireEnv(ENV_BASE_URL);
String compartmentId = requireEnv(ENV_COMPARTMENT_ID);
String projectId = requireEnv(ENV_PROJECT_ID);

String profile = envOrDefault(ENV_PROFILE, "DEFAULT");
String authType = envOrDefault(ENV_AUTH_TYPE, "security_token");
String opcRequestId = "genai_openai_file_upload_example";

Map<String, String> defaultHeaders = new HashMap<>();
defaultHeaders.put("opc-compartment-id", compartmentId);
defaultHeaders.put("opc-request-id", opcRequestId);
defaultHeaders.put("OpenAi-Project", projectId);

this.ociOpenAI = OciOpenAI.builder()
.defaultHeaders(defaultHeaders)
.baseUrl(baseUrl)
.compartmentId(compartmentId)
.authType(authType)
.profile(profile)
.logRequestsAndResponses("info")
.build();
}

public void close() {ociOpenAI.close();}


public FileObject uploadFile(final String filePath) {
java.io.File file = new java.io.File(filePath);
FileCreateParams build = FileCreateParams.builder()
.purpose(FilePurpose.USER_DATA)
.file(file.toPath())
.build();
return ociOpenAI.files().create(build);
}

/**
* Uploads file content from an {@link InputStream} while also providing a filename.
*
* @param fileName filename to attach to the multipart part
* @param inputStream file content
* @return uploaded file
*/
public FileObject uploadFile(final String fileName, final InputStream inputStream) {
System.out.println("Uploading file using the InputStream of file " + fileName);
Objects.requireNonNull(fileName, "fileName");
Objects.requireNonNull(inputStream, "inputStream");

// Refer to https://github.com/openai/openai-java/issues/284
FileCreateParams params = FileCreateParams.builder()
.purpose(FilePurpose.USER_DATA)
.file(MultipartField.<InputStream>builder()
.value(inputStream)
.filename(fileName)
.build())
.build();

return ociOpenAI.files().create(params);
}

public void get(final String id) {
Objects.requireNonNull(id, "The file ID should not be null");
System.out.println("Retrieving FileObject using the ID " + id);
FileObject fileObject = ociOpenAI.files().retrieve(id);
System.out.println("Retrieved file object " + fileObject);
}

public void delete (final String id) {
Objects.requireNonNull(id, "The file ID should not be null");
System.out.println("Deleting FileObject using the ID " + id);
ociOpenAI.files().delete(id);
System.out.println("File with ID " + id + " deleted successfully");
}

/**
* Uploads raw bytes.
*/
public FileObject uploadFile(final String fileName, final byte[] bytes) {
Objects.requireNonNull(bytes, "bytes");
System.out.println("Uploading file using the byte[]. But wrapping byte array with ByteArrayInputStream");

// Work around to pass file name as the FileCreateParams dont have any method to set the
// fileName. Not having fileName set will fail the upload
return uploadFile(fileName, new ByteArrayInputStream(bytes));
}

public FileListPage listFiles() {
FileListPage list = ociOpenAI.files().list(FileListParams.builder().build());
System.out.println("Total files list is " + list.data().size());
list.data().forEach(System.out::println);
return list;
}

public static void main(String[] args) throws IOException {
String path1 = "examples/sample-files/AcceptAccessReview.html";
String path2 = "examples/sample-files/AcceptHighRisk.html";
String path3 = "examples/sample-files/AcceptRecommendation.html";

FileUploadExample fileUploadExample = new FileUploadExample();

try {
System.out.println("Uploading (path): " + Path.of(path1).toAbsolutePath());
FileObject uploaded1 = fileUploadExample.uploadFile(path1);
System.out.println(uploaded1);

System.out.println("Uploading (fileName+inputStream): " + Path.of(path2).toAbsolutePath());
try (InputStream in = Files.newInputStream(Path.of(path2))) {
FileObject uploaded2 = fileUploadExample.uploadFile(Path.of(path2).getFileName().toString(), in);
System.out.println(uploaded2);
}

try (InputStream in = Files.newInputStream(Path.of(path3))) {
FileObject uploaded3 = fileUploadExample.uploadFile(Path.of(path3).getFileName().toString(),
in.readAllBytes());
System.out.println(uploaded3);
}

FileListPage fileListPage = fileUploadExample.listFiles();
if (!fileListPage.data().isEmpty()) {
String id = fileListPage.data().stream().findAny().get().id();
fileUploadExample.get(id);

fileListPage.data().forEach(fileObject -> fileUploadExample.delete(fileObject.id()));
}
} finally {
fileUploadExample.close();
}
}

private static String requireEnv(String key) {
String value = System.getenv(key);
if (value == null || value.isBlank()) {
throw new IllegalStateException("Missing required environment variable: " + key);
}
return value;
}

private static String envOrDefault(String key, String defaultValue) {
String value = System.getenv(key);
return (value == null || value.isBlank()) ? defaultValue : value;
}
}
Loading
Loading