Making my own simple AI chatbot?

Hey, i’ve been needing an AI chatbot for my game, and i can’t wrap my head around all this python and HTTP and pricing stuff, so i thought i could just code my own. It’s nice because it doesn’t need to remember anything, just puke stuff out as a response to text.

The basis of my game is that players try to sell basic items to customers with an outrageous pitch, like “You rub this rock on your skin and it makes you live forever.” How can i make such a thing?

go get yourself a google gemini api key and just follow the instructions from the API, you get 30 prompts per minute for free with Gemini 2.0 Flash-Lite

4 Likes

yooo thanks i was looking for something free

hey uh, trying to set this up now and i can’t understand what to do

code?ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

here’s my code, obviously doesn’t work since i haven’t set it up with gemini yet, its currently set up with openrouter

--// AI setup
local url = "https://openrouter.ai/api/v1/chat/completions"
local headers = {
	["Authorization"] = "key" -- there is an actual key here
	["HTTP-Referer"] = "roblox.com", -- required by OpenRouter, can be anything
	["X-Title"] = "RobloxCustomerBot" -- optional, for dashboard naming
}

--// Sequence
while true do
	task.wait(3)
	Sounds.Bell:Play()
	task.wait(0.2)
	tweenIn:Play()
	task.wait(1)

	local product = products[math.random(1, #products)]
	local messagePicked = beginnings[math.random(1, #beginnings)]
	local textAsked = string.gsub(messagePicked, "<PRODUCT>", product)

	dialogueRF:InvokeClient(player, textAsked)

	local pitch = player.Chatted:Wait()

	local prompt = "The player is trying to sell you an item called '" .. product .. "'. They say: " .. pitch ..
		". Respond as a playful, slightly gullible customer who judges the pitch. At the end, say <YES> if you’ll buy, or <NO> if you won’t."

	local body = HTTPService:JSONEncode({
		model = "mistralai/mistral-7b-instruct",
		messages = {
			{ role = "user", content = prompt }
		}
	})

	local success, response = pcall(function()
		return HTTPService:PostAsync(url, body, Enum.HttpContentType.ApplicationJson, false, headers)
	end)

	if success then
		local result = HTTPService:JSONDecode(response)
		local message = result.choices and result.choices[1] and result.choices[1].message and result.choices[1].message.content or "Huh?"
		dialogueRF:InvokeClient(player, message)
	else
		warn("OpenRouter error:", response)
		dialogueRF:InvokeClient(player, "Uh... Let me sleep on it.")
	end

	task.wait(3)
	tweenOut:Play()
end

so you dont want to use gemini or what?

wait nevermind just read your message

1 Like

i want to use it with gemini i just dont know where to start, the code is trying to use a different AI but that doesn’t work


ok what the hell did they do to studio

1 Like

idk man it makes me sick i had to change it in beta features

ok im in a rush but use this http post for now, i think u still gotta format the body tho
https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=INSERT_KEY_HERE

thanks so much, but i put my key in the link and it says the page can’t be found

local HTTPService = game:GetService("HttpService")

local APIKey = "" -- Change this!!!
local URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=" .. APIKey




local prompt = "Say hello!"

local body = HTTPService:JSONEncode({
	contents = {
		["parts"] = {
			["text"] = prompt
		}
	}
})


local response = HTTPService:PostAsync(URL, body)



local data = HTTPService:JSONDecode(response)


print(data)
for Index, Value in data.candidates[1].content.parts do
	print(Value["text"])
	
end

if data.message == "success" then
	print(data.message)
end
1 Like

1 Like

thanks man! im really new to this http stuff so i really appreciate it

additionally some ai configuration stuff

["generationConfig"] = {
		["temperature"] = 0.8,
		["maxOutputTokens"] = 800,
		["topP"] = 0.8,
		["topK"] = 10
	}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.