npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@langchain/ibm

v0.1.0

Published

IBM watsonx.ai integrations for LangChain.js

Readme

@langchain/ibm

This package contains the LangChain.js integrations for IBM watsonx.ai via the @ibm-cloud/watsonx-ai SDK.

Installation

npm install @langchain/ibm @langchain/core

Components

Chat Models

The ChatWatsonx class provides chat model support with tool calling, structured output, and streaming.

import { ChatWatsonx } from "@langchain/ibm";

const model = new ChatWatsonx({
  model: "ibm/granite-3-8b-instruct",
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  projectId: "your-project-id",
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
  maxTokens: 200,
});

const result = await model.invoke("What is AI?");
console.log(result.content);

Streaming

const stream = await model.stream("Tell me about machine learning");

for await (const chunk of stream) {
  process.stdout.write(chunk.content);
}

Tool Calling

import { z } from "zod";

const modelWithTools = model.bindTools([
  {
    name: "get_weather",
    description: "Get the weather for a location",
    schema: z.object({
      location: z.string().describe("The city name"),
    }),
  },
]);

const response = await modelWithTools.invoke("What's the weather in NYC?");

Structured Output

import { z } from "zod";

const structuredModel = model.withStructuredOutput(
  z.object({
    answer: z.string(),
    confidence: z.number(),
  })
);

const result = await structuredModel.invoke("What is 2+2?");
// result: { answer: "4", confidence: 1.0 }

Model Gateway

const gatewayModel = new ChatWatsonx({
  model: "ibm/granite-3-8b-instruct",
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  modelGateway: true,
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
});

Deployment

const deployedModel = new ChatWatsonx({
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  idOrName: "your-deployment-id",
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
});

Text LLM

The WatsonxLLM class provides text-generation LLM support.

import { WatsonxLLM } from "@langchain/ibm";

const llm = new WatsonxLLM({
  model: "ibm/granite-3-8b-instruct",
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  projectId: "your-project-id",
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
  maxNewTokens: 200,
  temperature: 0.7,
});

const result = await llm.invoke("Explain quantum computing");
console.log(result);

Embeddings

The WatsonxEmbeddings class provides text embedding support.

import { WatsonxEmbeddings } from "@langchain/ibm";

const embeddings = new WatsonxEmbeddings({
  model: "ibm/slate-125m-english-rtrvr-v2",
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  projectId: "your-project-id",
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
});

const vector = await embeddings.embedQuery("Hello world");
const vectors = await embeddings.embedDocuments(["Hello", "World"]);

Document Reranking

The WatsonxRerank class provides document reranking support.

import { WatsonxRerank } from "@langchain/ibm";
import { Document } from "@langchain/core/documents";

const reranker = new WatsonxRerank({
  model: "cross-encoder/ms-marco-minilm-l-12-v2",
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  projectId: "your-project-id",
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
});

const documents = [
  new Document({ pageContent: "Paris is the capital of France." }),
  new Document({ pageContent: "Berlin is the capital of Germany." }),
  new Document({ pageContent: "Tokyo is the capital of Japan." }),
];

const results = await reranker.compressDocuments(
  documents,
  "European capitals"
);

Agent Toolkits

The WatsonxToolkit class provides access to watsonx.ai utility agent tools.

import { WatsonxToolkit } from "@langchain/ibm";

const toolkit = await WatsonxToolkit.init({
  version: "2024-05-31",
  serviceUrl: "https://us-south.ml.cloud.ibm.com",
  watsonxAIApikey: "your-api-key",
  watsonxAIAuthType: "iam",
});

const tools = toolkit.getTools();
const searchTool = toolkit.getTool("GoogleSearch", { maxResults: 5 });

Authentication

The package supports multiple authentication methods:

IAM Authentication

{
  watsonxAIAuthType: "iam",
  watsonxAIApikey: "your-api-key",
}

Bearer Token Authentication

{
  watsonxAIAuthType: "bearertoken",
  watsonxAIBearerToken: "your-token",
}

Cloud Pak for Data (CP4D) Authentication

{
  watsonxAIAuthType: "cp4d",
  watsonxAIUsername: "your-username",
  watsonxAIPassword: "your-password",
  watsonxAIUrl: "your-cp4d-url",
}

Related