Skip to main content

LangChain like python library written in rust

Project description

Rusted Chain

Rusted Chain is a Rust-powered LLM agent framework for Python. It combines the performance of Rust with the usability of Python, providing a LangChain-compatible API for building AI agents.

Features

  • Rust Core: Backend implemented in Rust using pyo3.
  • Multi-Model Support: Supports Gemini, OpenAI, and Claude.
  • LangChain Compatible: Accepts standard LangChain tools and @tool decorators.
  • Auto-Execution Loop: Handles tool calling and execution automatically.
  • Simple API: Straightforward interface for creating agents and tools.

Installation

pip install rusted-chain

Quick Start

Basic Usage

from rusted_chain import GeminiModel, OpenAIModel, ClaudeModel

# Initialize an agent
agent = GeminiModel(api_key="your-api-key")

# Simple text generation
response = agent.invoke("Tell me a joke about Rust.")
print(response.text)

Using Tools

You can pass Python functions directly. rusted-chain handles schema generation and execution.

from rusted_chain import GeminiModel

def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"The weather in {city} is sunny and 25°C."

# Create agent with tools
agent = GeminiModel(tools=[get_weather])

# Run the agent
result = agent.run("What's the weather like in Tokyo?")
print(result)
# Output: "The weather in Tokyo is sunny and 25°C."

Using LangChain Tools

You can also use existing LangChain tools or the @tool decorator.

from langchain_core.tools import tool
from rusted_chain import OpenAIModel

@tool
def multiply(a: int, b: int) -> int:
    """Multiplies two numbers."""
    return a * b

agent = OpenAIModel(tools=[multiply])

result = agent.run("What is 123 * 456?")
print(result)

Supported Models

Class Provider Env Variable
GeminiModel Google Gemini GOOGLE_API_KEY
OpenAIModel OpenAI (GPT-4, etc.) OPENAI_API_KEY
ClaudeModel Anthropic Claude ANTHROPIC_API_KEY

Advanced Usage

Manual Tool Control

Use invoke() to get the raw response (text or tool call) without auto-execution.

response = agent.invoke("Call my_tool")

if response.is_tool_call:
    print(f"Tool: {response.tool_call.name}")
    print(f"Args: {response.tool_call.args}")
else:
    print(response.text)

Performance benchmark (test_perf.py)

A small benchmarking script is included at test_perf.py to compare the request/response latency of rusted_chain vs a LangChain-based client when calling the Google Gemini model (the repository author used gemini-2.5-flash for tests).

pip install langchain-google-genai python-dotenv

How to run

  • Optional environment variables used by the script:
    • RC_BENCH_WARMUP — set to true/false (default: true) to perform warm-up calls before measurements.
    • RC_BENCH_REPEATS — number of timed repetitions (default: 4).

Example usage (from repo root)

# set API key and run the benchmark
export GOOGLE_API_KEY="your-google-api-key"
python test_perf.py

License

MIT

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

rusted_chain-1.1.0.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

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

rusted_chain-1.1.0-cp311-cp311-manylinux_2_34_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

File details

Details for the file rusted_chain-1.1.0.tar.gz.

File metadata

  • Download URL: rusted_chain-1.1.0.tar.gz
  • Upload date:
  • Size: 34.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for rusted_chain-1.1.0.tar.gz
Algorithm Hash digest
SHA256 891c5026b1beca41ff322b804577a7fc3566cad56e839687e673c13e1213720b
MD5 c2c728550c573ea790d03b51f316079d
BLAKE2b-256 23a3e44a0138734432b12b6678283c80ed11012ec283b9e8c78628c106356648

See more details on using hashes here.

File details

Details for the file rusted_chain-1.1.0-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rusted_chain-1.1.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 8cdd65161d8c75abec5b7a68bcb6829042f5fe433e526c3922da99b95900bd78
MD5 2750b01c66f7fb04a6d56dc490941f20
BLAKE2b-256 e02c0dbfc219251ea782a3616322eef65b1d79acc5e3d6ace2e33a6b66a643c6

See more details on using hashes here.

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