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

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

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading