Skip to content

Commit

Permalink
fix tool message list parts standard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jun 17, 2024
1 parent 5ee1302 commit 1f7e631
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
2 changes: 0 additions & 2 deletions libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,6 @@ export class ChatGoogleGenerativeAI
messages,
this._isMultimodalModel
);
console.log("prompt", prompt);
console.log("messages", messages);
const parameters = this.invocationParams(options);

// Handle streaming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ test("ChatGoogleGenerativeAI can bind and stream langchain tools", async () => {
expect("url" in toolCalls[0].args).toBe(true);
});

test.only("ChatGoogleGenerativeAI can handle streaming tool messages.", async () => {
test("ChatGoogleGenerativeAI can handle streaming tool messages.", async () => {
const model = new ChatGoogleGenerativeAI({
model: "gemini-1.5-pro",
maxRetries: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ class ChatGoogleGenerativeAIStandardIntegrationTests extends ChatModelIntegratio
},
});
}

async testToolMessageHistoriesListContent() {
this.skipTestMessage(
"testToolMessageHistoriesListContent",
"ChatGoogleGenerativeAI",
"Not implemented."
);
}
}

const testClass = new ChatGoogleGenerativeAIStandardIntegrationTests();
Expand Down
37 changes: 26 additions & 11 deletions libs/langchain-google-genai/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,34 @@ export function convertMessageContentToParts(
return [{ text: message.content }];
}

let functionCallParts: Part[] = [];
if (role === "function") {
console.log("Message!", message);
if (message.name && typeof message.content === "string") {
return [
{
functionResponse: {
name: message.name,
response: message.content,
},
functionCallParts.push({
functionResponse: {
name: message.name,
response: message.content,
},
];
});
} else {
throw new Error(
"ChatGoogleGenerativeAI requires tool messages to contain the tool name, and a string content."
);
}
} else {
console.log("message", message);
}
if ("tool_calls" in message) {
const castMessage = message as AIMessage;
if (castMessage.tool_calls && castMessage.tool_calls.length > 0) {
functionCallParts = castMessage.tool_calls.map((tc) => ({
functionCall: {
name: tc.name,
args: tc.args,
},
}));
}
}

return message.content.map((c) => {
const messageContentParts = message.content.map((c) => {
if (c.type === "text") {
return {
text: c.text,
Expand Down Expand Up @@ -143,9 +150,17 @@ export function convertMessageContentToParts(
};
} else if (c.type === "media") {
return messageContentMedia(c);
} else if (c.type === "tool_use") {
return {
functionCall: {
name: c.name,
args: c.input,
},
};
}
throw new Error(`Unknown content type ${(c as { type: string }).type}`);
});
return [...messageContentParts, ...functionCallParts];
}

export function convertBaseMessagesToContent(
Expand Down

0 comments on commit 1f7e631

Please sign in to comment.