Endpoint definition

Can anyone help me with what the endpoint for this script should be?

Chat GPT is telling me - The endpoint for the generate resource of the GPT-3 API is https://api.openai.com/v1/text-davinci-002/generate.

Bu that isnt working and I cant find an answer in the help docs and chatGPT keeps telling me that its correct

?

function generateDescription(name, category, description) { const prompt = Describe the business “${name}” that operates in the “${category}” category. ${description}; const apiKey = "YOUR_API_KEY"; const endpoint = "YOUR_ENDPOINT"; const model = "text-davinci-002"; const temperature = 0.5; const maxTokens = 700; const input = { apiKey, prompt, temperature, maxTokens, model }; const options = { method: "POST", headers: { "Content-Type": "application/json" }, payload: JSON.stringify(input) }; const response = UrlFetchApp.fetch(endpoint, options); const result = JSON.parse(response.getContentText()); return result.text; }

ChatGPT will “hallucinate” and happily give you non-facts as if they were facts. Best to stick with the docs. They’ve got examples for most modern languages …

Docs…

API docs…

Sounds like you want completions, which would be…

post https://api.openai.com/v1/completions

Then you would need to pass it a model like text-davinci-003.

Hope this helps.

(And welcome to the community!)

Thank you. I tried updating but still getting an error.

Image 2023-01-04 at 10.18.16 am

One of the problems is that you don’t post the apiKey as part of the POST. It needs to be in the header, like the content-type with the bearer text etc.

The JSON.stringify(input) may not be in the right format. Once you remove the apiKey from the input, output the stringify’ed text so we can what you end up with

Hopefully it includes the labels with the values. If it doesn’t that will be an issue too (Sorry I cant test stringify on my machine right now)

If you are lucky, removing the apiKey and putting it in the header (in the correct format) will fix your problem

1 Like
    headers: {
                Authorization: `Bearer ${api_key}`,
                "Content-Type": "application/json",
            },

https://beta.openai.com/docs/api-reference/authentication

Also see…

1 Like

Thank you both for your help. I had tried the Authorization header already but this is all a bit above my head as far as coding goes. I’ll have to find some help from somewhere that I can jump on a screen share with someone. Thank you both. :+1:t2:

1 Like

No problem. You may want to look for an API wrapper on GitHub (like the one I linked…)

Good luck!