MirrorYuChen
MirrorYuChen
Published on 2025-04-17 / 16 Visits
0
0

dify学习笔记之基于fastapi添加自定义工具

dify学习笔记之基于fastapi添加自定义工具

1.部署本地服务

  • (1) fastapi安装
>> pip install fastapi uvicorn
  • (2) 简单服务部署
'''
Author: chenjingyu
Date: 2025-04-10 15:11:21
Contact: 2458006466@qq.com
Description: main.py
'''
from fastapi import FastAPI
import uvicorn

# 注意,这里servers要填,后面关联dify有用
app = FastAPI(
    title="My Personal Tool",
    description="This is My Personal Tool API",
    version="1.0.0",
    servers=[
        {"url": "http://localhost:8888", "description": "My Personal Tool API"},
    ]
)

@app.get("/hello")
async def hello(person: str):
    return f"Hello, {person}"


if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8888)
  
  • (3) 启动服务
>> python main.py
INFO:     Started server process [68388]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8888 (Press CTRL+C to quit)
INFO:     127.0.0.1:34524 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:34524 - "GET /openapi.json HTTP/1.1" 200 OK
  • (4) 测试服务

​ 文档地址:http://localhost:8888/docs

fastapi_docs.png

fastapi_test.png

  • (5) 获取openapi对应json

fastapi_openapi.png

​ 把这个json文件拷贝下来,后面要用

2.dify添加自定义工具

  • (1) “工具” -> “自定义” -> “创建自定义工具”
    dify_tool.png

  • (2) 补充工具名称和 schema内容(前面获取到的openapi对应json),并测试

    dify_tool_config.png

    dify_tool_test.png

  • (3) 最后“保存”即可。

3.参考资料


Comment