3535
3636import org .springframework .ai .document .Document ;
3737import org .springframework .ai .document .DocumentMetadata ;
38- import org .springframework .ai .embedding .BatchingStrategy ;
3938import org .springframework .ai .embedding .EmbeddingModel ;
4039import org .springframework .ai .embedding .EmbeddingOptionsBuilder ;
41- import org .springframework .ai .embedding .TokenCountBatchingStrategy ;
4240import org .springframework .ai .observation .conventions .VectorStoreProvider ;
4341import org .springframework .ai .observation .conventions .VectorStoreSimilarityMetric ;
4442import org .springframework .ai .util .JacksonUtils ;
152150 * @author Thomas Vitale
153151 * @author Soby Chacko
154152 * @author Sebastien Deleuze
153+ * @author Jihoon Kim
155154 * @since 1.0.0
156155 */
157156public class PgVectorStore extends AbstractObservationVectorStore implements InitializingBean {
@@ -162,6 +161,8 @@ public class PgVectorStore extends AbstractObservationVectorStore implements Ini
162161
163162 public static final String DEFAULT_TABLE_NAME = "vector_store" ;
164163
164+ public static final PgIdType DEFAULT_ID_TYPE = PgIdType .UUID ;
165+
165166 public static final String DEFAULT_VECTOR_INDEX_NAME = "spring_ai_vector_index" ;
166167
167168 public static final String DEFAULT_SCHEMA_NAME = "public" ;
@@ -187,6 +188,8 @@ public class PgVectorStore extends AbstractObservationVectorStore implements Ini
187188
188189 private final String schemaName ;
189190
191+ private final PgIdType idType ;
192+
190193 private final boolean schemaValidation ;
191194
192195 private final boolean initializeSchema ;
@@ -224,6 +227,7 @@ protected PgVectorStore(PgVectorStoreBuilder builder) {
224227 : this .vectorTableName + "_index" ;
225228
226229 this .schemaName = builder .schemaName ;
230+ this .idType = builder .idType ;
227231 this .schemaValidation = builder .vectorTableValidationsEnabled ;
228232
229233 this .jdbcTemplate = builder .jdbcTemplate ;
@@ -272,13 +276,13 @@ private void insertOrUpdateBatch(List<Document> batch, List<Document> documents,
272276 public void setValues (PreparedStatement ps , int i ) throws SQLException {
273277
274278 var document = batch .get (i );
279+ var id = convertIdToPgType (document .getId ());
275280 var content = document .getText ();
276281 var json = toJson (document .getMetadata ());
277282 var embedding = embeddings .get (documents .indexOf (document ));
278283 var pGvector = new PGvector (embedding );
279284
280- StatementCreatorUtils .setParameterValue (ps , 1 , SqlTypeValue .TYPE_UNKNOWN ,
281- UUID .fromString (document .getId ()));
285+ StatementCreatorUtils .setParameterValue (ps , 1 , SqlTypeValue .TYPE_UNKNOWN , id );
282286 StatementCreatorUtils .setParameterValue (ps , 2 , SqlTypeValue .TYPE_UNKNOWN , content );
283287 StatementCreatorUtils .setParameterValue (ps , 3 , SqlTypeValue .TYPE_UNKNOWN , json );
284288 StatementCreatorUtils .setParameterValue (ps , 4 , SqlTypeValue .TYPE_UNKNOWN , pGvector );
@@ -303,6 +307,19 @@ private String toJson(Map<String, Object> map) {
303307 }
304308 }
305309
310+ private Object convertIdToPgType (String id ) {
311+ if (this .initializeSchema ) {
312+ return UUID .fromString (id );
313+ }
314+
315+ return switch (getIdType ()) {
316+ case UUID -> UUID .fromString (id );
317+ case TEXT -> id ;
318+ case INTEGER , SERIAL -> Integer .valueOf (id );
319+ case BIGSERIAL -> Long .valueOf (id );
320+ };
321+ }
322+
306323 @ Override
307324 public Optional <Boolean > doDelete (List <String > idList ) {
308325 int updateCount = 0 ;
@@ -412,6 +429,10 @@ private String getFullyQualifiedTableName() {
412429 return this .schemaName + "." + this .vectorTableName ;
413430 }
414431
432+ private PgIdType getIdType () {
433+ return this .idType ;
434+ }
435+
415436 private String getVectorTableName () {
416437 return this .vectorTableName ;
417438 }
@@ -489,6 +510,12 @@ public enum PgIndexType {
489510
490511 }
491512
513+ public enum PgIdType {
514+
515+ UUID , TEXT , INTEGER , SERIAL , BIGSERIAL
516+
517+ }
518+
492519 /**
493520 * Defaults to CosineDistance. But if vectors are normalized to length 1 (like OpenAI
494521 * embeddings), use inner product (NegativeInnerProduct) for best performance.
@@ -584,6 +611,8 @@ public static final class PgVectorStoreBuilder extends AbstractVectorStoreBuilde
584611
585612 private String vectorTableName = PgVectorStore .DEFAULT_TABLE_NAME ;
586613
614+ private PgIdType idType = PgVectorStore .DEFAULT_ID_TYPE ;
615+
587616 private boolean vectorTableValidationsEnabled = PgVectorStore .DEFAULT_SCHEMA_VALIDATION ;
588617
589618 private int dimensions = PgVectorStore .INVALID_EMBEDDING_DIMENSION ;
@@ -614,6 +643,11 @@ public PgVectorStoreBuilder vectorTableName(String vectorTableName) {
614643 return this ;
615644 }
616645
646+ public PgVectorStoreBuilder idType (PgIdType idType ) {
647+ this .idType = idType ;
648+ return this ;
649+ }
650+
617651 public PgVectorStoreBuilder vectorTableValidationsEnabled (boolean vectorTableValidationsEnabled ) {
618652 this .vectorTableValidationsEnabled = vectorTableValidationsEnabled ;
619653 return this ;
0 commit comments