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

aws[minor]: Account for content strings and tool use #6406

Merged
merged 6 commits into from
Aug 6, 2024
Merged
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
97 changes: 45 additions & 52 deletions libs/langchain-aws/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,60 +82,53 @@ export function convertToConverseMessages(messages: BaseMessage[]): {
.map((msg) => {
if (msg._getType() === "ai") {
const castMsg = msg as AIMessage;
const assistantMsg: BedrockMessage = {
role: "assistant",
content: [],
};

if (castMsg.tool_calls && castMsg.tool_calls.length) {
assistantMsg.content = castMsg.tool_calls.map((tc) => ({
toolUse: {
toolUseId: tc.id,
name: tc.name,
input: tc.args,
},
}));
}

if (typeof castMsg.content === "string" && castMsg.content !== "") {
return {
role: "assistant",
content: [
{
text: castMsg.content,
},
],
};
} else {
if (castMsg.tool_calls && castMsg.tool_calls.length) {
return {
role: "assistant",
content: castMsg.tool_calls.map((tc) => ({
toolUse: {
toolUseId: tc.id,
name: tc.name,
input: tc.args,
},
})),
};
} else if (Array.isArray(castMsg.content)) {
const contentBlocks: ContentBlock[] = castMsg.content.map(
(block) => {
if (block.type === "text" && block.text !== "") {
return {
text: block.text,
};
} else {
const blockValues = Object.fromEntries(
Object.values(block).filter(([key]) => key !== "type")
);
throw new Error(
`Unsupported content block type: ${
block.type
} with content of ${JSON.stringify(blockValues, null, 2)}`
);
}
}
);
return {
role: "assistant",
content: contentBlocks,
};
} else {
throw new Error(
`Invalid message content: empty string. '${msg._getType()}' must contain non-empty content.`
);
}
assistantMsg.content?.push({
text: castMsg.content,
});
} else if (Array.isArray(castMsg.content)) {
const contentBlocks: ContentBlock[] = castMsg.content.map((block) => {
if (block.type === "text" && block.text !== "") {
return {
text: block.text,
};
} else {
const blockValues = Object.fromEntries(
Object.values(block).filter(([key]) => key !== "type")
);
throw new Error(
`Unsupported content block type: ${
block.type
} with content of ${JSON.stringify(blockValues, null, 2)}`
);
}
});

assistantMsg.content = [
...(assistantMsg.content ? assistantMsg.content : []),
...contentBlocks,
];
}
return assistantMsg;
} else if (msg._getType() === "human" || msg._getType() === "generic") {
if (typeof msg.content === "string" && msg.content !== "") {
return {
role: "user",
role: "user" as const,
content: [
{
text: msg.content,
Expand All @@ -159,7 +152,7 @@ export function convertToConverseMessages(messages: BaseMessage[]): {
}
});
return {
role: "user",
role: "user" as const,
content: contentBlocks,
};
} else {
Expand All @@ -172,7 +165,7 @@ export function convertToConverseMessages(messages: BaseMessage[]): {
if (typeof castMsg.content === "string") {
return {
// Tool use messages are always from the user
role: "user",
role: "user" as const,
content: [
{
toolResult: {
Expand All @@ -189,7 +182,7 @@ export function convertToConverseMessages(messages: BaseMessage[]): {
} else {
return {
// Tool use messages are always from the user
role: "user",
role: "user" as const,
content: [
{
toolResult: {
Expand Down
Loading