Skip to main content

LangChain integration for PubNub real-time messaging

Project description

langchain-pubnub

PyPI version Python Version License: MIT

LangChain integration for PubNub real-time messaging platform. This package provides tools that allow LangChain agents to publish messages, fetch message history, and subscribe to real-time updates.

Installation

pip install langchain-pubnub

Quick Start

Using the Toolkit

from langchain_pubnub import PubNubToolkit

# Initialize the toolkit
toolkit = PubNubToolkit(
    publish_key="your-publish-key",  # or "demo" for testing
    subscribe_key="your-subscribe-key",  # or "demo" for testing
    user_id="my-langchain-agent"
)

# Get all tools
tools = toolkit.get_tools()

# Or access individual tools
publish_tool = toolkit.publish_tool
history_tool = toolkit.history_tool
subscribe_tool = toolkit.subscribe_tool

Using the Factory Function

from langchain_pubnub import create_pubnub_tools

tools = create_pubnub_tools(
    publish_key="demo",
    subscribe_key="demo",
    user_id="my-agent"
)

With a LangChain Agent

from langchain_pubnub import create_pubnub_tools
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import PromptTemplate

# Create tools
tools = create_pubnub_tools(
    publish_key="your-publish-key",
    subscribe_key="your-subscribe-key",
    user_id="my-agent"
)

# Initialize LLM
llm = ChatOpenAI(model="gpt-4")

# Create agent
prompt = PromptTemplate.from_template("""...""")
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Use the agent
agent_executor.invoke({
    "input": "Send a greeting to the 'announcements' channel"
})

Available Tools

PubNubPublishTool

Publish messages to a PubNub channel.

result = publish_tool.invoke({
    "channel": "my-channel",
    "message": {"text": "Hello, World!"},
    "meta": {"priority": "high"}  # optional
})

Parameters:

  • channel (str, required): The channel to publish to
  • message (str | dict, required): The message payload (must be JSON-serializable)
  • meta (dict, optional): Metadata for message filtering

PubNubHistoryTool

Fetch historical messages from PubNub channels.

result = history_tool.invoke({
    "channels": ["channel-1", "channel-2"],
    "count": 25,
    "include_meta": True
})

Parameters:

  • channels (list[str], required): Channels to fetch history from
  • count (int, optional): Number of messages per channel (default: 25, max: 100)
  • include_meta (bool, optional): Include message metadata (default: False)
  • start (int, optional): Start timetoken for pagination
  • end (int, optional): End timetoken for pagination

PubNubSubscribeTool

Subscribe to a channel and collect real-time messages.

result = subscribe_tool.invoke({
    "channel": "my-channel",
    "timeout": 5,
    "max_messages": 10
})

Parameters:

  • channel (str, required): The channel to subscribe to
  • timeout (int, optional): Max seconds to wait (default: 5)
  • max_messages (int, optional): Max messages to collect (default: 10)

Configuration Options

The PubNubToolkit accepts additional configuration options:

toolkit = PubNubToolkit(
    publish_key="your-publish-key",
    subscribe_key="your-subscribe-key",
    user_id="my-agent",
    ssl=True,  # Enable SSL (default: True)
    # Additional PNConfiguration options...
)

Development

Setup

# Clone the repository
git clone https://github.com/pubnub/langchain-pubnub.git
cd langchain-pubnub

# Install dependencies
pip install -e ".[dev]"

# Install test dependencies
pip install -e ".[test]"

Running Tests

# Run unit tests
pytest tests/unit_tests/ -v

# Run integration tests (requires network)
pytest tests/integration_tests/ -v

# Run all tests with coverage
pytest --cov=langchain_pubnub --cov-report=term-missing

# Skip integration tests
SKIP_INTEGRATION_TESTS=true pytest

Linting

# Run ruff linter
ruff check .

# Run ruff formatter
ruff format .

Type Checking

mypy langchain_pubnub

Requirements

  • Python >= 3.9
  • langchain-core >= 0.2.0
  • pubnub >= 10.0.0
  • pydantic >= 2.0.0

License

MIT License - see LICENSE for details.

Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_pubnub-0.1.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_pubnub-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file langchain_pubnub-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_pubnub-0.1.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for langchain_pubnub-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a7ff5679cf6629814384b2736eb2544a340eaa675b18f11af51323e56b040c8e
MD5 7a9d3c5b148b15c32bc9dc36438b4510
BLAKE2b-256 6a8909658fac875ead851f732b18c7fd250c90d8ecdd89cb65bad922b24f5f2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_pubnub-0.1.0.tar.gz:

Publisher: publish.yml on pubnub/langchain-pubnub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_pubnub-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_pubnub-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b689458c5093e4bd990de3fddd3fbc1d10941cc6f11ce6e62683f58b3abd6407
MD5 759643e15f1500ae6c0da8e56b39875f
BLAKE2b-256 00c5d98f4139a1d6ce212e8201c962b9300315469d5d2a01fa6818af173938e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_pubnub-0.1.0-py3-none-any.whl:

Publisher: publish.yml on pubnub/langchain-pubnub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page