How would I be able to get responses back from HuggingFace's AI?

Hello! I’m here to ask if there was a way for me to return back a text response from HuggingFace’s AI models. I’ve looked into this all day and all of them seems to be outdated. I’m aware that I need to use an API key. However all responses specifically being used from HuggingFace returns a 404 error. I specifically want to use HuggingFace due to it’s generous rate limit for free users. I also do not want to use ROBLOX’s text generation AI due to it’s extreme restricted filter and forcing you to set your game’s rating to moderate/restricted.

local HttpService = game:GetService("HttpService")
local Bearerkey = "Bearer (obviously not going to show my API key here)"

function AskQuestion(inputText)
	-- Import the HttpService


	-- Define the API URL
	local API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-base"

	-- Define the headers with your authorization key
	local headers = {
		["Authorization"] = Bearerkey
	}

	-- Define a function to query the API with a payload
	local function query(payload)
		-- Encode the payload as a JSON string
		local jsonPayload = HttpService:JSONEncode(payload)
		-- Send a POST request to the API URL with the headers and the payload
		local response = HttpService:PostAsync(API_URL, jsonPayload, Enum.HttpContentType.ApplicationJson, false, headers)
		-- Decode the response as a JSON table
		local jsonResponse = HttpService:JSONDecode(response)
		-- Return the JSON table
		return jsonResponse
	end

	-- Define your input text

	-- Query the API with your input text as the inputs field
	local output = query({
		["inputs"] = inputText
	})
	local generatedText = output[1].generated_text
	-- or
	local generatedText = output[1]["generated_text"]
	return generatedText
	-- Print the output
end

print(AskQuestion("Hello, How are you?"))

Also no matter what model I use it still will return a 404 error, sometimes a 401.

For API keys you can use that btw

I think it have to do with HttpService fails Or you have to run it on Roblox player order to get work?

(if this makes no sense or seems all over the place just skip to the end, i was really REALLY tired writing this)
i looked into their api here and your request is wrong. first, you should be using RequestAsync instead of post async, the url for completion requests is https://router.huggingface.co/v1/chat/completions, and the body needs to contain messages, an array of arrays, stream which i have a feeling might be optional, and model, a string. for the model i just used the first thing i saw on the api page, openai/gpt-oss-120b:fireworks-ai, but you can switch it if you want. the local query function should look something like:

local function query(payload)
		local response = HttpService:RequestAsync({
			Url = url,
			Method = "POST",
			Headers = headers,
			Body = HttpService:JSONEncode(payload),
		})
		return HttpService:JSONDecode(response.Body).choices[1].message.content
	end

since we’re using RequestAsync now, and you need to specify the Content-Type header now too, so the headers variable should look like this:

local headers = {
		["Authorization"] = "Bearer "..Bearerkey,
		["Content-Type"] = "application/json",
	}

next you need to fix the query call to use the correct keys, which are messages, model and stream you might not actually need to specify stream, but hugging face’s api documentation is bad so i just assumed it was true by default
so the updated call should be:

local output = query({
		["messages"] = {
			{
				["role"] = "user",
				["content"] = inputText,
			}
		},
		["model"] = "openai/gpt-oss-120b:fireworks-ai",
		["stream"] = false,
	})

also this version of the query call wont work with vlm’s because they require more data in the content section, so if you ever get a ‘bad request’ error after switching models, it might be because of that. so altogether the completed script should look something like this:

local HttpService = game:GetService("HttpService")
local Bearerkey = "redacted"--dont add bearer here
local url = "https://router.huggingface.co/v1/chat/completions"

function AskQuestion(inputText)

	local headers = {
		["Authorization"] = "Bearer "..Bearerkey,
		["Content-Type"] = "application/json",
	}
	
	local function query(payload)
		local response = HttpService:RequestAsync({
			Url = url,
			Method = "POST",
			Headers = headers,
			Body = HttpService:JSONEncode(payload),
		})
		return HttpService:JSONDecode(response.Body).choices[1].message.content
	end

	
	local output = query({
		["messages"] = {
			{
				["role"] = "user",
				["content"] = inputText,
			}
		},
		["model"] = "openai/gpt-oss-120b:fireworks-ai",
		["stream"] = false,
	})
	return output
end

print(AskQuestion("what element number is mercury?"))

running it should give an output similar to this
image

this took me so long to figure out (hugging face has awful documentation) and you dont know how happy i was when i finally got it to respond. also like @Yarik_superpro said you should probably use secrets for Bearerkey, and again i was super tired writing this whole thing so hopefully its not too confusing or random

2 Likes

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