From a5be894fa75c73e74d49bb832e1a968f52884bd7 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 17 Jul 2024 15:22:48 -0700 Subject: [PATCH 1/2] partners[minor]: Throw error if tool_choice is supplied but not supported --- libs/langchain-cohere/src/chat_models.ts | 8 ++++++-- .../langchain-community/src/chat_models/bedrock/web.ts | 4 ++++ libs/langchain-google-common/src/chat_models.ts | 4 ++++ libs/langchain-google-common/src/types.ts | 4 ++-- libs/langchain-google-genai/src/chat_models.ts | 10 +++++++--- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libs/langchain-cohere/src/chat_models.ts b/libs/langchain-cohere/src/chat_models.ts index eb6887e53dc6..d0dc03703244 100644 --- a/libs/langchain-cohere/src/chat_models.ts +++ b/libs/langchain-cohere/src/chat_models.ts @@ -14,7 +14,6 @@ import { BaseLanguageModelInput, ToolDefinition, isOpenAITool, - type BaseLanguageModelCallOptions, } from "@langchain/core/language_models/base"; import { isStructuredTool } from "@langchain/core/utils/function_calling"; import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager"; @@ -22,6 +21,7 @@ import { type BaseChatModelParams, BaseChatModel, LangSmithParams, + BaseChatModelCallOptions, } from "@langchain/core/language_models/chat_models"; import { ChatGeneration, @@ -84,7 +84,7 @@ interface TokenUsage { } export interface ChatCohereCallOptions - extends BaseLanguageModelCallOptions, + extends BaseChatModelCallOptions, Partial>, Partial>, Pick { @@ -346,6 +346,10 @@ export class ChatCohere< } invocationParams(options: this["ParsedCallOptions"]) { + if (options.tool_choice) { + throw new Error("'tool_choice' call option is not supported by ChatCohere.") + } + const params = { model: this.model, preamble: options.preamble, diff --git a/libs/langchain-community/src/chat_models/bedrock/web.ts b/libs/langchain-community/src/chat_models/bedrock/web.ts index adc42de6bdbf..3aef91ec7da2 100644 --- a/libs/langchain-community/src/chat_models/bedrock/web.ts +++ b/libs/langchain-community/src/chat_models/bedrock/web.ts @@ -378,6 +378,10 @@ export class BedrockChat } override invocationParams(options?: this["ParsedCallOptions"]) { + if (options?.tool_choice) { + throw new Error("'tool_choice' call option is not supported by BedrockChat.") + } + const callOptionTools = formatTools(options?.tools ?? []); return { tools: [...(this._anthropicTools ?? []), ...callOptionTools], diff --git a/libs/langchain-google-common/src/chat_models.ts b/libs/langchain-google-common/src/chat_models.ts index 52a7748f2028..148d844afebf 100644 --- a/libs/langchain-google-common/src/chat_models.ts +++ b/libs/langchain-google-common/src/chat_models.ts @@ -339,6 +339,10 @@ export abstract class ChatGoogleBase * Get the parameters used to invoke the model */ override invocationParams(options?: this["ParsedCallOptions"]) { + if (options?.tool_choice) { + throw new Error(`'tool_choice' call option is not supported by ${this.getName()}.`); + } + return copyAIModelParams(this, options); } diff --git a/libs/langchain-google-common/src/types.ts b/libs/langchain-google-common/src/types.ts index 3fd20669de7c..1d314eb1f5d9 100644 --- a/libs/langchain-google-common/src/types.ts +++ b/libs/langchain-google-common/src/types.ts @@ -1,7 +1,7 @@ import type { BaseLLMParams } from "@langchain/core/language_models/llms"; -import { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base"; import { StructuredToolInterface } from "@langchain/core/tools"; import type { JsonStream } from "./utils/stream.js"; +import type { BaseChatModelCallOptions } from "@langchain/core/language_models/chat_models"; /** * Parameters needed to setup the client connection. @@ -120,7 +120,7 @@ export interface GoogleAIBaseLLMInput GoogleAISafetyParams {} export interface GoogleAIBaseLanguageModelCallOptions - extends BaseLanguageModelCallOptions, + extends BaseChatModelCallOptions, GoogleAIModelRequestParams, GoogleAISafetyParams { /** diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index e9dab61acd86..3b6327951470 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -18,12 +18,12 @@ import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs"; import { getEnvironmentVariable } from "@langchain/core/utils/env"; import { BaseChatModel, - LangSmithParams, + type BaseChatModelCallOptions, + type LangSmithParams, type BaseChatModelParams, } from "@langchain/core/language_models/chat_models"; import { NewTokenIndices } from "@langchain/core/callbacks/base"; import { - BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions, ToolDefinition, @@ -59,7 +59,7 @@ export type BaseMessageExamplePair = { }; export interface GoogleGenerativeAIChatCallOptions - extends BaseLanguageModelCallOptions { + extends BaseChatModelCallOptions { tools?: | StructuredToolInterface[] | GoogleGenerativeAIFunctionDeclarationsTool[]; @@ -364,6 +364,10 @@ export class ChatGoogleGenerativeAI invocationParams( options?: this["ParsedCallOptions"] ): Omit { + if (options?.tool_choice) { + throw new Error("'tool_choice' call option is not supported by ChatGoogleGenerativeAI.") + } + const tools = options?.tools as | GoogleGenerativeAIFunctionDeclarationsTool[] | StructuredToolInterface[] From c5427f3b0df5828e3a23d8128d95ab8b6d5da4f1 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 17 Jul 2024 15:28:27 -0700 Subject: [PATCH 2/2] chore: lint files --- libs/langchain-cohere/src/chat_models.ts | 4 +++- libs/langchain-community/src/chat_models/bedrock/web.ts | 4 +++- libs/langchain-google-common/src/chat_models.ts | 4 +++- libs/langchain-google-common/src/types.ts | 2 +- libs/langchain-google-genai/src/chat_models.ts | 4 +++- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/libs/langchain-cohere/src/chat_models.ts b/libs/langchain-cohere/src/chat_models.ts index d0dc03703244..749300e99c86 100644 --- a/libs/langchain-cohere/src/chat_models.ts +++ b/libs/langchain-cohere/src/chat_models.ts @@ -347,7 +347,9 @@ export class ChatCohere< invocationParams(options: this["ParsedCallOptions"]) { if (options.tool_choice) { - throw new Error("'tool_choice' call option is not supported by ChatCohere.") + throw new Error( + "'tool_choice' call option is not supported by ChatCohere." + ); } const params = { diff --git a/libs/langchain-community/src/chat_models/bedrock/web.ts b/libs/langchain-community/src/chat_models/bedrock/web.ts index 3aef91ec7da2..e066c69b2922 100644 --- a/libs/langchain-community/src/chat_models/bedrock/web.ts +++ b/libs/langchain-community/src/chat_models/bedrock/web.ts @@ -379,7 +379,9 @@ export class BedrockChat override invocationParams(options?: this["ParsedCallOptions"]) { if (options?.tool_choice) { - throw new Error("'tool_choice' call option is not supported by BedrockChat.") + throw new Error( + "'tool_choice' call option is not supported by BedrockChat." + ); } const callOptionTools = formatTools(options?.tools ?? []); diff --git a/libs/langchain-google-common/src/chat_models.ts b/libs/langchain-google-common/src/chat_models.ts index 148d844afebf..72caeab4aecf 100644 --- a/libs/langchain-google-common/src/chat_models.ts +++ b/libs/langchain-google-common/src/chat_models.ts @@ -340,7 +340,9 @@ export abstract class ChatGoogleBase */ override invocationParams(options?: this["ParsedCallOptions"]) { if (options?.tool_choice) { - throw new Error(`'tool_choice' call option is not supported by ${this.getName()}.`); + throw new Error( + `'tool_choice' call option is not supported by ${this.getName()}.` + ); } return copyAIModelParams(this, options); diff --git a/libs/langchain-google-common/src/types.ts b/libs/langchain-google-common/src/types.ts index 1d314eb1f5d9..c883be766a0e 100644 --- a/libs/langchain-google-common/src/types.ts +++ b/libs/langchain-google-common/src/types.ts @@ -1,7 +1,7 @@ import type { BaseLLMParams } from "@langchain/core/language_models/llms"; import { StructuredToolInterface } from "@langchain/core/tools"; -import type { JsonStream } from "./utils/stream.js"; import type { BaseChatModelCallOptions } from "@langchain/core/language_models/chat_models"; +import type { JsonStream } from "./utils/stream.js"; /** * Parameters needed to setup the client connection. diff --git a/libs/langchain-google-genai/src/chat_models.ts b/libs/langchain-google-genai/src/chat_models.ts index 3b6327951470..6a8f492e81de 100644 --- a/libs/langchain-google-genai/src/chat_models.ts +++ b/libs/langchain-google-genai/src/chat_models.ts @@ -365,7 +365,9 @@ export class ChatGoogleGenerativeAI options?: this["ParsedCallOptions"] ): Omit { if (options?.tool_choice) { - throw new Error("'tool_choice' call option is not supported by ChatGoogleGenerativeAI.") + throw new Error( + "'tool_choice' call option is not supported by ChatGoogleGenerativeAI." + ); } const tools = options?.tools as