Docling IBM Models

3.13.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize and use `WatsonxLLM` and `WatsonxEmbeddings` provided by `docling-ibm-models` within the LangChain framework. It requires IBM Cloud API Key, region, and project ID, typically provided via environment variables. The example shows generating text with an LLM and creating embeddings for text.

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.")

view raw JSON →