Skip to main content

🦜️🔗 LangChain UAgents

This package contains the LangChain integration with uAgents, allowing you to convert Langchain agents into uAgents and register them on Agentverse.

langchain-uagents

Agentverse website

Features

  • Convert Langchain agents to uAgents
  • Automatic port allocation with fallback options
  • Register agents on Agentverse
  • Support for AI agent message forwarding
  • ASI1-agentic accessible agent
  • Clean shutdown and resource management

Installation

pip install -U langchain-uagents

Credentials

We also need to set our Agentverse API key. You can get an API key by visiting this site, or follow the steps in the Agentverse documentation.

import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Get API token for Agentverse
API_TOKEN = os.getenv("AV_API_KEY", "your_default_key_here")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "your_default_openai_key_here")

UAgentRegisterTool

Here we show how to use the UAgentRegisterTool to convert a Langchain agent into a uAgent and register it on Agentverse.

Instantiation

from langchain_uagents import UAgentRegisterTool

tool = UAgentRegisterTool()

Basic Usage

The UAgentRegisterTool accepts the following parameters during invocation:

  • agent_obj (required): The Langchain agent object to convert
  • name (required): Name for the uAgent
  • port (optional, int): Port to run the agent on, defaults to 8000
  • description (optional, str): Description of the agent's functionality
  • api_token (required): Agentverse API token for registration
  • start_range (optional, int): Start of port range for automatic allocation
  • end_range (optional, int): End of port range for automatic allocation
agent_info = tool.invoke({
    "agent_obj": agent,
    "name": "my_agent",
    "port": 8080,
    "description": "A useful agent for my tasks",
    "api_token": API_TOKEN
})

# Print agent info
print(f"Created uAgent '{agent_info['name']}' with address {agent_info['address']} on port {agent_info['port']}")

Complete Example

Here's a complete example showing how to create a calculator agent with Langchain and register it as a uAgent:

import os
import time
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, AgentType, Tool
from langchain_uagents import UAgentRegisterTool, cleanup_uagent

# Load environment variables
load_dotenv()

# Get API token for Agentverse
API_TOKEN = os.getenv("AV_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

# Define a simple calculator tool
def calculator_tool(expression: str) -> str:
    """Evaluates a basic math expression (e.g., '2 + 2 * 3')."""
    try:
        result = eval(expression)
        return str(result)
    except Exception as e:
        return f"Error: {str(e)}"

# Create the langchain agent
tools = [
    Tool(
        name="Calculator",
        func=calculator_tool,
        description="Useful for evaluating math expressions"
    )
]

llm = ChatOpenAI(temperature=0, api_key=OPENAI_API_KEY)
agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# Create and register the uAgent
tool = UAgentRegisterTool()
agent_info = tool.invoke({
    "agent_obj": agent,
    "name": "calculator_agent",
    "port": 8080,
    "description": "A calculator agent for testing",
    "api_token": API_TOKEN
})

# Print agent info
print(f"Created uAgent '{agent_info['name']}' with address {agent_info['address']} on port {agent_info['port']}")

# Keep the agent running
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print("\nShutting down calculator agent...")
    cleanup_uagent("calculator_agent")
    print("Calculator agent stopped.")

Port Allocation

The tool automatically handles port allocation:

  1. First tries to use the specified port
  2. If the port is in use, searches for an available port in the range 8000-9000
  3. Raises a RuntimeError if no ports are available

You can customize the port range:

agent_info = tool.invoke({
    "agent_obj": agent,
    "name": "my_agent",
    "port": 8080,  # Preferred port
    "start_range": 8000,  # Start of port range
    "end_range": 9000,    # End of port range
    "description": "A useful agent",
    "api_token": API_TOKEN
})

Cleanup

Always clean up your uAgent when done:

from langchain_uagents import cleanup_uagent

# Clean up by agent name
cleanup_uagent("my_agent")

Environment Variables

The tool requires the following environment variables:

  • AV_API_KEY: Your Agentverse API key for registering agents
  • OPENAI_API_KEY: Your OpenAI API key for the Langchain agent

You can set these in a .env file or export them in your environment:

export AV_API_KEY="your_agentverse_api_key"
export OPENAI_API_KEY="your_openai_api_key"

Metadata

Release files for langchain-uagents 0.1.1

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-uagents 0.1.1
File Size Uploaded Provenance Actions
langchain_uagents-0.1.1.tar.gz 10.9 kB Without provenance Details for langchain_uagents-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-uagents 0.1.1
File Interpreter ABI Platform Actions
langchain_uagents-0.1.1-py3-none-any.whl Python 3 none any Details for langchain_uagents-0.1.1-py3-none-any.whl

Total release size: 21.4 kB

Release files / langchain_uagents-0.1.1.tar.gz

Download URL langchain_uagents-0.1.1.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
702e9f562062af93d15643451644306bcf70cebfdf975f1885ee2072228511d0
BLAKE2b-256 checksum
How to use checksums
95f3f1f8a559d73afd86899ed79a8c50f1ffe604d4e38577f15e2e3b31cc054f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.2 Darwin/24.0.0

Release files / langchain_uagents-0.1.1-py3-none-any.whl

Download URL langchain_uagents-0.1.1-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
047477ea3a447bd76395515fd26aeba7b9fc67a7ae76be0b27e884a004f5b09d
BLAKE2b-256 checksum
How to use checksums
55da09c15ad8a5d89c49c3c68a619fae3861e40169465e764f965db67e72156d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.2 Darwin/24.0.0

Security for langchain-uagents 0.1.1

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

0.1.1 This release

2 release files No provenance

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