Skip to content

Commit

Permalink
aws[minor]: Account for content strings and tool use (#6406)
Browse files Browse the repository at this point in the history
* aws[minor]: Account for content strings and tool use

* chore: lint files

* cr

* Update libs/langchain-aws/src/common.ts

* cr

* chore: lint files
  • Loading branch information
bracesproul authored Aug 6, 2024
1 parent 0ed1646 commit 890b818
Showing 1 changed file with 45 additions and 52 deletions.
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

0 comments on commit 890b818

Please sign in to comment.