Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Nov 2, 2024
1 parent 971e311 commit d4d3171
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions libs/langchain-google-genai/src/tests/chat_models.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ test("Supports tool_choice", async () => {
expect(response.tool_calls?.length).toBe(1);
});

describe("GoogleSearchRetrievalTool", () => {
describe.only("GoogleSearchRetrievalTool", () => {
test("Supports GoogleSearchRetrievalTool", async () => {
const searchRetrievalTool: GoogleSearchRetrievalTool = {
googleSearchRetrieval: {
Expand Down Expand Up @@ -625,7 +625,7 @@ describe("GoogleSearchRetrievalTool", () => {
});
});

describe("CodeExecutionTool", () => {
describe.only("CodeExecutionTool", () => {
test("Supports CodeExecutionTool", async () => {
const codeExecutionTool: CodeExecutionTool = {
codeExecution: {}, // Simply pass an empty object to enable it.
Expand Down
15 changes: 9 additions & 6 deletions libs/langchain-google-genai/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,14 @@ export function convertResponseContentToChatGenerationChunk(
const functionCalls = response.functionCalls();
const [candidate] = response.candidates;
const { content: candidateContent, ...generationInfo } = candidate;
let content: MessageContent;
let content: MessageContent | undefined;
// Checks if some parts do not have text. If false, it means that the content is a string.
if (!candidateContent?.parts.some((p) => !("text" in p))) {
if (
candidateContent?.parts &&
candidateContent.parts.every((p) => "text" in p)
) {
content = candidateContent.parts.map((p) => p.text).join("");
} else {
} else if (candidateContent.parts) {
content = candidateContent.parts.map((p) => {
if ("text" in p) {
return {
Expand All @@ -356,9 +359,9 @@ export function convertResponseContentToChatGenerationChunk(
}

let text = "";
if (typeof content === "string") {
if (content && typeof content === "string") {
text = content;
} else if ("text" in content[0]) {
} else if (content && typeof content === "object" && "text" in content[0]) {
text = content[0].text;
}

Expand All @@ -377,7 +380,7 @@ export function convertResponseContentToChatGenerationChunk(
return new ChatGenerationChunk({
text,
message: new AIMessageChunk({
content,
content: content || "",
name: !candidateContent ? undefined : candidateContent.role,
tool_call_chunks: toolCallChunks,
// Each chunk can have unique "generationInfo", and merging strategy is unclear,
Expand Down
52 changes: 34 additions & 18 deletions libs/langchain-google-genai/src/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function convertToolsToGenAI(
return { tools: genAITools, toolConfig };
}

function processTools(tools: GoogleGenerativeAIToolType[]) {
function processTools(tools: GoogleGenerativeAIToolType[]): GenerativeAITool[] {
let functionDeclarationTools: FunctionDeclaration[] = [];
const genAITools: GenerativeAITool[] = [];

Expand All @@ -46,23 +46,39 @@ function processTools(tools: GoogleGenerativeAIToolType[]) {
}
});

return genAITools.map((tool) => {
if (
functionDeclarationTools?.length > 0 &&
"functionDeclarations" in tool
) {
const newTool = {
functionDeclarations: [
...(tool.functionDeclarations || []),
...functionDeclarationTools,
],
};
// Clear the functionDeclarationTools array so it is not passed again
functionDeclarationTools = [];
return newTool;
}
return tool;
});
const genAIFunctionDeclaration = genAITools.find(
(t) => "functionDeclarations" in t
);
if (genAIFunctionDeclaration) {
return genAITools.map((tool) => {
if (
functionDeclarationTools?.length > 0 &&
"functionDeclarations" in tool
) {
const newTool = {
functionDeclarations: [
...(tool.functionDeclarations || []),
...functionDeclarationTools,
],
};
// Clear the functionDeclarationTools array so it is not passed again
functionDeclarationTools = [];
return newTool;
}
return tool;
});
}

return [
...genAITools,
...(functionDeclarationTools.length > 0
? [
{
functionDeclarations: functionDeclarationTools,
},
]
: []),
];
}

function createToolConfig(
Expand Down

0 comments on commit d4d3171

Please sign in to comment.