4

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-**********************************************

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Overstack
  • 39
  • 1
  • 1
  • 5
  • What you call “vague instructions” are just [PHP namespaces](https://www.php.net/manual/en/language.namespaces.php). From the link you provided, the key is passed to the constructor which is omitted in your example – Chris Haas Sep 21 '22 at 04:26

2 Answers2

8

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.

ORHAN ERDAY
  • 1,020
  • 8
  • 31
0

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

  • 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. – Nuriddin Rashidov Jan 14 '23 at 20:50