Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The model won't print/return any results. #12

Closed
geosem42 opened this issue Jun 18, 2022 · 21 comments
Closed

The model won't print/return any results. #12

geosem42 opened this issue Jun 18, 2022 · 21 comments

Comments

@geosem42
Copy link

Hi there, I'm trying to set up a working example using Laravel but when I dd($complete) I get false.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Orhanerday\OpenAi\OpenAi;

class HomeController extends Controller
{
    public function index() {
        $open_ai = new OpenAi(env('OPEN_AI_API_KEY'));
        $engines = $open_ai->engines();

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

        $data = json_decode($complete, true);

        dd($engines);
    }
}

$open_ai returns a collection with engine, headers and contentTypes

$engines returns false as well, it's not even reading the library.

$data returns null

@orhanerday
Copy link
Owner

orhanerday commented Jun 18, 2022

I test on my local (Laravel v9.17.0 (PHP v8.1.6), and I didn't find any issue like that.

returned $engines
image
Returned $data
image

I don't recommend using dd, because they didn't have a friendly JSON format, return data then use JSON viewer instead of.

TROUBLESHOOTING

  • Did you install the package with composer require orhanerday/open-ai ?
  • Please check your OpenAI credit.
  • You should get an error when providing a wrong API key, just in case; check your API key twice.

@geosem42
Copy link
Author

I am stumped. I get a blank page when I return $data or return $engines.

Yes, I have installed orhanerday/open-ai and I have plenty of credits in my account.

But what's weird is that I am not getting an error when I put a wrong API key.

@orhanerday
Copy link
Owner

orhanerday commented Jun 18, 2022

Could you please run the following curl command?

curl https://api.openai.com/v1/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "text-davinci-002",
  "prompt": "Say this is a test",
  "max_tokens": 5
}'

You should see response like this;

{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-002",
  "choices": [
    {
      "text": "\n\nThis is a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ]
}

I just wanna check if you can easily access OpenAI servers. If you see any error notice me.

After that, I need your PHP, and laravel versions.
Also, I need to check (should returned 1) cURL exists in PHP, for that put the following code to your index function;

public function index(){
      return function_exists('curl_version');
...

@geosem42
Copy link
Author

geosem42 commented Jun 18, 2022

The curl command works in gitbash

curl

function_exists('curl_version') returns 1
Curl version is 7.79.1
Laravel version 9.17.0
PHP version 8.1.0

@orhanerday
Copy link
Owner

I didn't catch any reason for the bug. So I prepare another troubleshooting

  • lets pretty sure our curl is working. For this, put code your PHP file

// From URL to get webpage contents.
$url = "https://www.geeksforgeeks.org/";
 
// Initialize a CURL session.
$ch = curl_init();
 
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
//grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
 
$result = curl_exec($ch);
 
echo $result; 

Did you see the gfg page?

  • Also can you install laravel with docker?

@geosem42
Copy link
Author

I am still getting a blank page.

Very weird, because curl -V works.

I am downloading Docker Desktop but I've never used it before.

@orhanerday
Copy link
Owner

I think cURL is disabled in PHP, for enabling can you apply these steps?

  • Open php.ini (it’s usually in /etc/ or in the PHP folder on the server).
  • Search for extension=php_curl.dll. Uncomment it by removing the semi-colon( ; ) in front of it.
  • Restart the Apache Server.

@geosem42
Copy link
Author

Thank you for taking the time to help me out, I appreciate it.

extension=curl was uncommented, I changed it to php_curl and php_curl.dll but got the same blank page.

Also please note I am using wampserver and curl is enabled

wampserver

@orhanerday orhanerday changed the title Model returning false The model won't print/return results. Jun 18, 2022
@orhanerday
Copy link
Owner

Sorry about that, Let's try with Docker then. I'll be waiting for your response.

@geosem42
Copy link
Author

It finally worked! I got curl to finally work.

Thank you for your patience, I can't wait to see the limits of this library.

@orhanerday
Copy link
Owner

That's great to hear that! it's not boring, so I don't need any patience :). I feel glad while helping you guys.

Have a great day!

@orhanerday orhanerday changed the title The model won't print/return results. The model won't print/return any results. Jun 19, 2022
@silicahd
Copy link

silicahd commented Oct 8, 2022

return function_exists('curl_version');

How did you fix this issue? I have been going at it for about two days and I am getting the same thing on multiple systems.

@silicahd
Copy link

silicahd commented Oct 8, 2022

I didn't catch any reason for the bug. So I prepare another troubleshooting

  • lets pretty sure our curl is working. For this, put code your PHP file

// From URL to get webpage contents.
$url = "https://www.geeksforgeeks.org/";
 
// Initialize a CURL session.
$ch = curl_init();
 
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
//grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
 
$result = curl_exec($ch);
 
echo $result; 

Did you see the gfg page?

  • Also can you install laravel with docker?

I think there might be an issue, because the code you posted does not work. But this code works. Sorry for reopening this but i have not been able to get this to work for a few days now. works in terminal but not once i install it.

https://www.plus2net.com/php_tutorial/curl-demo.php

@silicahd
Copy link

silicahd commented Oct 8, 2022

I didn't catch any reason for the bug. So I prepare another troubleshooting

  • lets pretty sure our curl is working. For this, put code your PHP file

// From URL to get webpage contents.
$url = "https://www.geeksforgeeks.org/";
 
// Initialize a CURL session.
$ch = curl_init();
 
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
//grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_URL, $url);
 
$result = curl_exec($ch);
 
echo $result; 

Did you see the gfg page?

  • Also can you install laravel with docker?

This is so odd. The code you posted does not work.
$url = 'https://api.plos.org/search?q=title:DNA'; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer API CODE HERE' )); curl_setopt($ch, CURLOPT_URL,$url); $result=curl_exec($ch); curl_close($ch); return $result;

This code works just fine

        $url = 'https://api.plos.org/search?q=title:DNA';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER,     array(
            'Authorization: Bearer API CODE HERE'
            ));
        curl_setopt($ch, CURLOPT_URL,$url);
        $result=curl_exec($ch);
        curl_close($ch);
        return $result;

But if i change the URL to
https://api.openai.com/v1/models
and add my api it will return null

The odd thing is that i can test this via Postman or Curl terminal with no issues. I am sure this is not something on the code side but i just cant figure this out it baffles me.

@orhanerday
Copy link
Owner

orhanerday commented Oct 8, 2022

hey @silicahd, could you create an index.php and put following example.

<?php

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

use Orhanerday\OpenAi\OpenAi;

$open_ai = new OpenAi('sk-gjtv.....');

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

var_dump($complete);

can you share your response?

@silicahd
Copy link

silicahd commented Oct 8, 2022

hey @silicahd, could you create an index.php and put following example.

<?php

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

use Orhanerday\OpenAi\OpenAi;

$open_ai = new OpenAi('sk-gjtv.....');

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

var_dump($complete);

can you share your response?

I found the issue but cant figure out how to fix it. I add the line to get the error from Curl and it came back to "SSL certificate problem: certificate has expired" . My SSL is there so i am trying to figure out if we can do insecure so figure out why CURL gives this response.

@silicahd
Copy link

silicahd commented Oct 8, 2022

For anybody running into this if you are using something like aaPanel or others likely your certificate is expired.

Credit to this fix to the guys from the aaPanel forum.
@orhanerday thank you for the support and the amazing package.

mkdir -p /etc/pki/tls/certs wget -O /etc/pki/tls/certs/ca-bundle.crt https://curl.se/ca/cacert.pem --no-check-certificate chmod 444 /etc/pki/tls/certs/ca-bundle.crt

@orhanerday
Copy link
Owner

I'm so glad for everything solved out. Also thank you so much for good wishes.

@davidblanco-m
Copy link

davidblanco-m commented Jan 11, 2023

Hello,

Had the same issue, it seems to be related to the SSL certificate as @silicahd mentioned, I managed to get around it by adding the following flags in the $curl_info variable:

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,

at the end of the open-ai/src/OpenAi.php file

thank you @orhanerday for this amazing repo 👍

@Thefrenchdev
Copy link

Thefrenchdev commented Jan 18, 2023

Edit : Neevermind after 24hours it works

I'm having the same issue with Symfony.
here's my service.php :

public function getAnswer(string $question) : string
    {
        $open_ai_key = $this->parameterBag->get('OPENAI_API_KEY');
        $open_ai = new OpenAi($open_ai_key);
        $complete = $open_ai->completion([
            'model' => 'davinci',
            'prompt' => 'Hello',
            'temperature' => 0.9,
            'max_tokens' => 150,
            'frequency_penalty' => 0,
            'presence_penalty' => 0.6,        ]);
        $json = json_decode($complete, true);        
        var_dump($json);
        return $complete;
    }

@bdkoder
Copy link

bdkoder commented Feb 8, 2023

Hello,

Had the same issue, it seems to be related to the SSL certificate as @silicahd mentioned, I managed to get around it by adding the following flags in the $curl_info variable:

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,

at the end of the open-ai/src/OpenAi.php file

thank you @orhanerday for this amazing repo 👍

Thank you very much. It's saved me hours of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants