Esp32 Gemini chatbot system and safety settings

Hi,
I’m building a simple chatbot using the Gemini api on an esp32 using the Arduino IDE. I have it working nicely, but only with basic settings. I use POST to send a payload string, which is constructed thus:

String payload = String(“{"contents": [{"parts":[{"text":” + res + "}]}],"generationConfig": {"maxOutputTokens": " + (String)Gemini_Max_Tokens + “}}”);

where ‘res’ is the string typed by the user (eventually, this will come from speech recognition). This works fine, but I want to add a system instruction to alter the charcter of the response (eg to that of an angry robot), and also modify the safety settings - but I cannot figure out how to include these in the above line of code. Could I please ask if there are any Ardunio experts willing to help out - many thanks.

Welcome to the forum.

This cookbook sample shows how to build the system instruction: cookbook/quickstarts/rest/System_instructions_REST.ipynb at main · google-gemini/cookbook · GitHub

Hope that helps!

1 Like

Welcome @David_Lane

Here are the REST API samples from cookbook.

Instead of using speech recognition to transcribe the string, you can simply prompt the models with voice.

Thankyou both, for your replies. I’ve read the documents to which you refer, but I’m sorry to say that they have not helped me (for now). Whilst I understand some of this, I am stuck on trying to get the required params into the format shown in my original post - will have another go later, thanks again.

One more documentation reference: Generating content  |  Gemini API  |  Google AI for Developers
The code you have so far fills in the contents[] and generationConfig fields described in the API reference. You only need to add the systemInstruction field. That field is prefixed by the string identifier "system_instruction" as shown in the sample code in cookbook/quickstarts/rest/System_instructions_REST.ipynb at main · google-gemini/cookbook · GitHub.

Hope that helps.

Thanks all…I’ve cracked it… Had a light bulb moment and submitted my query to the Gemini API itself - which returned a working code in3 seconds. Sigh.
Should anyone writing a ESP32 based chatbot find their way here, this is the vital line:
String payload = String(“{"contents": [{"parts":[{"text":"where are you from?"} ]}],"safetySettings": [{"category":"HARM_CATEGORY_HARASSMENT","threshold":"BLOCK_ALL"},{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"BLOCK_ALL"},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"BLOCK_ALL"}],"systemInstruction":{"role":"user","parts":[{"text":"answer as an angry robot"}]},"generationConfig":{"temperature":1.8,"maxOutputTokens":” + (String)Gemini_Max_Tokens + “}}”);