Skip to main content

SmartGraph

SmartGraph is a powerful Python library for building stateful, multi-component applications with Large Language Models (LLMs). Built on top of the ReactiveX for Python (reactivex) framework, SmartGraph provides a reactive, flexible, and maintainable system for creating complex data processing pipelines.

Think of SmartGraph as "Svelte for backend LLM components" - it offers a simple, reactive approach to building complex LLM-powered applications, similar to how Svelte simplifies frontend development.

Features

  • Declarative and reactive framework for defining workflows
  • Support for both simple linear and complex branching workflows
  • Powerful state management capabilities
  • Multi-component support with easy integration of LLMs and pre-built toolkits
  • Compilation step for graph validation and runtime configuration
  • Comprehensive error handling and logging

Installation

Install SmartGraph using pip:

pip install smartgraph

Quick Start

Here's an example of how to use SmartGraph to create a search assistant that can answer questions using web search results:

import asyncio
import os
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from smartgraph import ReactiveSmartGraph
from smartgraph.components import CompletionComponent
from smartgraph.tools.duckduckgo_toolkit import DuckDuckGoToolkit
from smartgraph.logging import SmartGraphLogger

# Load environment variables
load_dotenv()

# Set up logging
logger = SmartGraphLogger.get_logger()
logger.set_level("INFO")

# Create a graph and pipeline
graph = ReactiveSmartGraph()
pipeline = graph.create_pipeline("SearchAssistant")

# Initialize DuckDuckGoToolkit
ddg_toolkit = DuckDuckGoToolkit()

# Add CompletionComponent with DuckDuckGoToolkit
completion = CompletionComponent(
    name="SearchAssistant",
    model="gpt-4o-mini",
    api_key=os.getenv("OPENAI_API_KEY"),
    toolkits=[ddg_toolkit],
    system_context="You are a helpful assistant that can search the internet for information.",
)

pipeline.add_component(completion)

# Compile the graph
graph.compile()

# Create FastAPI app
app = FastAPI()

class SearchQuery(BaseModel):
    query: str

@app.post("/search")
async def search(search_query: SearchQuery):
    try:
        result = await graph.execute("SearchAssistant", {"message": search_query.query})
        return {"response": result.get("ai_response", "No response generated.")}
    except Exception as e:
        logger.error(f"An error occurred: {str(e)}")
        raise HTTPException(status_code=500, detail="An error occurred during processing")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

This example sets up a FastAPI application with a /search endpoint. When a POST request is sent to this endpoint with a search query, SmartGraph processes the query using the DuckDuckGoToolkit to search the web and the CompletionComponent to generate a response based on the search results.

To run this example:

  1. Save the code in a file (e.g., main.py).
  2. Set up your environment variables (OPENAI_API_KEY) in a .env file.
  3. Install the required dependencies (smartgraph, fastapi, uvicorn, python-dotenv).
  4. Run the script: python main.py
  5. Send a POST request to http://localhost:8000/search with a JSON body like {"query": "What is the capital of France?"}.

This example demonstrates how SmartGraph can be used to create a simple API for a search assistant, showcasing its ability to integrate different components and toolkits in a reactive pipeline.

Documentation

For more detailed information on how to use SmartGraph, please refer to our documentation.

Core Concepts

  • ReactiveSmartGraph: The main class representing the entire graph structure.
  • Pipeline: A sequence of connected components that process data.
  • ReactiveComponent: The base class for all components in SmartGraph.
  • CompletionComponent: A component for integrating Large Language Models.
  • Toolkits: Pre-built components for common tasks like web searches and memory management.

Advanced Features

  • Custom component creation
  • Complex branching workflows
  • Asynchronous API integration
  • Caching and retry mechanisms
  • Input validation
  • Graph visualization

Contributing

We welcome contributions! Please see our contributing guide for details on how to get started.

License

SmartGraph is released under the MIT License. See the LICENSE file for more details.

Support

If you encounter any issues or have questions, please file an issue on the GitHub issue tracker.

Acknowledgements

SmartGraph is built on top of the excellent ReactiveX for Python library. We're grateful to the ReactiveX community for their work.

Release files for smartgraph 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smartgraph 0.2.0
File Size Uploaded
smartgraph-0.2.0.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smartgraph 0.2.0
File Interpreter ABI Platform
smartgraph-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.7 kB

Release files / smartgraph-0.2.0.tar.gz

Download URL smartgraph-0.2.0.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c8c85f6eeff9db423f2f98a57a4c9ebcf2d3327c8e5d77da08843a7d2731ba1d
BLAKE2b-256 checksum
How to use checksums
fbb10bda93e911e29d5361a67971de2e5c7a1f1e12ba07e478d98da283c011c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.9 Linux/6.5.0-1025-azure

Release files / smartgraph-0.2.0-py3-none-any.whl

Download URL smartgraph-0.2.0-py3-none-any.whl
Size 26.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78979c35c1f096a5c229b8b3da4adf57384dde10553f64d8af9093644f3f68ff
BLAKE2b-256 checksum
How to use checksums
56c1ce64901cf771bed5975a50f14469b2e92860c80bb40a4f6c73713b666858
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.9 Linux/6.5.0-1025-azure

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

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