(Open Source) ezLLM: Integrating an AI System into Roblox

ezLLM: Integrating an AI System into Roblox

Hello developers!

I’ve been working on a project to connect an AI system to Roblox. This system uses a Python API that communicates with the DuckDuckGo Chat service to send messages and get responses from models like Claude 3 Hiaku, GPT 4 Mini, Llama 3.1 70B, and Mixtral 8x7B.


Project Repo


Script

local HttpService = game:GetService("HttpService")

-- URL for the Python server running Flask
local url = "http://localhost:5000/gpt_response"  -- Make sure this URL is correct

-- Function to get the response from the server
local function response(user_input)
    -- Create the request data with the user's message
    -- Make sure the format is a list of messages (a table with a dictionary containing the message)
    local requestData = {
        messages = {
            {
                role = "user",
                content = user_input
            }
        }
    }

    -- Try to make the HTTP request with `pcall`
    local success, response = pcall(function()
        -- Convert the data into JSON
        local jsonData = HttpService:JSONEncode(requestData)

        -- Send the POST request to the given URL
        local response = HttpService:PostAsync(url, jsonData, Enum.HttpContentType.ApplicationJson)

        -- Return the response
        return response
    end)

    -- If the request is successful, process the response
    if success then
        local decodedResponse
        -- Try to decode the JSON response
        local successDecoding, decodedResponse = pcall(function()
            return HttpService:JSONDecode(response)
        end)

        if successDecoding then
            print("Response: " .. decodedResponse.response)
        else
            print("Error decoding the response: " .. decodedResponse)
        end
    else
        print("HTTP request error: " .. response)
    end
end

-- Call the function with a test message
response("Hello!")


How It Works

  1. API Connection: We use Roblox’s HttpService to send POST requests to a server, which communicates with DuckDuckGo Chat.
  2. Sending Data: The message that the player sends is sent as JSON to the server under the messages property.
  3. Response Handling: The API returns a JSON response that is processed and decoded within Roblox Studio and displays the response in the console.

Why Use This?

  • Smarter Interactions: Integrate advanced AI models into your Roblox game, such as Claude 3 Hiaku and GPT-4 Mini, to create smarter NPCs and scenarios that dynamically respond to player interactions.

Tips

  • Security First:

    • Protect your API with authentication to avoid abuse or unauthorized access.
  • Optimize for Speed:

    • keep in mind that HTTP requests can introduce latency into the game, so optimizing the API and data flow is crucial for fast and efficient responses.
  • Rate Limits:

    • If your game has lots of players, implement limits to prevent your server from being overwhelmed.

What’s Next?

I am working on a feature to save interaction history, which will improve the quality of responses by allowing the AI to remember past conversations and provide more contextual responses.


If you want to test this AI ChatBot system in Roblox, you can do it in the Roblox experience: Typewrite AI | Beta - Roblox

Python and Luau resources can be found on the Github repository

14 Likes

Great resource! Thanks for sharing :heart:

2 Likes

GPL-3 is very risky to have it in your games. Might want to have a look at other libraries.

Edit: I can see why. gpt4free is GPL-3 licensed, so the code is GPL-3 as well. I use LlamaFile for self-host (which uses Apache 2.0 license instead).

2 Likes

Why do you say that GPL3 is risky?

If you create any modifications to the code, you need to release the code using the same license to the public. Not great for keeping commercial codes private.

Basically, if you modify this code for your game, you will literally have to release the whole game under GPLv3. Otherwise, you run the risk of getting lawsuits.

Thanks for warning me about that, I will make a change to the whole structure and use a different license. :saluting_face:

Seriously, use LlamaFile. gpt4free isn’t that great when interacting with code games.

You’ll be able to use the more open source Llama models.

It looks like the creator updated it to the MIT license, I guess we can now use it in our games

Probably, but the commit history in the GitHub is a bit of an issue though since it clearly shows GPLv3 if you look far enough. @CompressedByte might get into trouble for not showing the license in the first place.

@CompressedByte, I recommend you set the current repository to private/delete and create a new public repository using the same name.

You are right, thank you very much for the recommendation.