Skip to content

Commit

Permalink
partners[minor]: Throw error if tool_choice is supplied but not suppo…
Browse files Browse the repository at this point in the history
…rted (#6119)

* partners[minor]: Throw error if tool_choice is supplied but not supported

* chore: lint files
  • Loading branch information
bracesproul committed Jul 17, 2024
1 parent 227447f commit 639308d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
10 changes: 8 additions & 2 deletions libs/langchain-cohere/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ 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";
import {
type BaseChatModelParams,
BaseChatModel,
LangSmithParams,
BaseChatModelCallOptions,
} from "@langchain/core/language_models/chat_models";
import {
ChatGeneration,
Expand Down Expand Up @@ -84,7 +84,7 @@ interface TokenUsage {
}

export interface ChatCohereCallOptions
extends BaseLanguageModelCallOptions,
extends BaseChatModelCallOptions,
Partial<Omit<Cohere.ChatRequest, "message" | "tools">>,
Partial<Omit<Cohere.ChatStreamRequest, "message" | "tools">>,
Pick<ChatCohereInput, "streamUsage"> {
Expand Down Expand Up @@ -346,6 +346,12 @@ 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,
Expand Down
6 changes: 6 additions & 0 deletions libs/langchain-community/src/chat_models/bedrock/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ 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],
Expand Down
6 changes: 6 additions & 0 deletions libs/langchain-google-common/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ export abstract class ChatGoogleBase<AuthOptions>
* 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);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/langchain-google-common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 { BaseChatModelCallOptions } from "@langchain/core/language_models/chat_models";
import type { JsonStream } from "./utils/stream.js";

/**
Expand Down Expand Up @@ -120,7 +120,7 @@ export interface GoogleAIBaseLLMInput<AuthOptions>
GoogleAISafetyParams {}

export interface GoogleAIBaseLanguageModelCallOptions
extends BaseLanguageModelCallOptions,
extends BaseChatModelCallOptions,
GoogleAIModelRequestParams,
GoogleAISafetyParams {
/**
Expand Down
12 changes: 9 additions & 3 deletions libs/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -59,7 +59,7 @@ export type BaseMessageExamplePair = {
};

export interface GoogleGenerativeAIChatCallOptions
extends BaseLanguageModelCallOptions {
extends BaseChatModelCallOptions {
tools?:
| StructuredToolInterface[]
| GoogleGenerativeAIFunctionDeclarationsTool[];
Expand Down Expand Up @@ -364,6 +364,12 @@ export class ChatGoogleGenerativeAI
invocationParams(
options?: this["ParsedCallOptions"]
): Omit<GenerateContentRequest, "contents"> {
if (options?.tool_choice) {
throw new Error(
"'tool_choice' call option is not supported by ChatGoogleGenerativeAI."
);
}

const tools = options?.tools as
| GoogleGenerativeAIFunctionDeclarationsTool[]
| StructuredToolInterface[]
Expand Down

0 comments on commit 639308d

Please sign in to comment.