Skip to content

Commit

Permalink
feat: completions, edits, embeddings and models
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Sep 25, 2022
0 parents commit 6e2ccb7
Show file tree
Hide file tree
Showing 54 changed files with 1,812 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_size = 2
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/tests export-ignore
/.github export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
CHANGELOG.md export-ignore
CONTRIBUTING.md export-ignore
phpstan.neon.dist export-ignore
phpunit.xml.dist export-ignore
README.md export-ignore
rector.php export-ignore
5 changes: 5 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These are supported funding model platforms

github: nunomaduro
patreon: nunomaduro
custom: https://www.paypal.com/paypalme/enunomaduro
47 changes: 47 additions & 0 deletions .github/workflows/formats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Formats

on: ['push', 'pull_request']

jobs:
ci:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1]
dependency-version: [prefer-lowest, prefer-stable]

name: Formats P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}

steps:

- name: Checkout
uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, mbstring, zip
tools: prestissimo
coverage: pcov

- name: Install Composer dependencies
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist

- name: Coding Style Checks
run: composer test:lint

- name: Refacto Checks
run: composer test:refacto

- name: Type Checks
run: composer test:types
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests

on: ['push', 'pull_request']

jobs:
ci:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
php: [8.1, 8.2]
dependency-version: [prefer-lowest, prefer-stable]

name: Tests P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}

steps:

- name: Checkout
uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, mbstring, zip
coverage: none

- name: Install Composer dependencies
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist

- name: Unit Tests
run: composer test:unit
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.phpunit.cache
/.php-cs-fixer.cache
/.php-cs-fixer.php
/composer.lock
/phpunit.xml
/vendor/
*.swp
*.swo
playground/*
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
- Adds first version
61 changes: 61 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# CONTRIBUTING

Contributions are welcome, and are accepted via pull requests.
Please review these guidelines before submitting any pull requests.

## Process

1. Fork the project
1. Create a new branch
1. Code, test, commit and push
1. Open a pull request detailing your changes. Make sure to follow the [template](.github/PULL_REQUEST_TEMPLATE.md)

## Guidelines

* Please ensure the coding style running `composer lint`.
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful.
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
* Please remember that we follow [SemVer](http://semver.org/).

## Setup

Clone your fork, then install the dev dependencies:
```bash
composer install
```

## Refacto

Refacto your code:
```bash
composer refacto
```

## Lint

Lint your code:
```bash
composer lint
```

## Tests

Run all tests:
```bash
composer test
```

Check code quality:
```bash
composer test:refacto
```

Check types:
```bash
composer test:types
```

Unit tests:
```bash
composer test:unit
```
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Nuno Maduro <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
114 changes: 114 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<p align="center">
<img src="https://raw.githubusercontent.com/openai-php/art/master/editor-without-bg.png" width="600" alt="OpenAI PHP">
<p align="center">
<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>
<a href="https://packagist.org/packages/openai-php/openAI"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/openai-php/openAI"></a>
<a href="https://packagist.org/packages/openai-php/openAI"><img alt="Latest Version" src="https://img.shields.io/packagist/v/openai-php/openAI"></a>
<a href="https://packagist.org/packages/openai-php/openAI"><img alt="License" src="https://img.shields.io/packagist/l/openai-php/openAI"></a>
</p>
</p>

------
**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).

> This project is a work-in-progress. Code and documentation are currently under development and are subject to change.
## Get Started

> **Requires [PHP 8.1+](https://php.net/releases/)**
First, install OpenAI via the [Composer](https://getcomposer.org/) package manager:

```bash
composer require openai-php/client dev-master
```

Then, interact with OpenAI's API:

```php
$client = OpenAI::client('YOUR_API_KEY');

$result = $client->completions()->create([
'model' => 'davinci',
'prompt' => 'PHP is',
]);

echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.
```

## TODO



- [x] Models
- [x] Completions
- [x] Edits
- [x] Embeddings
- [ ] Files
- [ ] FineTunes
- [ ] Moderations
- [ ] Classifications

## Usage

### `Models` Resource

#### `list`

Lists the currently available models, and provides basic information about each one such as the owner and availability.

```php
$client->models()->list(); // ['data' => [...], ...]
```

#### `retrieve`

Retrieves a model instance, providing basic information about the model such as the owner and permissioning.

```php
$client->models()->retrieve($model); // ['id' => 'text-davinci-002', ...]
```

### `Completions` Resource

#### `create`

Creates a completion for the provided prompt and parameters.

```php
$client->completions()->create($parameters); // ['choices' => [...], ...]
```

### `Edits` Resource

#### `create`

Creates a new edit for the provided input, instruction, and parameters.

```php
$client->edits()->create(); // ['choices' => [...], ...]
```

### `Embeddings` Resource

#### `create`

Creates an embedding vector representing the input text.

```php
$client->embeddings()->create(); // ['data' => [...], ...]
```

### `Files` Resource

#### `list`

Returns a list of files that belong to the user's organization.

```php
$client->files()->list(); // ['data' => [...], ...]
```

---

OpenAI PHP is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.
16 changes: 16 additions & 0 deletions Tests/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use OpenAI\Resources\Completions;
use OpenAI\Resources\Models;

it('has models', function () {
$openAI = OpenAI::client('foo');

expect($openAI->models())->toBeInstanceOf(Models::class);
});

it('has completions', function () {
$openAI = OpenAI::client('foo');

expect($openAI->completions())->toBeInstanceOf(Completions::class);
});
26 changes: 26 additions & 0 deletions Tests/Fixtures/Completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* @return array<string, mixed>
*/
function completion(): array
{
return [
'id' => 'cmpl-5uS6a68SwurhqAqLBpZtibIITICna',
'object' => 'text_completion',
'created' => 1664136088,
'model' => 'davinci',
'choices' => [[
'text' => "el, she elaborates more on the Corruptor's role, suggesting K",
'index' => 0,
'logprobs' => null,
'finish_reason' => 'length',
],
],
'usage' => [
'prompt_tokens' => 1,
'completion_tokens' => 16,
'total_tokens' => 17,
],
];
}
Loading

0 comments on commit 6e2ccb7

Please sign in to comment.