Skip to content

Commit 0af066f

Browse files
authored
Merge pull request #60 from weaviate/properties-field-for-groupedresult
Support properties field in generative groupedResult field
2 parents 3a4efeb + c49fe52 commit 0af066f

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/graphql/generate.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface GenerateArgs {
22
groupedTask?: string;
3+
groupedProperties?: string[];
34
singlePrompt?: string;
45
}
56

@@ -11,10 +12,12 @@ export interface GenerateParts {
1112

1213
export class GraphQLGenerate {
1314
private groupedTask?: string;
15+
private groupedProperties?: string[];
1416
private singlePrompt?: string;
1517

1618
constructor(args: GenerateArgs) {
1719
this.groupedTask = args.groupedTask;
20+
this.groupedProperties = args.groupedProperties;
1821
this.singlePrompt = args.singlePrompt;
1922
}
2023

@@ -27,8 +30,15 @@ export class GraphQLGenerate {
2730
str += `singleResult:{prompt:"${this.singlePrompt.replace(/[\n\r]+/g, '')}"}`;
2831
results.push('singleResult');
2932
}
30-
if (this.groupedTask) {
31-
str += `groupedResult:{task:"${this.groupedTask.replace(/[\n\r]+/g, '')}"}`;
33+
if (this.groupedTask || (this.groupedProperties !== undefined && this.groupedProperties.length > 0)) {
34+
const args: string[] = [];
35+
if (this.groupedTask) {
36+
args.push(`task:"${this.groupedTask.replace(/[\n\r]+/g, '')}"`);
37+
}
38+
if (this.groupedProperties !== undefined && this.groupedProperties.length > 0) {
39+
args.push(`properties:${JSON.stringify(this.groupedProperties)}`);
40+
}
41+
str += `groupedResult:{${args.join(',')}}`;
3242
results.push('groupedResult');
3343
}
3444
str += `){${results.join(' ')}}`;

src/graphql/getter.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,43 @@ describe('generative search', () => {
13391339

13401340
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
13411341
});
1342+
1343+
test('groupedTask with properties', () => {
1344+
const expectedQuery =
1345+
'{Get{Mammal{name taxonomy _additional{generate(groupedResult:' +
1346+
'{task:"Explain why platypi can lay eggs",properties:["title","description"]}){error groupedResult}}}}}';
1347+
1348+
new Getter(mockClient)
1349+
.withClassName('Mammal')
1350+
.withGenerate({
1351+
groupedTask: 'Explain why platypi can lay eggs',
1352+
groupedProperties: ['title', 'description'],
1353+
})
1354+
.withFields('name taxonomy')
1355+
.do();
1356+
1357+
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
1358+
});
1359+
1360+
test('single prompt and grouped task with properties', () => {
1361+
const expectedQuery =
1362+
'{Get{Mammal{name taxonomy _additional{generate(singleResult:' +
1363+
'{prompt:"How tall is a baby giraffe?"}groupedResult:{task:' +
1364+
'"Explain how the heights of mammals relate to their prefferred food sources",properties:["property"]})' +
1365+
'{error singleResult groupedResult}}}}}';
1366+
1367+
new Getter(mockClient)
1368+
.withClassName('Mammal')
1369+
.withFields('name taxonomy')
1370+
.withGenerate({
1371+
singlePrompt: 'How tall is a baby giraffe?',
1372+
groupedTask: 'Explain how the heights of mammals relate to their prefferred food sources',
1373+
groupedProperties: ['property'],
1374+
})
1375+
.do();
1376+
1377+
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
1378+
});
13421379
});
13431380

13441381
describe('groupBy valid searchers', () => {

src/graphql/journey.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,22 @@ Tastes like a fresh ocean breeze: {review}`,
13431343
});
13441344
});
13451345

1346+
test('groupedTask with groupedProperties', async () => {
1347+
await client.graphql
1348+
.get()
1349+
.withClassName('Wine')
1350+
.withFields('name review')
1351+
.withGenerate({
1352+
groupedTask: 'Describe the following as a LinkedIn Ad:',
1353+
groupedProperties: ['name', 'review'],
1354+
})
1355+
.do()
1356+
.then((res: any) => {
1357+
expect(res.data.Get.Wine[0]._additional.generate.groupedResult).toBeDefined();
1358+
expect(res.data.Get.Wine[0]._additional.generate.error).toBeNull();
1359+
});
1360+
});
1361+
13461362
test('singlePrompt and groupedTask', async () => {
13471363
await client.graphql
13481364
.get()

0 commit comments

Comments
 (0)