Skip to content

Commit 8e328b7

Browse files
committed
update langchain
1 parent a6a4cb8 commit 8e328b7

File tree

6 files changed

+124
-29
lines changed

6 files changed

+124
-29
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import com.datastax.astra.client.Collection;
2+
import com.datastax.astra.client.DataAPIClient;
3+
import com.datastax.astra.client.DataAPIOptions;
4+
import com.datastax.astra.client.Database;
5+
import com.datastax.astra.client.model.Document;
6+
import com.datastax.astra.client.model.FindIterable;
7+
8+
import static com.datastax.astra.client.model.SimilarityMetric.COSINE;
9+
10+
public class QuickStartTraining {
11+
12+
public static void main(String[] args) {
13+
String astraToken = System.getenv("ASTRA_DB_APPLICATION_TOKEN");
14+
String astraApiEndpoint = System.getenv("ASTRA_DB_API_ENDPOINT");
15+
16+
DataAPIClient client = new DataAPIClient(astraToken, DataAPIOptions.builder().build());
17+
System.out.println("Connected to AstraDB");
18+
19+
Database db = client.getDatabase(astraApiEndpoint, "default_keyspace");
20+
System.out.println("Connected to Database.");
21+
22+
// Create a collection. The default similarity metric is cosine.
23+
Collection<Document> collection = db
24+
.createCollection("vector_test", 5, COSINE);
25+
System.out.println("Created a collection");
26+
27+
collection.insertMany(
28+
new Document("1")
29+
.append("text", "ChatGPT integrated sneakers that talk to you")
30+
.vector(new float[]{0.1f, 0.15f, 0.3f, 0.12f, 0.05f}),
31+
new Document("2")
32+
.append("text", "An AI quilt to help you sleep forever")
33+
.vector(new float[]{0.45f, 0.09f, 0.01f, 0.2f, 0.11f}),
34+
new Document("3")
35+
.append("text", "A deep learning display that controls your mood")
36+
.vector(new float[]{0.1f, 0.05f, 0.08f, 0.3f, 0.6f}));
37+
System.out.println("Inserted documents into the collection");
38+
39+
// Perform a similarity search
40+
FindIterable<Document> resultsSet = collection.find(
41+
new float[]{0.15f, 0.1f, 0.1f, 0.35f, 0.55f},
42+
10
43+
);
44+
resultsSet.forEach(System.out::println);
45+
46+
// Delete the collection
47+
collection.drop();
48+
System.out.println("Deleted the collection");
49+
}
50+
}

examples/src/main/java/Quickstart.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import com.datastax.astra.client.Collection;
22
import com.datastax.astra.client.DataAPIClient;
3+
import com.datastax.astra.client.DataAPIOptions;
34
import com.datastax.astra.client.Database;
45
import com.datastax.astra.client.model.Document;
56
import com.datastax.astra.client.model.FindIterable;
@@ -18,7 +19,7 @@ public static void main(String[] args) {
1819

1920
// Initialize the client. The keyspace parameter is optional if you use
2021
// "default_keyspace".
21-
DataAPIClient client = new DataAPIClient(astraToken);
22+
DataAPIClient client = new DataAPIClient(astraToken, DataAPIOptions.builder().build());
2223
System.out.println("Connected to AstraDB");
2324

2425
Database db = client.getDatabase(astraApiEndpoint, "default_keyspace");

examples/src/main/java/com/datastax/astra/client/database/CreateCollection.java

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,33 @@
99

1010
public class CreateCollection {
1111
public static void main(String[] args) {
12-
Database db = new Database("API_ENDPOINT", "TOKEN");
12+
13+
Database db = new Database(
14+
System.getenv("ASTRA_DB_API_ENDPOINT"),
15+
System.getenv("ASTRA_DB_APPLICATION_TOKEN"));
1316

1417
// Create a non-vector collection
1518
Collection<Document> simple1 = db.createCollection("col");
1619

17-
Collection<Document> vector1 = db
18-
.createCollection("vector1", 14, SimilarityMetric.DOT_PRODUCT);
20+
// Default Id Collection
21+
Collection<Document> defaultId = db.createCollection("defaultId", CollectionOptions
22+
.builder()
23+
.defaultIdType(CollectionIdTypes.OBJECT_ID)
24+
.build());
25+
26+
// -- Indexing
27+
Collection<Document> indexingDeny = db.createCollection("indexing1", CollectionOptions
28+
.builder()
29+
.indexingDeny("blob")
30+
.build());
31+
// Create a collection with indexing (allow) - cannot use allow and denay at the same time
32+
Collection<Document> indexingAllow = db.createCollection("allow1", CollectionOptions
33+
.builder()
34+
.indexingAllow("metadata")
35+
.build());
36+
37+
// Vector
38+
Collection<Document> vector1 = db.createCollection("vector1", 14, SimilarityMetric.DOT_PRODUCT);
1939

2040
// Create a vector collection
2141
Collection<Document> vector2 = db.createCollection("vector2", CollectionOptions
@@ -24,22 +44,27 @@ public static void main(String[] args) {
2444
.vectorSimilarity(SimilarityMetric.EUCLIDEAN)
2545
.build());
2646

27-
// Create a collection with indexing (deny)
28-
Collection<Document> indexing1 = db.createCollection("indexing1", CollectionOptions
29-
.builder()
30-
.indexingDeny("blob")
31-
.build());
47+
// Create a collection for the db
48+
Collection<Document> collection_vectorize_header = db.createCollection(
49+
"collection_vectorize_header",
50+
// Create collection with a Service in vectorize (No API KEY)
51+
CollectionOptions.builder()
52+
.vectorDimension(1536)
53+
.vectorSimilarity(SimilarityMetric.DOT_PRODUCT)
54+
.vectorize("openai", "text-embedding-ada-002")
55+
.build());
56+
57+
// Create a collection for the db
58+
Collection<Document> collection_vectorize_shared_key = db.createCollection(
59+
"collection_vectorize_shared_key",
60+
// Create collection with a Service in vectorize (No API KEY)
61+
CollectionOptions.builder()
62+
.vectorDimension(1536)
63+
.vectorSimilarity(SimilarityMetric.DOT_PRODUCT)
64+
.vectorize("openai", "text-embedding-ada-002", "OPENAI_API_KEY" )
65+
.build());
66+
3267

33-
// Create a collection with indexing (allow) - cannot use allow and denay at the same time
34-
Collection<Document> allow1 = db.createCollection("allow1", CollectionOptions
35-
.builder()
36-
.indexingAllow("metadata")
37-
.build());
3868

39-
// Enforce default id type could be objectid, uuid, uuivv6, uuidv7
40-
Collection<Document> defaultId = db.createCollection("defaultId", CollectionOptions
41-
.builder()
42-
.defaultIdType(CollectionIdTypes.OBJECT_ID)
43-
.build());
4469
}
4570
}

langchain4j-astradb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</parent>
1313

1414
<properties>
15-
<langchain4j.version>0.33.0</langchain4j.version>
15+
<langchain4j.version>0.34.0</langchain4j.version>
1616
</properties>
1717

1818
<dependencyManagement>

langchain4j-astradb/src/main/java/com/datastax/astra/langchain4j/store/embedding/AstraDbEmbeddingStore.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ public List<String> addAll(List<Embedding> embeddings) {
175175
*/
176176
public List<String> addAllVectorize(List<TextSegment> textSegmentList) {
177177
return addAll(null, textSegmentList);
178-
179178
}
179+
180180
/**
181181
* Add multiple embeddings as a single action.
182182
*
@@ -301,6 +301,25 @@ public void removeAll(dev.langchain4j.store.embedding.filter.Filter filter) {
301301
astraDBCollection.deleteMany(AstraDbFilterMapper.map(filter));
302302
}
303303

304+
/**
305+
* Truncate the Repository
306+
*
307+
*/
308+
@Override
309+
public void removeAll() {
310+
astraDBCollection.deleteAll();
311+
}
312+
313+
/**
314+
* Accessing the underlying collection to have even better filter capabilities, SORT, projection
315+
*
316+
* @return
317+
* associated AstraDB collection
318+
*/
319+
public Collection<Document> getCollection() {
320+
return astraDBCollection;
321+
}
322+
304323
/**
305324
* Semantic search with metadata filtering.
306325
*

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2222

2323
<!-- Third Party Libraries -->
24-
<devops-sdk.version>1.2.7</devops-sdk.version>
24+
<devops-sdk.version>1.2.8</devops-sdk.version>
2525
<slf4j.version>2.0.9</slf4j.version>
26-
<logback.version>1.5.6</logback.version>
26+
<logback.version>1.5.7</logback.version>
2727
<jackson.version>2.17.2</jackson.version>
2828
<lombok.version>1.18.34</lombok.version>
2929
<retry4j.version>0.15.0</retry4j.version>
30-
<awaitility.version>4.2.1</awaitility.version>
30+
<awaitility.version>4.2.2</awaitility.version>
3131
<mockwebserver.version>4.12.0</mockwebserver.version>
32-
<uuid-generator.version>5.0.0</uuid-generator.version>
32+
<uuid-generator.version>5.1.0</uuid-generator.version>
3333

3434
<!-- Test -->
3535
<test.skipped>false</test.skipped>
@@ -42,18 +42,18 @@
4242
<maven.plugin.compiler.target>11</maven.plugin.compiler.target>
4343
<version.maven.plugin.compiler>3.13.0</version.maven.plugin.compiler>
4444
<version.maven.plugin.coveralls>4.3.0</version.maven.plugin.coveralls>
45-
<version.maven.plugin.dependency>3.7.1</version.maven.plugin.dependency>
45+
<version.maven.plugin.dependency>3.8.0</version.maven.plugin.dependency>
4646
<version.maven.plugin.enforcer>3.5.0</version.maven.plugin.enforcer>
47-
<version.maven.plugin.gpg>3.2.4</version.maven.plugin.gpg>
47+
<version.maven.plugin.gpg>3.2.5</version.maven.plugin.gpg>
4848
<version.maven.plugin.jacoco>0.8.12</version.maven.plugin.jacoco>
49-
<version.maven.plugin.javadoc>3.8.0</version.maven.plugin.javadoc>
49+
<version.maven.plugin.javadoc>3.10.0</version.maven.plugin.javadoc>
5050
<version.maven.plugin.jar>3.4.2</version.maven.plugin.jar>
5151
<version.maven.plugin.license>2.4.0</version.maven.plugin.license>
5252
<version.maven.plugin.nexus>1.7.0</version.maven.plugin.nexus>
5353
<version.maven.plugin.release>3.1.1</version.maven.plugin.release>
5454
<version.maven.plugin.resources>3.3.1</version.maven.plugin.resources>
5555
<version.maven.plugin.source>3.3.1</version.maven.plugin.source>
56-
<version.maven.plugin.surefire>3.2.5</version.maven.plugin.surefire>
56+
<version.maven.plugin.surefire>3.5.0</version.maven.plugin.surefire>
5757

5858
</properties>
5959

0 commit comments

Comments
 (0)