@langchain/google-common
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -6,3 +6,3 @@ import { type BaseMessage } from "@langchain/core/messages"; | ||
import { AIMessageChunk } from "@langchain/core/messages"; | ||
import { BaseLanguageModelInput, StructuredOutputMethodOptions } from "@langchain/core/language_models/base"; | ||
import { BaseLanguageModelInput, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base"; | ||
import type { z } from "zod"; | ||
@@ -27,3 +27,3 @@ import { Runnable } from "@langchain/core/runnables"; | ||
*/ | ||
export interface ChatGoogleBaseInput<AuthOptions> extends BaseChatModelParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams, GoogleAISafetyParams { | ||
export interface ChatGoogleBaseInput<AuthOptions> extends BaseChatModelParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams, GoogleAISafetyParams, Pick<GoogleAIBaseLanguageModelCallOptions, "streamUsage"> { | ||
} | ||
@@ -49,2 +49,3 @@ /** | ||
safetyHandler: GoogleAISafetyHandler; | ||
streamUsage: boolean; | ||
protected connection: ChatConnection<AuthOptions>; | ||
@@ -60,3 +61,3 @@ protected streamedConnection: ChatConnection<AuthOptions>; | ||
get platform(): GooglePlatformType; | ||
bindTools(tools: (StructuredToolInterface | Record<string, unknown>)[], kwargs?: Partial<GoogleAIBaseLanguageModelCallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, GoogleAIBaseLanguageModelCallOptions>; | ||
bindTools(tools: (StructuredToolInterface | Record<string, unknown> | ToolDefinition)[], kwargs?: Partial<GoogleAIBaseLanguageModelCallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, GoogleAIBaseLanguageModelCallOptions>; | ||
_llmType(): string; | ||
@@ -63,0 +64,0 @@ /** |
@@ -5,2 +5,3 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env"; | ||
import { AIMessageChunk } from "@langchain/core/messages"; | ||
import { isOpenAITool, } from "@langchain/core/language_models/base"; | ||
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables"; | ||
@@ -14,3 +15,3 @@ import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools"; | ||
import { ensureParams } from "./utils/failed_handler.js"; | ||
import { zodToGeminiParameters } from "./utils/zod_to_gemini_parameters.js"; | ||
import { jsonSchemaToGeminiParameters, zodToGeminiParameters, } from "./utils/zod_to_gemini_parameters.js"; | ||
class ChatConnection extends AbstractGoogleLLMConnection { | ||
@@ -98,2 +99,10 @@ constructor(fields, caller, client, streaming) { | ||
} | ||
if (isOpenAITool(structuredTool)) { | ||
return { | ||
name: structuredTool.function.name, | ||
description: structuredTool.function.description ?? | ||
`A function available to call.`, | ||
parameters: jsonSchemaToGeminiParameters(structuredTool.function.parameters), | ||
}; | ||
} | ||
return structuredTool; | ||
@@ -187,2 +196,8 @@ }), | ||
}); | ||
Object.defineProperty(this, "streamUsage", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: true | ||
}); | ||
Object.defineProperty(this, "connection", { | ||
@@ -203,2 +218,3 @@ enumerable: true, | ||
fields?.safetyHandler ?? new DefaultGeminiSafetyHandler(); | ||
this.streamUsage = fields?.streamUsage ?? this.streamUsage; | ||
const client = this.buildClient(fields); | ||
@@ -265,2 +281,3 @@ this.buildConnection(fields ?? {}, client); | ||
const stream = response.data; | ||
let usageMetadata; | ||
// Loop until the end of the stream | ||
@@ -271,2 +288,12 @@ // During the loop, yield each time we get a chunk from the streaming parser | ||
const output = await stream.nextChunk(); | ||
if (output && | ||
output.usageMetadata && | ||
this.streamUsage !== false && | ||
options.streamUsage !== false) { | ||
usageMetadata = { | ||
input_tokens: output.usageMetadata.promptTokenCount, | ||
output_tokens: output.usageMetadata.candidatesTokenCount, | ||
total_tokens: output.usageMetadata.totalTokenCount, | ||
}; | ||
} | ||
const chunk = output !== null | ||
@@ -279,2 +306,3 @@ ? safeResponseToChatGeneration({ data: output }, this.safetyHandler) | ||
content: "", | ||
usage_metadata: usageMetadata, | ||
}), | ||
@@ -281,0 +309,0 @@ }); |
@@ -98,2 +98,8 @@ import type { BaseLLMParams } from "@langchain/core/language_models/llms"; | ||
export interface GoogleAIBaseLanguageModelCallOptions extends BaseLanguageModelCallOptions, GoogleAIModelRequestParams, GoogleAISafetyParams { | ||
/** | ||
* Whether or not to include usage data, like token counts | ||
* in the streamed response chunks. | ||
* @default true | ||
*/ | ||
streamUsage?: boolean; | ||
} | ||
@@ -100,0 +106,0 @@ /** |
@@ -464,2 +464,11 @@ import { v4 as uuidv4 } from "uuid"; | ||
})); | ||
let usageMetadata; | ||
if ("usageMetadata" in response.data) { | ||
usageMetadata = { | ||
input_tokens: response.data.usageMetadata.promptTokenCount, | ||
output_tokens: response.data.usageMetadata | ||
.candidatesTokenCount, | ||
total_tokens: response.data.usageMetadata.totalTokenCount, | ||
}; | ||
} | ||
ret = [ | ||
@@ -471,2 +480,3 @@ new ChatGenerationChunk({ | ||
tool_call_chunks: toolCallChunks, | ||
usage_metadata: usageMetadata, | ||
}), | ||
@@ -473,0 +483,0 @@ text: combinedText, |
import type { z } from "zod"; | ||
import { GeminiFunctionSchema } from "../types.js"; | ||
import { GeminiFunctionSchema, GeminiJsonSchema } from "../types.js"; | ||
export declare function removeAdditionalProperties(obj: Record<string, any>): GeminiJsonSchema; | ||
export declare function zodToGeminiParameters(zodObj: z.ZodType<any>): GeminiFunctionSchema; | ||
export declare function jsonSchemaToGeminiParameters(schema: Record<string, any>): GeminiFunctionSchema; |
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
function removeAdditionalProperties(schema) { | ||
const updatedSchema = { ...schema }; | ||
if (Object.hasOwn(updatedSchema, "additionalProperties")) { | ||
delete updatedSchema.additionalProperties; | ||
export function removeAdditionalProperties( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
obj) { | ||
if (typeof obj === "object" && obj !== null) { | ||
const newObj = { ...obj }; | ||
if ("additionalProperties" in newObj && | ||
typeof newObj.additionalProperties === "boolean") { | ||
delete newObj.additionalProperties; | ||
} | ||
for (const key in newObj) { | ||
if (key in newObj) { | ||
if (Array.isArray(newObj[key])) { | ||
newObj[key] = newObj[key].map(removeAdditionalProperties); | ||
} | ||
else if (typeof newObj[key] === "object" && newObj[key] !== null) { | ||
newObj[key] = removeAdditionalProperties(newObj[key]); | ||
} | ||
} | ||
} | ||
return newObj; | ||
} | ||
if (updatedSchema.properties) { | ||
const keys = Object.keys(updatedSchema.properties); | ||
removeProperties(updatedSchema.properties, keys, 0); | ||
} | ||
if (Object.hasOwn(updatedSchema, "items") && updatedSchema.items) { | ||
updatedSchema.items = removeAdditionalProperties(updatedSchema.items); | ||
} | ||
return updatedSchema; | ||
return obj; | ||
} | ||
function removeProperties(properties, keys, index) { | ||
if (index >= keys.length) { | ||
return; | ||
} | ||
const key = keys[index]; | ||
// eslint-disable-next-line no-param-reassign | ||
properties[key] = removeAdditionalProperties(properties[key]); | ||
removeProperties(properties, keys, index + 1); | ||
} | ||
export function zodToGeminiParameters( | ||
@@ -32,3 +32,2 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
// const jsonSchema = zodToJsonSchema(zodObj) as any; | ||
const jsonSchema = removeAdditionalProperties(zodToJsonSchema(zodObj)); | ||
@@ -38,1 +37,11 @@ const { $schema, ...rest } = jsonSchema; | ||
} | ||
export function jsonSchemaToGeminiParameters( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
schema) { | ||
// Gemini doesn't accept either the $schema or additionalProperties | ||
// attributes, so we need to explicitly remove them. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const jsonSchema = removeAdditionalProperties(schema); | ||
const { $schema, ...rest } = jsonSchema; | ||
return rest; | ||
} |
{ | ||
"name": "@langchain/google-common", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Core types and classes for Google services.", | ||
@@ -43,3 +43,3 @@ "type": "module", | ||
"dependencies": { | ||
"@langchain/core": ">0.1.56 <0.3.0", | ||
"@langchain/core": ">=0.2.9 <0.3.0", | ||
"uuid": "^9.0.0", | ||
@@ -46,0 +46,0 @@ "zod-to-json-schema": "^3.22.4" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195230
5144