AI Text Generation in ROBLOX; (Google Gemini API)

Steps :
-Go to your game’s security settings and switch Allow HTTP Requests to on
-Replace the apiKey with your Gemini API key; Get your Gemini API key here (must be 18+ and be in availible countries, you’ll be redirected here if you’re not eligible)
-Change prompt string to your prompt
-Do whatever you want with msgfinal as it’s what Gemini retuened

Recommendation :
-I recommend filtering the results using TextService:FilterStringAsync() as you can’t guarantee the results are up to ROBLOX TOS standards.
-Get trustworthy developers as you have to trust your developers with your Gemini API key.

Code down below :

local HttpService = game:GetService("HttpService")

local apiKey = "insert ur api key here"
local url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key="..apiKey

local prompt = "insert ur prompt here"
local requestBody = HttpService:JSONEncode({
	contents = {
		{
			parts = {
				{ text = prompt }
			}
		}
	}
})

local success, response = pcall(function()
	return HttpService:PostAsync(url, requestBody, Enum.HttpContentType.ApplicationJson, false)
end)

-- Handle the response
if success then
	local decodedResponse = HttpService:JSONDecode(response)
	
	local msgfinal
	
	for index,text in pairs(decodedResponse["candidates"][1]["content"]["parts"][1]) do
		msgfinal = text
	end
	
	print(msgfinal)
else
	print("error: "..response)
end
4 Likes

You could just use the new Secret API to hide your API key securely

3 Likes

I’ve got a question, having not used HTTP service much before and considering the pings to external sources, how long does it take to recieve the response? Is it something to be improved in the future and not yet usable in real time or is it regular latency for real time usage?

for me it took about 5 seconds to generate 4 paragraphs of text so it’s pretty fast

Wait what’s so bad about sharing my API key? Is it ok to share it with my friends, if I literally generated it today?

a lot of bad things can happen when you share your key, someone can use it for malicious purposes and get you banned. (for example: hate speech)

I wish there was a way to make AI remember the past prompts sent

There is but it’s just not included in the post.

Alternatively, I have created some code for google gemma api on huggingface. In my project I am currently using 2 LLMs and 2 distinct apis. So with this service I could increase that to 3 and distribute the AI load to all 3 models and APis for maximum usage and real time performance.
Chatbot & LLM Artificial Intelligence Model API Code Documentation FREE (Open Source) and Other Useful APIs - Resources / Community Resources - Developer Forum | Roblox

If the creator of this post doesn’t update with how to include context in the model conversation history I will take a look when I get around to incorporating this api to my AI stack.
It has a unique structure which will take some testing to figure out

["candidates"][1]["content"]["parts"][1]
1 Like

Hey, please do post about how to add context/conversation history to the AI Chatbot!