n8n_stack/imports/workflows/ollama-chatbot.json
dschlueter 60143b8061 feat: Chatbot-Workflows mit Ollama-Memory und Session-Management
- docker-compose: extra_hosts für host.docker.internal (Ollama-Zugang)
- ollama-chatbot: Gesprächsverlauf per session_id in PostgreSQL
- ollama-chat-cleanup: stündliche TTL-Bereinigung (2h Inaktivität)
- ollama-chat-delete: DELETE /webhook/chat-session Endpoint

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-06 02:44:13 +02:00

130 lines
4.9 KiB
JSON

{
"id": "1001",
"name": "Lokaler KI-Chatbot (Ollama)",
"nodes": [
{
"parameters": {
"httpMethod": "POST",
"path": "chat",
"responseMode": "responseNode",
"options": {}
},
"id": "node-wh-001",
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [100, 300],
"webhookId": "ollama-chat-webhook"
},
{
"parameters": {
"jsCode": "const body = $input.first().json.body;\nif (!body?.message) throw new Error('Kein \"message\"-Feld im Request-Body');\nreturn [{ json: {\n session_id: body.session_id || Math.random().toString(36).slice(2, 18),\n message: body.message,\n model: body.model || 'qwen3:8b'\n}}];"
},
"id": "node-cd-001",
"name": "Prepare",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [300, 300]
},
{
"parameters": {
"operation": "executeQuery",
"query": "SELECT messages FROM chat_sessions WHERE session_id = $1",
"additionalFields": {
"queryParams": "={{ JSON.stringify([$json.session_id]) }}"
}
},
"id": "node-pg-001",
"name": "Load History",
"type": "n8n-nodes-base.postgres",
"typeVersion": 2.5,
"position": [500, 300],
"credentials": {
"postgres": {
"id": "SETUP_REQUIRED",
"name": "n8n PostgreSQL"
}
}
},
{
"parameters": {
"jsCode": "const prep = $('Prepare').first().json;\nconst row = $input.first()?.json;\nconst history = Array.isArray(row?.messages) ? row.messages : [];\nconst messages = [...history, { role: 'user', content: prep.message }];\nreturn [{ json: { session_id: prep.session_id, model: prep.model, messages } }];"
},
"id": "node-cd-002",
"name": "Build Messages",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [700, 300]
},
{
"parameters": {
"method": "POST",
"url": "http://host.docker.internal:11434/api/chat",
"sendBody": true,
"contentType": "json",
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ model: $json.model, messages: $json.messages, stream: false }) }}",
"options": { "timeout": 120000 }
},
"id": "node-hr-001",
"name": "Ollama Chat",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [900, 300]
},
{
"parameters": {
"jsCode": "const build = $('Build Messages').first().json;\nconst resp = $input.first().json;\nconst answer = resp.message.content;\nconst updatedMessages = [...build.messages, { role: 'assistant', content: answer }];\nreturn [{ json: { session_id: build.session_id, messages: updatedMessages, answer, model: resp.model } }];"
},
"id": "node-cd-003",
"name": "Append Response",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [1100, 300]
},
{
"parameters": {
"operation": "executeQuery",
"query": "INSERT INTO chat_sessions (session_id, messages, updated_at) VALUES ($1, $2::jsonb, NOW()) ON CONFLICT (session_id) DO UPDATE SET messages = $2::jsonb, updated_at = NOW()",
"additionalFields": {
"queryParams": "={{ JSON.stringify([$json.session_id, JSON.stringify($json.messages)]) }}"
}
},
"id": "node-pg-002",
"name": "Save History",
"type": "n8n-nodes-base.postgres",
"typeVersion": 2.5,
"position": [1300, 300],
"credentials": {
"postgres": {
"id": "SETUP_REQUIRED",
"name": "n8n PostgreSQL"
}
}
},
{
"parameters": {
"respondWith": "json",
"responseBody": "={{ JSON.stringify({ session_id: $('Append Response').first().json.session_id, answer: $('Append Response').first().json.answer, model: $('Append Response').first().json.model }) }}",
"options": {}
},
"id": "node-rw-001",
"name": "Respond",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [1500, 300]
}
],
"connections": {
"Webhook": { "main": [[{ "node": "Prepare", "type": "main", "index": 0 }]] },
"Prepare": { "main": [[{ "node": "Load History", "type": "main", "index": 0 }]] },
"Load History": { "main": [[{ "node": "Build Messages", "type": "main", "index": 0 }]] },
"Build Messages": { "main": [[{ "node": "Ollama Chat", "type": "main", "index": 0 }]] },
"Ollama Chat": { "main": [[{ "node": "Append Response", "type": "main", "index": 0 }]] },
"Append Response": { "main": [[{ "node": "Save History", "type": "main", "index": 0 }]] },
"Save History": { "main": [[{ "node": "Respond", "type": "main", "index": 0 }]] }
},
"active": false,
"settings": { "executionOrder": "v1" },
"tags": []
}