Greetings, I’m trying to make an AI Assistant with a custom prompt so it behaves differently e.g Prompt: Act like a cowboy from the wild west, similar to Character.AI. I asked ChatGPT to assist since I don’t know where to start.
I want to say that I’m not good at coding outside of Luau, so if there are any obvious mistakes, you can blame it on ChatGPT, I’m just kidding
Ignore the comments GPT made
Python-Code:
import openai
import flask
from flask import Flask, request, jsonify
app = Flask(__name__)
# Replace with your actual OpenAI API key
openai.api_key = "MyAPIKey" -- I inserted mine, dont worry
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
user_message = data.get("message", "")
if not user_message:
return jsonify({"error": "Message field is required"}), 400
try:
# Correct API call to OpenAI
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Or gpt-4 if you are using it
messages=[{"role": "user", "content": user_message}]
)
ai_reply = response["choices"][0]["message"]["content"]
return jsonify({"response": ai_reply})
except Exception as e:
print(f"Error: {str(e)}") # Print error for debugging
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)
The file is called ai_server.py.
This is the server-side of Roblox Studio, I’m sure the client isn’t the problem but the server might be, so here is the code:
local HttpService = game:GetService("HttpService")
local replicatedstorage = game:GetService("ReplicatedStorage")
local sendmessage = replicatedstorage:WaitForChild("SendMessage")
-- Function to send a message to the AI
function sendMessageToAI(cmessage)
local url = "" -- My URL is not blank, but for safety purposes, hidden.
local data = {
message = cmessage
}
local jsonData = HttpService:JSONEncode(data)
local response = HttpService:PostAsync(url, jsonData, Enum.HttpContentType.ApplicationJson)
local responseData = HttpService:JSONDecode(response)
return responseData.response or "No response from AI."
end
-- Listen for the event and respond with AI-generated text
sendmessage.OnServerEvent:Connect(function(player, cmessage)
local response = sendMessageToAI(cmessage)
-- Send the response back to the player who initiated the message
sendmessage:FireClient(player, response)
end)
That’s about all the code there is. FYP I’ve completed these steps, but lmk if I should retry them:
- Turned on HTTPService in Studio
- Tried both in Studio and Roblox.
- Downloaded Python
- Installed pip install flask openai
- Turned on the server before testing
- Debugged multiple problems
So after testing multiple times, I’m pretty sure the Roblox side works, it’s probably the python code or OpenAI version that’s causing the error.
I’m trying to implement a temporary system, so I can test features for now and how I can implement it so that It can handle mass amounts of prompts soon.
If you have any questions about anything that I missed, please ask! Also if there’s a better way of implementing this please let me know