Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
afirstenberg committed Dec 27, 2024
1 parent 90e6236 commit 4cb0d50
Show file tree
Hide file tree
Showing 7 changed files with 557 additions and 541 deletions.
15 changes: 11 additions & 4 deletions libs/langchain-google-common/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
return true;
}

computeGoogleSearchToolAdjustmentFromModel(): Exclude<GoogleSearchToolSetting, boolean> {
computeGoogleSearchToolAdjustmentFromModel(): Exclude<
GoogleSearchToolSetting,
boolean
> {
if (this.modelName.startsWith("gemini-1.0")) {
return "googleSearchRetrieval";
} else if (this.modelName.startsWith("gemini-1.5")) {
Expand All @@ -108,7 +111,9 @@ export class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
}
}

computeGoogleSearchToolAdjustment(apiConfig: GeminiAPIConfig): Exclude<GoogleSearchToolSetting, true> {
computeGoogleSearchToolAdjustment(
apiConfig: GeminiAPIConfig
): Exclude<GoogleSearchToolSetting, true> {
const adj = apiConfig.googleSearchToolAdjustment;
if (adj === undefined || adj === true) {
return this.computeGoogleSearchToolAdjustmentFromModel();
Expand All @@ -118,8 +123,10 @@ export class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
}

buildGeminiAPI(): GoogleAIAPI {
const apiConfig: GeminiAPIConfig = this.apiConfig as GeminiAPIConfig ?? {};
const googleSearchToolAdjustment = this.computeGoogleSearchToolAdjustment(apiConfig);
const apiConfig: GeminiAPIConfig =
(this.apiConfig as GeminiAPIConfig) ?? {};
const googleSearchToolAdjustment =
this.computeGoogleSearchToolAdjustment(apiConfig);
const geminiConfig: GeminiAPIConfig = {
useSystemInstruction: this.useSystemInstruction,
googleSearchToolAdjustment,
Expand Down
5 changes: 3 additions & 2 deletions libs/langchain-google-common/src/tests/chat_models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,9 @@ describe("Mock ChatGoogle - Gemini", () => {
expect(result).toHaveProperty("response_metadata");
expect(result.response_metadata).toHaveProperty("groundingMetadata");
expect(result.response_metadata).toHaveProperty("groundingSupport");
expect(Array.isArray(result.response_metadata.groundingSupport)).toEqual(true);
expect(Array.isArray(result.response_metadata.groundingSupport)).toEqual(
true
);
expect(result.response_metadata.groundingSupport).toHaveLength(4);
});

Expand Down Expand Up @@ -1248,7 +1250,6 @@ describe("Mock ChatGoogle - Gemini", () => {

expect(record.opts.data.tools[0]).toHaveProperty("googleSearch");
});

});

describe("Mock ChatGoogle - Anthropic", () => {
Expand Down
36 changes: 9 additions & 27 deletions libs/langchain-google-common/src/tests/data/chat-6-mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,56 +40,38 @@
"endIndex": 100,
"text": "The Los Angeles Dodgers won the 2024 World Series, defeating the New York Yankees 4-1 in the series."
},
"groundingChunkIndices": [
0
],
"confidenceScores": [
0.95898277
]
"groundingChunkIndices": [0],
"confidenceScores": [0.95898277]
},
{
"segment": {
"startIndex": 308,
"endIndex": 377,
"text": "It was also their first World Series win in a full season since 1988."
},
"groundingChunkIndices": [
1
],
"confidenceScores": [
0.96841997
]
"groundingChunkIndices": [1],
"confidenceScores": [0.96841997]
},
{
"segment": {
"startIndex": 379,
"endIndex": 508,
"text": "Mookie Betts earned his third World Series ring (2018, 2020, and 2024), becoming the only active player with three championships."
},
"groundingChunkIndices": [
2
],
"confidenceScores": [
0.99043523
]
"groundingChunkIndices": [2],
"confidenceScores": [0.99043523]
},
{
"segment": {
"startIndex": 510,
"endIndex": 611,
"text": "Shohei Ohtani, in his first year with the Dodgers, also experienced his first post-season appearance."
},
"groundingChunkIndices": [
0
],
"confidenceScores": [
0.95767003
]
"groundingChunkIndices": [0],
"confidenceScores": [0.95767003]
}
],
"webSearchQueries": [
"2024 MLB World Series winner"
]
"webSearchQueries": ["2024 MLB World Series winner"]
},
"avgLogprobs": -0.040494912748883484
}
Expand Down
14 changes: 7 additions & 7 deletions libs/langchain-google-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ export interface GeminiCitation {
}

export interface GoogleTypeDate {
year: number; // 1-9999 or 0 to specify a date without a year
year: number; // 1-9999 or 0 to specify a date without a year
month: number; // 1-12 or 0 to specify a year without a month and day
day: number; // Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant
day: number; // Must be from 1 to 31 and valid for the year and month, or 0 to specify a year by itself or a year and month where the day isn't significant
}

export interface GeminiGroundingMetadata {
Expand All @@ -328,7 +328,7 @@ export interface GeminiGroundingMetadata {

export interface GeminiSearchEntryPoint {
renderedContent?: string;
sdkBlob?: string; // Base64 encoded JSON representing array of tuple.
sdkBlob?: string; // Base64 encoded JSON representing array of tuple.
}

export interface GeminiGroundingChunk {
Expand Down Expand Up @@ -378,8 +378,8 @@ export interface GeminiContent {
*/
export interface GeminiTool {
functionDeclarations?: GeminiFunctionDeclaration[];
googleSearchRetrieval?: GoogleSearchRetrieval; // Gemini-1.5
googleSearch?: GoogleSearch; // Gemini-2.0
googleSearchRetrieval?: GoogleSearchRetrieval; // Gemini-1.5
googleSearch?: GoogleSearch; // Gemini-2.0
retrieval?: VertexAIRetrieval;
}

Expand All @@ -395,13 +395,13 @@ export type GoogleSearchToolSetting =
export const GeminiSearchToolAttributes = [
"googleSearchRetrieval",
"googleSearch",
]
];

export const GeminiToolAttributes = [
"functionDeclaration",
"retrieval",
...GeminiSearchToolAttributes,
]
];

export interface GoogleSearchRetrieval {
dynamicRetrievalConfig?: {
Expand Down
21 changes: 12 additions & 9 deletions libs/langchain-google-common/src/utils/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import type {
GoogleAISafetyHandler,
GeminiPartFunctionCall,
GoogleAIAPI,
GeminiAPIConfig, GeminiGroundingSupport,
GeminiAPIConfig,
GeminiGroundingSupport,
} from "../types.js";
import { GoogleAISafetyError } from "./safety.js";
import { MediaBlob } from "../experimental/utils/media_core.js";
Expand Down Expand Up @@ -757,7 +758,7 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {
): GeminiGroundingSupport[][] {
const ret: GeminiGroundingSupport[][] = [];

if (!groundingSupports || groundingSupports.length === 0){
if (!groundingSupports || groundingSupports.length === 0) {
return [];
}

Expand All @@ -769,7 +770,6 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {
} else {
ret[partIndex] = [groundingSupport];
}

});

return ret;
Expand All @@ -786,10 +786,13 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {

// Citation and grounding information connected to each part / ChatGeneration
// to make sure they are available in downstream filters.
const candidate = (response?.data as GenerateContentResponseData)?.candidates?.[0];
const candidate = (response?.data as GenerateContentResponseData)
?.candidates?.[0];
const groundingMetadata = candidate?.groundingMetadata;
const citationMetadata = candidate?.citationMetadata;
const groundingParts = groundingSupportByPart(groundingMetadata?.groundingSupports);
const groundingParts = groundingSupportByPart(
groundingMetadata?.groundingSupports
);

const ret = parts.map((part, index) => {
const gen = partToChatGeneration(part);
Expand Down Expand Up @@ -1080,7 +1083,7 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {
function searchToolName(tool: GeminiTool): string | undefined {
for (const name of GeminiSearchToolAttributes) {
if (name in tool) {
return name
return name;
}
}
return undefined;
Expand All @@ -1092,7 +1095,7 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {
if (orig && adj && adj !== orig) {
return {
[adj as string]: {},
}
};
} else {
return tool;
}
Expand All @@ -1108,13 +1111,13 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {
// Gemini Tools may be normalized to different tool names
const langChainTools: StructuredToolParams[] = [];
const otherTools: GeminiTool[] = [];
tools.forEach(tool => {
tools.forEach((tool) => {
if (isLangChainTool(tool)) {
langChainTools.push(tool);
} else {
otherTools.push(cleanGeminiTool(tool as GeminiTool));
}
})
});

const result: GeminiTool[] = [...otherTools];

Expand Down
12 changes: 5 additions & 7 deletions libs/langchain-google-vertexai/src/tests/chat_models.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const testGeminiModelNames = [
["gemini-1.5-flash-002"],
["gemini-2.0-flash-exp"],
// ["gemini-2.0-flash-thinking-exp-1219"],
]
];

/*
* Some models may have usage quotas still.
Expand All @@ -76,7 +76,7 @@ const testGeminiModelNames = [
const testGeminiModelDelay: Record<string, number> = {
"gemini-2.0-flash-exp": 5000,
"gemini-2.0-flash-thinking-exp-1219": 5000,
}
};

describe.each(testGeminiModelNames)("GAuth Gemini Chat (%s)", (modelName) => {
let recorder: GoogleRequestRecorder;
Expand All @@ -88,9 +88,9 @@ describe.each(testGeminiModelNames)("GAuth Gemini Chat (%s)", (modelName) => {

const delay = testGeminiModelDelay[modelName] ?? 0;
if (delay) {
console.log(`Delaying for ${delay}ms`)
console.log(`Delaying for ${delay}ms`);
// eslint-disable-next-line no-promise-executor-return
await new Promise(resolve => setTimeout(resolve,delay));
await new Promise((resolve) => setTimeout(resolve, delay));
}
});

Expand Down Expand Up @@ -573,8 +573,7 @@ describe.each(testGeminiModelNames)("GAuth Gemini Chat (%s)", (modelName) => {

test("Supports GoogleSearchTool", async () => {
const searchTool: GeminiTool = {
googleSearch: {
},
googleSearch: {},
};
const model = new ChatVertexAI({
modelName,
Expand Down Expand Up @@ -611,7 +610,6 @@ describe.each(testGeminiModelNames)("GAuth Gemini Chat (%s)", (modelName) => {
}
expect(finalMsg.content as string).toContain("Dodgers");
});

});

describe("GAuth Anthropic Chat", () => {
Expand Down
Loading

0 comments on commit 4cb0d50

Please sign in to comment.