File tree 4 files changed +104
-2
lines changed
4 files changed +104
-2
lines changed Original file line number Diff line number Diff line change 7
7
<groupId >dev.langchain4j</groupId >
8
8
<artifactId >neo4j-example</artifactId >
9
9
<version >1.0.0-beta4</version >
10
-
10
+ <parent >
11
+ <groupId >org.springframework.boot</groupId >
12
+ <artifactId >spring-boot-starter-parent</artifactId >
13
+ <version >3.2.5</version >
14
+ <relativePath />
15
+ </parent >
16
+
11
17
<properties >
12
18
<maven .compiler.source>17</maven .compiler.source>
13
19
<maven .compiler.target>17</maven .compiler.target>
14
20
<project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
21
+ <java .version>17</java .version>
15
22
</properties >
16
23
17
24
<dependencies >
18
-
25
+ <dependency >
26
+ <groupId >org.springframework.boot</groupId >
27
+ <artifactId >spring-boot-starter-web</artifactId >
28
+ </dependency >
29
+ <dependency >
30
+ <groupId >dev.langchain4j</groupId >
31
+ <artifactId >langchain4j-spring-boot-starter</artifactId >
32
+ <version >${project.version} </version >
33
+ </dependency >
19
34
<dependency >
20
35
<groupId >dev.langchain4j</groupId >
21
36
<artifactId >langchain4j-community-neo4j</artifactId >
22
37
<version >${project.version} </version >
23
38
</dependency >
39
+ <dependency >
40
+ <groupId >dev.langchain4j</groupId >
41
+ <artifactId >langchain4j-community-neo4j-spring-boot-starter</artifactId >
42
+ <version >${project.version} </version >
43
+ </dependency >
24
44
25
45
<dependency >
26
46
<groupId >dev.langchain4j</groupId >
Original file line number Diff line number Diff line change
1
+ package com .example .demo ;
2
+
3
+ import dev .langchain4j .data .embedding .Embedding ;
4
+ import dev .langchain4j .data .segment .TextSegment ;
5
+ import dev .langchain4j .model .embedding .EmbeddingModel ;
6
+ import dev .langchain4j .store .embedding .EmbeddingStore ;
7
+ import dev .langchain4j .store .embedding .EmbeddingSearchRequest ;
8
+ import org .springframework .web .bind .annotation .*;
9
+
10
+ import java .util .List ;
11
+
12
+ @ RestController
13
+ @ RequestMapping ("/api/embeddings" )
14
+ public class EmbeddingController {
15
+
16
+ private final EmbeddingStore <TextSegment > store ;
17
+ private final EmbeddingModel model ;
18
+
19
+ public EmbeddingController (EmbeddingStore <TextSegment > store , EmbeddingModel model ) {
20
+ this .store = store ;
21
+ this .model = model ;
22
+ }
23
+
24
+ @ PostMapping ("/add" )
25
+ public String add (@ RequestBody String text ) {
26
+ TextSegment segment = TextSegment .from (text );
27
+ Embedding embedding = model .embed (text ).content ();
28
+ return store .add (embedding , segment );
29
+ }
30
+
31
+ @ PostMapping ("/search" )
32
+ public List <String > search (@ RequestBody String query ) {
33
+ Embedding queryEmbedding = model .embed (query ).content ();
34
+ EmbeddingSearchRequest request = EmbeddingSearchRequest .builder ()
35
+ .queryEmbedding (queryEmbedding )
36
+ .maxResults (5 )
37
+ .build ();
38
+ return store .search (request ).matches ()
39
+ .stream ()
40
+ .map (i -> i .embedded ().text ()).toList ();
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .demo ;
2
+
3
+ import dev .langchain4j .model .embedding .onnx .allminilml6v2 .AllMiniLmL6V2EmbeddingModel ;
4
+ import org .springframework .boot .SpringApplication ;
5
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6
+ import org .springframework .context .annotation .Bean ;
7
+
8
+ /**
9
+ * NOTE:
10
+ * This example assumes we have a Neo4j instance with Bolt URI bolt://localhost:7687, username 'neo4j' and password 'pass1234'
11
+ * as specified in the application.properties.
12
+ * If needed, change the properties values to address a running instance.
13
+ *
14
+ * To add an embedding
15
+ * curl -X POST localhost:8083/api/embeddings/add -H "Content-Type: text/plain" -d "embeddingTest"
16
+ *
17
+ * To search embeddings
18
+ * curl -X POST localhost:8083/api/embeddings/search -H "Content-Type: text/plain" -d "querySearchTest"
19
+ */
20
+ @ SpringBootApplication
21
+ public class SpringBootExample {
22
+
23
+ public static void main (String [] args ) {
24
+ SpringApplication .run (SpringBootExample .class , args );
25
+ }
26
+
27
+ @ Bean
28
+ public AllMiniLmL6V2EmbeddingModel embeddingModel () {
29
+ return new AllMiniLmL6V2EmbeddingModel ();
30
+ }
31
+
32
+ }
Original file line number Diff line number Diff line change
1
+ spring.application.name =LangChain4j Spring Boot Example
2
+ server.port =8083
3
+
4
+ langchain4j.community.neo4j.auth.uri =bolt://localhost:7687
5
+ langchain4j.community.neo4j.auth.user =neo4j
6
+ langchain4j.community.neo4j.auth.password =pass1234
7
+ langchain4j.community.neo4j.label =CustomLabel
8
+ langchain4j.community.neo4j.dimension =384
You can’t perform that action at this time.
0 commit comments