#API on php - return error please HELP

63 messages · Page 1 of 1 (latest)

terse osprey
#

Hi ! Could somebody help me please ? I want to do the FIRST api test php .. I think i have been installed Composer and orhanerday/open-ai

but I have :

{ "error": { "message": "Invalid URL (POST /v1/generations)", "type": "invalid_request_error", "param": null, "code": null } }

Code :

<?php

// Inclure les fichiers nécessaires
require_once '../vendor/autoload.php';

// Définir les paramètres de l'API
$api_key = "sk-MY API";
$model = "NOM_DU_MODELE";

// Préparer les données à envoyer à l'API
$data = [
'model' => $model,
'prompt' => "Entrez votre prompt ici",
'length' => 20,
'temperature' => 0.5,
];

// Envoyer une requête POST à l'API
$curl = curl_init("https://api.openai.com/v1/generations");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer $api_key"
]);

// Récupérer la réponse de l'API
$response = curl_exec($curl);

// Vérifier si la requête a réussi
if ($response === false) {
// La requête a échoué, afficher l'erreur
echo "Erreur : " . curl_error($curl);
} else {
// La requête a réussi, décoder la réponse et afficher le texte généré
$response_data = json_decode($response, true);
echo $response_data['data'][0]['text'];
}

// Fermer la connexion cURL
curl_close($curl);
?>

So I tried oficial php code ... blank ...

Thanks for help !!

ivory ibex
#

There is also no such field named “length”, and you also must provide a max_tokens parameter

terse osprey
#

HI, thanks, done but :

#

{ "error": { "message": "That model does not exist", "type": "invalid_request_error", "param": null, "code": null } }

#

with :

#

// Préparer les données à envoyer à l'API
$data = [
'model' => $model,
'prompt' => "Entrez votre prompt ici",
'temperature' => 0.5,
'max_tokens' => 150,

];
ivory ibex
#

Change your nom de module to “text-davinci-003”

terse osprey
#

$data = [
'model' => $model,
'prompt' => "Entrez votre prompt ici",
'temperature' => 0.5,
'max_tokens' => 150,

];

// Envoyer une requête POST à l'API
$curl = curl_init("https://api.openai.com/v1/completions");
curl_setopt($curl, CURLOPT_POST, true);

#

Done

#

now I have

#

{"id":"cmpl-6SCmVv4Y0pQxNTZGj91ANubHJTSRy","object":"text_completion","created":1672180335,"model":"text-davinci-003","choices":[{"text":"\n\n$","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":7,"completion_tokens":3,"total_tokens":10}}

ivory ibex
#

It looks like it succeeded!

#

Now, change your prompt to something else

#

For example set your prompt to be “The capital of France is:”

#

And see what it responds

#

The davinci reply is contained in choices[0].text, as you can see in your response

terse osprey
#

{"id":"cmpl-6SCoBM8jM6zbkkoojbJCgHsp2rPKX","object":"text_completion","created":1672180439,"model":"text-davinci-003","choices":[{"text":"\n\nParis","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":6,"completion_tokens":3,"total_tokens":9}}

ivory ibex
#

now you can see that the “text” field says Paris

#

You’re all set to go, it is correctly completing your prompts!

terse osprey
#

Good !

#

But It is not what i would like to do !

#

I'd like to generate a text

#

from a subiect ...

ivory ibex
#

Well you need to adjust your prompt, to get it to do what you want

terse osprey
#

with ada

ivory ibex
#

your code works, the rest is adjusting the prompt

#

oh

#

with Ada?

terse osprey
#

yes

ivory ibex
#

You want to generate embeddings?

#

Ada is for embeddings

#

not GPT

terse osprey
#

i don t know what is embeddings

#

i want to give to the api an order, or a subjiect, like : "tell me the story of universe"

#

and the api return a text with the story ...

ivory ibex
#

You already have the code to do that.

#

Like I said, you need to edit your prompt.

#

Change your prompt to "Tell me the story of universe."

#

and see what it returns

terse osprey
#

I just tried it works fine !

ivory ibex
#

Perfect, so now you can just change the prompt to what you want and it will give you stories

terse osprey
#

very fine

#

can you help me more for a setting please ?

ivory ibex
#

Yeah what do you need help with?

terse osprey
#

{"id":"cmpl-6SCyKaEDVlKkAk2SpcF8UvxXt0AFM","object":"text_completion","created":1672181068,"model":"text-davinci-003","choices":[{"text":"\n\nThe environment is fragile and must be protected for future generations.","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":14,"total_tokens":23}}

#

I want to get "The environment is fragile and must be protected for future generations." into a $variable

ivory ibex
#

I don't remember how to do this in php, but basically, you need to access the response dictionary, then access "choices" at array index 0, then access "text".

terse osprey
#

So I understand i have to use davinci with 0.02$/1k token for what i need ? I can't use Ada, ore Babbage or Curie to do this job ?

ivory ibex
#

It is a JSON response

ivory ibex
#

For ada, change the nom de model to "text-ada-001"

#

but ada is very underpowered

terse osprey
#

yes i see it !

#

For Babbage and Curie what I have to change ?

ivory ibex
terse osprey
#

ok thanks

#

I think the best code was

#

But does not works

ivory ibex
#

yeah not sure, never used that beofre

terse osprey
#

Ok thanks you so much !