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
-
API Connection: We use Roblox’s
HttpService
to send POST requests to a server, which communicates with DuckDuckGo Chat. -
Sending Data: The message that the player sends is sent as JSON to the server under the
messages
property. - 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.