Skip to content

Commit

Permalink
feat: tool run endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Dec 13, 2023
1 parent 91af4d6 commit 7b59011
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
38 changes: 35 additions & 3 deletions apps/server/controllers/tool.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import List
from fastapi import APIRouter

from typings.tool import ToolOutput
from tools.get_tools import get_all_tools
from fastapi import APIRouter, Depends
from fastapi_sqlalchemy import db

from tools.get_tools import get_all_tools, get_tool_by_slug
from typings.auth import UserAccount
from typings.tool import ToolOutput, ToolRunInput, ToolRunOutput
from utils.auth import authenticate_by_token_or_api_key

router = APIRouter()

Expand All @@ -23,3 +27,31 @@ def get_tools() -> List[ToolOutput]:

return get_all_tools()
# return convert_tools_to_tool_list(db_tools)


@router.post("/run", response_model=ToolRunOutput)
def run_tool(
input: ToolRunInput, auth: UserAccount = Depends(authenticate_by_token_or_api_key)
) -> ToolRunOutput:
"""
Run a tool.
Args:
auth (UserAccount): Authenticated user account.
tool_id (int): ID of the tool to run.
Returns:
ToolRunOutput: Tool output.
"""

agent_with_configs = None

tool = get_tool_by_slug(
input.toolkit_slug, input.tool_slug, db, auth.account, agent_with_configs
)

try:
response = tool._run(input.query)
return ToolRunOutput(response=response, is_success=True)
except Exception as e:
return ToolRunOutput(response=str(e), is_success=True)
12 changes: 12 additions & 0 deletions apps/server/typings/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ class ToolOutput(BaseModel):
slug: str
fields: List[ToolFieldOutput]
tools: List[SingleToolOutput]


class ToolRunInput(BaseModel):
query: str
toolkit_slug: str
tool_slug: str
# agent_id: str


class ToolRunOutput(BaseModel):
response: str
is_success: bool

0 comments on commit 7b59011

Please sign in to comment.