From 80c5bd06e2cc266818cc63530e59dbfc07667e33 Mon Sep 17 00:00:00 2001 From: wpdev-mac-bashar Date: Thu, 2 Mar 2023 02:40:37 +0600 Subject: [PATCH 1/3] added chatgpt (chat-completions) API --- README.md | 20 ++++++++++++++++++++ src/OpenAi.php | 26 ++++++++++++++++++++++++++ src/Url.php | 9 +++++++++ tests/OpenAiTest.php | 19 +++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/README.md b/README.md index f5fb628..7338dbe 100644 --- a/README.md +++ b/README.md @@ -419,6 +419,26 @@ $classification = $open_ai->classification([ ]); ``` +## Chat + +Given a chat conversation, the model will return a chat completion response. + + ```php +$complete = $open_ai->chat([ + 'model' => 'gpt-3.5-turbo', + 'messages' => [ + [ + "role" => "user", + "content": "Hello!" + ] + ], + 'temperature' => 1.0, + 'max_tokens' => 4096, + 'frequency_penalty' => 0, + 'presence_penalty' => 0, +]); +``` + ## Content Moderations Given a input text, outputs if the model classifies it as violating OpenAI's content policy. diff --git a/src/OpenAi.php b/src/OpenAi.php index 6ed4664..630af9e 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -200,6 +200,32 @@ public function moderation($opts) return $this->sendRequest($url, 'POST', $opts); } + + /** + * @param $opts + * @param null $stream + * @return bool|string + * @throws Exception + */ + public function chat($opts, $stream = null) + { + if ($stream != null && array_key_exists('stream', $opts)) { + if (! $opts['stream']) { + throw new Exception( + 'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.' + ); + } + + $this->stream_method = $stream; + } + + $opts['model'] = $opts['model'] ?? $this->model; + $url = Url::chatUrl(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + /** * @param $opts * @return bool|string diff --git a/src/Url.php b/src/Url.php index aa86c8d..903971b 100644 --- a/src/Url.php +++ b/src/Url.php @@ -133,4 +133,13 @@ public static function embeddings(): string { return self::OPEN_AI_URL . "/embeddings"; } + + /** + * @param + * @return string + */ + public static function chatUrl(): string + { + return self::OPEN_AI_URL . "/chat/completions"; + } } diff --git a/tests/OpenAiTest.php b/tests/OpenAiTest.php index 22b83b8..2e8db54 100644 --- a/tests/OpenAiTest.php +++ b/tests/OpenAiTest.php @@ -227,3 +227,22 @@ $this->assertStringContainsString('data', $result); })->group('working'); + + +it('should handle simple chat completion using the new endpoint', function () use ($open_ai) { + $result = $open_ai->chat([ + 'model' => 'gpt-3.5-turbo', + 'messages' => [ + [ + "role" => "user", + "content" => "Hello!" + ] + ], + 'temperature' => 0.9, + "max_tokens" => 150, + "frequency_penalty" => 0, + "presence_penalty" => 0, + ]); + + $this->assertStringContainsString('text', $result); +})->group('working'); From e93fc5bc5f4922e012495fd3f8ad115d45fa4402 Mon Sep 17 00:00:00 2001 From: wpdev-mac-bashar Date: Thu, 2 Mar 2023 03:38:20 +0600 Subject: [PATCH 2/3] added chatgpt (chat-completions) API --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7338dbe..e5f860d 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ $complete = $open_ai->chat([ 'messages' => [ [ "role" => "user", - "content": "Hello!" + "content" => "Hello!" ] ], 'temperature' => 1.0, From db5cd5a10e72f75923239cfc73d65dae3595772a Mon Sep 17 00:00:00 2001 From: wpdev-mac-bashar Date: Thu, 2 Mar 2023 04:13:32 +0600 Subject: [PATCH 3/3] added chatgpt (chat-completions) API --- src/OpenAi.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 630af9e..fa7f17a 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -8,6 +8,7 @@ class OpenAi { private string $engine = "davinci"; private string $model = "text-davinci-002"; + private string $chatModel = "gpt-3.5-turbo"; private array $headers; private array $contentTypes; private int $timeout = 0; @@ -219,7 +220,7 @@ public function chat($opts, $stream = null) $this->stream_method = $stream; } - $opts['model'] = $opts['model'] ?? $this->model; + $opts['model'] = $opts['model'] ?? $this->chatModel; $url = Url::chatUrl(); $this->baseUrl($url);