If you encounter an error like "API key not valid. Please pass a valid API key." while chatting with the chatbot, please follow these steps:
- Go to Google AI Studio.
- Navigate to the API key section and create a new API key.
Your API key will look something like this: AIzaSyAtpnKGX13bTgmx0l_gQeatYvdWvY_wOTQ
Note: The API is free but has a limited number of usage requests.
- Open your project folder in VS Code.
- Locate to the
script.jsfile in your project. - Find the
API_KEYvariable and replacePASTE-YOUR-API-KEYwith your actual API key.
- Save the
script.jsfile after adding your API key. - Open
index.htmlin your browser to verify that Chatbot is working correctly.
This chatbot uses the Gemini beta model, gemini-1.5-flash, which allows more free requests within a shorter timeframe. If you need greater reliability, you can switch to the stable model, gemini-1.5-pro. While the free version of this model has stricter request limits, upgrading to a paid plan will remove these restrictions.
To switch to the gemini-1.5-pro stable model, update the API_URL in the script.js file as follows:
const API_URL = https://generativelanguage.googleapis.com/v1/models/gemini-1.5-pro:generateContent?key=${API_KEY};
If you still get an error or get stuck, feel free to message me on https://cloudtechnologycomputing.com
Happy coding!