Skip to content

Commit

Permalink
fix(google-common): Search grounding formatting (#7471)
Browse files Browse the repository at this point in the history
  • Loading branch information
afirstenberg authored Jan 10, 2025
1 parent 8ad8547 commit e6fad4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions libs/langchain-google-common/src/output_parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export abstract class BaseGoogleSearchOutputParser extends BaseLLMOutputParser<s
): GroundingInfo | undefined {
if ("message" in generation) {
const responseMetadata = generation?.message?.response_metadata;
const metadata = responseMetadata.groundingMetadata;
const metadata = responseMetadata?.groundingMetadata;
const supports =
responseMetadata.groundingSupport ?? metadata.groundingSupports ?? [];
responseMetadata?.groundingSupport ?? metadata?.groundingSupports ?? [];
if (metadata) {
return {
metadata,
Expand Down Expand Up @@ -144,7 +144,7 @@ export abstract class BaseGoogleSearchOutputParser extends BaseLLMOutputParser<s
* @param grounding
*/
protected searchSuggestion(grounding: GroundingInfo): string {
return grounding.metadata.searchEntryPoint?.renderedContent ?? "";
return grounding?.metadata?.searchEntryPoint?.renderedContent ?? "";
}

protected annotateText(text: string, grounding: GroundingInfo): string {
Expand Down Expand Up @@ -198,7 +198,8 @@ export class SimpleGoogleSearchOutputParser extends BaseGoogleSearchOutputParser

protected textSuffix(_text: string, grounding: GroundingInfo): string {
let ret = "\n";
const chunks: GeminiGroundingChunk[] = grounding.metadata.groundingChunks;
const chunks: GeminiGroundingChunk[] =
grounding?.metadata?.groundingChunks ?? [];
chunks.forEach((chunk, index) => {
ret = `${ret}${this.chunkToString(chunk, index)}\n`;
});
Expand Down
19 changes: 19 additions & 0 deletions libs/langchain-google-common/src/tests/output_parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,23 @@ describe("GoogleSearchOutputParsers", () => {

expect(result).toEqual(expectation);
});

test("non-grounded", async () => {
const record: Record<string, any> = {};
const projectId = mockId();
const authOptions: MockClientAuthInfo = {
record,
projectId,
resultFile: "chat-1-mock.json",
};

const model = new ChatGoogle({
authOptions,
modelName: "gemini-1.5-pro-002",
});
const parser = new SimpleGoogleSearchOutputParser();
const chain = model.pipe(parser);
const result = await chain.invoke("Flip a coin.");
expect(result).toEqual("T");
});
});

0 comments on commit e6fad4a

Please sign in to comment.