Loading...
Loading...
Build LLM applications using Dify's visual workflow platform. Use when creating AI chatbots, implementing RAG pipelines, developing agents with tools, managing knowledge bases, deploying LLM apps, or building workflows with drag-and-drop. Supports hundreds of LLMs, Docker/Kubernetes deployment.
npx skill4agent add aeonbridge/ab-anthropic-claude-skills dify-llm-platform# Clone the repository
git clone https://github.com/langgenius/dify.git
cd dify
# Navigate to docker directory
cd docker
# Copy environment example
cp .env.example .env
# Edit .env with your configuration
vim .env
# Start Dify
docker compose up -d
# Access the dashboard
open http://localhost/install# Sign up at https://cloud.dify.ai
# Get 200 free GPT-4 calls
# No installation requiredhelm repo add dify https://langgenius.github.io/dify-helm
helm install dify dify/dify# Using AWS CDK
cdk deploy DifyStack.env# API Service
API_URL=http://localhost:5001
# Web Service
WEB_URL=http://localhost:3000
# Database
DB_USERNAME=postgres
DB_PASSWORD=your_password
DB_HOST=db
DB_PORT=5432
DB_DATABASE=dify
# Redis
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
# Storage (S3, Azure Blob, or local)
STORAGE_TYPE=local
STORAGE_LOCAL_PATH=storage
# Vector Database
VECTOR_STORE=weaviate # or pgvector, qdrant, milvus
# API Keys for LLM Providers
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key# After installation, access http://localhost/install
# Complete initial setup wizard
# Create your first workspaceYou are a helpful customer service assistant for TechCorp.
You can help users with:
- Product information
- Order tracking
- Technical support
- Account management
Be friendly, professional, and concise.import requests
API_KEY = "your_dify_api_key"
API_URL = "http://localhost/v1"
# Send a chat message
response = requests.post(
f"{API_URL}/chat-messages",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"inputs": {},
"query": "What are your business hours?",
"response_mode": "streaming",
"conversation_id": "",
"user": "user-123"
}
)
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))const fetch = require('node-fetch');
const API_KEY = 'your_dify_api_key';
const API_URL = 'http://localhost/v1';
async function sendMessage(query) {
const response = await fetch(`${API_URL}/chat-messages`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
inputs: {},
query: query,
response_mode: 'blocking',
user: 'user-123'
})
});
return await response.json();
}
sendMessage('Hello!').then(console.log);Workflow Steps:
1. Start → User Message
2. Knowledge Retrieval → Search documentation
3. Conditional Branch:
- If relevant docs found → Use context
- If not found → Use general knowledge
4. LLM Node → Generate response
5. Tool Node → Create ticket (if needed)
6. End → Return response{{retrieval.score}} > 0.7Context: {{retrieval.context}}
User question: {{user.query}}
Provide a helpful answer based on the context.
If the context doesn't contain the answer, say so politely.def main(data: dict) -> dict:
# Access workflow variables
user_query = data.get('query', '')
# Custom processing
processed = user_query.upper()
# Return results
return {
'result': processed,
'length': len(user_query)
}function main(data) {
const query = data.query || '';
// Process data
const words = query.split(' ');
return {
word_count: words.length,
first_word: words[0]
};
}Agent Configuration:
Model: gpt-4
Reasoning Mode: ReAct
Tools:
- Google Search
- Wikipedia
- Web Scraper
System Prompt: |
You are a research assistant that helps users find
accurate information from reliable sources.
Always:
1. Search for current information
2. Cite your sources
3. Verify facts from multiple sources# Define custom tool for Dify
{
"name": "check_inventory",
"description": "Check product inventory levels",
"parameters": {
"type": "object",
"properties": {
"product_id": {
"type": "string",
"description": "Product identifier"
}
},
"required": ["product_id"]
}
}Strategy: fixed_size
Chunk Size: 500 tokens
Overlap: 50 tokens
Use Case: General documents, articlesStrategy: paragraph
Min Size: 100 tokens
Max Size: 800 tokens
Use Case: Well-formatted documentsStrategy: semantic
Model: text-embedding-ada-002
Similarity Threshold: 0.8
Use Case: Complex technical documentsType: vector
Top K: 5
Score Threshold: 0.7
Embedding Model: text-embedding-3-largeType: hybrid
Vector Weight: 0.7
Keyword Weight: 0.3
Reranking: enabled
Reranking Model: cross-encoder/ms-marco-MiniLM-L-12-v2POST /v1/chat-messages
Content-Type: application/json
Authorization: Bearer {api_key}
{
"inputs": {},
"query": "Your question here",
"response_mode": "streaming",
"user": "user-identifier"
}POST /v1/completion-messages
Content-Type: application/json
Authorization: Bearer {api_key}
{
"inputs": {
"name": "John",
"topic": "AI"
},
"response_mode": "blocking",
"user": "user-123"
}POST /v1/messages/{message_id}/feedbacks
Content-Type: application/json
Authorization: Bearer {api_key}
{
"rating": "like",
"user": "user-123"
}from dify_client import DifyClient
client = DifyClient(api_key="your_api_key")
# Chat completion
response = client.chat(
query="What is Dify?",
user="user-123",
conversation_id=None
)
print(response.answer)for chunk in client.chat_stream(
query="Explain quantum computing",
user="user-123"
):
print(chunk.delta, end="", flush=True)Metrics to Track:
- Response latency
- Token usage
- Error rates
- User satisfaction scores
- Knowledge retrieval accuracydocker compose up# Check logs
docker compose logs
# Verify environment variables
cat .env
# Ensure ports are available
lsof -i :3000
lsof -i :5001
# Reset and restart
docker compose down -v
docker compose up -d# Via API - trigger reindex
POST /v1/datasets/{dataset_id}/documents/{document_id}/processingcache:
enabled: true
ttl: 3600response_mode: "streaming"max_tokens: 500 # Reduce if possible
temperature: 0.7
top_p: 0.9# Monitor PostgreSQL
docker exec -it dify-db psql -U postgres -c "\
SELECT pid, query, state, wait_event_type \
FROM pg_stat_activity WHERE state != 'idle';"# Verify API keys in .env
cat .env | grep API_KEY
# Check provider status
curl https://status.openai.com/api/v2/status.json
# Implement retry logic
max_retries: 3
retry_delay: 1000 # milliseconds# In .env
LOG_LEVEL=DEBUGdocker stats# model_providers/custom_provider.py
from dify.core.model_runtime import ModelProvider
class CustomProvider(ModelProvider):
def get_models(self):
return [
{
'model': 'custom-gpt',
'label': 'Custom GPT Model',
'model_type': 'llm'
}
]
def invoke(self, model, credentials, prompt, **kwargs):
# Custom API call logic
response = your_api_call(prompt)
return responseWorkflow:
- Node1: LLM Call
- Parallel:
- Node2a: Knowledge Retrieval
- Node2b: External API Call
- Node3: Combine Results# Cache expensive operations
if cache.exists(query_hash):
return cache.get(query_hash)
else:
result = expensive_operation()
cache.set(query_hash, result, ttl=3600)
return resultServices:
API:
replicas: 3
load_balancer: nginx
Worker:
replicas: 5
queue: redis
Database:
primary: postgres-main
replicas: 2
backup: dailyMonitoring:
- Prometheus: Metrics collection
- Grafana: Visualization
- Loki: Log aggregation
- Alertmanager: Alerts# Clone repository
git clone https://github.com/langgenius/dify.git
cd dify
# See deployment guide
# https://docs.dify.ai/development/deploy-from-source