-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add vector search and tests * add api request limit per user * add tests for limiter * add validation and tests for title and description max length
- Loading branch information
1 parent
3cd1320
commit 45a7e53
Showing
36 changed files
with
969 additions
and
161 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
52 changes: 52 additions & 0 deletions
52
backend/src/main/java/com/github/jonashonecker/backend/ticket/EmbeddingService.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,52 @@ | ||
package com.github.jonashonecker.backend.ticket; | ||
|
||
import com.github.jonashonecker.backend.ticket.domain.embedding.EmbeddingRequestDTO; | ||
import com.github.jonashonecker.backend.ticket.domain.embedding.EmbeddingResponseDTO; | ||
import com.github.jonashonecker.backend.ticket.domain.ticket.TicketWithTitleAndDescription; | ||
import com.github.jonashonecker.backend.ticket.exception.EmbeddingFailedException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
public class EmbeddingService { | ||
|
||
private final RestClient restClient; | ||
|
||
public EmbeddingService( | ||
@Value("${app.openai-api-key}") String apiKey, | ||
@Value("${app.openai-embedding-baseUrl}") String baseUrl | ||
) { | ||
this.restClient = RestClient.builder() | ||
.baseUrl(baseUrl) | ||
.defaultHeader("Authorization", "Bearer " + apiKey) | ||
.build(); | ||
} | ||
|
||
public List<Double> getEmbeddingVectorForTicket(TicketWithTitleAndDescription ticket) { | ||
return getEmbeddingVector("<h1>" + ticket.title() + "</h1>" + ticket.description()); | ||
} | ||
|
||
public List<Double> getEmbeddingVectorForSearchText(String searchText) { | ||
return getEmbeddingVector(searchText); | ||
} | ||
|
||
private List<Double> getEmbeddingVector(String text) { | ||
EmbeddingResponseDTO response = this.restClient.post() | ||
.body(new EmbeddingRequestDTO(text)) | ||
.retrieve() | ||
.body(EmbeddingResponseDTO.class); | ||
|
||
if (response == null) { | ||
throw new EmbeddingFailedException("Failed to retrieve embedding vector for ticket"); | ||
} | ||
|
||
return response.data() | ||
.stream() | ||
.filter(o -> "embedding".equals(o.object())) | ||
.flatMap(o -> o.embedding().stream()) | ||
.toList(); | ||
} | ||
} |
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
backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketRepository.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
52 changes: 52 additions & 0 deletions
52
...d/src/main/java/com/github/jonashonecker/backend/ticket/TicketRepositoryVectorSearch.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,52 @@ | ||
package com.github.jonashonecker.backend.ticket; | ||
|
||
import com.github.jonashonecker.backend.ticket.domain.ticket.Ticket; | ||
import com.mongodb.client.AggregateIterable; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoCollection; | ||
import org.bson.conversions.Bson; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.mongodb.client.model.Aggregates.vectorSearch; | ||
import static com.mongodb.client.model.search.SearchPath.fieldPath; | ||
|
||
@Repository | ||
public class TicketRepositoryVectorSearch { | ||
|
||
private final MongoClient mongoClient; | ||
|
||
@Value("${spring.data.mongodb.database}") | ||
private String databaseName; | ||
|
||
@Value("${vectorSearch.index.name}") | ||
private String indexName; | ||
|
||
@Value("${vectorSearch.index.fieldPath}") | ||
private String fieldPath; | ||
|
||
public TicketRepositoryVectorSearch(MongoClient mongoClient) { | ||
this.mongoClient = mongoClient; | ||
} | ||
|
||
private MongoCollection<Ticket> getTicketCollection() { | ||
return mongoClient.getDatabase(databaseName).getCollection("tickets", Ticket.class); | ||
} | ||
|
||
public List<Ticket> findTicketsByVector(List<Double> embedding) { | ||
int numCandidates = 100; | ||
int limit = 5; | ||
List<Bson> pipeline = List.of( | ||
vectorSearch( | ||
fieldPath(fieldPath), | ||
embedding, | ||
indexName, | ||
numCandidates, | ||
limit)); | ||
AggregateIterable<Ticket> iterable = getTicketCollection().aggregate(pipeline, Ticket.class); | ||
return iterable.into(new ArrayList<>()); | ||
} | ||
} |
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
11 changes: 0 additions & 11 deletions
11
backend/src/main/java/com/github/jonashonecker/backend/ticket/domain/NewTicketDTO.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
backend/src/main/java/com/github/jonashonecker/backend/ticket/domain/UpdateTicketDTO.java
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...in/java/com/github/jonashonecker/backend/ticket/domain/embedding/EmbeddingRequestDTO.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,10 @@ | ||
package com.github.jonashonecker.backend.ticket.domain.embedding; | ||
|
||
public record EmbeddingRequestDTO( | ||
String input, | ||
String model | ||
) { | ||
public EmbeddingRequestDTO(String input) { | ||
this(input, "text-embedding-3-large"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...n/java/com/github/jonashonecker/backend/ticket/domain/embedding/EmbeddingResponseDTO.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,8 @@ | ||
package com.github.jonashonecker.backend.ticket.domain.embedding; | ||
|
||
import java.util.List; | ||
|
||
public record EmbeddingResponseDTO( | ||
List<EmbeddingResponseDataDTO> data | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
...va/com/github/jonashonecker/backend/ticket/domain/embedding/EmbeddingResponseDataDTO.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,9 @@ | ||
package com.github.jonashonecker.backend.ticket.domain.embedding; | ||
|
||
import java.util.List; | ||
|
||
public record EmbeddingResponseDataDTO( | ||
String object, | ||
List<Double> embedding | ||
) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...onecker/backend/ticket/domain/Status.java → .../backend/ticket/domain/ticket/Status.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
Oops, something went wrong.