diff --git a/oracle-example/src/main/java/OracleDocumentLoaderExample.java b/oracle-example/src/main/java/OracleDocumentLoaderExample.java new file mode 100644 index 00000000..75d73a26 --- /dev/null +++ b/oracle-example/src/main/java/OracleDocumentLoaderExample.java @@ -0,0 +1,116 @@ +import com.fasterxml.jackson.databind.ObjectMapper; +import dev.langchain4j.data.document.Document; +import dev.langchain4j.data.document.loader.oracle.DirectoryPreference; +import dev.langchain4j.data.document.loader.oracle.FilePreference; +import dev.langchain4j.data.document.loader.oracle.OracleDocumentLoader; +import dev.langchain4j.data.document.loader.oracle.TablePreference; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import oracle.ucp.jdbc.PoolDataSource; +import oracle.ucp.jdbc.PoolDataSourceFactory; + +/** + * Demonstrate loading documents from the file system or a table. + * The documents can be in any format supported by the Oracle Text filter + * including Word, PDF, HTML, and text files. If it is a rich text document + * like Word or PDF, it will be converted into plain text and contain any + * metadata associated with it. + * + * This example requires the following environment variables: + * ORACLE_JDBC_URL + * ORACLE_JDBC_USER + * ORACLE_JDBC_PASSWORD + * DEMO_FILE + * DEMO_DIRECTORY + * DEMO_OWNER + * DEMO_TABLE + * DEMO_COLUMN + */ +public class OracleDocumentLoaderExample { + + public static void main(String[] args) throws SQLException, IOException { + loadFromFile(); + loadFromDirectory(); + loadFromTable(); + } + + private static void loadFromFile() throws IOException, SQLException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // Can build pref as a string + // String pref = "{\"file\": \"...\"}"; + // Alternatively, can use FilePreference + ObjectMapper mapper = new ObjectMapper(); + FilePreference loaderPref = new FilePreference(); + loaderPref.setFilename(System.getenv("DEMO_FILE")); + String pref = mapper.writeValueAsString(loaderPref); + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + + List docs = loader.loadDocuments(pref); + for (Document doc : docs) { + System.out.println("metadata=" + doc.metadata()); + System.out.println("text=" + doc.text()); + } + } + + private static void loadFromDirectory() throws IOException, SQLException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // Can build pref as a string + // String pref = "{\"dir\": \"...\"}"; + // Alternatively, can use DirectoryPreference + ObjectMapper mapper = new ObjectMapper(); + DirectoryPreference loaderPref = new DirectoryPreference(); + loaderPref.setDirectory(System.getenv("DEMO_DIRECTORY")); + String pref = mapper.writeValueAsString(loaderPref); + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + + List docs = loader.loadDocuments(pref); + for (Document doc : docs) { + System.out.println("metadata=" + doc.metadata()); + System.out.println("text=" + doc.text()); + } + } + + private static void loadFromTable() throws IOException, SQLException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // Can build pref as a string + // String pref = "{\"owner\": \"...\", \"tablename\": \"...\", \"colname\": \"...\"}"; + // Alternatively, can use TablePreference + ObjectMapper mapper = new ObjectMapper(); + TablePreference loaderPref = new TablePreference(); + loaderPref.setOwner(System.getenv("DEMO_OWNER")); + loaderPref.setTableName(System.getenv("DEMO_TABLE")); + loaderPref.setColumnName(System.getenv("DEMO_COLUMN")); + String pref = mapper.writeValueAsString(loaderPref); + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + + List docs = loader.loadDocuments(pref); + for (Document doc : docs) { + System.out.println("metadata=" + doc.metadata()); + System.out.println("text=" + doc.text()); + } + } + +} diff --git a/oracle-example/src/main/java/OracleDocumentSplitterExample.java b/oracle-example/src/main/java/OracleDocumentSplitterExample.java new file mode 100644 index 00000000..d379432f --- /dev/null +++ b/oracle-example/src/main/java/OracleDocumentSplitterExample.java @@ -0,0 +1,46 @@ +import dev.langchain4j.data.document.Document; +import dev.langchain4j.data.document.loader.oracle.OracleDocumentLoader; +import dev.langchain4j.data.document.splitter.oracle.OracleDocumentSplitter; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import oracle.ucp.jdbc.PoolDataSource; +import oracle.ucp.jdbc.PoolDataSourceFactory; + +/** + * Demonstrate chunking or splitting text in a document. You can customize how + * to split the content such as by words, characters, or vocabulary (for tokens) + * to match a tokenizer in the preference. + * + * This example requires the following environment variables: + * ORACLE_JDBC_URL + * ORACLE_JDBC_USER + * ORACLE_JDBC_PASSWORD + * DEMO_FILE + */ +public class OracleDocumentSplitterExample { + + public static void main(String[] args) throws SQLException, IOException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + String loadPref = "{\"file\": \"" + System.getenv("DEMO_FILE") + "\"}"; + String splitPref = "{\"by\": \"words\", \"max\": 100}"; + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + OracleDocumentSplitter splitter = new OracleDocumentSplitter(conn, splitPref); + + List docs = loader.loadDocuments(loadPref); + for (Document doc : docs) { + String[] segments = splitter.split(doc.text()); + for (String segment : segments) { + System.out.println("segment=" + segment); + } + } + } +} diff --git a/oracle-example/src/main/java/OracleEmbeddingModelExample.java b/oracle-example/src/main/java/OracleEmbeddingModelExample.java new file mode 100644 index 00000000..bf6d803a --- /dev/null +++ b/oracle-example/src/main/java/OracleEmbeddingModelExample.java @@ -0,0 +1,99 @@ +import dev.langchain4j.data.embedding.Embedding; +import dev.langchain4j.data.segment.TextSegment; +import dev.langchain4j.model.oracle.OracleEmbeddingModel; +import dev.langchain4j.model.output.Response; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import oracle.ucp.jdbc.PoolDataSource; +import oracle.ucp.jdbc.PoolDataSourceFactory; + +/** + * Demonstrate getting the vector embeddings. You can customize which provider + * to use such as database for an ONNX model or with a third-party provider. + * + * This example requires the following environment variables: + * ORACLE_JDBC_URL + * ORACLE_JDBC_USER + * ORACLE_JDBC_PASSWORD + * DEMO_ONNX_DIR + * DEMO_ONNX_FILE + * DEMO_ONNX_MODEL + * DEMO_CREDENTIAL + */ +public class OracleEmbeddingModelExample { + + public static void main(String[] args) throws SQLException { + databaseEmbeddings(); + thirdPartyEmbeddings(); + } + + public static void databaseEmbeddings() throws SQLException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // load an ONNX model into the database + // remember to create a directory alias with + // create or replace directory MODEL_DIR as '/path/to/model'; + OracleEmbeddingModel.loadOnnxModel( + conn, + System.getenv("DEMO_ONNX_DIR"), + System.getenv("DEMO_ONNX_FILE"), + System.getenv("DEMO_ONNX_MODEL")); + + String pref = "{\"provider\": \"database\", \"model\": \"" + System.getenv("DEMO_ONNX_MODEL") + "\"}"; + + OracleEmbeddingModel model = new OracleEmbeddingModel(conn, pref); + + // embed a single string + Response response = model.embed("I love Java"); + Embedding embedding = response.content(); + System.out.println(embedding); + + // embed a list of text + List textSegments = new ArrayList<>(); + textSegments.add(TextSegment.from("I like soccer.")); + textSegments.add(TextSegment.from("I love Stephen King.")); + textSegments.add(TextSegment.from("The weather is good today.")); + Response> resp = model.embedAll(textSegments); + System.out.println(resp.content()); + } + + public static void thirdPartyEmbeddings() throws SQLException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // For a third-party provider, remember to create a credential + // with dbms_vector.create_credential() and then refer to it here + String pref = "{\n" + + " \"provider\": \"ocigenai\",\n" + + " \"credential_name\": \"" + System.getenv("DEMO_CREDENTIAL") + "\",\n" + + " \"url\": \"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/embedText\",\n" + + " \"model\": \"cohere.embed-english-light-v3.0\"\n" + + "}"; + + OracleEmbeddingModel model = new OracleEmbeddingModel(conn, pref); + + // embed a single string + Response response = model.embed("I love Java"); + Embedding embedding = response.content(); + System.out.println(embedding); + + // embed a list of text + List textSegments = new ArrayList<>(); + textSegments.add(TextSegment.from("I like soccer.")); + textSegments.add(TextSegment.from("I love Stephen King.")); + textSegments.add(TextSegment.from("The weather is good today.")); + Response> resp = model.embedAll(textSegments); + System.out.println(resp.content()); + } +} diff --git a/oracle-example/src/main/java/OracleIngestExample.java b/oracle-example/src/main/java/OracleIngestExample.java new file mode 100644 index 00000000..b30132f5 --- /dev/null +++ b/oracle-example/src/main/java/OracleIngestExample.java @@ -0,0 +1,120 @@ +import dev.langchain4j.data.document.Document; +import dev.langchain4j.data.document.loader.oracle.OracleDocumentLoader; +import dev.langchain4j.data.document.splitter.oracle.OracleDocumentSplitter; +import dev.langchain4j.data.embedding.Embedding; +import dev.langchain4j.data.segment.TextSegment; +import dev.langchain4j.model.oracle.OracleEmbeddingModel; +import dev.langchain4j.store.embedding.EmbeddingMatch; +import dev.langchain4j.store.embedding.EmbeddingSearchRequest; +import dev.langchain4j.store.embedding.EmbeddingSearchResult; +import dev.langchain4j.store.embedding.EmbeddingStoreIngestor; +import static dev.langchain4j.store.embedding.oracle.CreateOption.CREATE_OR_REPLACE; +import dev.langchain4j.store.embedding.oracle.EmbeddingTable; +import dev.langchain4j.store.embedding.oracle.OracleEmbeddingStore; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import oracle.ucp.jdbc.PoolDataSource; +import oracle.ucp.jdbc.PoolDataSourceFactory; + +/** + * Demonstrate how to ingest documents using an OracleEmbeddingStore to hide + * the manual steps of ingesting into an embedding store for search/retrieval. + * + * This example requires the following environment variables: + * ORACLE_JDBC_URL + * ORACLE_JDBC_USER + * ORACLE_JDBC_PASSWORD + * DEMO_ONNX_DIR + * DEMO_ONNX_FILE + * DEMO_ONNX_MODEL + * DEMO_DIRECTORY + */ +public class OracleIngestExample { + + public static void main(String[] args) throws SQLException, IOException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // load the ONNX model for embedding + OracleEmbeddingModel.loadOnnxModel( + conn, + System.getenv("DEMO_ONNX_DIR"), + System.getenv("DEMO_ONNX_FILE"), + System.getenv("DEMO_ONNX_MODEL")); + + // set the loader, splitter, and embedding preferences + String loaderPref = "{\"dir\": \"" + System.getenv("DEMO_DIRECTORY") + "\"}"; + String splitterPref = "{\"by\": \"words\", \"max\": 100}"; + String embedderPref = "{\"provider\": \"database\", \"model\": \"" + System.getenv("DEMO_ONNX_MODEL") + "\"}"; + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + OracleDocumentSplitter splitter = new OracleDocumentSplitter(conn, splitterPref); + OracleEmbeddingModel embeddingModel = new OracleEmbeddingModel(conn, embedderPref); + + // setup the embedding store + + // set column names for the output table + String tableName = "TEST"; + String idColumn = "ID"; + String embeddingColumn = "EMBEDDING"; + String textColumn = "TEXT"; + String metadataColumn = "METADATA"; + + // build() should create a table with the configured names + OracleEmbeddingStore embeddingStore = OracleEmbeddingStore.builder() + .dataSource(pds) + .embeddingTable(EmbeddingTable.builder() + .createOption(CREATE_OR_REPLACE) + .name(tableName) + .idColumn(idColumn) + .embeddingColumn(embeddingColumn) + .textColumn(textColumn) + .metadataColumn(metadataColumn) + .build()) + .build(); + + // build an ingestor with the following components + EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder() + .documentSplitter(splitter) + .embeddingModel(embeddingModel) + .embeddingStore(embeddingStore) + .build(); + + // load and ingest the documents + // this will call the splitter to split into segments, + // embedding model to get the embeddings, and then store the + // embeddings into the embedding store for further search / retrieval + List docs = loader.loadDocuments(loaderPref); + ingestor.ingest(docs); + + // get the question + String question = "Who is John Doe?"; + + // get the vector representation + Embedding questionAsVector = embeddingModel.embed(question).content(); + + // perform the vector search + EmbeddingSearchResult result = embeddingStore.search( + EmbeddingSearchRequest.builder() + .queryEmbedding(questionAsVector) + .maxResults(3) + .minScore(0.6) + .build() + ); + + // display the results + System.out.println(question); + List> results = result.matches(); + for (EmbeddingMatch match : results) { + System.out.println("\nScore: " + match.score()); + System.out.println("Metadata: " + match.embedded().metadata()); + System.out.println("Text: " + match.embedded().text()); + } + } +} diff --git a/oracle-example/src/main/java/OracleIngestManualExample.java b/oracle-example/src/main/java/OracleIngestManualExample.java new file mode 100644 index 00000000..376f5de0 --- /dev/null +++ b/oracle-example/src/main/java/OracleIngestManualExample.java @@ -0,0 +1,131 @@ +import dev.langchain4j.data.document.Document; +import dev.langchain4j.data.document.loader.oracle.OracleDocumentLoader; +import dev.langchain4j.data.document.splitter.oracle.OracleDocumentSplitter; +import dev.langchain4j.data.embedding.Embedding; +import dev.langchain4j.data.segment.TextSegment; +import dev.langchain4j.model.oracle.OracleEmbeddingModel; +import dev.langchain4j.model.oracle.OracleSummaryLanguageModel; +import dev.langchain4j.model.output.Response; +import dev.langchain4j.store.embedding.EmbeddingMatch; +import dev.langchain4j.store.embedding.EmbeddingSearchRequest; +import static dev.langchain4j.store.embedding.oracle.CreateOption.CREATE_OR_REPLACE; +import dev.langchain4j.store.embedding.oracle.EmbeddingTable; +import dev.langchain4j.store.embedding.oracle.OracleEmbeddingStore; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import oracle.ucp.jdbc.PoolDataSource; +import oracle.ucp.jdbc.PoolDataSourceFactory; + +/** + * Demonstrate how to ingest documents by using the low-level LangChain4j + * APIs with OracleDocumentLoader, OracleSummaryLanguageModel, + * OracleDocumentSplitter, and OracleEmbeddingModel to load documents, + * generate a summary, split the text, and get the vector embeddings. + * + * This example requires the following environment variables: + * ORACLE_JDBC_URL + * ORACLE_JDBC_USER + * ORACLE_JDBC_PASSWORD + * DEMO_ONNX_DIR + * DEMO_ONNX_FILE + * DEMO_ONNX_MODEL + * DEMO_DIRECTORY + */ +public class OracleIngestManualExample { + + public static void main(String[] args) throws SQLException, IOException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // load the ONNX model for embedding + OracleEmbeddingModel.loadOnnxModel( + conn, + System.getenv("DEMO_ONNX_DIR"), + System.getenv("DEMO_ONNX_FILE"), + System.getenv("DEMO_ONNX_MODEL")); + + // set the loader, splitter, embedding, and summary model preferences + String loaderPref = "{\"dir\": \"" + System.getenv("DEMO_DIRECTORY") + "\"}"; + String splitterPref = "{\"by\": \"words\", \"max\": 100}"; + String embedderPref = "{\"provider\": \"database\", \"model\": \"" + System.getenv("DEMO_ONNX_MODEL") + "\"}"; + String summaryPref = "{\"provider\": \"database\", \"glevel\": \"S\"}"; + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + OracleDocumentSplitter splitter = new OracleDocumentSplitter(conn, splitterPref); + OracleEmbeddingModel embeddingModel = new OracleEmbeddingModel(conn, embedderPref); + OracleSummaryLanguageModel summaryModel = new OracleSummaryLanguageModel(conn, summaryPref); + + // Load the documents with the information that you would like to search on + List docs = loader.loadDocuments(loaderPref); + + // Split document into segments + List allSegments = new ArrayList<>(); + for (Document doc : docs) { + // Example of modifying the metadata + // For each doc, add a summary that will get copied into each segment + Response resp = summaryModel.generate(doc.text()); + doc.metadata().put("summary", resp.content()); + + List segments = splitter.split(doc); + allSegments.addAll(segments); + } + + // Embed segments (convert them into vectors that represent the meaning) using embedding model + List embeddings = embeddingModel.embedAll(allSegments).content(); + + // setup the embedding store + + // set column names for the output table + String tableName = "TEST"; + String idColumn = "ID"; + String embeddingColumn = "EMBEDDING"; + String textColumn = "TEXT"; + String metadataColumn = "METADATA"; + + // build() should create a table with the configured names + OracleEmbeddingStore embeddingStore = OracleEmbeddingStore.builder() + .dataSource(pds) + .embeddingTable(EmbeddingTable.builder() + .createOption(CREATE_OR_REPLACE) + .name(tableName) + .idColumn(idColumn) + .embeddingColumn(embeddingColumn) + .textColumn(textColumn) + .metadataColumn(metadataColumn) + .build()) + .build(); + + // Store embeddings into embedding store for further search / retrieval + embeddingStore.addAll(embeddings, allSegments); + + // Get the question + String question = "Who is John Doe?"; + + // Embed the question + Embedding questionEmbedding = embeddingModel.embed(question).content(); + + // Find relevant embeddings in embedding store by semantic similarity + EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder() + .queryEmbedding(questionEmbedding) + .maxResults(3) + .minScore(0.6) + .build(); + List> relevantEmbeddings = embeddingStore.search(embeddingSearchRequest).matches(); + + // display the results + System.out.println(question); + for (EmbeddingMatch match : relevantEmbeddings) { + System.out.println("\nScore: " + match.score()); + System.out.println("Metadata: " + match.embedded().metadata()); + System.out.println("Text: " + match.embedded().text()); + } + } + +} diff --git a/oracle-example/src/main/java/OracleSummaryLanguageModelExample.java b/oracle-example/src/main/java/OracleSummaryLanguageModelExample.java new file mode 100644 index 00000000..cc987184 --- /dev/null +++ b/oracle-example/src/main/java/OracleSummaryLanguageModelExample.java @@ -0,0 +1,80 @@ +import dev.langchain4j.data.document.Document; +import dev.langchain4j.data.document.loader.oracle.OracleDocumentLoader; +import dev.langchain4j.model.oracle.OracleSummaryLanguageModel; +import dev.langchain4j.model.output.Response; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import oracle.ucp.jdbc.PoolDataSource; +import oracle.ucp.jdbc.PoolDataSourceFactory; + +/** + * Demonstrate summarizing a document. You can customize which provider to use + * such as database for extracting a summary with Oracle Text or with a + * third-party provider. + * + * Define the following environment variables before running + * ORACLE_JDBC_URL + * ORACLE_JDBC_USER + * ORACLE_JDBC_PASSWORD + * DEMO_FILE + * DEMO_CREDENTIAL + */ +public class OracleSummaryLanguageModelExample { + + public static void main(String[] args) throws SQLException, IOException { + databaseSummary(); + thirdPartySummary(); + } + + public static void databaseSummary() throws SQLException, IOException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + String loadPref = "{\"file\": \"" + System.getenv("DEMO_FILE") + "\"}"; + String summaryPref = "{\"provider\": \"database\", \"gLevel\": \"S\"}"; + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + OracleSummaryLanguageModel model = new OracleSummaryLanguageModel(conn, summaryPref); + + List docs = loader.loadDocuments(loadPref); + for (Document doc : docs) { + Response resp = model.generate(doc.text()); + System.out.println("summary=" + resp.content()); + } + } + + public static void thirdPartySummary() throws SQLException, IOException { + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); + pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); + pds.setURL(System.getenv("ORACLE_JDBC_URL")); + pds.setUser(System.getenv("ORACLE_JDBC_USER")); + pds.setPassword(System.getenv("ORACLE_JDBC_PASSWORD")); + Connection conn = pds.getConnection(); + + // For a third-party provider, remember to create a credential + // with dbms_vector.create_credential() and then refer to it here + String loadPref = "{\"file\": \"" + System.getenv("DEMO_FILE") + "\"}"; + String summaryPref = "{\n" + + " \"provider\": \"ocigenai\",\n" + + " \"credential_name\": \"" + System.getenv("DEMO_CREDENTIAL") + "\",\n" + + " \"url\": \"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/chat\",\n" + + " \"model\": \"cohere.command-r-08-2024\",\n" + + "}"; + String proxy = System.getenv("DEMO_PROXY"); + + OracleDocumentLoader loader = new OracleDocumentLoader(conn); + OracleSummaryLanguageModel model = new OracleSummaryLanguageModel(conn, summaryPref, proxy); + + List docs = loader.loadDocuments(loadPref); + for (Document doc : docs) { + Response resp = model.generate(doc.text()); + System.out.println("summary=" + resp.content()); + } + } +} diff --git a/oracle-example/src/main/resources/example-files/biography-of-john-doe.txt b/oracle-example/src/main/resources/example-files/biography-of-john-doe.txt new file mode 100644 index 00000000..272c7db8 --- /dev/null +++ b/oracle-example/src/main/resources/example-files/biography-of-john-doe.txt @@ -0,0 +1,49 @@ +John Doe: An Imaginary Luminary + +Early Life and Education +John Doe was born on April 1, 1980, in the quaint town of Fictionville, USA. +The only child of Jane and Joe Doe, he exhibited an early inclination towards creativity and innovation. +Growing up, John was fascinated by the stories his mother, a local librarian, would read to him, +which sparked his love for literature and storytelling. + +Doe attended Fictionville High School, where he excelled academically and was a star athlete. +His teachers often noted his exceptional ability to blend creativity with logic. +John's high school science fair project, a prototype of an environmentally-friendly engine, won him the +National Young Innovators Award. +This achievement earned him a scholarship to Prestige University, where he majored in Mechanical Engineering +and minored in Creative Writing. + +Career Beginnings +After graduating with honors, Doe joined a renowned tech firm, InnoTech, as a junior engineer. +His innovative approach and dedication quickly propelled him up the ranks. +By the age of 30, John had become the lead engineer of InnoTech's research and development department, +where he spearheaded groundbreaking projects in renewable energy. + +However, his love for writing never waned. +In his spare time, John wrote short stories, many of which were published in esteemed literary magazines. +His unique blend of scientific knowledge and creative storytelling earned him a devoted following. + +Entrepreneurial Ventures +At 35, Doe ventured into entrepreneurship, founding EcoSolutions, a company focused on sustainable technologies. +Under his leadership, EcoSolutions developed the "GreenDrive," an eco-friendly engine that revolutionized +the automotive industry. +His success in business was paralleled by his literary accomplishments; he published his debut novel, +"The Inventor's Dilemma," which became a bestseller. + +Philanthropy and Personal Life +John Doe's success in business and literature was matched by his commitment to philanthropy. +He established the "Doe Foundation" to support education and environmental initiatives. +His efforts earned him various humanitarian awards. + +In his personal life, John is known to be a private individual. +He married his college sweetheart, Emily, and they have two children. +The family resides in a sustainable home in Fictionville, where Doe enjoys gardening and mentoring young entrepreneurs. + +Legacy +John Doe's legacy is multifaceted: a pioneering engineer, a celebrated author, and a dedicated philanthropist. +His contributions to technology and literature have left an indelible mark on the world, +inspiring countless individuals to pursue their dreams with passion and determination. + +As he often says, "Innovation is the intersection of imagination and reality." +John Doe's life is a testament to this belief, a blend of the fantastical and the tangible, +making him not just a figure of success, but a symbol of the limitless potential of the human spirit. \ No newline at end of file diff --git a/oracle-example/src/main/resources/example-files/story-about-happy-carrot.docx b/oracle-example/src/main/resources/example-files/story-about-happy-carrot.docx new file mode 100644 index 00000000..c6f1e951 Binary files /dev/null and b/oracle-example/src/main/resources/example-files/story-about-happy-carrot.docx differ diff --git a/oracle-example/src/main/resources/example-files/story-about-happy-carrot.pdf b/oracle-example/src/main/resources/example-files/story-about-happy-carrot.pdf new file mode 100644 index 00000000..76969ed6 Binary files /dev/null and b/oracle-example/src/main/resources/example-files/story-about-happy-carrot.pdf differ diff --git a/oracle-example/src/main/resources/example-files/story-about-happy-carrot.txt b/oracle-example/src/main/resources/example-files/story-about-happy-carrot.txt new file mode 100644 index 00000000..66ae976d --- /dev/null +++ b/oracle-example/src/main/resources/example-files/story-about-happy-carrot.txt @@ -0,0 +1,28 @@ +Once upon a time in the town of VeggieVille, there lived a cheerful carrot named Charlie. +Charlie was a radiant carrot, always beaming with joy and positivity. +His vibrant orange skin and lush green top were a sight to behold, but it was his infectious laughter and warm personality that really set him apart. + +Charlie had a diverse group of friends, each a vegetable with their own unique characteristics. +There was Bella the blushing beetroot, always ready with a riddle or two; Timmy the timid tomato, a gentle soul with a heart of gold; and Percy the prankster potato, whose jokes always brought a smile to everyone's faces. +Despite their differences, they shared a close bond, their friendship as robust as their natural goodness. + +Their lives were filled with delightful adventures, from playing hide-and-seek amidst the leafy lettuce to swimming in the dewy droplets that pooled on the cabbage leaves. +Their favorite place, though, was the sunlit corner of the vegetable patch, where they would bask in the warmth of the sun, share stories, and have hearty laughs. + +One day, a bunch of pesky caterpillars invaded VeggieVille. +The vegetables were terrified, fearing they would be nibbled to nothingness. +But Charlie, with his usual sunny disposition, had an idea. +He proposed they host a grand feast for the caterpillars, with the juiciest leaves from the outskirts of the town. +Charlie's optimism was contagious, and his friends eagerly joined in to prepare the feast. + +When the caterpillars arrived, they were pleasantly surprised. +They enjoyed the feast and were so impressed with the vegetables' hospitality that they promised not to trouble VeggieVille again. +In return, they agreed to help pollinate the flowers, contributing to a more lush and vibrant VeggieVille. + +Charlie's idea had saved the day, but he humbly attributed the success to their teamwork and friendship. +They celebrated their victory with a grand party, filled with laughter, dance, and merry games. +That night, under the twinkling stars, they made a pact to always stand by each other, come what may. + +From then on, the story of the happy carrot and his friends spread far and wide, a tale of friendship, unity, and positivity. +Charlie, Bella, Timmy, and Percy continued to live their joyful lives, their laughter echoing through VeggieVille. +And so, the tale of the happy carrot and his friends serves as a reminder that no matter the challenge, with optimism, teamwork, and a bit of creativity, anything is possible. \ No newline at end of file diff --git a/oracle-example/src/main/resources/example-files/sub-directory/story-about-happy-carrot.md b/oracle-example/src/main/resources/example-files/sub-directory/story-about-happy-carrot.md new file mode 100644 index 00000000..66ae976d --- /dev/null +++ b/oracle-example/src/main/resources/example-files/sub-directory/story-about-happy-carrot.md @@ -0,0 +1,28 @@ +Once upon a time in the town of VeggieVille, there lived a cheerful carrot named Charlie. +Charlie was a radiant carrot, always beaming with joy and positivity. +His vibrant orange skin and lush green top were a sight to behold, but it was his infectious laughter and warm personality that really set him apart. + +Charlie had a diverse group of friends, each a vegetable with their own unique characteristics. +There was Bella the blushing beetroot, always ready with a riddle or two; Timmy the timid tomato, a gentle soul with a heart of gold; and Percy the prankster potato, whose jokes always brought a smile to everyone's faces. +Despite their differences, they shared a close bond, their friendship as robust as their natural goodness. + +Their lives were filled with delightful adventures, from playing hide-and-seek amidst the leafy lettuce to swimming in the dewy droplets that pooled on the cabbage leaves. +Their favorite place, though, was the sunlit corner of the vegetable patch, where they would bask in the warmth of the sun, share stories, and have hearty laughs. + +One day, a bunch of pesky caterpillars invaded VeggieVille. +The vegetables were terrified, fearing they would be nibbled to nothingness. +But Charlie, with his usual sunny disposition, had an idea. +He proposed they host a grand feast for the caterpillars, with the juiciest leaves from the outskirts of the town. +Charlie's optimism was contagious, and his friends eagerly joined in to prepare the feast. + +When the caterpillars arrived, they were pleasantly surprised. +They enjoyed the feast and were so impressed with the vegetables' hospitality that they promised not to trouble VeggieVille again. +In return, they agreed to help pollinate the flowers, contributing to a more lush and vibrant VeggieVille. + +Charlie's idea had saved the day, but he humbly attributed the success to their teamwork and friendship. +They celebrated their victory with a grand party, filled with laughter, dance, and merry games. +That night, under the twinkling stars, they made a pact to always stand by each other, come what may. + +From then on, the story of the happy carrot and his friends spread far and wide, a tale of friendship, unity, and positivity. +Charlie, Bella, Timmy, and Percy continued to live their joyful lives, their laughter echoing through VeggieVille. +And so, the tale of the happy carrot and his friends serves as a reminder that no matter the challenge, with optimism, teamwork, and a bit of creativity, anything is possible. \ No newline at end of file