Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/fenn/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fenn.agents import Flow
from fenn.agents.node import ThinkNode, ActNode, ObserveNode
from fenn.agents.tools import get_tool_schema
import yaml

class Agent:
Expand Down Expand Up @@ -27,6 +28,7 @@ def run(self, user_input):
{"role": "system", "content": self.config["agent"]["system_prompt"]},
{"role": "user", "content": user_input}
],
"tools": get_tool_schema(),
"iterations": 0,
"max_iterations": self.config["agent"]["max_iterations"],
"last_thought": None,
Expand Down
17 changes: 12 additions & 5 deletions src/fenn/agents/node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fenn.agents import Node
from fenn.agents.tools import TOOLS
from fenn.agents.tools import execute_tool

class ThinkNode(Node):
def prep(self, shared):
Expand All @@ -22,13 +22,20 @@ def prep(self, shared):
return shared["last_thought"]

def exec(self, thought):
line = [l for l in thought.split("\n") if l.startswith("Action:")][0]
tool_call = line.replace("Action:", "").strip()
if "Action:" in thought:
line = [l for l in thought.split("\n") if l.startswith("Action:")][0]
tool_call = line.replace("Action:", "").strip()
else:
tool_call = thought.strip()

tool_name = tool_call.split("(")[0]
tool_arg = tool_call.split("(")[1].rstrip(")")
args_str = tool_call.split("(")[1].rstrip(")")
tool_args = [arg.strip() for arg in args_str.split(",")]

result = TOOLS[tool_name](tool_arg)
try:
result = execute_tool(tool_name, *tool_args)
except Exception as e:
result = f"Error: {e}"
return result

def post(self, shared, prep_res, exec_res):
Expand Down
42 changes: 32 additions & 10 deletions src/fenn/agents/tools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
TOOLS = {
"search": lambda query: f"Search result for: {query}",
"calculator": lambda expr: str(eval(expr))
}

# This is what you put in the LLM prompt so it knows what tools exist
TOOL_DESCRIPTIONS = """
- search(query): Search the web
- calculator(expr): Evaluate a math expression
"""
import inspect
from functools import wraps
from typing import Dict, Any

TOOLS_REGISTRY: Dict[str, Dict[str, Any]] = {}

def tool(func):
"""
A decorator that registers an executable function as a tool
"""
@wraps(func)
def decorator(*args, **kwargs):
return func(*args, **kwargs)
tool_name = func.__name__
tools_description = inspect.getdoc(func) or "No description provided"

TOOLS_REGISTRY[tool_name] = {
"schema": {
"name": tool_name,
"description": tools_description
},
"execute": func
}
return decorator

def get_tool_schema():
return [info["schema"] for info in TOOLS_REGISTRY.values()]

def execute_tool(name: str, *args, **kwargs):
if name not in TOOLS_REGISTRY:
raise ValueError(f"Tool '{name}' is not registered.")
return TOOLS_REGISTRY[name]["execute"](*args, **kwargs)
Loading