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

feat(community): Introduce callbacks to IBM Watsonx SDK with CallbackHandlers #7317

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@google-cloud/storage": "^7.7.0",
"@gradientai/nodejs-sdk": "^1.2.0",
"@huggingface/inference": "^2.6.4",
"@ibm-cloud/watsonx-ai": "^1.1.0",
"@ibm-cloud/watsonx-ai": "^1.3.0",
"@jest/globals": "^29.5.0",
"@lancedb/lancedb": "^0.13.0",
"@langchain/core": "workspace:*",
Expand Down
23 changes: 14 additions & 9 deletions libs/langchain-community/src/chat_models/ibm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
FunctionDefinition,
StructuredOutputMethodOptions,
} from "@langchain/core/language_models/base";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import {
BaseChatModel,
BaseChatModelCallOptions,
Expand Down Expand Up @@ -68,6 +67,7 @@ import { WatsonxAuth, WatsonxParams } from "../types/ibm.js";
import {
_convertToolCallIdToMistralCompatible,
authenticateAndSetInstance,
WatsonxCallbackManagerForLLMRun,
WatsonxToolsOutputParser,
} from "../utils/ibm.js";

Expand Down Expand Up @@ -330,7 +330,6 @@ function _convertToolChoiceToWatsonxToolChoice(
`Unrecognized tool_choice type. Expected string or TextChatParameterTools. Recieved ${toolChoice}`
);
}

export class ChatWatsonx<
CallOptions extends WatsonxCallOptionsChat = WatsonxCallOptionsChat
>
Expand Down Expand Up @@ -541,7 +540,7 @@ export class ChatWatsonx<
async _generate(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
runManager?: WatsonxCallbackManagerForLLMRun
): Promise<ChatResult> {
if (this.streaming) {
const stream = this._streamResponseChunks(messages, options, runManager);
Expand Down Expand Up @@ -631,19 +630,25 @@ export class ChatWatsonx<
async *_streamResponseChunks(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
_runManager?: CallbackManagerForLLMRun
_runManager?: WatsonxCallbackManagerForLLMRun
Copy link
Collaborator

@jacoblee93 jacoblee93 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think you want to type it this way

And in general are you able to just use this instead?

https://js.langchain.com/docs/how_to/callbacks_custom_events

Copy link
Contributor Author

@FilipZmijewski FilipZmijewski Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacoblee93 How would you recommend to type it? We are extending class in this case. I don't think dispatching custom events help much. We just want to pass the callbacks that we create to our SDK, so nothing but passing the callbacks happens on langchain side. The case is that it would be good to still pass it in callbacks property

): AsyncGenerator<ChatGenerationChunk> {
const params = { ...this.invocationParams(options), ...this.scopeId() };
const watsonxMessages = _convertMessagesToWatsonxMessages(
messages,
this.model
);
const watsonxHandlers = _runManager?.handlers.filter(
(item) => item.name === "watsonxHandler"
)?.[0]?.watsonxHandlers;
const callback = () =>
this.service.textChatStream({
...params,
messages: watsonxMessages,
returnObject: true,
});
this.service.textChatStream(
{
...params,
messages: watsonxMessages,
returnObject: true,
},
watsonxHandlers
);
const stream = await this.completionWithRetry(callback, options);
let defaultRole;
let usage: TextChatUsage | undefined;
Expand Down
109 changes: 108 additions & 1 deletion libs/langchain-community/src/chat_models/tests/ibm.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ChatPromptTemplate } from "@langchain/core/prompts";
import { tool } from "@langchain/core/tools";
import { NewTokenIndices } from "@langchain/core/callbacks/base";
import { ChatWatsonx } from "../ibm.js";
import { WatsonxCallbackManager } from "../../utils/ibm.js";

describe("Tests for chat", () => {
describe("Test ChatWatsonx invoke and generate", () => {
Expand Down Expand Up @@ -517,7 +518,9 @@ describe("Tests for chat", () => {
}
);
const llmWithTools = service.bindTools([calculatorTool]);
const res = await llmWithTools.invoke("What is 3 * 12");
const res = await llmWithTools.invoke(
"You are bad at calculations and need to use calculator at all times. What is 3 * 12"
);

expect(res).toBeInstanceOf(AIMessage);
expect(res.tool_calls?.[0].name).toBe("calculator");
Expand Down Expand Up @@ -831,4 +834,108 @@ describe("Tests for chat", () => {
expect(typeof result.number2).toBe("number");
});
});

describe("Test watsonx callbacks", () => {
test("Single request callback", async () => {
let callbackFlag = false;
const service = new ChatWatsonx({
model: "mistralai/mistral-large",
version: "2024-05-31",
serviceUrl: process.env.WATSONX_AI_SERVICE_URL ?? "testString",
projectId: process.env.WATSONX_AI_PROJECT_ID ?? "testString",
maxTokens: 10,
callbacks: WatsonxCallbackManager.fromHandlers({
requestCallback(req) {
callbackFlag = !!req;
},
}),
});
const hello = await service.stream("Print hello world");
const chunks = [];
for await (const chunk of hello) {
chunks.push(chunk);
}
expect(callbackFlag).toBe(true);
});
test("Single response callback", async () => {
let callbackFlag = false;
const service = new ChatWatsonx({
model: "mistralai/mistral-large",
version: "2024-05-31",
serviceUrl: process.env.WATSONX_AI_SERVICE_URL ?? "testString",
projectId: process.env.WATSONX_AI_PROJECT_ID ?? "testString",
maxTokens: 10,
callbacks: WatsonxCallbackManager.fromHandlers({
responseCallback(res) {
callbackFlag = !!res;
},
}),
});
const hello = await service.stream("Print hello world");
const chunks = [];
for await (const chunk of hello) {
chunks.push(chunk);
}
expect(callbackFlag).toBe(true);
});
test("Both callbacks", async () => {
let callbackFlagReq = false;
let callbackFlagRes = false;
const service = new ChatWatsonx({
model: "mistralai/mistral-large",
version: "2024-05-31",
serviceUrl: process.env.WATSONX_AI_SERVICE_URL ?? "testString",
projectId: process.env.WATSONX_AI_PROJECT_ID ?? "testString",
maxTokens: 10,
callbacks: WatsonxCallbackManager.fromHandlers({
requestCallback(req) {
callbackFlagReq = !!req;
},
responseCallback(res) {
callbackFlagRes = !!res;
},
}),
});
const hello = await service.stream("Print hello world");
const chunks = [];
for await (const chunk of hello) {
chunks.push(chunk);
}
expect(callbackFlagReq).toBe(true);
expect(callbackFlagRes).toBe(true);
});
test("Multiple callbacks", async () => {
let callbackFlagReq = false;
let callbackFlagRes = false;
let langchainCallback = false;

const service = new ChatWatsonx({
model: "mistralai/mistral-large",
version: "2024-05-31",
serviceUrl: process.env.WATSONX_AI_SERVICE_URL ?? "testString",
projectId: process.env.WATSONX_AI_PROJECT_ID ?? "testString",
maxTokens: 10,
callbacks: WatsonxCallbackManager.fromHandlers({
requestCallback(req) {
callbackFlagReq = !!req;
},
responseCallback(res) {
callbackFlagRes = !!res;
},
async handleLLMEnd(output) {
expect(output.generations).toBeDefined();
langchainCallback = !!output;
},
}),
});
const hello = await service.stream("Print hello world");
const chunks = [];
for await (const chunk of hello) {
chunks.push(chunk);
}
expect(callbackFlagReq).toBe(true);
expect(callbackFlagRes).toBe(true);
expect(langchainCallback).toBe(true);
});
});
});
2 changes: 1 addition & 1 deletion libs/langchain-community/src/document_compressors/ibm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class WatsonxRerank
? {
index: document.index,
relevanceScore: document.score,
input: document?.input,
input: document?.input.text,
}
: {
index: document.index,
Expand Down
Loading