dev.langchain4j.store.embedding.oracle.EmbeddingTable Maven / Gradle / Ivy
package dev.langchain4j.store.embedding.oracle;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.UUID;
import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;
/**
*
* Represents a database table where embeddings, text, and metadata are stored. The columns of this table are listed
* below.
*
*
*
* Table Columns
*
*
* Name
* Type
* Description
*
* id
* VARCHAR(36)
*
* Primary key. Used to store {@link UUID} strings which are generated by
* {@link OracleEmbeddingStore#add(Embedding)},
* {@link OracleEmbeddingStore#add(Embedding, TextSegment)}, {@link OracleEmbeddingStore#addAll(List)}, and
* {@link OracleEmbeddingStore#add(Embedding, TextSegment)}
*
*
* embedding
* VECTOR(*, FLOAT32)
*
* Stores the {@link Embedding#vector()} passed to {@link OracleEmbeddingStore#add(Embedding)},
* {@link OracleEmbeddingStore#add(Embedding, TextSegment)}, {@link OracleEmbeddingStore#addAll(List)}, and
* {@link OracleEmbeddingStore#add(Embedding, TextSegment)}. Never stores NULL.
*
*
* text
* CLOB
*
* Stores the {@link TextSegment#text()} passed to {@link OracleEmbeddingStore#add(Embedding, TextSegment)} and
* {@link OracleEmbeddingStore#addAll(List, List)}. Stores NULL when {@link OracleEmbeddingStore#add(Embedding)}
* and {@link OracleEmbeddingStore#addAll(List)} are called.
*
*
* metadata
* JSON
*
* Stores the {@link TextSegment#metadata()} passed to {@link OracleEmbeddingStore#add(Embedding, TextSegment)}
* and {@link OracleEmbeddingStore#addAll(List, List)}. Stores NULL when
* {@link OracleEmbeddingStore#add(Embedding)} and {@link OracleEmbeddingStore#addAll(List)} are called.
*
*
*
* The column names listed above are used by default, but alternative names may be configured using
* {@link Builder} methods.
*
*
*/
public final class EmbeddingTable {
/** Option which configures how the {@link #create(DataSource)} method creates this table */
private final CreateOption createOption;
/** The name of this table */
private final String name;
/** Name of a column which stores an id. */
private final String idColumn;
/** Name of a column which stores an embedding. */
private final String embeddingColumn;
/** Name of a column which stores text. */
private final String textColumn;
/** Name of a column which stores metadata. */
private final String metadataColumn;
private EmbeddingTable(Builder builder) {
createOption = builder.createOption;
name = builder.name;
idColumn = builder.idColumn;
embeddingColumn = builder.embeddingColumn;
textColumn = builder.textColumn;
metadataColumn = builder.metadataColumn;
}
/**
* Creates a table configured by the {@link Builder} of this EmbeddingTable. No table is created if the Builder was
* configured with {@link CreateOption#CREATE_NONE}.
*
* @param dataSource Data source that connects to an Oracle Database where the table is (possibly) created.
*
* @throws SQLException If an error prevents the table from being created.
*/
void create(DataSource dataSource) throws SQLException {
if (createOption == CreateOption.CREATE_NONE)
return;
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
) {
if (createOption == CreateOption.CREATE_OR_REPLACE)
statement.addBatch("DROP TABLE IF EXISTS " + name);
statement.addBatch("CREATE TABLE IF NOT EXISTS " + name
+ "(" + idColumn + " VARCHAR(36) NOT NULL, "
+ embeddingColumn + " VECTOR(*, FLOAT32) NOT NULL, "
+ textColumn + " CLOB, "
+ metadataColumn + " JSON, "
+ "PRIMARY KEY (" + idColumn + "))");
statement.executeBatch();
}
}
/**
* Returns the name of this table.
*
* @return The name of this table. Not null.
*/
public String name() {
return name;
}
/**
* Returns the name of this table's ID column.
*
* @return The name of this table's ID column. Not null.
*/
public String idColumn() {
return idColumn;
}
/**
* Returns the name of this table's embedding column.
*
* @return The name of this table's embedding column. Not null.
*/
public String embeddingColumn() {
return embeddingColumn;
}
/**
* Returns the name of this table's text column.
*
* @return The name of this table's text column. Not null.
*/
public String textColumn() {
return textColumn;
}
/**
* Returns the name of this table's metadata column.
*
* @return The name of this table's text column. Not null.
*/
public String metadataColumn() {
return metadataColumn;
}
/**
* Returns a builder that configures a new EmbeddingTable.
*
* @return An EmbeddingTable builder. Not null.
*/
public static Builder builder() {
return new Builder();
}
/**
* A builder that configures and builds an {@link EmbeddingTable}.
*/
public static class Builder {
// These fields are specified by method level JavaDocs
private CreateOption createOption = CreateOption.CREATE_NONE;
private String name;
private String idColumn = "id";
private String embeddingColumn = "embedding";
private String textColumn = "text";
private String metadataColumn = "metadata";
private Builder() {}
/**
* Configures the option to create (or not create) a table. The default is {@link CreateOption#CREATE_NONE},
* which means that no attempt is made to create a table.
*
* @param createOption Option for creating the index. Not null.
*
* @return This builder. Not null.
*/
public Builder createOption(CreateOption createOption) {
ensureNotNull(createOption, "createOption");
this.createOption = createOption;
return this;
}
/**
* Configures the name of a table where embeddings are stored and retrieved from. A name must be configured,
* there is no default name.
*
* @param name Name of database table. Not null.
*
* @return This builder. Not null.
*/
public Builder name(String name) {
ensureNotNull(name, "name");
this.name = name;
return this;
}
/**
* Configures the name of a column which stores an id. The default name is "id".
*
* @param idColumn Name of the id column. Not null.
*
* @return This builder. Not null.
*/
public Builder idColumn(String idColumn) {
ensureNotNull(idColumn, "idColumn");
this.idColumn = idColumn;
return this;
}
/**
* Configures the name of a column which stores an embedding. The default name is "embedding".
*
* @param embeddingColumn Name of the id column. Not null.
*
* @return This builder. Not null.
*/
public Builder embeddingColumn(String embeddingColumn) {
ensureNotNull(embeddingColumn, "embeddingColumn");
this.embeddingColumn = embeddingColumn;
return this;
}
/**
* Configures the name of a column which stores text. The default name is "text".
*
* @param textColumn Name of the text column. Not null.
*
* @return This builder. Not null.
*/
public Builder textColumn(String textColumn) {
ensureNotNull(textColumn, "textColumn");
this.textColumn = textColumn;
return this;
}
/**
* Configures the name of a column which stores metadata. The default name is "metadata".
*
* @param metadataColumn Name of the metadata column. Not null.
*
* @return This builder. Not null.
*/
public Builder metadataColumn(String metadataColumn) {
ensureNotNull(metadataColumn, "metadataColumn");
this.metadataColumn = metadataColumn;
return this;
}
/**
* Returns a new EmbeddingTable configured by this builder.
*
* @return A new EmbeddingTable. Not null.
*
* @throws IllegalArgumentException If this builder is missing any required configuration.
*/
public EmbeddingTable build() {
// Check required parameters
ensureNotNull(name, "name");
return new EmbeddingTable(this);
}
}
}