Build a Modular Skill-Based Agent System for LLMs with Dynamic Tool Routing in Python

0


class CalculatorSkill(Skill):
def _define_metadata(self):
return SkillMetadata(
name=”calculator”,
description=”Evaluate mathematical expressions. Supports arithmetic, powers, and ”
“math functions: sqrt, abs, round, log, sin, cos, tan.”,
category=SkillCategory.REASONING,
tags=[“math”, “arithmetic”, “compute”],
output_type=”text”, cost_estimate=0.0,
)

def _define_schema(self):
return {“type”: “object”,
“properties”: {“expression”: {“type”: “string”,
“description”: “A Python math expression e.g. ‘2**10 + sqrt(144)'”}},
“required”: [“expression”]}

def execute(self, expression: str) -> str:
import math
safe = {“__builtins__”: {}, “sqrt”: math.sqrt, “abs”: abs, “round”: round,
“pow”: pow, “log”: math.log, “pi”: math.pi, “e”: math.e,
“sin”: math.sin, “cos”: math.cos, “tan”: math.tan}
try:
return f”Result: {eval(expression, safe)}”
except Exception as ex:
return f”Error: {ex}”

class TextSummarizerSkill(Skill):
def _define_metadata(self):
return SkillMetadata(
name=”text_summarizer”,
description=”Summarize text at three verbosity levels: brief (1-2 sentences), ”
“standard (1 paragraph), or detailed (structured bullets).”,
category=SkillCategory.GENERATION,
tags=[“summarize”, “nlp”, “text”, “writing”],
)

def _define_schema(self):
return {“type”: “object”,
“properties”: {
“text”: {“type”: “string”},
“mode”: {“type”: “string”, “enum”: [“brief”, “standard”, “detailed”],
“default”: “standard”}},
“required”: [“text”]}

def execute(self, text: str, mode: str = “standard”) -> str:
instructions = {“brief”: “in 1-2 sentences”, “standard”: “in one paragraph”,
“detailed”: “as structured bullet points covering main ideas, key details, and conclusions”}
r = client.chat.completions.create(
model=MODEL, max_tokens=300,
messages=[
{“role”: “system”, “content”: f”Summarize {instructions.get(mode, instructions[‘standard’])}. Be concise.”},
{“role”: “user”, “content”: text}])
return r.choices[0].message.content

class DataAnalystSkill(Skill):
def _define_metadata(self):
return SkillMetadata(
name=”data_analyst”,
description=”Analyse structured data (JSON or CSV) and extract statistical insights, ”
“trends, or answer specific questions.”,
category=SkillCategory.DATA,
tags=[“data”, “analysis”, “statistics”, “csv”, “json”],
)

def _define_schema(self):
return {“type”: “object”,
“properties”: {
“data”: {“type”: “string”, “description”: “Data as JSON array or CSV”},
“question”: {“type”: “string”, “description”: “Analytical question to answer”}},
“required”: [“data”, “question”]}

def execute(self, data: str, question: str) -> str:
r = client.chat.completions.create(
model=MODEL, max_tokens=400,
messages=[
{“role”: “user”, “content”: f”Data:\n{data}\n\nQuestion: {question}”}])
return r.choices[0].message.content

class CodeGeneratorSkill(Skill):
def _define_metadata(self):
return SkillMetadata(
name=”code_generator”,
description=”Generate clean, commented Python code for a given task with a brief explanation.”,
category=SkillCategory.GENERATION,
tags=[“code”, “python”, “programming”, “script”],
)

def _define_schema(self):
return {“type”: “object”,
“properties”: {
“task”: {“type”: “string”},
“language”: {“type”: “string”, “default”: “python”}},
“required”: [“task”]}

def execute(self, task: str, language: str = “python”) -> str:
r = client.chat.completions.create(
model=MODEL, max_tokens=500,
messages=[
{“role”: “system”, “content”: f”Expert {language} developer. Write clean, commented code with a one-line explanation.”},
{“role”: “user”, “content”: task}])
return r.choices[0].message.content



Source link

You might also like
Leave A Reply

Your email address will not be published.