Asking for help connecting to the AI api

I have a site where I want to use the AI API to give the AI a book’s information and write a full description of that book for me (like a small article to publish on the site).

Is it free to use chatgpt API to do this?

Where should I buy an AI API key? How do I buy it? Which AI model should I use to write the best articles for my site?

Hi and welcome to the Developer Forum!

If you visit platform.openai.com you will be able to create an account there, you can use the AI for free if your usage is low, otherwise you can add a payment card to your account and increase your usage limits. A prepay credit account will grant access to GPT-3.5 and GPT-4 models for you to experiment with.

1 Like

I have created an account. I connected the api key to a php code, which I put the code source below.
https://github.com/Radulepy/PHP-ChatGPT/blob/main/index.php

But when running the code, I get this error:
array(1) { ["error"]=> array(4) { ["message"]=> string(76) "You exceeded your current quota, please check your plan and billing details." ["type"]=> string(18) "insufficient_quota" ["param"]=> NULL ["code"]=> string(18) "insufficient_quota" } }

I never managed to generate a text using the API, and I received a 401 error using other packages as well.
https://github.com/tectalichq/public-openai-client-php/discussions/37

Is there a way to test the API?

That is indicating you need to add a payment method to your account here

2 Likes

How much do I have to pay exactly? Because I want to get a credit card according to that cost

You can get started with a $5 minimum payment.

1 Like

You can see the cost per 1000 tokens, which are the encoded language that goes in and out of the AI model.

1 Like

I charged my account $10 to test it first. To begin with, I plan to produce 100,000 articles, each of which is about 700 words in the field of books.

Which model should I use that produces the quality of the text and the price is suitable?

What pattern should I give it so that it can produce complete and accurate explanations for me? (I don’t know much about patterns)

Give gpt-3.5-turbo a try, you’ll need to experiment with 3.5 and 4 to decide which one you like the result from more, GPT-4 is more expensive but often has better quality results.

1 Like

Let’s see how well your very language can be used to tell gpt-3.5-turbo what you want.

API programming:

system role message:

You are a writing assistant.

user role message

I have a site where I want to use the AI API to give the AI a book’s information and write a full description of that book for me (like a small article to publish on the site).
Write this book description.
-–
Book: Publication Manual of the APA (7th Edition)
Summary: The Publication Manual of the APA is one of the most recognized style manuals for writers, editors, scholars, and students. The book is user-friendly, and that’s why our customers love the book! It’s no wonder why it’s one of the top selling textbooks.
Textbook’s ISBN: 9781433832161
By American Psychological Association – APA

AI response output:

Introducing the Publication Manual of the APA (7th Edition), a highly esteemed style manual that has become a staple for writers, editors, scholars, and students alike. With its user-friendly approach, this book has garnered immense popularity among our customers, making it a top-selling textbook in its field.

The Publication Manual of the APA serves as an indispensable guide, offering comprehensive instructions on writing, formatting, and citing sources in accordance with the American Psychological Association’s guidelines. Whether you are a seasoned writer or a novice, this manual provides the necessary tools to enhance your writing skills and ensure adherence to the highest standards of academic integrity.

This seventh edition of the manual builds upon its predecessors, incorporating the latest updates and advancements in the field of academic writing. It equips readers with the knowledge to effectively communicate their ideas, present research findings, and craft scholarly papers that meet the rigorous requirements of the APA style.

With its clear and concise explanations, the Publication Manual of the APA empowers users to navigate the complexities of citation and referencing effortlessly. It offers invaluable insights into proper citation formats, enabling writers to avoid plagiarism and give credit where it is due. This meticulous attention to detail is what sets this manual apart and makes it an indispensable resource for anyone engaged in scholarly writing.

The textbook’s ISBN…

(temperature=0.5)

  • The AI may know the book and simply be able to use its own knowledge to write the description.
  • If not, it may fill in the description with plausible but not quite true things.
1 Like

I want to write a PHP code that sends a text to gpt-3.5-turbo and gets the result. For example, send the information of a book and receive explanations about that book

I used the following package:
https://packagist.org/packages/haozi-team/chatgpt-php?query=chatgpt

But after running it displays this error:
Fatal error: Uncaught Tectalic\OpenAi\ClientException: Unsuccessful response. HTTP status code: 401 (Unauthorized). in /private/var/www/chatgpt/vendor/tectalic/openai/src/Handlers/ChatCompletions.php:150 Stack trace: #0 /private/var/www/chatgpt/index.php(21): Tectalic\OpenAi\Handlers\ChatCompletions->toModel() #1 {main} thrown in /private/var/www/chatgpt/vendor/tectalic/openai/src/Handlers/ChatCompletions.php on line 150

Is there a solution to my problem? Or can you introduce me another way to do this?

What’s your code look like?

There’s two endpoints currently…

1 Like
require 'vendor/autoload.php';

$openaiClient = \Tectalic\OpenAi\Manager::build(
    new \GuzzleHttp\Client(),
    new \Tectalic\OpenAi\Authentication(getenv('sk-xxxxxxxxxxxxxxxxxx'))
);

/** @var \Tectalic\OpenAi\Models\ChatCompletions\CreateResponse $response */
$response = $openaiClient->chatCompletions()->create(
    new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest([
        'model' => 'gpt-3.5-turbo',
        'messages' => [
            [
                'role' => 'user',
                'content' => 'Give me a complete explanation about the book Clean Code written by Robert Martin'
            ],
        ],
    ])
)->toModel();

echo $response->choices[0]->message->content;

My api key is absolutely correct and I already tested it on several platforms and it works correctly

Now I need a code that can send a text or pattern to AI and get a full description like an article

7 months ago README.md ChatGPT PHP SDK | Package Official and Reverse Engineered ChatGPT API for PHP.

I would try using one of the officially supported PHP libraries…

PHP

https://platform.openai.com/docs/libraries/community-libraries

1 Like

You are unauthorized.

This is the use example of that library:

new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY'))

It seems either you: get the key from the environment, or you don’t use getenv if you want to hard-code a string.

This would look for an environment variable named the same your key to see what’s stored inside…

getenv('sk-xxxxxxxxxxxxxxxxxx')
1 Like

Good catch.

I’d still recommend an officially sanctioned library rather than the one from /haozi-team/that hasn’t been updated in a while…

1 Like

Plus nobody says you have to use the same environment variable as every other production AI server just waiting for remote code execution.

rand_seed1=UqRvK1GwS2XcZ3YtPmLo
rand_seed2=hA5bNf8sWnYzDjKpRtQv

1 Like

I used the following library:
https://packagist.org/packages/orhanerday/open-ai

I ran the following code:

require __DIR__ . '/vendor/autoload.php';

use Orhanerday\OpenAi\OpenAi;

$open_ai_key = getenv('sk-xxxxxxx');
$open_ai = new OpenAi($open_ai_key);

$text = $open_ai->chat([
    'model' => 'gpt-3.5-turbo',
    'prompt' => 'Give me a complete explanation about the book Clean Code written by Robert Martin',
    'temperature' => 1.0,
    'max_tokens' => 1000,
    'frequency_penalty' => 0,
    'presence_penalty' => 0,
]);

$response = json_decode($text, true);
var_dump($response);

But I got the following error:
array(1) { ["error"]=> array(4) { ["message"]=> string(363) "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys." ["type"]=> string(21) "invalid_request_error" ["param"]=> NULL ["code"]=> NULL } }

While I have entered the API correctly! I have charged my account and I have 10 dollars credit!

You’re asking for an environmental variable named the API key as @_j mentioned.

1 Like

Same problem shown. Solution above.

$open_ai_key = 'sk-xxxxxxx';
2 Likes