Trying to connect to openai api?

I am trying to use Network Actions

Is it posibble? Or do i have to use a PHP in between…

All i can get is an error response

invalid url (POST …
“type”: “invalid_request_error”

Any ideas?

Auth need to be send with “HEAD” but i can’t use HEAD method i guess… am i right?
Only post and get methods can be used for now in gdevelop…

:wave: Hi and welcome here :slight_smile:
Looks like HEAD is one of the available choices.
image

Did you pay attention to this?

Hi again…

i don’t think cross domin is the issue since there is a API to answer to all requests.

There’s the CURL example…

curl https://api.openai.com/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "text-davinci-003",
  "prompt": "count to 10",
  "temperature": 0.7,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'

Which as a responce will give 1,2,3,4,5,6,7,8,9,10
as an answer…

and the same code as javascript can be:

fetch(‘https://api.openai.com/v1/completions’, {
method: ‘POST’,
body: JSON.stringify({
model: ‘text-davinci-003’,
prompt: ‘count to 10’,
temperature: 0.7,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
}),
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${OPENAI_API_KEY}
}
});

Not really sure who your second response is in reply to. As Greench mentioned above, HEAD calls are viable with network commands.

Note that CURL calls have nothing to do with cross domain because they run from a workstation or server, not a webservice. if you are running a GDevelop game locally/from an EXE it isn’t going to be treated as cross domain.

If you are running it from a web host as an HTML5 game, CORS/cross domain requirements will matter though as they are activating via the user’s browser. It is browsers and webservers/webservices that disallow CORS, not necessarily the game engine.

Thanks for the additional information… But i solved it with the help of Javascript. Now i can get response from Openai into Gdevelop.

var ret = fetch('https://api.openai.com/v1/completions', {
  method: 'POST',
  body: JSON.stringify({
    model: 'text-davinci-003',
    prompt: 'count to 10',
    temperature: 0.7,
    max_tokens: 2000,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0
  }),
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer YOUR API KEY HERE`
  }
});

ret.then((response) => response.json())
    .then((responseJSON) => {
       // do stuff with responseJSON here...
       objects[0].setString(JSON.stringify(responseJSON)+"++");
    });


objects[0].setString("Started request");

Here is the code that i use if anyone interested…

1 Like

Hi kutlus,

I am very interested in how you managed to integrate the API into gdevelop.

Could you explain a bit more in detail how to do it?
I have never coded in JS.

Thank you very much for the time you will take to answer me.