Skip to content

Commit

Permalink
fix(ai): forward user-agent with package metadata in generation and c…
Browse files Browse the repository at this point in the history
…onversation requests (#3029)
  • Loading branch information
atierian authored Nov 18, 2024
1 parent 1dd63da commit 9a0ac8b
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
export function request(ctx) {
const { args, request } = ctx;
const { graphqlApiEndpoint } = ctx.stash;
const userAgent = createUserAgent(request);

const selectionSet = 'associatedUserMessageId contentBlockDeltaIndex contentBlockDoneAtIndex contentBlockIndex contentBlockText contentBlockToolUse { toolUseId name input } conversationId id stopReason owner errors { errorType message }';

Expand Down Expand Up @@ -705,7 +706,12 @@ export function request(ctx) {
responseMutation: streamingResponseMutation,
graphqlApiEndpoint,
modelConfiguration,
request: { headers: { authorization: authHeader } },
request: {
headers: {
authorization: authHeader,
'x-amz-user-agent': userAgent,
}
},
messageHistoryQuery,
toolsConfiguration,
streamResponse: true,
Expand Down Expand Up @@ -735,7 +741,17 @@ export function response(ctx) {
};
return response;
}
"

function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-conversation-transformer#0.7.1';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}"
`;

exports[`ConversationTransformer valid schemas should transform conversation route with inference configuration: SendMessageMutation resolver code 1`] = `
Expand Down Expand Up @@ -1508,6 +1524,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
export function request(ctx) {
const { args, request } = ctx;
const { graphqlApiEndpoint } = ctx.stash;
const userAgent = createUserAgent(request);

const selectionSet = 'associatedUserMessageId contentBlockDeltaIndex contentBlockDoneAtIndex contentBlockIndex contentBlockText contentBlockToolUse { toolUseId name input } conversationId id stopReason owner errors { errorType message }';

Expand Down Expand Up @@ -1546,7 +1563,12 @@ export function request(ctx) {
responseMutation: streamingResponseMutation,
graphqlApiEndpoint,
modelConfiguration,
request: { headers: { authorization: authHeader } },
request: {
headers: {
authorization: authHeader,
'x-amz-user-agent': userAgent,
}
},
messageHistoryQuery,
toolsConfiguration,
streamResponse: true,
Expand Down Expand Up @@ -1576,7 +1598,17 @@ export function response(ctx) {
};
return response;
}
"

function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-conversation-transformer#0.7.1';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}"
`;

exports[`ConversationTransformer valid schemas should transform conversation route with model query tool including relationships: SendMessageMutation resolver code 1`] = `
Expand Down Expand Up @@ -2349,6 +2381,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
export function request(ctx) {
const { args, request } = ctx;
const { graphqlApiEndpoint } = ctx.stash;
const userAgent = createUserAgent(request);

const selectionSet = 'associatedUserMessageId contentBlockDeltaIndex contentBlockDoneAtIndex contentBlockIndex contentBlockText contentBlockToolUse { toolUseId name input } conversationId id stopReason owner errors { errorType message }';

Expand Down Expand Up @@ -2387,7 +2420,12 @@ export function request(ctx) {
responseMutation: streamingResponseMutation,
graphqlApiEndpoint,
modelConfiguration,
request: { headers: { authorization: authHeader } },
request: {
headers: {
authorization: authHeader,
'x-amz-user-agent': userAgent,
}
},
messageHistoryQuery,
toolsConfiguration,
streamResponse: true,
Expand Down Expand Up @@ -2417,7 +2455,17 @@ export function response(ctx) {
};
return response;
}
"

function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-conversation-transformer#0.7.1';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}"
`;

exports[`ConversationTransformer valid schemas should transform conversation route with model query tool: SendMessageMutation resolver code 1`] = `
Expand Down Expand Up @@ -3190,6 +3238,7 @@ exports[`ConversationTransformer valid schemas should transform conversation rou
export function request(ctx) {
const { args, request } = ctx;
const { graphqlApiEndpoint } = ctx.stash;
const userAgent = createUserAgent(request);

const selectionSet = 'associatedUserMessageId contentBlockDeltaIndex contentBlockDoneAtIndex contentBlockIndex contentBlockText contentBlockToolUse { toolUseId name input } conversationId id stopReason owner errors { errorType message }';

Expand Down Expand Up @@ -3228,7 +3277,12 @@ export function request(ctx) {
responseMutation: streamingResponseMutation,
graphqlApiEndpoint,
modelConfiguration,
request: { headers: { authorization: authHeader } },
request: {
headers: {
authorization: authHeader,
'x-amz-user-agent': userAgent,
}
},
messageHistoryQuery,
toolsConfiguration,
streamResponse: true,
Expand Down Expand Up @@ -3258,7 +3312,17 @@ export function response(ctx) {
};
return response;
}
"

function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-conversation-transformer#0.7.1';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}"
`;

exports[`ConversationTransformer valid schemas should transform conversation route with query tools: SendMessageMutation resolver code 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ function invokeLambda(): ResolverFunctionDefinition {
});
}

const packageName = 'amplify-graphql-conversation-transformer';
const packageVersion = require('../../package.json').version;

/**
* The substitutions for the invoke lambda resolver function.
*/
Expand All @@ -113,6 +116,7 @@ function invokeLambdaResolverSubstitutions(config: ConversationDirectiveConfigur
LIST_QUERY_LIMIT: 'undefined',
STREAMING_RESPONSE_MUTATION_NAME: config.assistantResponseStreamingMutation.field.name.value,
STREAMING_RESPONSE_MUTATION_INPUT_TYPE_NAME: config.assistantResponseStreamingMutation.input.name.value,
PACKAGE_METADATA: `'${packageName}#${packageVersion}'`,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { util } from '@aws-appsync/utils';
export function request(ctx) {
const { args, request } = ctx;
const { graphqlApiEndpoint } = ctx.stash;
const userAgent = createUserAgent(request);

const selectionSet = '[[SELECTION_SET]]';

Expand Down Expand Up @@ -41,7 +42,12 @@ export function request(ctx) {
responseMutation: streamingResponseMutation,
graphqlApiEndpoint,
modelConfiguration,
request: { headers: { authorization: authHeader } },
request: {
headers: {
authorization: authHeader,
'x-amz-user-agent': userAgent,
}
},
messageHistoryQuery,
toolsConfiguration,
streamResponse: true,
Expand Down Expand Up @@ -71,3 +77,14 @@ export function response(ctx) {
};
return response;
}

function createUserAgent(request) {
const packageMetadata = [[PACKAGE_METADATA]];
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = `${userAgent} md/${packageMetadata}`;
} else {
userAgent = `lib/${packageMetadata}`;
}
return userAgent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ export function request(ctx) {
const prompt = "";
const args = JSON.stringify(ctx.args);
const inferenceConfig = undefined;
const userAgent = createUserAgent(ctx.request);
return {
resourcePath: '/model/anthropic.claude-3-haiku-20240307-v1:0/converse',
method: 'POST',
params: {
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'x-amz-user-agent': userAgent,
},
body: {
messages: [
{
Expand Down Expand Up @@ -121,6 +125,17 @@ export function response(ctx) {
}
return value;
}
function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-generation-transformer#0.2.7';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}
",
}
`;
Expand Down Expand Up @@ -189,12 +204,16 @@ export function request(ctx) {
const prompt = "You are a helpful assistant that generates recipes.";
const args = JSON.stringify(ctx.args);
const inferenceConfig = undefined;
const userAgent = createUserAgent(ctx.request);
return {
resourcePath: '/model/anthropic.claude-3-haiku-20240307-v1:0/converse',
method: 'POST',
params: {
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'x-amz-user-agent': userAgent,
},
body: {
messages: [
{
Expand Down Expand Up @@ -246,6 +265,17 @@ export function response(ctx) {
}
return value;
}
function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-generation-transformer#0.2.7';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}
",
}
`;
Expand Down Expand Up @@ -313,12 +343,16 @@ export function request(ctx) {
const prompt = "Make a string based on the description.";
const args = JSON.stringify(ctx.args);
const inferenceConfig = undefined;
const userAgent = createUserAgent(ctx.request);
return {
resourcePath: '/model/anthropic.claude-3-haiku-20240307-v1:0/converse',
method: 'POST',
params: {
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'x-amz-user-agent': userAgent,
},
body: {
messages: [
{
Expand Down Expand Up @@ -370,6 +404,17 @@ export function response(ctx) {
}
return value;
}
function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-generation-transformer#0.2.7';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}
"
`;
Expand Down Expand Up @@ -454,12 +499,16 @@ export function request(ctx) {
const prompt = "Make a string based on the description.";
const args = JSON.stringify(ctx.args);
const inferenceConfig = undefined;
const userAgent = createUserAgent(ctx.request);
return {
resourcePath: '/model/anthropic.claude-3-haiku-20240307-v1:0/converse',
method: 'POST',
params: {
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'x-amz-user-agent': userAgent,
},
body: {
messages: [
{
Expand Down Expand Up @@ -507,6 +556,17 @@ export function response(ctx) {
return value;
}
function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-generation-transformer#0.2.7';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}
",
}
`;
Expand All @@ -520,12 +580,16 @@ export function request(ctx) {
const prompt = "Generate a string based on the description.";
const args = JSON.stringify(ctx.args);
const inferenceConfig = { inferenceConfig: {"maxTokens":100,"temperature":0.7,"topP":0.9} };
const userAgent = createUserAgent(ctx.request);
return {
resourcePath: '/model/anthropic.claude-3-haiku-20240307-v1:0/converse',
method: 'POST',
params: {
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'x-amz-user-agent': userAgent,
},
body: {
messages: [
{
Expand Down Expand Up @@ -573,6 +637,17 @@ export function response(ctx) {
return value;
}
function createUserAgent(request) {
const packageMetadata = 'amplify-graphql-generation-transformer#0.2.7';
let userAgent = request.headers['x-amz-user-agent'];
if (userAgent) {
userAgent = \`\${userAgent} md/\${packageMetadata}\`;
} else {
userAgent = \`lib/\${packageMetadata}\`;
}
return userAgent;
}
",
}
`;
Loading

0 comments on commit 9a0ac8b

Please sign in to comment.