-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from weaviate/generative_search_support
Adds generative openai support
- Loading branch information
Showing
13 changed files
with
406 additions
and
21 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
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
16 changes: 11 additions & 5 deletions
16
src/main/java/technology/semi/weaviate/client/v1/graphql/query/fields/Fields.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 |
---|---|---|
@@ -1,26 +1,32 @@ | ||
package technology.semi.weaviate.client.v1.graphql.query.fields; | ||
|
||
import java.util.Arrays; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import lombok.experimental.FieldDefaults; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.ObjectUtils; | ||
import technology.semi.weaviate.client.v1.graphql.query.argument.Argument; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@EqualsAndHashCode | ||
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) | ||
public class Fields implements Argument { | ||
Field[] fields; | ||
|
||
@Override | ||
public String build() { | ||
if (this.fields != null && this.fields.length > 0) { | ||
return StringUtils.joinWith(" ", Arrays.stream(this.fields).map(Field::build).toArray()); | ||
if (ObjectUtils.isEmpty(fields)) { | ||
return ""; | ||
} | ||
return ""; | ||
return Arrays.stream(fields) | ||
.map(Field::build) | ||
.collect(Collectors.joining(" ")); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...java/technology/semi/weaviate/client/v1/graphql/query/fields/GenerativeSearchBuilder.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,48 @@ | ||
package technology.semi.weaviate.client.v1.graphql.query.fields; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import lombok.experimental.FieldDefaults; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) | ||
public class GenerativeSearchBuilder { | ||
|
||
String singleResultPrompt; | ||
String groupedResultTask; | ||
|
||
|
||
public Field build() { | ||
Set<String> nameParts = new LinkedHashSet<>(); | ||
Set<String> fieldNames = new LinkedHashSet<>(); | ||
|
||
if (StringUtils.isNotBlank(singleResultPrompt)) { | ||
nameParts.add(String.format("singleResult:{prompt:\"\"\"%s\"\"\"}", singleResultPrompt)); | ||
fieldNames.add("singleResult"); | ||
} | ||
if (StringUtils.isNotBlank(groupedResultTask)) { | ||
nameParts.add(String.format("groupedResult:{task:\"\"\"%s\"\"\"}", groupedResultTask)); | ||
fieldNames.add("groupedResult"); | ||
} | ||
|
||
if (nameParts.isEmpty()) { | ||
return Field.builder().build(); | ||
} | ||
|
||
fieldNames.add("error"); | ||
String name = String.format("generate(%s)", StringUtils.join(nameParts, " ")); | ||
Field[] fields = fieldNames.stream() | ||
.map(n -> Field.builder().name(n).build()) | ||
.toArray(Field[]::new); | ||
|
||
return Field.builder().name(name).fields(fields).build(); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
.../technology/semi/weaviate/client/v1/graphql/query/fields/GenerativeSearchBuilderTest.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,66 @@ | ||
package technology.semi.weaviate.client.v1.graphql.query.fields; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GenerativeSearchBuilderTest { | ||
|
||
@Test | ||
public void shouldBuildEmptyField() { | ||
GenerativeSearchBuilder generativeSearchBuilder = GenerativeSearchBuilder.builder() | ||
.build(); | ||
|
||
Field generate = generativeSearchBuilder.build(); | ||
|
||
assertThat(generate.getName()).isBlank(); | ||
assertThat(generate.getFields()).isNull(); | ||
} | ||
|
||
@Test | ||
public void shouldBuildSingleResultPromptField() { | ||
GenerativeSearchBuilder generativeSearchBuilder = GenerativeSearchBuilder.builder() | ||
.singleResultPrompt("What is the meaning of life?") | ||
.build(); | ||
|
||
Field generate = generativeSearchBuilder.build(); | ||
|
||
assertThat(generate.getName()).isEqualTo("generate(" + | ||
"singleResult:{prompt:\"\"\"What is the meaning of life?\"\"\"}" + | ||
")"); | ||
assertThat(generate.getFields()).extracting(Field::getName) | ||
.containsExactly("singleResult", "error"); | ||
} | ||
|
||
@Test | ||
public void shouldBuildGroupedResultTaskField() { | ||
GenerativeSearchBuilder generativeSearchBuilder = GenerativeSearchBuilder.builder() | ||
.groupedResultTask("Explain why these magazines or newspapers are about finance") | ||
.build(); | ||
|
||
Field generate = generativeSearchBuilder.build(); | ||
|
||
assertThat(generate.getName()).isEqualTo("generate(" + | ||
"groupedResult:{task:\"\"\"Explain why these magazines or newspapers are about finance\"\"\"}" + | ||
")"); | ||
assertThat(generate.getFields()).extracting(Field::getName) | ||
.containsExactly("groupedResult", "error"); | ||
} | ||
|
||
@Test | ||
public void shouldBuildBothSingleResultPromptAndGroupedResultTaskField() { | ||
GenerativeSearchBuilder generativeSearchBuilder = GenerativeSearchBuilder.builder() | ||
.singleResultPrompt("What is the meaning of life?") | ||
.groupedResultTask("Explain why these magazines or newspapers are about finance") | ||
.build(); | ||
|
||
Field generate = generativeSearchBuilder.build(); | ||
|
||
assertThat(generate.getName()).isEqualTo("generate(" + | ||
"singleResult:{prompt:\"\"\"What is the meaning of life?\"\"\"} " + | ||
"groupedResult:{task:\"\"\"Explain why these magazines or newspapers are about finance\"\"\"}" + | ||
")"); | ||
assertThat(generate.getFields()).extracting(Field::getName) | ||
.containsExactly("singleResult", "groupedResult", "error"); | ||
} | ||
} |
Oops, something went wrong.