How do I limit large token usage in Open AI?

Essentially, I’m creating talkable NPC’s in-game with Open AI for a game I’m working on. The issue is, players are able to request the NPC to say a lot of words, by just saying in chat “say 1000 words.” That’s using a lot of tokens per request, causing the AI to spend more money per request on my end.

In my script, under content part that I send to Open AI for NPC personalities, I specifically wrote “Token cap per request of 80 (Ex. 1000 words)” (This goes with the long string of context for the AI, as shown in the picture)

I’m still honestly very new to Open AI, so I’m unsure of how to farther script the AI to do exactly what I want, other than to directly tell the AI everything I want it to do under “content.”

Video attached below of what’s happening!

External Media

for the similar functionality, I used gemini, which is or was free but had some limits. for your problem i would check if player’s message contains some key words like “1000 words” and just don’t process that message using TCS

But then what if the player asked it for 999 words? or 1001 words?

There is a token limit parameter you can put into your request but I’d advice just looking it up on OpenAI’s docs, as this isn’t exactly a roblox problem and I can’t help with specifics

Use a hosted Ollama instance so that you aren’t being charged up the rear for a piss-poor LLM (ChatGPT). Ollama lets you choose any specific model you want to use and for a roblox game you genuinely do not need a really good one, just a basically knowledgeable one.

Use string patterns to look for numbers in a string, then search for other keywords such as “word” or “wrds”, use some simple logic to determine if the number(s) have any relation to a keyword.

I’m still new to Open AI, where would I input a token limit parameter in my request? Would that be in Lua? Or the Open AI website itself?

I could just extract all string values from the player’s chat message and put it all into a table, and run a loop looking for any numbers above 60 or something, as an example if I want a max word cap of 60

I have figured out a solution! It’s almost perfect, the AI model just doesn’t finish it’s sentence, I wish there was a way to limit max tokens, and so the AI model would take that into consideration and try to make a full sentence within the provided token limit, but this is fine and works just fine for me!

The thing that you are trying to achieve here is something that Roblox is currently working on (Generative Text API) which should be coming by the end of this year from what I am aware of and you should be able to enroll into testing it (as long as that is not closed yet) as that method should be easier and better customisable. Either way, nice script

Edit: I’ve just beeen seeing what people in the generative text API testing has been saying and apparently because chatgpt can be so easily bypasses in terms of filtering if players can use it to say anything then it could get the game banned so just be careful.

Don’t worry about that, I’ve implemented Roblox’s chat filter system into my AI system, so before showing the text to the player, it undergoes the text filter module, and will be hash-tagged if Roblox doesn’t like what my Open AI model said lol

1 Like

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