openai-php / client

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

[Request] I want to put MetaInformation in StreamResponse. #328

Closed rkgo55 closed 5 months ago

rkgo55 commented 5 months ago

This library has always helped me.

Now, if we can get MetaInformation in StreamResponse now, we can handle the timing of when the RateLimit is about to be hit.

This should be very useful for implementing robust services.

I can confirm that it works fine in my environment with the following modifications. (OpenAI / Azure)

# Fix All new StreamResponse()
-         return new StreamResponse(CreateStreamedResponse::class, $response);
+        return new StreamResponse(CreateStreamedResponse::class, $response, MetaInformation::from($response->getHeaders()));
    }
}

# Fix StreamResponse constructor
    public function __construct(
        private readonly string $responseClass,
        private readonly ResponseInterface $response,
+        private readonly MetaInformation $meta,
rkgo55 commented 5 months ago

It would be better to make the following modifications as in the SpeechStreamResponse response

    /**
     * Creates a new Stream Response instance.
     *
     * @param  class-string<TResponse>  $responseClass
     */
    public function __construct(
        private readonly string $responseClass,
        private readonly ResponseInterface $response,
    ) {
        //
    }

+    public function meta(): MetaInformation
+    {
+        // @phpstan-ignore-next-line
+        return MetaInformation::from($this->response->getHeaders());
+    }
rkgo55 commented 5 months ago

Sorry, it already existed.