-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[vector-database] Add support for Astra Collections
- Loading branch information
Showing
12 changed files
with
320 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rc/main/resources/META-INF/services/ai.langstream.ai.agents.datasource.DataSourceProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
ai.langstream.ai.agents.datasource.impl.AstraDataSource | ||
ai.langstream.ai.agents.datasource.impl.CassandraDataSourceProvider | ||
ai.langstream.ai.agents.datasource.impl.JdbcDataSourceProvider |
117 changes: 117 additions & 0 deletions
117
...rc/main/java/ai/langstream/agents/vector/astra/AstraCollectionsAssetsManagerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.agents.vector.astra; | ||
|
||
import ai.langstream.api.model.AssetDefinition; | ||
import ai.langstream.api.runner.assets.AssetManager; | ||
import ai.langstream.api.runner.assets.AssetManagerProvider; | ||
import ai.langstream.api.util.ConfigurationUtils; | ||
import io.stargate.sdk.doc.exception.CollectionNotFoundException; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class AstraCollectionsAssetsManagerProvider implements AssetManagerProvider { | ||
|
||
@Override | ||
public boolean supports(String assetType) { | ||
return "astra-collection".equals(assetType); | ||
} | ||
|
||
@Override | ||
public AssetManager createInstance(String assetType) { | ||
|
||
switch (assetType) { | ||
case "astra-collection": | ||
return new AstraDBCollectionAssetManager(); | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
private abstract static class BaseAstraAssetManager implements AssetManager { | ||
|
||
AstraCollectionsDataSource datasource; | ||
AssetDefinition assetDefinition; | ||
|
||
@Override | ||
public void initialize(AssetDefinition assetDefinition) { | ||
this.datasource = buildDataSource(assetDefinition); | ||
this.assetDefinition = assetDefinition; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
if (datasource != null) { | ||
datasource.close(); | ||
} | ||
} | ||
} | ||
|
||
private static class AstraDBCollectionAssetManager extends BaseAstraAssetManager { | ||
|
||
@Override | ||
public boolean assetExists() throws Exception { | ||
String collection = getCollection(); | ||
log.info("Checking if collection {} exists", collection); | ||
return datasource.getAstraDB().isCollectionExists(collection); | ||
} | ||
|
||
@Override | ||
public void deployAsset() throws Exception { | ||
int vectorDimension = getVectorDimension(); | ||
|
||
String collection = getCollection(); | ||
log.info("Create collection {} with vector dimension {}", collection, vectorDimension); | ||
datasource.getAstraDB().createCollection(collection, vectorDimension); | ||
} | ||
|
||
private String getCollection() { | ||
return ConfigurationUtils.getString("collection", null, assetDefinition.getConfig()); | ||
} | ||
|
||
private int getVectorDimension() { | ||
return ConfigurationUtils.getInt("vector-dimension", 1536, assetDefinition.getConfig()); | ||
} | ||
|
||
@Override | ||
public boolean deleteAssetIfExists() throws Exception { | ||
String collection = getCollection(); | ||
|
||
log.info("Deleting collection {}", collection); | ||
|
||
try { | ||
datasource.getAstraDB().deleteCollection(collection); | ||
return true; | ||
} catch (CollectionNotFoundException e) { | ||
log.info( | ||
"collection does not exist, maybe it was deleted by another agent ({})", | ||
e.toString()); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
private static AstraCollectionsDataSource buildDataSource(AssetDefinition assetDefinition) { | ||
AstraCollectionsDataSource dataSource = new AstraCollectionsDataSource(); | ||
Map<String, Object> datasourceDefinition = | ||
ConfigurationUtils.getMap("datasource", Map.of(), assetDefinition.getConfig()); | ||
Map<String, Object> configuration = | ||
ConfigurationUtils.getMap("configuration", Map.of(), datasourceDefinition); | ||
dataSource.initialize(configuration); | ||
return dataSource; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...or-agents/src/main/java/ai/langstream/agents/vector/astra/AstraCollectionsDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.agents.vector.astra; | ||
|
||
import ai.langstream.api.util.ConfigurationUtils; | ||
import com.datastax.oss.streaming.ai.datasource.QueryStepDataSource; | ||
import com.dtsx.astra.sdk.AstraDB; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class AstraCollectionsDataSource implements QueryStepDataSource { | ||
|
||
AstraDB astraDB; | ||
|
||
@Override | ||
public void initialize(Map<String, Object> dataSourceConfig) { | ||
log.info( | ||
"Initializing CassandraDataSource with config {}", | ||
ConfigurationUtils.redactSecrets(dataSourceConfig)); | ||
String astraToken = ConfigurationUtils.getString("token", "", dataSourceConfig); | ||
String astraEndpoint = ConfigurationUtils.getString("endpoint", "", dataSourceConfig); | ||
this.astraDB = new AstraDB(astraToken, astraEndpoint); | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public List<Map<String, Object>> fetchData(String query, List<Object> params) { | ||
if (log.isDebugEnabled()) { | ||
log.debug( | ||
"Executing query {} with params {} ({})", | ||
query, | ||
params, | ||
params.stream() | ||
.map(v -> v == null ? "null" : v.getClass().toString()) | ||
.collect(Collectors.joining(","))); | ||
} | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> executeStatement( | ||
String query, List<String> generatedKeys, List<Object> params) { | ||
if (log.isDebugEnabled()) { | ||
log.debug( | ||
"Executing statement {} with params {} ({})", | ||
query, | ||
params, | ||
params.stream() | ||
.map(v -> v == null ? "null" : v.getClass().toString()) | ||
.collect(Collectors.joining(","))); | ||
} | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public AstraDB getAstraDB() { | ||
return astraDB; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...s/src/main/java/ai/langstream/agents/vector/astra/AstraCollectionsDataSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.agents.vector.astra; | ||
|
||
import ai.langstream.ai.agents.datasource.DataSourceProvider; | ||
import com.datastax.oss.streaming.ai.datasource.QueryStepDataSource; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class AstraCollectionsDataSourceProvider implements DataSourceProvider { | ||
|
||
@Override | ||
public boolean supports(Map<String, Object> dataSourceConfig) { | ||
String service = (String) dataSourceConfig.get("service"); | ||
return "astra-collections".equals(service); | ||
} | ||
|
||
@Override | ||
public QueryStepDataSource createDataSourceImplementation( | ||
Map<String, Object> dataSourceConfig) { | ||
return new AstraCollectionsDataSource(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...vector-agents/src/main/java/ai/langstream/agents/vector/astra/AstraCollectionsWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.agents.vector.astra; | ||
|
||
import ai.langstream.api.database.VectorDatabaseWriter; | ||
import ai.langstream.api.database.VectorDatabaseWriterProvider; | ||
import ai.langstream.api.runner.code.Record; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class AstraCollectionsWriter implements VectorDatabaseWriterProvider { | ||
|
||
@Override | ||
public boolean supports(Map<String, Object> dataSourceConfig) { | ||
return "astra-collections".equals(dataSourceConfig.get("service")); | ||
} | ||
|
||
@Override | ||
public VectorDatabaseWriter createImplementation(Map<String, Object> datasourceConfig) { | ||
return new AstraCollectionsDatabaseWriter(datasourceConfig); | ||
} | ||
|
||
private static class AstraCollectionsDatabaseWriter implements VectorDatabaseWriter { | ||
|
||
private final Map<String, Object> datasourceConfig; | ||
|
||
public AstraCollectionsDatabaseWriter(Map<String, Object> datasourceConfig) { | ||
this.datasourceConfig = datasourceConfig; | ||
} | ||
|
||
@Override | ||
public void initialise(Map<String, Object> agentConfiguration) {} | ||
|
||
@Override | ||
public CompletableFuture<?> upsert(Record record, Map<String, Object> context) { | ||
return CompletableFuture.failedFuture(new UnsupportedOperationException()); | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
} | ||
} |
Oops, something went wrong.