2026-05-06 02:44:13 +02:00
{
"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" ,
2026-05-06 03:57:34 +02:00
"query" : "=WITH ensure AS (INSERT INTO chat_sessions (session_id, messages) VALUES ('{{ $json.session_id }}', '[]') ON CONFLICT (session_id) DO NOTHING) SELECT COALESCE(messages, '[]'::jsonb) AS messages FROM chat_sessions WHERE session_id = '{{ $json.session_id }}'" ,
"additionalFields" : { }
2026-05-06 02:44:13 +02:00
} ,
"id" : "node-pg-001" ,
"name" : "Load History" ,
"type" : "n8n-nodes-base.postgres" ,
"typeVersion" : 2.5 ,
"position" : [ 500 , 300 ] ,
"credentials" : {
"postgres" : {
2026-05-06 03:57:34 +02:00
"id" : "pnmeNZ3ONDWrQP8M" ,
"name" : "Postgres account"
2026-05-06 02:44:13 +02:00
}
}
} ,
{
"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" : {
2026-05-06 03:57:34 +02:00
"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 }];\nconst m = JSON.stringify(updatedMessages).replace(/'/g, \"''\");\nconst upsert_query = `INSERT INTO chat_sessions (session_id, messages, updated_at) VALUES ('${build.session_id}', '${m}'::jsonb, NOW()) ON CONFLICT (session_id) DO UPDATE SET messages = '${m}'::jsonb, updated_at = NOW()`;\nreturn [{ json: { session_id: build.session_id, answer, model: resp.model, upsert_query } }];"
2026-05-06 02:44:13 +02:00
} ,
"id" : "node-cd-003" ,
"name" : "Append Response" ,
"type" : "n8n-nodes-base.code" ,
"typeVersion" : 2 ,
"position" : [ 1100 , 300 ]
} ,
{
"parameters" : {
"operation" : "executeQuery" ,
2026-05-06 03:57:34 +02:00
"query" : "={{ $json.upsert_query }}" ,
"additionalFields" : { }
2026-05-06 02:44:13 +02:00
} ,
"id" : "node-pg-002" ,
"name" : "Save History" ,
"type" : "n8n-nodes-base.postgres" ,
"typeVersion" : 2.5 ,
"position" : [ 1300 , 300 ] ,
"credentials" : {
"postgres" : {
2026-05-06 03:57:34 +02:00
"id" : "pnmeNZ3ONDWrQP8M" ,
"name" : "Postgres account"
2026-05-06 02:44:13 +02:00
}
}
} ,
{
"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" : [ ]
}