How do you implement OpenAI GPT-3 Api Client in PHP?
Asked Answered
P

2

5

I need help understanding the vague instructions on https://packagist.org/packages/orhanerday/open-ai

I downloaded the package from https://github.com/orhanerday/open-ai

I installed the package by running "composer require orhanerday/open-ai" in my Command Prompt

Instructions stop making sense from there.....

What does the "use Orhanerday\OpenAi\OpenAi;" code mean and where is it applied? Am I to create a php file say index.php with content:

<?php

use Orhanerday\OpenAi\OpenAi;

$complete = $open_ai->complete([

   'engine' => 'davinci',
   'prompt' => 'Hello',
   'temperature' => 0.9,
   'max_tokens' => 150,
   'frequency_penalty' => 0,
   'presence_penalty' => 0.6,
]
?>

how and where do I add my api key? Do I create a file Orhanerday\OpenAi\OpenAi.php and enter my api key there? i.e. OPENAI_API_KEY=sk-**********************************************

Presbyterate answered 21/9, 2022 at 2:30 Comment(1)
What you call “vague instructions” are just PHP namespaces. From the link you provided, the key is passed to the constructor which is omitted in your exampleLazaro
B
9

You should define the $open_ai variable as an OpenAI object by passing your private KEY value. for example; new OpenAi('Your-OPENAI-KEY');

Complete code;

<?php


use Orhanerday\OpenAi\OpenAi;

$open_ai = new OpenAi('OPEN-AI-KEY');// <- define the variable.

$complete = $open_ai->complete([
    'engine' => 'davinci',
    'prompt' => 'Hello',
    'temperature' => 0.9,
    'max_tokens' => 150,
    'frequency_penalty' => 0,
    'presence_penalty' => 0.6,
]);

I also added the Quick Start Part to orhanerday/OpenAI readme.

Bartholomeo answered 30/9, 2022 at 5:9 Comment(0)
R
0

First you have you include thé 'autoload'file. 'use' doesn't mean that you have to create a file yourself.

Radloff answered 11/1, 2023 at 7:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sordid

© 2022 - 2024 — McMap. All rights reserved.