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

fix(google-common): Search grounding formatting #7471

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 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,7 @@ 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 ??[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: run yarn format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Fixed

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");
})
});