API connection deliver error: you must provide a model parameter

I try to connect the API and my response looks like this:

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

But I provide a model parameter in my script. I checkalso some other different things out:

  • check dokumentation
  • check my API key, also create a new one and use it
  • also I implemet my organisation as parameter (by name or by ID) (because I define a organisation on the API Key site)
  • I ask chatGPT - this tool is really helpful - but everything looks fine
  • I also change my request to recive the model-list over API - this works perfect - so my API Key schould works

I don’t have any idea left, why my connection will not work and I got this error message ‘you must provide a model parameter’

Thats my script…

		$api_key = 'my-secret-API-key';
		$api_endpoint = 'https://api.openai.com/v1/completions';

		$api_params = array(
		'model' => 'text-davinci-003',
		'prompt' => 'Hallo? Das ist nur ein Test...',
		'temperature' => 0.9,
		'max_tokens' => 150,
		'frequency_penalty' => 0,
		'presence_penalty' => 0.6,
		'organization' => 'org-...my-organisation-ID'
		);

		$api_query = http_build_query($api_params);
		$iCurlTimeout = 120;
		$ch = curl_init();
		
		curl_setopt($ch, CURLOPT_URL, $api_endpoint);
		curl_setopt($ch, CURLOPT_POST, true);
		// curl_setopt($ch, CURLOPT_TIMEOUT, $iCurlTimeout);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $api_query);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		  "Content-type: application/x-www-form-urlencoded",
		  "Authorization: Bearer $api_key"
		));

		$api_response = curl_exec($ch);
		print "<pre>output: "; print_r($api_response); print "</pre>";

		curl_close($ch);

I hope, somebody have an idea!?

cooper64

You cant use http_build_query

This makes a url encoded string. You need a JSON string instead

1 Like

Thanks for your answer…

I still use http_build_query in my script and the JSON string looks fine in result.

In the meantime I try out to send the request via GET mothod. Same result - error message ‘you must provide a model parameter’ - it’s really crazy

also my http-header looks fine:

    [url] => https://api.openai.com/v1/completions?model=text-davinci-003&prompt=Hallo%3F+Das+ist+nur+ein+Test...&temperature=0.9&max_tokens=150&frequency_penalty=0&presence_penalty=0.6&organization=org-my-company-ID
    [content_type] => application/json; charset=utf-8
    [http_code] => 400
    [header_size] => 246
    [request_size] => 366
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.40087
    [namelookup_time] => 0.000754
    [connect_time] => 0.125174
    [pretransfer_time] => 0.252158
    [size_upload] => 0
    [size_download] => 167
    [speed_download] => 417
    [speed_upload] => 0
    [download_content_length] => 167
    [upload_content_length] => -1
    [starttransfer_time] => 0.400848
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => xxx.xxx.xxx.xxx
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => xxx.xxx.xxx.xxx
    [local_port] => 42238
    [http_version] => 3
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 252067
    [connect_time_us] => 125174
    [namelookup_time_us] => 754
    [pretransfer_time_us] => 252158
    [redirect_time_us] => 0
    [starttransfer_time_us] => 400848
    [total_time_us] => 400870

I don’t see any mistakes…

cooper64

Are you saying that $api_query is a JSON string?

If this is PHP, I thought it would return something like

model=text-devinci-003&prompt=Hallo?+Das+ist+nur+ein+Test…&temperature=0.9

etc - which is not JSON

I would fully expect $api_repsonse to be JSON - no matter what you did in the code above.

I think you need json_encode instead

1 Like
[url] => https://api.openai.com/v1/completions?model=text-davinci-003&prompt=Hallo%3F+Das+ist+nur+ein+Test...&temperature=0.9&max_tokens=150&frequency_penalty=0&presence_penalty=0.6&organization=org-my-company-ID

I thougt it’s JSON conform, but now I doubt it. It’s maybe just URL string conform.
That might be a good tip from you, I’ll test that.

Thanks a lot.

1 Like

Great, it works now!! :smile:

I change:

$api_query = http_build_query($api_params);

to 

$api_query = json_encode($api_params);

and in that context also:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		  "Content-type: application/x-www-form-urlencoded",
		  "Authorization: Bearer $api_key"
		));

to 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		  "Content-Type: application/json",
		  "Authorization: Bearer $api_key"
		));

delete also my ‘organization’-param (course that was the next error)

and now I got an response…

Cool, thanks a lot :upside_down_face:

2 Likes

I had the same problem because I used GET ,not POST

2 Likes