Skip to content

Commit

Permalink
Fix InvokeAIRaw bedrock subaction's schema (elastic#192015)
Browse files Browse the repository at this point in the history
## Summary

Fix regression on `InvokeAIRaw` bedrock subaction introduced by
elastic#191434
  • Loading branch information
pgayvallet authored Sep 3, 2024
1 parent 448c9e1 commit 84c6815
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 56 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion x-pack/plugins/stack_connectors/common/bedrock/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ export const InvokeAIActionResponseSchema = schema.object({
});

export const InvokeAIRawActionParamsSchema = schema.object({
messages: schema.arrayOf(BedrockMessageSchema),
messages: schema.arrayOf(
schema.object({
role: schema.string(),
content: schema.any(),
})
),
model: schema.maybe(schema.string()),
temperature: schema.maybe(schema.number()),
stopSequences: schema.maybe(schema.arrayOf(schema.string())),
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/stack_connectors/common/bedrock/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ export type RunActionResponse = TypeOf<typeof RunActionResponseSchema>;
export type StreamingResponse = TypeOf<typeof StreamingResponseSchema>;
export type DashboardActionParams = TypeOf<typeof DashboardActionParamsSchema>;
export type DashboardActionResponse = TypeOf<typeof DashboardActionResponseSchema>;
export type BedRockMessage = TypeOf<typeof BedrockMessageSchema>;
export type BedrockMessage = TypeOf<typeof BedrockMessageSchema>;
export type BedrockToolChoice = TypeOf<typeof BedrockToolChoiceSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
InvokeAIRawActionParams,
InvokeAIRawActionResponse,
RunApiLatestResponse,
BedRockMessage,
BedrockMessage,
BedrockToolChoice,
} from '../../../common/bedrock/types';
import {
Expand Down Expand Up @@ -422,7 +422,7 @@ const formatBedrockBody = ({
tools,
toolChoice,
}: {
messages: BedRockMessage[];
messages: BedrockMessage[];
stopSequences?: string[];
temperature?: number;
maxTokens?: number;
Expand All @@ -440,9 +440,9 @@ const formatBedrockBody = ({
tool_choice: toolChoice,
});

interface FormattedBedRockMessage {
interface FormattedBedrockMessage {
role: string;
content: string | BedRockMessage['rawContent'];
content: string | BedrockMessage['rawContent'];
}

/**
Expand All @@ -452,15 +452,15 @@ interface FormattedBedRockMessage {
* @param messages
*/
const ensureMessageFormat = (
messages: BedRockMessage[],
messages: BedrockMessage[],
systemPrompt?: string
): {
messages: FormattedBedRockMessage[];
messages: FormattedBedrockMessage[];
system?: string;
} => {
let system = systemPrompt ? systemPrompt : '';

const newMessages = messages.reduce<FormattedBedRockMessage[]>((acc, m) => {
const newMessages = messages.reduce<FormattedBedrockMessage[]>((acc, m) => {
if (m.role === 'system') {
system = `${system.length ? `${system}\n` : ''}${m.content}`;
return acc;
Expand Down

0 comments on commit 84c6815

Please sign in to comment.