2024-10-02 20:11:28 +08:00
|
|
|
|
import os
|
|
|
|
|
|
from fastapi import APIRouter, Body
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
from typing import List, Dict, Any, Optional
|
|
|
|
|
|
|
2025-03-29 19:13:56 +08:00
|
|
|
|
from src.agents import agent_manager
|
2024-10-02 20:11:28 +08:00
|
|
|
|
|
|
|
|
|
|
tool = APIRouter(prefix="/tool")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Tool(BaseModel):
|
|
|
|
|
|
name: str
|
|
|
|
|
|
title: str
|
|
|
|
|
|
description: str
|
|
|
|
|
|
url: str
|
2025-03-28 00:48:33 +08:00
|
|
|
|
method: Optional[str] = "POST"
|
2025-03-03 19:46:36 +08:00
|
|
|
|
params: Optional[Dict[str, Any]] = None
|
2025-03-29 19:13:56 +08:00
|
|
|
|
metadata: Optional[Dict[str, Any]] = None
|
2024-10-02 20:11:28 +08:00
|
|
|
|
|
|
|
|
|
|
@tool.get("/", response_model=List[Tool])
|
|
|
|
|
|
async def route_index():
|
|
|
|
|
|
tools = [
|
|
|
|
|
|
Tool(
|
|
|
|
|
|
name="text-chunking",
|
|
|
|
|
|
title="文本分块",
|
|
|
|
|
|
description="将文本分块以更好地理解。可以输入文本或者上传文件。",
|
|
|
|
|
|
url="/tools/text-chunking",
|
|
|
|
|
|
method="POST",
|
|
|
|
|
|
),
|
|
|
|
|
|
Tool(
|
|
|
|
|
|
name="pdf2txt",
|
|
|
|
|
|
title="PDF转文本",
|
|
|
|
|
|
description="将PDF文件转换为文本文件。",
|
|
|
|
|
|
url="/tools/pdf2txt",
|
|
|
|
|
|
method="POST",
|
2025-03-28 00:48:33 +08:00
|
|
|
|
),
|
|
|
|
|
|
Tool(
|
|
|
|
|
|
name="agent",
|
|
|
|
|
|
title="智能体(Dev)",
|
|
|
|
|
|
description="智能体演练平台",
|
|
|
|
|
|
url="/tools/agent",
|
2024-10-02 20:11:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2025-03-29 19:13:56 +08:00
|
|
|
|
for agent in agent_manager.agents.values():
|
|
|
|
|
|
tools.append(
|
|
|
|
|
|
Tool(
|
|
|
|
|
|
name=agent.name,
|
|
|
|
|
|
title=agent.name,
|
|
|
|
|
|
description=agent.description,
|
|
|
|
|
|
url=f"/agent/{agent.name}",
|
|
|
|
|
|
method="POST",
|
2025-03-31 22:20:29 +08:00
|
|
|
|
metadata=agent.config_schema.to_dict(),
|
2025-03-29 19:13:56 +08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-10-02 20:11:28 +08:00
|
|
|
|
return tools
|
|
|
|
|
|
|
|
|
|
|
|
@tool.post("/text-chunking")
|
|
|
|
|
|
async def text_chunking(text: str = Body(...), params: Dict[str, Any] = Body(...)):
|
|
|
|
|
|
from src.core.indexing import chunk
|
|
|
|
|
|
nodes = chunk(text, params=params)
|
|
|
|
|
|
return {"nodes": [node.to_dict() for node in nodes]}
|
|
|
|
|
|
|
|
|
|
|
|
@tool.post("/pdf2txt")
|
|
|
|
|
|
async def handle_pdf2txt(file: str = Body(...)):
|
2025-03-08 20:26:00 +08:00
|
|
|
|
from src.plugins import ocr
|
|
|
|
|
|
text = ocr.process_pdf(file)
|
2024-10-02 20:11:28 +08:00
|
|
|
|
return {"text": text}
|
2025-03-28 00:48:33 +08:00
|
|
|
|
|