Docling IBM Models
This package provides AI models from IBM, including Large Language Models (LLMs) and embeddings, specifically designed for use with the LangChain framework. It is also an optional dependency for the Docling PDF conversion package, enabling advanced AI capabilities within Docling. Currently at version 3.13.0, it follows a relatively frequent release cadence, with updates typically occurring on a monthly basis.
Warnings
- gotcha IBM Cloud API Key, region, and project ID are mandatory for all model interactions with Watsonx.ai services and must be correctly configured. Failure to provide these credentials or providing incorrect ones will result in authentication or authorization errors.
- gotcha The availability and compatibility of specific LLM or embedding models (e.g., 'google/flan-ul2', 'intfloat/multilingual-e5-large') can vary by IBM Cloud region and your project's entitlements. Using an unavailable or unauthorized model will result in runtime errors.
- breaking When using `docling-ibm-models` with the main `docling` package, be aware that `docling-ibm-models` may pin `docling` to a specific, older version (e.g., `docling==0.1.0`) in its optional dependencies. Installing a newer `docling` version independently can lead to compatibility issues or unexpected behavior.
- gotcha Depending on the specific models or underlying libraries (e.g., `sentence-transformers` for some embeddings), initial usage might trigger large model downloads, consuming significant disk space and bandwidth. This can impact application startup time and resource usage, especially in containerized environments.
Install
-
pip install docling-ibm-models -
pip install "docling-ibm-models[docling]"
Imports
- WatsonxLLM
from docling_ibm_models.llms import WatsonxLLM
- WatsonxEmbeddings
from docling_ibm_models.embeddings import WatsonxEmbeddings
Quickstart
import os
from docling_ibm_models.llms import WatsonxLLM
from docling_ibm_models.embeddings import WatsonxEmbeddings
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
# --- IBM Cloud Credentials (set as environment variables) ---
# IBM_CLOUD_API_KEY="YOUR_IBM_CLOUD_API_KEY"
# IBM_CLOUD_REGION="us-south" # Example: 'us-south', 'jp-tok', 'eu-de'
# IBM_CLOUD_PROJECT_ID="YOUR_IBM_CLOUD_PROJECT_ID"
api_key = os.environ.get("IBM_CLOUD_API_KEY", "")
region = os.environ.get("IBM_CLOUD_REGION", "us-south")
project_id = os.environ.get("IBM_CLOUD_PROJECT_ID", "")
if not all([api_key, region, project_id]):
print("Please set IBM_CLOUD_API_KEY, IBM_CLOUD_REGION, and IBM_CLOUD_PROJECT_ID environment variables.")
print("Skipping LLM and Embeddings quickstart examples.")
else:
try:
print("\n--- Initializing WatsonxLLM ---")
# Initialize the WatsonxLLM
llm = WatsonxLLM(
api_key=api_key,
region=region,
project_id=project_id,
model_id="google/flan-ul2", # Example model, check available models for your region
temperature=0.7,
max_new_tokens=200
)
# Create a simple LangChain chain
prompt = PromptTemplate.from_template("What is the capital of {country}?")
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
# Invoke the chain
response = chain.invoke({"country": "France"})
print(f"Capital of France: {response.strip()}")
print("\n--- Initializing WatsonxEmbeddings ---")
# Initialize WatsonxEmbeddings
embeddings_model = WatsonxEmbeddings(
api_key=api_key,
region=region,
project_id=project_id,
model_id="intfloat/multilingual-e5-large" # Example embedding model
)
# Embed documents
texts_to_embed = ["Hello world from IBM models", "Docling integration example"]
text_embeddings = embeddings_model.embed_documents(texts_to_embed)
print(f"Embeddings for '{texts_to_embed[0]}' (first 5 dims): {text_embeddings[0][:5]}...")
except Exception as e:
print(f"\nAn error occurred during quickstart execution: {e}")
print("Ensure your IBM Cloud credentials are correct, model IDs are valid for your project/region, and you have necessary entitlements.")