[HELP]Garry's Mod Lua API use: invalid_request_error

Hi, I am trying to create a addon for Garry’s mod using the API in Lua. I keep getting the Error:

"error": {
        "message": "you must provide a model parameter",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }

My Code:

print("AI Loaded!")
timer.Simple( 5, function()
    local url = "https://api.openai.com/v1/chat/completions"
    local headers = {
        ["Content-Type"] = "application/json",
        ["Authorization"] = "Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    }

    local body = {
        ["model"] = "gpt-3.5-turbo", 
        ["messages"] = {
            ["role"] = "user", 
            ["content"] = "Hello!"
        }
    }

    http.Post(url, body, 
        function( body, length, headers, code )
            print("AI RESPONSE " .. body)
        end,
        function( message )
            print("AI FAILURE " .. message)
        end,
        headers)
end)

If I am not mistaken, the body param should be as follows for davini, (an example only):


local body = {
    model = "text-davinci-003",
    prompt = "Lua is a programming language that can be used to create games and other applications. ",
    temperature = 0.5,
    max_tokens = 50,
}

You can change prompt to messages for turbo format in the same general Lua format, since the above example is for davinci and not for turbo.

HTH

:slight_smile:

I still seem to be getting the same error:

{
    "error": {
        "message": "you must provide a model parameter",
        "type": "invalid_request_error",
        "param": null,
        "code": null
}

Here’s my new code:

print("AI Loaded!")
timer.Simple( 5, function()
    local data = {
    model = "text-davinci-003",
    prompt = "Lua is a programming language that can be used to create games and other applications. ",
    temperature = 0.5,
    max_tokens = 50,
}

    HTTP({
        url = "https://api.openai.com/v1/completions",
        method= "POST", 
        headers= { 
            ["Content-Type"] = "application/json",
            ["Authorization"] = "Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        },
        success= function( code, body, headers ) 
            print("IT WORKED " .. body)
        end, 
        failed = function( err ) 
            print("IT DIDNT WORK " .. err)
        end,
        body=util.TableToJSON(data)
    })
end)


I am not a Lua person, @thattrollagen

Having said that, in your “new code” i do not see where you are passing the params to the http method.

Are you using ChatGPT to writw code for you?

:slight_smile:

i thought I was pass the Parameters:

local data = {
    model = "text-davinci-003",
    prompt = "Lua is a programming language that can be used to create games and other applications. ",
    temperature = 0.5,
    max_tokens = 50,
}

into the body as a json object?

body=util.TableToJSON(data)
1 Like

Ah OK.

Sorry, I am not a Lua programmer so I will refrain from showing how little I know about Lua.

Hopefully, we have Lua programmers here who can further assist.

:slight_smile:

1 Like