A collection of preset efficient prompts packaged into LLM agents.
Project description
LLM Task Agents
A collection of preset efficient prompts packaged into LLM agents. LLM Task Agents is a Python package for creating and managing different agents that can handle tasks like image and text classification, SQL query generation, and JSON structure management. The agents are built on top of large language models (LLMs) and are designed to be modular and easy to integrate.
Features
- Text Classification Agent: Classifies text into predefined categories using LLM-based prompts.
- SQL Agent: Runs SQL queries on databases and returns structured results.
- JSON Agent: Handles JSON validation and generation based on predefined schemas.
- Image Classification Agent: Classifies images into predefined categories using LLM models.
Installation
From PyPI
You can install the package via pip once it is uploaded to PyPI:
pip install llm-task-agents
Usage
Below are examples of how to use the different agents provided by the package.
Text Classification Agent
from llm_task_agents.agent_factory import AgentFactory
import os
from rich.console import Console
from rich.table import Table
from rich.syntax import Syntax
# Initialize the console
console = Console()
# Text classification agent
text = "Je vois la vie en rose."
labels = ["Positive", "Negative"]
agent = AgentFactory.get_agent(
agent_type="text",
llm_api_url=os.getenv("OLLAMA_API_BASE"),
model="llama3.2:3b"
)
# Run the agent to classify text
result = agent.run(text=text, labels=labels)
# Display results
console.print("TEXT CLASSIFICATION AGENT")
console.print(f"[bold]Text:[/bold]\n{text}")
console.print(f"[bold]Labels:[/bold]\n{labels}")
console.print(f"[bold]Result:[/bold]\n{result}")
SQL Agent
from llm_task_agents.agent_factory import AgentFactory
import os
from rich.console import Console
from rich.table import Table
from rich.syntax import Syntax
# Initialize the console
console = Console()
# SQL Agent
user_request = "Show total sales per month"
agent = AgentFactory.get_agent(
agent_type="sql",
llm_api_url=os.getenv("OLLAMA_API_BASE"),
model="llama3.2:3b",
database_driver="mysql",
database_username=os.getenv("MYSQL_USER", "root"),
database_password=os.getenv("MYSQL_PASSWORD", "password"),
database_host=os.getenv("MYSQL_HOST", "localhost"),
database_port="3306",
database_name="chinook",
# debug=True,
)
# Get the list of tables
tables = agent.list_tables()
# Generate the SQL query
sql_query = agent.run(
user_request=user_request,
tables=tables,
allowed_statements=["SELECT"]
)
# Function to display tables using rich Table
def display_tables(tables):
table = Table(title="Database Tables")
table.add_column("Table Name", justify="left", style="cyan", no_wrap=True)
for table_name in tables:
table.add_row(table_name)
console.print(table)
# Display results
console.print("SQL AGENT")
display_tables(tables)
console.print(f"[bold]User Request:[/bold] {user_request}")
if sql_query:
console.print("[bold green]Valid SQL Query:[/bold green]")
syntax = Syntax(sql_query, "sql", theme="monokai", line_numbers=True)
console.print(syntax)
else:
console.print("[red]Failed to generate a valid SQL query.[/red]")
JSON Agent
from llm_task_agents.agent_factory import AgentFactory
import os
from rich.console import Console
import json
# Initialize the console
console = Console()
# JSON Agent
task = "Create 3 detailed, realistic character personas for a fantasy adventure game."
structure = {
"personas": [
{
"first_name": "string",
"last_name": "string",
"age": "int",
"gender": "string",
"job": "string",
"description": "string"
}
]
}
agent = AgentFactory.get_agent(
agent_type="json",
llm_api_url=os.getenv("OLLAMA_API_BASE"),
model="llama3.2:3b"
)
# Run the agent to generate JSON
personas = agent.run(
task=task,
structure=structure
)
# Display results
console.print("JSON AGENT")
console.print(f"[bold]Task:[/bold]\n{task}")
console.print(f"[bold]Structure:[/bold]\n{json.dumps(structure, indent=4)}")
console.print(f"[bold]JSON:[/bold]\n{json.dumps(personas, indent=4)}")
Image Classification Agent
from llm_task_agents.agent_factory import AgentFactory
import os
from rich.console import Console
from PIL import Image
import requests
import tempfile
# Initialize the console
console = Console()
# Image classification agent
image_url = "https://image.pollinations.ai/prompt/Grape"
image = Image.open(requests.get(image_url, stream=True).raw)
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_file:
image.save(tmp_file.name)
image_path = tmp_file.name
labels = ["Apple", "Lemon", "Cherry", "Orange", "Banana", "Pineapple", "Melon", "Watermelon", "Peach", "Grape"]
agent = AgentFactory.get_agent(
agent_type="image",
llm_api_url=os.getenv("OLLAMA_API_BASE"),
model="minicpm-v:8b-2.6-fp16"
)
# Run the agent to classify image
label = agent.run(image_path=image_path, labels=labels)
# Display results
console.print("IMAGE CLASSIFICATION AGENT")
console.print(f"[bold]Image:[/bold]\n{image_path}")
console.print(f"[bold]Labels:[/bold]\n{labels}")
console.print(f"[bold]Result:[/bold]\n{label}")
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built using the
ollama
LLM API. - SQL query management with SQLAlchemy.
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
Hashes for llm_task_agents-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77f37ee3312ddb96a8bdbde5e63409c7200381e6a8a01e60e764118361e2c735 |
|
MD5 | e9e4ca4204b5e778ea348aef404cecb9 |
|
BLAKE2b-256 | f3a40b1d652a5d64fabe4d59a97ee766907c47c7bce4354c5c332fa0839c5b32 |