Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ai-constructs): support no input graphql query tools #1907

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-parents-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/ai-constructs': patch
---

support no input graphql query tools
2 changes: 1 addition & 1 deletion packages/ai-constructs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type ConversationTurnEvent = {
dataTools?: Array<ToolDefinition & {
graphqlRequestInputDescriptor: {
queryName: string;
selectionSet: string[];
selectionSet: string;
propertyTypes: Record<string, string>;
};
}>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void describe('events tool provider', () => {
},
graphqlRequestInputDescriptor: {
queryName: 'queryName1',
selectionSet: ['selection1'],
selectionSet: 'selection1',
propertyTypes: {
property1: 'type1',
},
Expand All @@ -51,7 +51,7 @@ void describe('events tool provider', () => {
},
graphqlRequestInputDescriptor: {
queryName: 'queryName2',
selectionSet: ['selection2'],
selectionSet: 'selection2',
propertyTypes: {
property1: 'type2',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,38 @@ const testCases: Array<TestCase> = [
},
graphqlRequestInputDescriptor: {
queryName: 'testQueryName1',
selectionSet: ['testSelection1', 'testSelection2'],
selectionSet: 'testSelection1 testSelection2',
propertyTypes: {
property1: 'string',
property2: 'number',
property1: 'String!',
property2: 'Int',
},
},
},
expectedQuery: `
query ToolQuery($property1: string!, $property2: number) {
query ToolQuery($property1: String!, $property2: Int) {
testQueryName1(property1: $property1, property2: $property2) {
testSelection1
testSelection2
testSelection1 testSelection2
}
}
`,
},
{
toolDefinition: {
name: 'toolName2',
description: 'toolDescription2',
inputSchema: {
json: {},
},
graphqlRequestInputDescriptor: {
queryName: 'testQueryName2',
selectionSet: 'testSelection3 testSelection4',
propertyTypes: {},
},
},
expectedQuery: `
query ToolQuery {
testQueryName2 {
testSelection3 testSelection4
}
}
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ export class GraphQlQueryFactory {
toolDefinition: ConversationTurnEventToolConfiguration
): string => {
const { graphqlRequestInputDescriptor } = toolDefinition;
const { selectionSet, propertyTypes, queryName } =
graphqlRequestInputDescriptor;

let topLevelQueryArgs = '';
let queryArgs = '';
if (toolDefinition.inputSchema?.json) {
const inputSchemaJson = toolDefinition.inputSchema
.json as InputSchemaJson;
topLevelQueryArgs = Object.entries(inputSchemaJson.properties)
.map(([name]) => {
let type = propertyTypes[name];
if (
inputSchemaJson.required.find(
(requiredField) => requiredField === name
)
) {
type += '!';
}
return `$${name}: ${type}`;
})
.join(', ');

queryArgs = Object.entries(inputSchemaJson.properties)
.map(([name]) => `${name}: $${name}`)
.join(', ');
}

const selectionSetString = selectionSet.join('\n');
const { selectionSet, queryName } = graphqlRequestInputDescriptor;
const [topLevelQueryArgs, queryArgs] = this.createQueryArgs(toolDefinition);

const query = `
query ToolQuery(${topLevelQueryArgs}) {
${queryName}(${queryArgs}) {
${selectionSetString}
query ToolQuery${topLevelQueryArgs} {
${queryName}${queryArgs} {
${selectionSet}
}
}
`;

return query;
};

private createQueryArgs = (
toolDefinition: ConversationTurnEventToolConfiguration
): [string, string] => {
const { inputSchema } = toolDefinition;
if (!inputSchema?.json) {
return ['', ''];
}

const { properties } = inputSchema.json as InputSchemaJson;
if (!properties) {
return ['', ''];
}
const { propertyTypes } = toolDefinition.graphqlRequestInputDescriptor;
const propertyNames = Object.keys(properties);

const topLevelQueryArgs = propertyNames
.map((name) => `$${name}: ${propertyTypes[name]}`)
.join(', ');

const queryArgs = propertyNames
.map((name) => `${name}: $${name}`)
.join(', ');

return [`(${topLevelQueryArgs})`, `(${queryArgs})`];
};
}
2 changes: 1 addition & 1 deletion packages/ai-constructs/src/conversation/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type ConversationTurnEvent = {
ToolDefinition & {
graphqlRequestInputDescriptor: {
queryName: string;
selectionSet: string[];
selectionSet: string;
propertyTypes: Record<string, string>;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class ConversationHandlerTestProject extends TestProjectBase {
},
graphqlRequestInputDescriptor: {
queryName: 'getTemperature',
selectionSet: ['value', 'unit'],
selectionSet: 'value unit',
propertyTypes: {
city: 'String',
},
Expand Down
Loading