Skip to content

Commit

Permalink
Merge pull request #57 from bashar94/main
Browse files Browse the repository at this point in the history
added chatgpt (chat-completions) API
  • Loading branch information
orhanerday authored Mar 2, 2023
2 parents 88bb838 + db5cd5a commit 4cba470
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -200,6 +201,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->chatModel;
$url = Url::chatUrl();
$this->baseUrl($url);

return $this->sendRequest($url, 'POST', $opts);
}

/**
* @param $opts
* @return bool|string
Expand Down
9 changes: 9 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
19 changes: 19 additions & 0 deletions tests/OpenAiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit 4cba470

Please sign in to comment.