Gemini-Pro Chat with conversations

IMPORTANT: This flow requires the @google/generative-ai npm module, to install it run npm install @google/generative-ai from the directory where Node-RED is installed. Then set functionExternalModules to true in your 'settings.js' file.

There are two input fields in the dashboard, the first is tailored to assist with writing Node-RED function node code. And the second is to just message Gemini without any system prompt.

You will need to have an API KEY from here: https://makersuite.google.com/app/apikey

IMPORTANT: Open the function node and replace the text "Your-API-KEY-Here" with a valid API KEY and deploy the flow to begin.

This version of Gemini Chat supports multi turn conversations.

[{"id":"dc9882f8f5cbd294","type":"tab","label":"Gemini Chat","disabled":false,"info":"","env":[]},{"id":"ac6be42f93f92b26","type":"change","z":"dc9882f8f5cbd294","name":"","rules":[{"t":"set","p":"history","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":320,"wires":[["b461b94cfc392e9c"]]},{"id":"5156194e376e0d95","type":"inject","z":"dc9882f8f5cbd294","name":"reset","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":370,"y":360,"wires":[["ac6be42f93f92b26"]]},{"id":"751197d68078c67b","type":"function","z":"dc9882f8f5cbd294","name":"save history","func":"// Function Node to Save Payloads as an Array of Objects in a Flow Variable\n\n// Check if the flow variable 'history' exists, if not, initialize it as an empty array\nif (!flow.get('history', \"file\")) {\n    flow.set('history', [], \"file\");\n}\n\n// Get the current array from the flow variable\nlet history = flow.get('history', \"file\");\n\n// Determine the role from the message (use \"modle\" if msg.role is not defined)\nlet role = msg.role || \"model\";\n\n// Create an object with role and parts properties\nlet conversationObject = {\n    role: msg.role,\n    parts: msg.payload,\n};\n\n// Add the current conversation object to the array\nhistory.push(conversationObject);\n\n// Update the flow variable with the modified array\nflow.set('history', history, \"file\");\n\n// Return the original message\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":180,"wires":[["70b2ba8f972204a2"]]},{"id":"cfec7d73042c25b6","type":"inject","z":"dc9882f8f5cbd294","name":"How many paws are in my house?","props":[{"p":"payload"},{"p":"role","v":"user","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"How many paws are in my house?","payloadType":"str","x":280,"y":260,"wires":[["f46b2a5183be972f","eb2dcc300693d42e"]]},{"id":"70b2ba8f972204a2","type":"debug","z":"dc9882f8f5cbd294","name":"debug 385","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":180,"wires":[]},{"id":"5c21de9b6bb526c5","type":"debug","z":"dc9882f8f5cbd294","name":"debug 386","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":280,"wires":[]},{"id":"eb2dcc300693d42e","type":"function","z":"dc9882f8f5cbd294","name":"Gemini","func":"const AI = googleGenerativeAi;\nconst GoogleGenerativeAI = AI.GoogleGenerativeAI;\n//msg.payload = GoogleGenerativeAI;\n\nconst genAI = new GoogleGenerativeAI(\"Your-API-KEY-Here\");\n// msg.topic = genAI;\n\n\nasync function run(message) {\n    // For text-only input, use the gemini-pro model\n    const model = genAI.getGenerativeModel({ model: \"gemini-pro\" });\n\n    const history = flow.get('history', \"file\") || [\n        {\n            role: \"user\",\n            parts: \"Hello.\",\n        },\n        {\n            role: \"model\",\n            parts: \"Great to meet you. What would you like to know?\",\n        },\n    ];\n    \n    const chat = model.startChat({\n        history: history,\n        generationConfig: {\n            maxOutputTokens: 200,\n        },\n    });\n\n    const msg = message;\n\n    const result = await chat.sendMessage(msg);\n    const response = await result.response;\n    const text = response.text();\n    node.send({\"payload\":text, \"role\": \"model\"});\n}\n\nrun(msg.payload);","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"googleGenerativeAi","module":"@google/generative-ai"}],"x":620,"y":280,"wires":[["5c21de9b6bb526c5","f46b2a5183be972f"]]},{"id":"f46b2a5183be972f","type":"delay","z":"dc9882f8f5cbd294","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":620,"y":180,"wires":[["751197d68078c67b","b461b94cfc392e9c"]]},{"id":"793780e3bcad66d7","type":"inject","z":"dc9882f8f5cbd294","name":"","props":[{"p":"payload"},{"p":"role","v":"user","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"I have 2 dogs.","payloadType":"str","x":350,"y":220,"wires":[["f46b2a5183be972f","eb2dcc300693d42e"]]},{"id":"9ef9f26fbdfc2918","type":"ui_template","z":"dc9882f8f5cbd294","group":"d1d7699f939a4cd8","name":"Chat window","order":5,"width":30,"height":60,"format":"<!-- Node-RED ui_template node's HTML to display a conversation from a payload array -->\n\n<!DOCTYPE html>\n<html lang=\"en\">\n\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>AI Chatbot Conversation</title>\n    <style>\n        /* Node-RED ui_template CSS with centering and rounded corners */\n        body {\n            font-family: Arial, sans-serif;\n            /* Specify a fallback font-family */\n            background-color: #222;\n            /* Set a default background color */\n            color: #fff;\n            /* Set a default text color */\n            margin: 0;\n            /* Remove default body margin */\n            padding: 0px;\n            /* Add padding to the body */\n        }\n\n        h1 {\n            text-align: center;\n            /* Center the heading */\n            color: #800000;\n            /* Heading color */\n        }\n\n        .conversation-container {\n            max-width: 80%;\n            /* Adjust as needed */\n            margin: 20px auto;\n            /* Center the container horizontally */\n        }\n\n        .message {\n            padding: 10px;\n            margin-bottom: 10px;\n            border-radius: 10px;\n        }\n\n        .user-message {\n            background-color: #3498db;\n            /* User message color */\n        }\n\n        .model-message {\n            background-color: #2ecc71;\n            /* Model message color */\n        }\n    </style>\n</head>\n\n<body>\n    <!-- Node-RED ui_template HTML -->\n    <h1>Gemini Conversation</h1>\n    <div class=\"conversation-container\" ng-repeat=\"msg in msg.payload\">\n        <div class=\"message\" ng-class=\"{'user-message': msg.role === 'user', 'model-message': msg.role === 'model'}\">\n            <strong>{{msg.role}}</strong>: {{msg.parts}}\n        </div>\n    </div>\n</body>\n\n</html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":1050,"y":320,"wires":[["d0b737ec0b5f5b51"]]},{"id":"bf05d4c3a1a56260","type":"ui_text_input","z":"dc9882f8f5cbd294","name":"","label":"Message Gemini","tooltip":"Message Gemini","group":"d1d7699f939a4cd8","order":2,"width":16,"height":1,"passthru":true,"mode":"text","delay":"0","topic":"","sendOnBlur":true,"className":"","topicType":"str","x":190,"y":180,"wires":[["95faa243e5d9a180"]]},{"id":"5b56df4dbc3f403a","type":"ui_button","z":"dc9882f8f5cbd294","name":"","group":"d1d7699f939a4cd8","order":3,"width":2,"height":1,"passthru":false,"label":"RESET","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":360,"y":320,"wires":[["ac6be42f93f92b26"]]},{"id":"b461b94cfc392e9c","type":"function","z":"dc9882f8f5cbd294","name":"send chat history","func":"msg.payload = flow.get('history', \"file\") || [];\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":320,"wires":[["9ef9f26fbdfc2918"]]},{"id":"d0b737ec0b5f5b51","type":"debug","z":"dc9882f8f5cbd294","name":"debug 387","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":320,"wires":[]},{"id":"95faa243e5d9a180","type":"function","z":"dc9882f8f5cbd294","name":"add role","func":"msg.role = \"user\";\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":180,"wires":[["f46b2a5183be972f","eb2dcc300693d42e"]]},{"id":"d1d7699f939a4cd8","type":"ui_group","name":"Default","tab":"3ade8121095fbcfa","order":1,"disp":true,"width":"30","collapse":false,"className":""},{"id":"3ade8121095fbcfa","type":"ui_tab","name":"Gemini Chat","icon":"dashboard","disabled":false,"hidden":false}]

Flow Info

Created 5 months, 1 week ago
Rating: 5 1

Actions

Rate:

Node Types

Core
  • change (x1)
  • debug (x3)
  • delay (x1)
  • function (x4)
  • inject (x3)
Other
  • tab (x1)
  • ui_button (x1)
  • ui_group (x1)
  • ui_tab (x1)
  • ui_template (x1)
  • ui_text_input (x1)

Tags

  • Gemini
  • chatbot
  • ai
  • conversations
  • google
  • generativeAI
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option