Skip to content

Commit

Permalink
Merge pull request #14 from dotCMS/issue-27473-embeddings-not-updatin…
Browse files Browse the repository at this point in the history
…g-correctly

dotCMS/core#27473 delete from all indices instead of only the default
  • Loading branch information
wezell authored Jan 31, 2024
2 parents 41a8127 + 7458e64 commit 66706e4
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/dotcms/ai/db/EmbeddingsDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.Map;
import java.util.TreeMap;

import static com.dotcms.ai.db.EmbeddingsDTO.ALL_INDICES;

public class EmbeddingsDB {


Expand Down Expand Up @@ -285,7 +287,7 @@ List<Object> appendParams(StringBuilder sql, EmbeddingsDTO dto) {
sql.append(" and host=? ");
params.add(dto.host);
}
if (UtilMethods.isSet(dto.indexName)) {
if (UtilMethods.isSet(dto.indexName) && !ALL_INDICES.equals(dto.indexName)) {
sql.append(" and lower(index_name)=lower(?) ");
params.add(dto.indexName);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/dotcms/ai/db/EmbeddingsDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class EmbeddingsDTO implements Serializable {
public final float temperature;
private final String[] operators = {"<->", "<=>", "<#>"};

public static final String ALL_INDICES = "all";

private EmbeddingsDTO(Builder builder) {
this.embeddings = (builder.embeddings == null) ? new Float[0] : builder.embeddings.toArray(new Float[0]);
this.identifier = builder.identifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.dotcms.ai.db.EmbeddingsDTO.ALL_INDICES;


public class EmbeddingContentListener implements ContentletListener<Contentlet> {

Expand Down Expand Up @@ -137,6 +139,7 @@ void deleteFromIndexes(Contentlet contentlet) {
EmbeddingsDTO dto = new EmbeddingsDTO.Builder()
.withIdentifier(contentlet.getIdentifier())
.withLanguage(contentlet.getLanguageId())
.withIndexName(ALL_INDICES)
.build();
EmbeddingsAPI.impl().deleteEmbedding(dto);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dotcms/ai/service/OpenAIChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface OpenAIChatService {
* <ul>
* <li>{@code "prompt"}: the actual prompt text
* <li>{@code "model"}: the model used for the generation
* <li>{@code "temperature"}: Temperature ranges from 0 to 1. Low temperature (0 to 0.3): More focused, coherent, and conservative outputs. Medium temperature (0.3 to 0.7): Balanced creativity and coherence. High temperature (0.7 to 1): Highly creative and diverse, but potentially less coherent.
* <li>{@code "temperature"}: determines the randomness of the response. 0 = deterministic, 2 = most random
* </ul>
* @return a JSONObject including the generated text and metadata
* <p>
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/com/dotcms/ai/viewtool/SearchTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Optional;

public class SearchTool implements ViewTool {

final private HttpServletRequest request;
final private Host host;
final private AppConfig app;
Expand All @@ -39,18 +38,15 @@ public class SearchTool implements ViewTool {
this.app = ConfigService.INSTANCE.config(this.host);
}


@Override
public void init(Object initData) {
/* unneeded because of constructor */
}


public Object query(Map<String, Object> mapIn) {
User user = PortalUtil.getUser(request);
EmbeddingsDTO searcher = EmbeddingsDTO.from(mapIn).withUser(user).build();


try {
return EmbeddingsAPI.impl(host).searchForContent(searcher);
} catch (Exception e) {
Expand All @@ -59,37 +55,29 @@ public Object query(Map<String, Object> mapIn) {
}

public Object query(String query) {

return query(query, "default");
}

public Object query(String query, String indexName) {
User user = PortalUtil.getUser(request);

EmbeddingsDTO searcher = new EmbeddingsDTO.Builder().withQuery(query).withIndexName(indexName).withUser(user).withLimit(50).withThreshold(.25f).build();


try {
return EmbeddingsAPI.impl(host).searchForContent(searcher);
} catch (Exception e) {
return Map.of("error", e.getMessage(), "stackTrace", Arrays.asList(e.getStackTrace()));
}


}

public Object related(ContentMap contentMap, String indexName) {

return related(contentMap.getContentObject(), indexName);

}

public Object related(Contentlet contentlet, String indexName) {
try {
User user = PortalUtil.getUser(request);
List<Field> fields = ContentToStringUtil.impl.get().guessWhatFieldsToIndex(contentlet);


Optional<String> contentToRelate = ContentToStringUtil.impl.get().parseFields(contentlet, fields);
if (contentToRelate.isEmpty()) {
return new JSONObject();
Expand All @@ -99,8 +87,6 @@ public Object related(Contentlet contentlet, String indexName) {
} catch (Exception e) {
return Map.of("error", e.getMessage(), "stackTrace", Arrays.asList(e.getStackTrace()));
}


}

}

0 comments on commit 66706e4

Please sign in to comment.