api.py 914 B

12345678910111213141516171819202122232425262728293031
  1. from fastapi import FastAPI, HTTPException
  2. from pydantic import BaseModel
  3. from agents.clean_agent.agent import execute_agent_with_api
  4. import uvicorn
  5. app = FastAPI(title="Knowledge Agent API", description="API for executing knowledge agent")
  6. @app.post("/execute")
  7. async def execute_agent(input: str):
  8. """
  9. 执行Agent处理用户指令
  10. Args:
  11. user_input: 包含用户指令的对象
  12. Returns:
  13. dict: 包含执行结果的字典
  14. """
  15. try:
  16. result = execute_agent_with_api(input)
  17. return {"status": "success", "result": result}
  18. except Exception as e:
  19. raise HTTPException(status_code=500, detail=f"执行Agent时出错: {str(e)}")
  20. @app.get("/")
  21. async def root():
  22. return {"message": "Knowledge Agent API 服务已启动,请使用 /execute 端点执行Agent"}
  23. if __name__ == "__main__":
  24. uvicorn.run(app, host="0.0.0.0", port=8000)