Skip to content

Commit 63e03cf

Browse files
authored
Update langchain4j.adoc
small updates
1 parent e89fb5a commit 63e03cf

File tree

1 file changed

+49
-75
lines changed

1 file changed

+49
-75
lines changed

modules/genai-ecosystem/pages/langchain4j.adoc

Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,23 @@ include::https://github.com/langchain4j/langchain4j-examples/raw/main/neo4j-exam
3535

3636
An example is avalable at: https://github.com/langchain4j/langchain4j-examples/tree/main/neo4j-example
3737

38+
////
39+
Full include might be a bit too much?
3840
[source,java]
3941
----
4042
include::https://github.com/langchain4j/langchain4j-examples/raw/main/neo4j-example/src/main/java/Neo4jEmbeddingStoreExample.java[]
4143
----
44+
////
45+
46+
=== Connection Setup
4247

43-
To create a Neo4jEmbeddingStore, we can write:
4448
[source,java]
4549
----
4650
Neo4jEmbeddingStore embeddingStore = Neo4jEmbeddingStore.builder().<builderParameters>.build();
4751
48-
49-
5052
// we can use with `withBasicAuth`
5153
.withBasicAuth(neo4jContainer.getBoltUrl(), USERNAME, ADMIN_PASSWORD)
54+
5255
// as alternative to driver:
5356
Driver driver = GraphDatabase.driver(neo4jContainer.getBoltUrl(), AuthTokens.basic(USERNAME, ADMIN_PASSWORD));
5457
----
@@ -72,16 +75,17 @@ Here is the complete builder list:
7275
| textProperty | "text" | the text property name
7376
| indexName | "vector" | the vector index name
7477
| databaseName | "neo4j" | the database name
75-
| retrievalQuery | "RETURN properties(node) AS metadata, node.`idProperty` AS `idProperty`, node.`textProperty` AS `textProperty`, node.`embeddingProperty` AS `embeddingProperty`, score" | the retrieval query
78+
| retrievalQuery | "RETURN properties(node) AS metadata, node.idProperty AS idProperty, node.textProperty AS textProperty, node.embeddingProperty AS embeddingProperty, score" | the retrieval query
7679
|===
7780

78-
=== Usage Examples
81+
== Usage Examples
7982

8083
LangChain4j provides the following classes for Neo4j integration:
84+
8185
- `Neo4jEmbeddingStore`: Implements the EmbeddingStore interface, enabling storing and querying vector embeddings in a Neo4j database.
8286
- `Neo4jText2CypherRetriever`: Implements the ContentRetriever interface for generating and executing Cypher queries from user questions, improving content retrieval from Neo4j databases.
8387

84-
==== Neo4jEmbeddingStore
88+
=== Neo4jEmbeddingStore
8589

8690
We can define an `Neo4jEmbeddingStore` with the required Java Driver instance and dimension and an optional label name in this way:
8791

@@ -96,33 +100,17 @@ Neo4jEmbeddingStore embeddingStore = Neo4jEmbeddingStore.builder()
96100
.build();
97101
----
98102

99-
We can use `withBasicAuth` as an alternative to `driver` creation:
100-
[source,java]
101-
----
102-
Neo4jEmbeddingStore embeddingStore = Neo4jEmbeddingStore.builder()
103-
.withBasicAuth('<boltURL>', '<username>', '<password>')
104-
.<otherBuilderParameters>
105-
.build();
106-
107-
/* instead of
108-
Driver driver = GraphDatabase.driver('<boltURL>', AuthTokens.basic('<username>', '<password>'));
109-
Neo4jEmbeddingStore embeddingStore = Neo4jEmbeddingStore.builder()
110-
.withDriver(driver)
111-
.<otherBuilderParameters>
112-
.build()
113-
*/
114-
115-
----
116-
117103
To add embeddings, execute:
104+
118105
[source,java]
119106
----
120107
Embedding embedding = embeddingModel.embed("embedText").content();
121108
String id = embeddingStore.add(embedding);
122109
// output: id of the embedding
123110
----
124111

125-
To add embeddings with id:
112+
To add embeddings with an id:
113+
126114
[source,java]
127115
----
128116
String id = randomUUID();
@@ -131,7 +119,18 @@ embeddingStore.add(id, embedding);
131119
// output: id of the embedding
132120
----
133121

134-
To add embeddings with segment:
122+
Add multiple embeddings:
123+
124+
[source,java]
125+
----
126+
Embedding firstEmbedding = embeddingModel.embed("firstEmbedText").content();
127+
Embedding secondEmbedding = embeddingModel.embed("secondEmbedText").content();
128+
List<String> ids = embeddingStore.addAll(asList(firstEmbedding, secondEmbedding));
129+
// output: list of the embedding ids
130+
----
131+
132+
To add embeddings with a `TextSegment`:
133+
135134
[source,java]
136135
----
137136
TextSegment segment = TextSegment.from(randomUUID());
@@ -140,7 +139,8 @@ String id = embeddingStore.add(embedding, segment);
140139
// output: id of the embedding
141140
----
142141

143-
To add embeddings with segment and metadata:
142+
To add embeddings with `TextSegment` and metadata:
143+
144144
[source,java]
145145
----
146146
TextSegment segment = TextSegment.from(randomUUID(), Metadata.from(METADATA_KEY, "test-value"));
@@ -149,20 +149,24 @@ String id = embeddingStore.add(embedding, segment);
149149
// output: id of the embedding
150150
----
151151

152-
Search embeddings:
152+
Add multiple embeddings with segments
153+
153154
[source,java]
154155
----
155-
Embedding embedding = embeddingModel.embed("embedText").content();
156-
String id = embeddingStore.add(embedding);
157-
final EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
158-
.queryEmbedding(embedding)
159-
.maxResults(10)
160-
.build();
161-
final List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(request).matches();
162-
// output: list of embeddings
156+
TextSegment firstSegment = TextSegment.from("firstText");
157+
Embedding firstEmbedding = embeddingModel.embed(firstSegment.text()).content();
158+
159+
TextSegment secondSegment = TextSegment.from("secondText");
160+
Embedding secondEmbedding = embeddingModel.embed(secondSegment.text()).content();
161+
162+
List<String> ids = embeddingStore.addAll(
163+
asList(firstEmbedding, secondEmbedding),
164+
asList(firstSegment, secondSegment)
165+
);
166+
// output: list of the embedding ids
163167
----
164168

165-
To search embeddings with `minScore`:
169+
Search embeddings:
166170

167171
[source,java]
168172
----
@@ -171,13 +175,15 @@ String id = embeddingStore.add(embedding);
171175
final EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
172176
.queryEmbedding(embedding)
173177
.maxResults(10)
174-
.minScore(0.15)
178+
.minScore(0.15) // Optional `minScore`
175179
.build();
180+
176181
final List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(request).matches();
177182
// output: list of embeddings
178183
----
179184

180185
To search embeddings with segment with custom metadata prefix:
186+
181187
[source,java]
182188
----
183189
String metadataPrefix = "metadata.";
@@ -192,6 +198,7 @@ Neo4jEmbeddingStore embeddingStore = Neo4jEmbeddingStore.builder()
192198
.build();
193199
194200
String metadataCompleteKey = metadataPrefix + METADATA_KEY;
201+
195202
TextSegment segment = TextSegment.from(randomUUID(), Metadata.from(METADATA_KEY, "test-value"));
196203
Embedding embedding = embeddingModel.embed(segment.text()).content();
197204
@@ -206,7 +213,8 @@ final List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(request
206213
// output: list of embeddings
207214
----
208215

209-
Search embeddings with segment with metadata and custom id prop:
216+
Search embeddings with segment with metadata-prefix and custom id property name:
217+
210218
[source,java]
211219
----
212220
String metadataPrefix = "metadata.";
@@ -236,44 +244,10 @@ final List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(request
236244
// output: list of embeddings
237245
----
238246

239-
Add multiple embeddings
240-
[source,java]
241-
----
242-
Embedding firstEmbedding = embeddingModel.embed("firstEmbedText").content();
243-
Embedding secondEmbedding = embeddingModel.embed("secondEmbedText").content();
244-
List<String> ids = embeddingStore.addAll(asList(firstEmbedding, secondEmbedding));
245-
// output: list of the embedding ids
246-
247-
final EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
248-
.queryEmbedding(firstEmbedding)
249-
.maxResults(10)
250-
.build();
251-
final List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(request).matches();
252-
// output: list of embeddings
253-
----
254-
255-
Add multiple embeddings with segments
256-
[source,java]
257-
----
258-
TextSegment firstSegment = TextSegment.from("firstText");
259-
Embedding firstEmbedding = embeddingModel.embed(firstSegment.text()).content();
260-
TextSegment secondSegment = TextSegment.from("secondText");
261-
Embedding secondEmbedding = embeddingModel.embed(secondSegment.text()).content();
247+
=== Neo4jText2CypherRetriever
262248

263-
List<String> ids = embeddingStore.addAll(
264-
asList(firstEmbedding, secondEmbedding),
265-
asList(firstSegment, secondSegment)
266-
); // output: list of the embedding ids
267-
268-
final EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
269-
.queryEmbedding(firstEmbedding)
270-
.maxResults(10)
271-
.build();
272-
final List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.search(request).matches();
273-
// output: list of embeddings
274-
----
249+
// todo more explanation
275250

276-
==== Neo4jText2CypherRetriever
277251
[source,java]
278252
----
279253
// create dataset

0 commit comments

Comments
 (0)