openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.6k stars 468 forks source link

Undefined array key "openai-processing-ms" #218

Closed DC-Sebastian closed 9 months ago

DC-Sebastian commented 9 months ago

I am trying to use the AI provider Anyscale, which provides Metas Llama-2 models and returns them in the same structure as Open AI. With the official Python Open AI library this is possible by simply setting the OPENAI_API_BASE URI to https://api.endpoints.anyscale.com/v1.

When I try to use the base uri api.endpoints.anyscale.com/v1 with openai-php I get the error message: Undefined array key "openai-processing-ms" in C:\xampp\htdocs\chatgpt\vendor\openai-php\client\src\Responses\Meta\MetaInformation.php on line 37

Any idea what could be causing this?

Here is the code I use for this: $client = OpenAI::factory() ->withApiKey($yourApiKey) ->withBaseUri('api.endpoints.anyscale.com/v1') ->make();

pb30 commented 9 months ago

This library expects a "openai-processing-ms" header in the response, but Anyscale does not include one

DC-Sebastian commented 9 months ago

Is there a possibility to remove the dependency for the header? The possibility of being able to use the Llama 2 models is quite useful.

sstalle commented 9 months ago

@DC-Sebastian You can add the header to a response before it is processed by the library. The exact way to do this would depend on the HTTP client you are using. Here is a quick example for Guzzle:

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\ResponseInterface;

$stack = HandlerStack::create();
$stack->push(
    Middleware::mapResponse(function (ResponseInterface $response) {
        return $response->withHeader('openai-processing-ms', '1');
    })
);
$guzzle = new Client(['handler' => $stack]);

$yourApiKey = '...';
$client = \OpenAI::factory()
    ->withApiKey($yourApiKey)
    ->withBaseUri('api.endpoints.anyscale.com/v1')
    ->withHttpClient($guzzle)
    ->make();
bianchi commented 9 months ago

This started happening for me too. I'm using Azure OpenAi.

rkgo55 commented 9 months ago

Hello. I also got the same error when using Azure OpenAi. I think the problem occurs when sending consecutive requests.

The same error may occur in the future due to specification changes on the API side. Therefore, it is a good idea to confirm the existence of all headers in the part where the header is obtained.