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
@tooldecorators. - 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.invoke("What's the weather like in Tokyo?")
print(result.text)
# 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.invoke("What is 123 * 456?")
print(result.text)
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
Single-Shot vs Auto-Execution
invoke() behaves differently depending on whether tools are configured:
- With Tools: It automatically runs the agent loop, executing tools until a final answer is reached. Returns an
AgentResponsecontaining the final text. - Without Tools: It performs a single-shot completion. Returns an
AgentResponsecontaining the 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 totrue/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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rusted_chain-1.1.1.tar.gz.
File metadata
- Download URL: rusted_chain-1.1.1.tar.gz
- Upload date:
- Size: 34.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf6aad6403aa4edb84839f0bde8bd54db21484f23fed7679aa44ded20ce6053a
|
|
| MD5 |
400e282f118bb0f0709925570981db29
|
|
| BLAKE2b-256 |
586fc278b8c581552fbc3fddbb45178adcf38bd25df731279e319b05c265f31e
|
File details
Details for the file rusted_chain-1.1.1-cp311-cp311-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: rusted_chain-1.1.1-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 5.1 MB
- Tags: CPython 3.11, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
819f8de5bb00c83d028ec36b6448c1da025924a8bba1b50e8b83f54971da8b3d
|
|
| MD5 |
3dfbd1d7cccc01940e867198aa2110ee
|
|
| BLAKE2b-256 |
ddf498d6c64e62fddbc1234aa90fd0c8c375122b077b7c7934bf1180d3d8c9c9
|