PaLM API json not workings

83 views Asked by At

ok... i solve my problem. the problem is on my self. i forgot to add the

return response

which is stupid on my side lol

here's the fixed code

--google palm--
--roblox lua

local Players = game:GetService("Players");
local HttpService = game:GetService("HttpService");
local LocalPlayer = Players.LocalPlayer;
local RequestFunctiom = request;
local function MakeRequest(Prompt)
    local success, response = pcall(function()
        return RequestFunctiom({
           Url = "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=KEY_HERE",
            Method = "POST",
            Headers = {
                ["Content-Type"] = "application/json",
            },
            Body = HttpService:JSONEncode({
            prompt = {
              text = Prompt
            },
            temperature = 0.7,
            candidateCount = 1,
            topP = 0.95,
            topK = 5,
            maxOutputTokens = 100,
            stopSequences = {"Human:", "Ai:"},
            })
        })
    end)
    print(response)
    return response
end
print("MakeRequest sending")
print("test1")
local HttpRequest = MakeRequest("Human: Hello how are you today \n\nAI:");
--wait(3) -- if this added the local Response will errored the error is "attempt to index nil with the Body"
local Response =  ". " .. string.sub(HttpService:JSONDecode(HttpRequest["Body"]).candidates[1].output, 2);
print(Response)
2

There are 2 answers

9
Linda Lawton - DaImTo On

I'm not sure what language that is but the proper JSon body for the request should be something like this

You defiantly have prompt and stop_sequences wrong.

{
  "prompt": "Write a poem about an adventure in an underwater castle\n\n",
  "model_name": "models/text-bison-001",
  "temperature": 0.7,
  "candidate_count": 1,
  "top_k": 40,
  "top_p": 0.95,
  "max_output_tokens": 1024,
  "stop_sequences": []
}

candidate_count

you have candidate_count set to three which means that HttpService:JSONDecode(response.Body).candidates is an array.

It may be failing on that

0
Random On

fixed version

answered my own question. i feel dumb

--google palm--
--roblox lua

local Players = game:GetService("Players");
local HttpService = game:GetService("HttpService");
local LocalPlayer = Players.LocalPlayer;
local RequestFunctiom = request;
local function MakeRequest(Prompt)
    local success, response = pcall(function()
        return RequestFunctiom({
           Url = "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=KEY_HERE",
            Method = "POST",
            Headers = {
                ["Content-Type"] = "application/json",
            },
            Body = HttpService:JSONEncode({
            prompt = {
              text = Prompt
            },
            temperature = 0.7,
            candidateCount = 1,
            topP = 0.95,
            topK = 5,
            maxOutputTokens = 100,
            stopSequences = {"Human:", "Ai:"},
            })
        })
    end)
    print(response)
    return response
end
print("MakeRequest sending")
print("test1")
local HttpRequest = MakeRequest("Human: Hello how are you today \n\nAI:");
--wait(3) -- if this added the local Response will errored the error is "attempt to index nil with the Body"
local Response =  ". " .. string.sub(HttpService:JSONDecode(HttpRequest["Body"]).candidates[1].output, 2);
print(Response)