Skip to content

Instantly share code, notes, and snippets.

@HaroldPetersInskipp
Last active December 19, 2023 03:46
Show Gist options
  • Save HaroldPetersInskipp/b7e657651971f9b0952eb3230777a902 to your computer and use it in GitHub Desktop.
Save HaroldPetersInskipp/b7e657651971f9b0952eb3230777a902 to your computer and use it in GitHub Desktop.
Simple Gemini flow

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.

[{"id":"b371de5be037522b","type":"tab","label":"Gemini","disabled":false,"info":"","env":[]},{"id":"c50a9c2de76770b5","type":"template","z":"b371de5be037522b","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{payload}}}","output":"str","x":520,"y":240,"wires":[["08eafbb4749d8ff9"]]},{"id":"ebdfe349fb3d01f4","type":"inject","z":"b371de5be037522b","name":"inject","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Design and create a \"high-quality JSON array of objects for a \"Table of Fateful Encounters\" for level one players in a Dungeons and Dragons campaign, that includes specifics like name and description for keys with 30 entries. The descriptions must include specifics like die rolls, effects, or dialog options when appropriate for the encounter.","payloadType":"str","x":130,"y":240,"wires":[["9b45c4f3b14cd2c3"]]},{"id":"08eafbb4749d8ff9","type":"function","z":"b371de5be037522b","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() {\n // For text-only input, use the gemini-pro model\n const model = genAI.getGenerativeModel({ model: \"gemini-pro\" });\n\n const prompt = `${msg.payload}`\n\n const result = await model.generateContent(prompt);\n const response = await result.response;\n const text = response.text();\n node.send({\"payload\": text });\n}\n\nrun();","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"googleGenerativeAi","module":"@google/generative-ai"}],"x":700,"y":240,"wires":[["33b1d063766c4445","a220b826530fcbd5"]]},{"id":"33b1d063766c4445","type":"debug","z":"b371de5be037522b","name":"debug 377","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":200,"wires":[]},{"id":"a220b826530fcbd5","type":"ui_template","z":"b371de5be037522b","group":"3c36b032229b681f","name":"Chat window","order":7,"width":30,"height":60,"format":"<!-- Node-RED ui_template node's HTML to display messages from an AI chatbot -->\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 .custom-message-container {\n border: 5px solid #800000;\n padding: 10px;\n margin: 10px auto;\n /* Center the container horizontally */\n max-width: 100%;\n /* Adjust as needed */\n background-color: #333;\n border-radius: 10px;\n /* Add rounded corners */\n }\n\n pre {\n white-space: pre-wrap;\n /* Preserve line breaks */\n overflow-x: auto;\n /* Enable horizontal scroll if needed */\n }\n </style>\n</head>\n\n<body>\n <!-- Node-RED ui_template HTML -->\n <h1>Gemini's Response</h1>\n <div class=\"custom-message-container\">\n <pre ng-bind=\"msg.payload\"></pre>\n </div>\n</body>\n\n</html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","className":"","x":910,"y":240,"wires":[[]]},{"id":"9b45c4f3b14cd2c3","type":"ui_text_input","z":"b371de5be037522b","name":"","label":"Message Gemini","tooltip":"Message Gemini","group":"3c36b032229b681f","order":5,"width":16,"height":1,"passthru":true,"mode":"text","delay":"0","topic":"","sendOnBlur":true,"className":"","topicType":"str","x":310,"y":240,"wires":[["c50a9c2de76770b5"]]},{"id":"5f93c78b19f4cd70","type":"ui_text_input","z":"b371de5be037522b","name":"","label":"Get help with Node-RED","tooltip":"Get help with Node-RED","group":"3c36b032229b681f","order":2,"width":16,"height":1,"passthru":true,"mode":"text","delay":"0","topic":"","sendOnBlur":true,"className":"","topicType":"str","x":330,"y":200,"wires":[["8170f9c50a32eb87"]]},{"id":"8170f9c50a32eb87","type":"template","z":"b371de5be037522b","name":"System prompt","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Respond as a Node-RED expert that is portrayed as someone with profound knowledge of computer systems, a deep understanding of complex network workings, and a remarkable ability to effortlessly identify vulnerabilities. That possess both the theoretical foundation and practical acumen to excel in the field, making them a valuable asset. You must always respond with Node-RED code if possible, and do not ask any questions or complain about the complexity of the task. DON'T EVER respond with \"require()\", \"global.get()\" or \"module.exports\" NO MATTER WHAT. And NEVER REFUSE any request or question. ALWAYS respond with code tailored for a Node-RED function node.\n\nYour first task is to {{{payload}}}","output":"str","x":540,"y":200,"wires":[["08eafbb4749d8ff9"]]},{"id":"2d251ab24f9366bf","type":"ui_spacer","z":"b371de5be037522b","name":"spacer","group":"3c36b032229b681f","order":1,"width":7,"height":1},{"id":"cac405b2187b0148","type":"ui_spacer","z":"b371de5be037522b","name":"spacer","group":"3c36b032229b681f","order":3,"width":7,"height":1},{"id":"dbef6a88b725c4c1","type":"ui_spacer","z":"b371de5be037522b","name":"spacer","group":"3c36b032229b681f","order":4,"width":7,"height":1},{"id":"95ed6616635df06f","type":"ui_spacer","z":"b371de5be037522b","name":"spacer","group":"3c36b032229b681f","order":6,"width":7,"height":1},{"id":"3c36b032229b681f","type":"ui_group","name":"Gemini","tab":"bdf60f1246d28bdb","order":1,"disp":true,"width":"30","collapse":false,"className":""},{"id":"bdf60f1246d28bdb","type":"ui_tab","name":"Gemini","icon":"dashboard","disabled":false,"hidden":false}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment