LiveChain
LiveChain is a Python framework for building real-time applications with AI agents. It adapts an event-driven approach to building reactive agentic workflows. The framework is also designed for building ambient agents that can operate in the background and react to events in the environment, enabling proactive behavior.
It integrates LiveKit for real-time communication (soon), LangGraph for workflow management, and LangChain for building AI agents.
Installation
pip install livechain
Features
- Fully compatible with LangGraph-based workflows
- LangChain-based AI agents
- Ambient agents that can operate in the background and react to events in the environment
- Real-time communication with LiveKit (example here)
Usage
Here's a simple example of creating a basic agent:
import asyncio
from typing import Annotated, List
from dotenv import load_dotenv
from langchain_core.messages import AIMessage, AnyMessage, HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import add_messages
from pydantic import BaseModel, Field
from livechain.graph.executor import Workflow
from livechain.graph.func import reactive, root, step, subscribe, cron
from livechain.graph.ops import channel_send, get_state, mutate_state
from livechain.graph.types import EventSignal
load_dotenv()
# Define the agent state
class AgentState(BaseModel):
messages: Annotated[List[AnyMessage], add_messages] = Field(default_factory=list)
has_started: bool = Field(default=False)
# Define event signals
class UserChatEvent(EventSignal):
message: HumanMessage
# Create steps for the workflow
@step()
async def init_system_prompt(state: AgentState):
message = SystemMessage(content="You are a helpful assistant.")
return {"messages": [message]}
@step()
async def chat_with_user():
messages = await get_state(AgentState)
llm = ChatOpenAI()
response = await llm.ainvoke(messages)
return {"messages": [response]}
# Subscribe to a event signal
@subscribe(UserChatEvent)
async def handle_user_chat(event: UserChatEvent):
await mutate_state({"messages": [event.message]})
await channel_send("user_message", event.message.content)
@reactive(AgentState, lambda state: state.has_started)
def on_start(old_state: AgentState, new_state: AgentState):
# reactive node that will be called whenever state.has_started has changed
print("Agent has started")
@cron(expr=interval(5))
def run_every_5_seconds():
# cron node that will be called every 5 seconds
print("Running every 5 seconds")
await channel_send("last_message", get_state(AgentState).messages[-1].content)
# Define the entry point
@root()
async def entrypoint():
# Initialize the agent
await init_system_prompt()
# Main loop
while True:
await chat_with_user()
await asyncio.sleep(1)
# Create and run the workflow
workflow = Workflow.from_routines(
root=entrypoint,
routines=[
handle_user_chat,
on_start,
run_every_5_seconds,
],
)
# Compile the workflow
executor = workflow.compile(AgentState)
# Subscribe to a channel
@executor.recv("user_message")
async def handle_user_message(message: str):
...
executor.start()
# trigger the workflow
executor.trigger_workflow()
# send events to the workflow
executor.publish_event(UserChatEvent(message=HumanMessage(content="Hello, how are you?")))
For more advanced examples, check the examples/ directory in the source code.
Requirements
- Python 3.12+
- Dependencies:
- livekit-agents
- langgraph
- langchain-core
- langchain-openai
- And others as listed in pyproject.toml
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for livechain 0.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| livechain-0.0.12.tar.gz | 138.5 kB | Without provenance | Details for livechain-0.0.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| livechain-0.0.12-py3-none-any.whl | Python 3 | none | any | Details for livechain-0.0.12-py3-none-any.whl |
Total release size: 160.5 kB
Release files / livechain-0.0.12.tar.gz
| Download URL | livechain-0.0.12.tar.gz |
|---|---|
| Size | 138.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d68753b22613aba473e8071973c70a9091edff30d7d550d08f2b270d044e39a3
|
|
BLAKE2b-256 checksum How to use checksums |
0ac45826e978407a8e2759dd769f8e0680956c88e9d3459a5c0e4a99a50d2b7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.4
|
Release files / livechain-0.0.12-py3-none-any.whl
| Download URL | livechain-0.0.12-py3-none-any.whl |
|---|---|
| Size | 22.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee676c8a7dfacba883f5287e9bf0e34ed8982b1af3944940bc0bf5b2a7dc74d7
|
|
BLAKE2b-256 checksum How to use checksums |
d577db68683505f22aa980f9bc3b52b4c0e115cf92c2094f616bd82269c87e70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.7.4
|
Security for livechain 0.0.12
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.