feat: Add Dify Adapter & Plugin Integration (Track A - 5 Points)#254
feat: Add Dify Adapter & Plugin Integration (Track A - 5 Points)#254david-mamani wants to merge 3 commits intoNevaMind-AI:mainfrom
Conversation
|
Hi @david-mamani, regarding the integration of MemU and Dify, could you create the tutorial? Either a text version or a YouTube video would be great. We hope more users will be able to integrate and successfully use MemU within Dify. Thank you! 🙏 |
OFC! |
|
Hi @Koimiao-zz, here is the tutorial you requested! You can follow these steps to test the integration using the files included in this PR. If you have any questions or run into any issues, please feel free to ask. How to Integrate & Use MemU in DifyThis guide will help you integrate MemU into Dify in under 5 minutes, giving your agents long-term memory capabilities. Part 1: Integration (The Setup)PrerequisitesBefore starting, ensure you have:
Step 1: Start MemUFirst, you need to run the MemU adapter server. If you followed the setup guide, run the following command in your terminal: # Make sure your OpenAI API key is set
export OPENAI_API_KEY=sk-...
uvicorn server:app --host 0.0.0.0 --port 8000Step 2: Public Access (For Dify Cloud)If you are using Dify Cloud, it cannot access ngrok http 8000Copy the HTTPS URL generated by ngrok (e.g., Step 3: Dify Setup
openapi: 3.1.0
info:
title: MemU Dify Integration
description: API Integration for MemU Memory System
version: 1.0.0
servers:
- url: http://localhost:8000 # <--- REPLACE THIS WITH YOUR NGROK URL
description: Local MemU Adapter Server
paths:
/add-memory:
post:
summary: Add to Memory
operationId: add_memory
description: Memorize content into MemU.
security:
- BearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
query:
type: string
description: The text content to memorize.
user_id:
type: string
description: Optional user identifier to scope the memory.
required:
- query
responses:
'200':
description: Memory added successfully
content:
application/json:
schema:
type: object
properties:
status:
type: string
message:
type: string
resource_id:
type: string
items_created:
type: integer
/search-memory:
post:
summary: Search Memory
operationId: search_memory
description: Retrieve relevant memories from MemU.
security:
- BearerAuth: []
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
query:
type: string
description: The search query.
user_id:
type: string
description: Optional user identifier to scope the search.
required:
- query
responses:
'200':
description: Retrieval results
content:
application/json:
schema:
type: object
properties:
result:
type: string
description: Formatted memory context.
metadata:
type: object
properties:
item_count:
type: integer
category_count:
type: integer
components:
securitySchemes:
BearerAuth:
type: http
scheme: bearer
Part 2: Usage (How to use it)Add to ChatflowNow that the tool is installed, you must add it to your Agent.
Example PromptsTo test if it works, type these prompts in the Dify chat preview: 1. Store Information (Triggers add_memory)
2. Recall Information (Triggers search_memory)
Part 3: Troubleshooting & Common ErrorsServer Won't Start (Module Not Found)Error: Solution:
Dify Connection ErrorError: Dify shows "Connection Refused", "Timeout", or "Network Error". Cause: You likely forgot to replace Solution:
|
|
Hi David, I tried the entire tests for this pr. It is suggested that in dify_adapter.py, it should add following code to run the server. Plz help to check # Standalone app for direct execution (e.g., uvicorn memu.integrations.dify_adapter:app)
def create_app():
"""Create a standalone FastAPI app with the Dify adapter router."""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from memu.app.service import MemoryService
memu_service: MemoryService | None = None
@asynccontextmanager
async def lifespan(fastapi_app: FastAPI): # noqa: ARG001
nonlocal memu_service
memu_service = MemoryService()
yield
def get_service_override() -> MemoryService:
if memu_service is None:
raise RuntimeError("MemU service not initialized")
return memu_service
fastapi_app = FastAPI(
title="MemU Dify Adapter",
description="API server for integrating MemU with Dify",
lifespan=lifespan,
)
fastapi_app.include_router(router)
fastapi_app.dependency_overrides[get_memu_service] = get_service_override
return fastapi_app
app = create_app()
|
c20ffdb to
613a11b
Compare
613a11b to
3ff41d2
Compare
|
@Koimiao-zz Thank you for the insightful review! I have implemented your suggestions to ensure the adapter runs standalone:
Verification:
|

2026 New Year Challenge Submission
Issue: #238 (Dify Adapter / Plugin for MemU)
Track: 5-Point Track (Agent Framework Plugins)
Description
This PR introduces a robust Dify Adapter for MemU, enabling seamless integration between Dify Agents and MemU's memory layer.
It implements a dedicated FastAPI Router and generates a fully compliant OpenAPI Specification that allows MemU to be imported into Dify as a "Custom Tool" in less than 30 seconds.
Key Features
dify-openapi-spec.yamlfor instant import in Dify UI.src/memu/integrations/dify_adapter.pyfor high-performance async handling.docs/integrations/dify_setup.md.Testing & Usability
tests/test_dify_adapter.pycovering success, error cases, and edge cases.make check(Ruff/Mypy) andmake test.uv syncuv run python -m memu.maindocs/integrations/dify-openapi-spec.yamlinto Dify as a Custom Tool.Checklist
resolves #238