Skip to content

Commit 6e2ccb7

Browse files
committed
feat: completions, edits, embeddings and models
0 parents  commit 6e2ccb7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1812
-0
lines changed

.editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.gitattributes

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/tests export-ignore
2+
/.github export-ignore
3+
.editorconfig export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
CHANGELOG.md export-ignore
7+
CONTRIBUTING.md export-ignore
8+
phpstan.neon.dist export-ignore
9+
phpunit.xml.dist export-ignore
10+
README.md export-ignore
11+
rector.php export-ignore

.github/FUNDING.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# These are supported funding model platforms
2+
3+
github: nunomaduro
4+
patreon: nunomaduro
5+
custom: https://www.paypal.com/paypalme/enunomaduro

.github/workflows/formats.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Formats
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ${{ matrix.os }}
8+
9+
strategy:
10+
fail-fast: true
11+
matrix:
12+
os: [ubuntu-latest]
13+
php: [8.1]
14+
dependency-version: [prefer-lowest, prefer-stable]
15+
16+
name: Formats P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
17+
18+
steps:
19+
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
23+
- name: Cache dependencies
24+
uses: actions/cache@v1
25+
with:
26+
path: ~/.composer/cache/files
27+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
28+
29+
- name: Setup PHP
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
php-version: ${{ matrix.php }}
33+
extensions: dom, mbstring, zip
34+
tools: prestissimo
35+
coverage: pcov
36+
37+
- name: Install Composer dependencies
38+
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
39+
40+
- name: Coding Style Checks
41+
run: composer test:lint
42+
43+
- name: Refacto Checks
44+
run: composer test:refacto
45+
46+
- name: Type Checks
47+
run: composer test:types

.github/workflows/tests.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Tests
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
os: [ubuntu-latest, macos-latest, windows-latest]
12+
php: [8.1, 8.2]
13+
dependency-version: [prefer-lowest, prefer-stable]
14+
15+
name: Tests P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
16+
17+
steps:
18+
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
22+
- name: Cache dependencies
23+
uses: actions/cache@v1
24+
with:
25+
path: ~/.composer/cache/files
26+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
27+
28+
- name: Setup PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: ${{ matrix.php }}
32+
extensions: dom, mbstring, zip
33+
coverage: none
34+
35+
- name: Install Composer dependencies
36+
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
37+
38+
- name: Unit Tests
39+
run: composer test:unit

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.phpunit.cache
2+
/.php-cs-fixer.cache
3+
/.php-cs-fixer.php
4+
/composer.lock
5+
/phpunit.xml
6+
/vendor/
7+
*.swp
8+
*.swo
9+
playground/*

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/).
6+
7+
## [Unreleased]
8+
- Adds first version

CONTRIBUTING.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# CONTRIBUTING
2+
3+
Contributions are welcome, and are accepted via pull requests.
4+
Please review these guidelines before submitting any pull requests.
5+
6+
## Process
7+
8+
1. Fork the project
9+
1. Create a new branch
10+
1. Code, test, commit and push
11+
1. Open a pull request detailing your changes. Make sure to follow the [template](.github/PULL_REQUEST_TEMPLATE.md)
12+
13+
## Guidelines
14+
15+
* Please ensure the coding style running `composer lint`.
16+
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful.
17+
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
18+
* Please remember that we follow [SemVer](http://semver.org/).
19+
20+
## Setup
21+
22+
Clone your fork, then install the dev dependencies:
23+
```bash
24+
composer install
25+
```
26+
27+
## Refacto
28+
29+
Refacto your code:
30+
```bash
31+
composer refacto
32+
```
33+
34+
## Lint
35+
36+
Lint your code:
37+
```bash
38+
composer lint
39+
```
40+
41+
## Tests
42+
43+
Run all tests:
44+
```bash
45+
composer test
46+
```
47+
48+
Check code quality:
49+
```bash
50+
composer test:refacto
51+
```
52+
53+
Check types:
54+
```bash
55+
composer test:types
56+
```
57+
58+
Unit tests:
59+
```bash
60+
composer test:unit
61+
```

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Nuno Maduro <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<p align="center">
2+
<img src="https://raw.githubusercontent.com/openai-php/art/master/editor-without-bg.png" width="600" alt="OpenAI PHP">
3+
<p align="center">
4+
<a href="https://github.com/openai-php/openAI/actions"><img alt="GitHub Workflow Status (master)" src="https://img.shields.io/github/workflow/status/openai-php/openAI/Tests/master"></a>
5+
<a href="https://packagist.org/packages/openai-php/openAI"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/openai-php/openAI"></a>
6+
<a href="https://packagist.org/packages/openai-php/openAI"><img alt="Latest Version" src="https://img.shields.io/packagist/v/openai-php/openAI"></a>
7+
<a href="https://packagist.org/packages/openai-php/openAI"><img alt="License" src="https://img.shields.io/packagist/l/openai-php/openAI"></a>
8+
</p>
9+
</p>
10+
11+
------
12+
**OpenAI PHP** is a supercharged PHP API client that allows you to interact with the [Open AI API](https://beta.openai.com/docs/api-reference/introduction).
13+
14+
> This project is a work-in-progress. Code and documentation are currently under development and are subject to change.
15+
16+
## Get Started
17+
18+
> **Requires [PHP 8.1+](https://php.net/releases/)**
19+
20+
First, install OpenAI via the [Composer](https://getcomposer.org/) package manager:
21+
22+
```bash
23+
composer require openai-php/client dev-master
24+
```
25+
26+
Then, interact with OpenAI's API:
27+
28+
```php
29+
$client = OpenAI::client('YOUR_API_KEY');
30+
31+
$result = $client->completions()->create([
32+
'model' => 'davinci',
33+
'prompt' => 'PHP is',
34+
]);
35+
36+
echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.
37+
```
38+
39+
## TODO
40+
41+
42+
43+
- [x] Models
44+
- [x] Completions
45+
- [x] Edits
46+
- [x] Embeddings
47+
- [ ] Files
48+
- [ ] FineTunes
49+
- [ ] Moderations
50+
- [ ] Classifications
51+
52+
## Usage
53+
54+
### `Models` Resource
55+
56+
#### `list`
57+
58+
Lists the currently available models, and provides basic information about each one such as the owner and availability.
59+
60+
```php
61+
$client->models()->list(); // ['data' => [...], ...]
62+
```
63+
64+
#### `retrieve`
65+
66+
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
67+
68+
```php
69+
$client->models()->retrieve($model); // ['id' => 'text-davinci-002', ...]
70+
```
71+
72+
### `Completions` Resource
73+
74+
#### `create`
75+
76+
Creates a completion for the provided prompt and parameters.
77+
78+
```php
79+
$client->completions()->create($parameters); // ['choices' => [...], ...]
80+
```
81+
82+
### `Edits` Resource
83+
84+
#### `create`
85+
86+
Creates a new edit for the provided input, instruction, and parameters.
87+
88+
```php
89+
$client->edits()->create(); // ['choices' => [...], ...]
90+
```
91+
92+
### `Embeddings` Resource
93+
94+
#### `create`
95+
96+
Creates an embedding vector representing the input text.
97+
98+
```php
99+
$client->embeddings()->create(); // ['data' => [...], ...]
100+
```
101+
102+
### `Files` Resource
103+
104+
#### `list`
105+
106+
Returns a list of files that belong to the user's organization.
107+
108+
```php
109+
$client->files()->list(); // ['data' => [...], ...]
110+
```
111+
112+
---
113+
114+
OpenAI PHP is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.

Tests/Client.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use OpenAI\Resources\Completions;
4+
use OpenAI\Resources\Models;
5+
6+
it('has models', function () {
7+
$openAI = OpenAI::client('foo');
8+
9+
expect($openAI->models())->toBeInstanceOf(Models::class);
10+
});
11+
12+
it('has completions', function () {
13+
$openAI = OpenAI::client('foo');
14+
15+
expect($openAI->completions())->toBeInstanceOf(Completions::class);
16+
});

Tests/Fixtures/Completion.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* @return array<string, mixed>
5+
*/
6+
function completion(): array
7+
{
8+
return [
9+
'id' => 'cmpl-5uS6a68SwurhqAqLBpZtibIITICna',
10+
'object' => 'text_completion',
11+
'created' => 1664136088,
12+
'model' => 'davinci',
13+
'choices' => [[
14+
'text' => "el, she elaborates more on the Corruptor's role, suggesting K",
15+
'index' => 0,
16+
'logprobs' => null,
17+
'finish_reason' => 'length',
18+
],
19+
],
20+
'usage' => [
21+
'prompt_tokens' => 1,
22+
'completion_tokens' => 16,
23+
'total_tokens' => 17,
24+
],
25+
];
26+
}

0 commit comments

Comments
 (0)