Using ai for a chatbot

Just asking as a question, is it possible to use OpenAI’s API to make an AI chatbot in roblox? if so any examples of how I should do it?

Ask ChatGPT itself? This is what it gave me:

local HttpService = game:GetService("HttpService")
local OPENAI_API_KEY = "YOUR_SECURELY_STORED_API_KEY"

local function getAIResponse(userMessage)
    local url = "https://api.openai.com/v1/engines/davinci-codex/completions"
    local headers = {
        ["Authorization"] = "Bearer " .. OPENAI_API_KEY,
        ["Content-Type"] = "application/json"
    }
    local body = HttpService:JSONEncode({
        prompt = userMessage,
        max_tokens = 150,
        temperature = 0.7,
        n = 1,
        stop = nil
    })

    local response = HttpService:PostAsync(url, body, Enum.HttpContentType.ApplicationJson, false, headers)
    local data = HttpService:JSONDecode(response)

    return data.choices[1].text
end

local function onPlayerChatted(player, message)
    local aiResponse = getAIResponse(message)
    player:SendChatMessage(aiResponse)
end

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        onPlayerChatted(player, message)
    end)
end)
1 Like

I have no idea why I couldn’t think of asking ChatGPT lol thanks for the response, but I was kinda trying to know what roblox thinks of this. as you know they don’t allow http requests to some sites such as discord maybe there’s a similar thing in place with OpenAI

1 Like

As far as I know, OpenAI requests are fine as long as they are sent correctly and abide by the rate limitations of both Roblox and OpenAI.

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