diff --git a/README.md b/README.md index ca2047e..0ff47f6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # faster_llm -`faster_llm` is a Python library that speeds up common data preprocessing and analysis tasks. It builds on familiar tools like NumPy, Pandas and scikit-learn while adding connectors for Large Language Model (LLM) frameworks such as LangChain and LlamaIndex. +`faster_llm` is a Python library that speeds up common data preprocessing and analysis tasks. It builds on familiar tools like NumPy, Pandas and scikit-learn while adding connectors for Large Language Model (LLM) frameworks such as LangChain and LlamaIndex. Recent updates introduce lightweight helpers for other agent libraries including AutoGen and CrewAI. ## Overview @@ -16,6 +16,7 @@ - Lightweight wrappers for Keras, PyTorch and PyTorch Lightning - Simple generator for producing fake data for experiments - MCP client for forwarding messages to LLM services and AI agents +- Wrappers for LangChain, LlamaIndex, AutoGen and CrewAI agents ## Quick Demo @@ -79,6 +80,9 @@ python examples/classification_example.py The same helper can forward metrics from Keras, PyTorch or PyTorch Lightning models to AI agents by using the library's MCP integration. +The new ``agents`` module also exposes utility functions to create LangChain +``AIMessage`` objects, LlamaIndex ``Document`` instances, run simple CrewAI +crews or spin up AutoGen assistants directly from your code. ## Bug Reports & Feature Requests diff --git a/faster_llm/__init__.py b/faster_llm/__init__.py index 4deee41..76805f2 100644 --- a/faster_llm/__init__.py +++ b/faster_llm/__init__.py @@ -9,4 +9,18 @@ __author_email__ = "poczta130@gmail.com" __maintainer__ = "unknown maintainer" __maintainer_email__ = "maintainer@example.com" -__github_username__ = "fuwiak" \ No newline at end of file +__github_username__ = "fuwiak" + +from .agents import ( + send_to_crewai, + send_with_autogen, + to_langchain_message, + to_llamaindex_document, +) + +__all__ = [ + "send_to_crewai", + "send_with_autogen", + "to_langchain_message", + "to_llamaindex_document", +] diff --git a/faster_llm/agents.py b/faster_llm/agents.py new file mode 100644 index 0000000..94bf4fb --- /dev/null +++ b/faster_llm/agents.py @@ -0,0 +1,83 @@ +"""Wrappers for popular AI agent frameworks.""" + +from __future__ import annotations + +from typing import Any + +try: # optional LangChain import + from langchain.schema import AIMessage +except Exception: # pragma: no cover - optional + class AIMessage: + """Fallback AIMessage when LangChain isn't installed.""" + + def __init__(self, content: str) -> None: + self.content = content + + +def to_langchain_message(message: str) -> AIMessage: + """Wrap ``message`` in a LangChain ``AIMessage`` object.""" + return AIMessage(message) + + +try: # optional LlamaIndex import + from llama_index.core import Document +except Exception: # pragma: no cover - optional + class Document: # type: ignore + """Simplified stand-in for LlamaIndex ``Document``.""" + + def __init__(self, text: str) -> None: + self.text = text + + +def to_llamaindex_document(text: str) -> Document: + """Create a LlamaIndex ``Document`` (or placeholder) from ``text``.""" + return Document(text) + + +try: # optional CrewAI import + from crewai import Crew +except Exception: # pragma: no cover - optional + class Crew: # type: ignore + """Basic replacement for a CrewAI ``Crew`` when the dependency is missing.""" + + def __init__(self, *_, **__) -> None: + self.messages: list[str] = [] + + def run(self, message: str) -> None: + self.messages.append(message) + + +def send_to_crewai(message: str) -> Crew: + """Send a message to a CrewAI ``Crew`` instance.""" + crew = Crew() + crew.run(message) + return crew + + +try: # optional AutoGen import + from autogen import AssistantAgent +except Exception: # pragma: no cover - optional + class AssistantAgent: # type: ignore + """Minimal stub of AutoGen's ``AssistantAgent``.""" + + def __init__(self, name: str, llm_config: dict | None = None) -> None: + self.name = name + self.messages: list[str] = [] + + def send(self, message: str) -> None: + self.messages.append(message) + + +def send_with_autogen(message: str, *, llm_config: dict | None = None) -> AssistantAgent: + """Create an AutoGen assistant and send it ``message``.""" + agent = AssistantAgent("assistant", llm_config=llm_config or {}) + agent.send(message) + return agent + + +__all__ = [ + "to_langchain_message", + "to_llamaindex_document", + "send_to_crewai", + "send_with_autogen", +] diff --git a/pyproject.toml b/pyproject.toml index 88f8519..c4e2470 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,10 @@ scikit-learn = ">=1.2.0" matplotlib = ">=3.6.2" mlflow = ">=2.0.0" langsmith = ">=0.1.0" +autogen = ">=0.2.0" +crewai = ">=0.1.0" +langchain = ">=0.0.350" +llama-index = ">=0.9.0" [build-system] requires = ["poetry-core"] diff --git a/requirements.txt b/requirements.txt index 2ce1e7d..eb6f860 100644 --- a/requirements.txt +++ b/requirements.txt @@ -102,3 +102,7 @@ widgetsnbextension>=4.0.5 mlflow>=2.0.0 langsmith>=0.1.0 +autogen>=0.2.0 +crewai>=0.1.0 +langchain>=0.0.350 +llama-index>=0.9.0 diff --git a/tests/test_agents.py b/tests/test_agents.py new file mode 100644 index 0000000..61d2234 --- /dev/null +++ b/tests/test_agents.py @@ -0,0 +1,35 @@ +import os +import sys + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) + +from faster_llm import ( + send_to_crewai, + send_with_autogen, + to_langchain_message, + to_llamaindex_document, +) + + +def test_to_langchain_message(): + msg = to_langchain_message("hi") + assert hasattr(msg, "content") + assert msg.content == "hi" + + +def test_to_llamaindex_document(): + doc = to_llamaindex_document("hello") + assert hasattr(doc, "text") + assert doc.text == "hello" + + +def test_send_to_crewai(): + crew = send_to_crewai("ping") + assert hasattr(crew, "messages") + assert "ping" in crew.messages + + +def test_send_with_autogen(): + agent = send_with_autogen("pong") + assert hasattr(agent, "messages") + assert "pong" in agent.messages