LMSystems SDK
Getting Started
The LMSystems SDK provides two main ways to work with graphs from the LMSystems marketplace:
- PurchasedGraph: Use this when you want to integrate a purchased graph as a subgraph within a new Langgraph application. This is ideal for building larger, composite applications that incorporate marketplace graphs as components.
- LmsystemsClient: Use this for direct, standalone usage of purchased graphs in your applications (e.g., chat applications). This provides a simpler interface when you just want to use the graph as-is.
This documentation will guide you through the process of installing the SDK and getting started with both approaches.
Installation
pip install lmsystems
Authentication
To use the SDK, you'll need an LMSystems API key. Get your API key by:
- Creating an account at LMSystems
- Navigating to your account settings
- Generating an API key
Store your API key securely using environment variables:
export LMSYSTEMS_API_KEY="your-api-key"
PurchasedGraph Class
The PurchasedGraph
class allows for you to use your Purchased Graph from our marketplace as a subgraph in a seperate Langgraph app (similar to Langgraph's RemoteGraph Class). Configure your graph's settings (like API keys and model preferences) through your account's purchased graphs page.
Basic Usage
from lmsystems.purchased_graph import PurchasedGraph
from langgraph.graph import StateGraph, START, MessagesState
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set required state values for your graph
# Check the 'Inputs' tab on your graph's page for required inputs
state_values = {
"input1": "value1",
"input2": "value2"
}
# Initialize the purchased graph
purchased_graph = PurchasedGraph(
graph_name="your-graph-name",
api_key=os.environ.get("LMSYSTEMS_API_KEY"),
default_state_values=state_values
)
# Create a parent graph with MessagesState schema
builder = StateGraph(MessagesState)
builder.add_node("purchased_node", purchased_graph)
builder.add_edge(START, "purchased_node")
graph = builder.compile()
# Invoke the graph
result = graph.invoke({
"messages": [{"role": "user", "content": "Your prompt here"}]
})
LmsystemsClient
For direct, low-level interaction with LMSystems graphs, the LmsystemsClient
offers more flexibility and control.
Basic Usage
from lmsystems.client import LmsystemsClient
import os
from dotenv import load_dotenv
import asyncio
# Load environment variables
load_dotenv()
async def main():
# Initialize the client
client = await LmsystemsClient.create(
graph_name="your-graph-name",
api_key=os.environ["LMSYSTEMS_API_KEY"]
)
# Create a new thread and run
try:
thread = await client.create_thread()
# Check the 'Inputs' tab on your graph's page for required inputs
run = await client.create_run(
thread,
input={
"messages": [{"role": "user", "content": "Your prompt here"}],
"input1": "value1",
"input2": "value2"
}
)
# Stream response
async for chunk in client.stream_run(thread, run):
print(chunk)
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
asyncio.run(main())
Example Usage: GitHub Agent
Here's a practical example using our "github-agent-6" graph, which analyzes GitHub repositories. Remember to configure your graph's settings in your account settings before using.
Using PurchasedGraph
from lmsystems.purchased_graph import PurchasedGraph
from langgraph.graph import StateGraph, START, MessagesState
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Set required state values for github-agent-6
state_values = {
"repo_url": "https://github.com/yourusername/yourrepo",
"github_token": "your-github-token",
"repo_path": "/path/to/repo"
}
purchased_graph = PurchasedGraph(
graph_name="github-agent-6",
api_key=os.environ.get("LMSYSTEMS_API_KEY"),
default_state_values=state_values
)
# Define your parent graph with MessagesState schema
builder = StateGraph(MessagesState)
builder.add_node("purchased_node", purchased_graph)
builder.add_edge(START, "purchased_node")
graph = builder.compile()
# Now you can invoke with just the messages, other state values are automatically included
result = graph.invoke({
"messages": [{"role": "user", "content": "what's this repo about?"}]
})
print(result)
# Optional: Stream outputs
for chunk in graph.stream({
"messages": [{"role": "user", "content": "what's this repo about?"}]
}, subgraphs=True):
print(chunk)
Using LmsystemsClient
from lmsystems.client import LmsystemsClient
from dotenv import load_dotenv
import os
import asyncio
# Load environment variables
load_dotenv()
# Async usage
async def main():
# Simple initialization with just graph name and API key
client = await LmsystemsClient.create(
graph_name="graph-name-id",
api_key=os.environ["LMSYSTEMS_API_KEY"]
)
# Create thread and run with error handling
try:
thread = await client.create_thread()
run = await client.create_run(
thread,
input={"messages": [{"role": "user", "content": "What's this repo about?"}],
"repo_url": "",
"repo_path": "",
"github_token": ""}
)
# Stream response
async for chunk in client.stream_run(thread, run):
print(chunk)
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
asyncio.run(main())
Error Handling
The SDK provides specific exceptions for different error cases:
AuthenticationError
: API key or authentication issuesGraphError
: Graph execution or configuration issuesInputError
: Invalid input parametersAPIError
: Backend communication issues
Example
from lmsystems.exceptions import LmsystemsError
try:
result = graph.invoke(input_data)
except AuthenticationError as e:
print(f"Authentication failed: {e}")
except GraphError as e:
print(f"Graph execution failed: {e}")
except InputError as e:
print(f"Invalid input: {e}")
except APIError as e:
print(f"API communication error: {e}")
except LmsystemsError as e:
print(f"General error: {e}")