VolkanSah / GPT-API-Integration-in-HTML-CSS-with-JS-PHP

A basic GPT conversation script designed to help you learn to interact with OpenAI's GPT technology. Includes best practices and a free security whitepaper.
https://github.com/VolkanSah/GPT-API-Integration-in-HTML-CSS-with-JS-PHP
MIT License
76 stars 55 forks source link

What API_ENDPOINT URL do you mean? #17

Closed j0k closed 1 year ago

j0k commented 1 year ago

According to gptchat.php I need to change "API endpoint URL". In original gptchat.php code line after this comment is

curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/engines/" . MODEL . "/completions");

Questions: 1) Does "CURLOPT_URL" is the same as "API endpoint URL" term? 2) According to readme.md ENDPOINT for gpt-3.5-turbo model is /v1/chat/completions. Where is exact API endpoint URL substring in gptchat.php? It looks like it mixed with MODEL in endpoing URL. Any suggestions?

Thanks

VolkanSah commented 1 year ago

Hello

Thank you for your question. The "API endpoint URL" is the endpoint to which you're sending your requests. In the snippet of code you've mentioned, the endpoint is "https://api.openai.com/v1/engines/" . MODEL . "/completions".

The CURLOPT_URL option that you set with curl_setopt is indeed the URL to which cURL is sending the request, so yes, that's essentially the "API endpoint URL".

Depending on the model you're using, you may need to change this endpoint. For instance, if you're using the gpt-3.5-turbo model, the endpoint should be "https://api.openai.com/v1/chat/completions".

The MODEL constant in your code should be replaced with the name of the model you're using. It seems like you have already defined this in your gptchat.php file.

This means if you're using the gpt-3.5-turbo model, your URL should look like this: "https://api.openai.com/v1/chat/completions".

Hopefully, that answers your question. Please remember to thoroughly read through the documentation as it can clarify many of your questions. Don't hesitate to ask further questions if you still have doubts. A :star: will support this project, do not forget :smile: Best Regards, Volkan Sah

incircolo commented 1 year ago

Me too i don't understand why you use this endpoint format "https://api.openai.com/v1/engines/" . MODEL . "/completions"

ie. i'm using gpt-3.5-turbo model (defined in config.php)

your code is

    curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions" . MODEL . "/completions");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
        "prompt" => $message,
        "max_tokens" => MAX_TOKENS,
        "temperature" => TEMPERATURE,
        "top_p" => TOP_P,
        "frequency_penalty" => FREQUENCY_PENALTY,
        "presence_penalty" => PRESENCE_PENALTY

why don't you use it instead, adding model in postfields ?

    curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
        "model" => MODEL,
        "prompt" => $message,
        "max_tokens" => MAX_TOKENS,
        "temperature" => TEMPERATURE,
        "top_p" => TOP_P,
        "frequency_penalty" => FREQUENCY_PENALTY,
        "presence_penalty" => PRESENCE_PENALTY

Thanks for your code.

VolkanSah commented 1 year ago

Thank you for your inquiry and your interest in this code. You raise a valid point about specifying the model within the postfields rather than embedding it into the URL. Both methods are viable and may suit different developers' needs and preferences.

In the provided code, I've chosen to incorporate the model into the URL to meet my specific needs, particularly when using various modules. However, your suggested approach is equally correct and may be more suitable depending on the context.

Please remember that it's essential to thoroughly read the documentation before working with the script. It contains a wealth of valuable information and examples, including for the GPT-3.5 Turbo. Please don't hesitate to ask further questions if you still have doubts—I'm here to assist you.

I'd like to point out the ChatGPT Security Best Practices guide that is placed at the very beginning of the README. This guide includes the exact method you are suggesting and provides a comprehensive guide to the most secure and efficient use of the ChatGPT API. It is crucial to familiarize oneself with these practices before moving forward.

Lastly, please understand that the Issues section on this GitHub repo is not intended to be a chatroom. For detailed discussions and inquiries, I kindly ask you to use the dedicated Discussions tab provided. This helps keep things organized and ensures all questions are adequately addressed.

I hope this clarifies things. Best of luck as you continue working with this code!