Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

langchain-ibm

This package provides the integration between LangChain and IBM watsonx.ai through the ibm-watsonx-ai SDK.

Installation

To use the langchain-ibm package, follow these installation steps:

pip install langchain-ibm

Usage

Setting up

To use IBM's models, you must have an IBM Cloud user API key. Here's how to obtain and set up your API key:

  1. Obtain an API Key: For more details on how to create and manage an API key, refer to IBM's documentation.
  2. Set the API Key as an Environment Variable: For security reasons, it's recommended to not hard-code your API key directly in your scripts. Instead, set it up as an environment variable. You can use the following code to prompt for the API key and set it as an environment variable:
import os
from getpass import getpass

watsonx_api_key = getpass()
os.environ["WATSONX_APIKEY"] = watsonx_api_key

In alternative, you can set the environment variable in your terminal.

  • Linux/macOS: Open your terminal and execute the following command:

    export WATSONX_APIKEY='your_ibm_api_key'
    

    To make this environment variable persistent across terminal sessions, add the above line to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file.

  • Windows: For Command Prompt, use:

    set WATSONX_APIKEY=your_ibm_api_key
    

Loading the model

You might need to adjust model parameters for different models or tasks. For more details on the parameters, refer to IBM's documentation.

parameters = {
    "decoding_method": "sample",
    "max_new_tokens": 100,
    "min_new_tokens": 1,
    "temperature": 0.5,
    "top_k": 50,
    "top_p": 1,
}

Initialize the WatsonxLLM class with the previously set parameters.

from langchain_ibm import WatsonxLLM

watsonx_llm = WatsonxLLM(
    model_id="PASTE THE CHOSEN MODEL_ID HERE",
    url="PASTE YOUR URL HERE",
    project_id="PASTE YOUR PROJECT_ID HERE",
    params=parameters,
)

Note:

  • You must provide a project_id or space_id. For more information refer to IBM's documentation.
  • Depending on the region of your provisioned service instance, use one of the urls described here.
  • You need to specify the model you want to use for inferencing through model_id. You can find the list of available models here.

Alternatively you can use Cloud Pak for Data credentials. For more details, refer to IBM's documentation.

watsonx_llm = WatsonxLLM(
    model_id="ibm/granite-13b-instruct-v2",
    url="PASTE YOUR URL HERE",
    username="PASTE YOUR USERNAME HERE",
    password="PASTE YOUR PASSWORD HERE",
    instance_id="openshift",
    version="4.8",
    project_id="PASTE YOUR PROJECT_ID HERE",
    params=parameters,
)

Create a Chain

Create PromptTemplate objects which will be responsible for creating a random question.

from langchain_core.prompts import PromptTemplate

template = "Generate a random question about {topic}: Question: "
prompt = PromptTemplate.from_template(template)

Provide a topic and run the LLMChain.

from langchain_core.output_parsers import StrOutputParser

llm_chain = prompt | watsonx_llm | StrOutputParser()
topic = "dog"
llm_chain.invoke(topic)

print(response)

Calling the Model Directly

To obtain completions, you can call the model directly using a string prompt.

# Calling a single prompt

response = watsonx_llm.invoke("Who is man's best friend?")
print(response)
# Calling multiple prompts

response = watsonx_llm.generate(
    [
        "The fastest dog in the world?",
        "Describe your chosen dog breed",
    ]
)
print(response)

Streaming the Model output

You can stream the model output.

for chunk in watsonx_llm.stream(
    "Describe your favorite breed of dog and why it is your favorite."
):
    print(chunk, end="", flush=True)

Metadata

Release files for langchain-ibm 1.0.0a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-ibm 1.0.0a2
File Size Uploaded Provenance Actions
langchain_ibm-1.0.0a2.tar.gz 57.8 kB Without provenance Details for langchain_ibm-1.0.0a2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-ibm 1.0.0a2
File Interpreter ABI Platform Actions
langchain_ibm-1.0.0a2-py3-none-any.whl Python 3 none any Details for langchain_ibm-1.0.0a2-py3-none-any.whl

Total release size: 104.7 kB

Release files / langchain_ibm-1.0.0a2.tar.gz

Download URL langchain_ibm-1.0.0a2.tar.gz
Size 57.8 kB
Tags Source
SHA-256 checksum
How to use checksums
beed015a0bdc3914064dd4f7c1e50250fdffe0dfff9de340bc83c8410b71bf40
BLAKE2b-256 checksum
How to use checksums
aaecebeb4577ce8e59ce25ad8a5c69bb286ac6d662737857460f1ccab8ae4db3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / langchain_ibm-1.0.0a2-py3-none-any.whl

Download URL langchain_ibm-1.0.0a2-py3-none-any.whl
Size 46.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba8ca3765e862036ae4cd0a11e74434004ac6557c588cb66090e832d11a6088a
BLAKE2b-256 checksum
How to use checksums
c20153ab5d9016e8c0c38a24e0747e154ce04c3b1065e232b1f3b65b108babc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Security for langchain-ibm 1.0.0a2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page