Download the PHP package sourceability/portal without Composer
On this page you can find all versions of the php package sourceability/portal. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sourceability/portal
More information about sourceability/portal
Files in sourceability/portal
Package portal
Short Description A CLI, PHP Library and Symfony Bundle that helps getting structured data out from GPT.
License MIT
Informations about the package portal
sourceability/portal
A CLI and PHP Library that helps getting structured data out from GPT.
Given a JSON Schema, GPT is perfectly capable of outputting JSON that conforms to the schema. This approach enables GPT to be used programmatically for non-conversational use cases.
For example, before parsing a user uploaded CSV, you could ask GPT to map its headers to the ones your code supports:
⚠️ Note that this library is experimental, and the API will change.
You are welcome to contribute by submitting issues, ideas, PRs, etc 🙂.
Installation
Trying out
You can try out YAML spells with docker:
Symfony support
The library includes a Symfony bundle.
Add the bundle to config/bundles.php
:
Then define the OPENAI_API_KEY=sk-XXX
environment variable, for example in .env.local
.
You can also configure the bundle:
You can invoke your service spells using their FQCN with the cast command (don't forget the quotes):
You can also define a short name with the #[AutoconfigureSpell]
attribute:
And invoke the spell with bin/console portal:cast Categorize
Static YAML
You can invoke portal with the path to a .yaml
with the following format:
Spell
The Spell interface is the main way to interact with this library.
You can think of a Spell as a way to create a function whose "implementation" is a GPT prompt:
A spell is defined by its Input/Output types TInput
and TOutput
.
So for example, a spell that accepts a number and returns an array of string, would use Spell<int, string<string>>
.
getSchema
With the getSchema
you return a JSON Schema:
Make sure to leverage the description and examples properties to give GPT more context and instructions:
Note that you can also leverage libraries that define a DSL to build schemas:
- goldspecdigital/oooas - see examples/goldspecdigital-oooas
- swaggest/json-schema - see examples/swaggest
getPrompt
The getPrompt
method is where you describe the desired behaviour:
transcribe
Finally, you can transform the json decoded GPT output into your output type:
getExamples
The getExamples
method returns 0 or many inputs examples. This is very useful when iterating on a prompt.
Casting
Once you've done all that, you can cast try your spell examples:
Or invoke your spell with the PHP Api:
$portal->transfer
If you don't need the Spell getExamples
and transcribe
, you can use transfer
:
CLI
You can pass your own JSON example to the portal cli:
Use -v
, -vv
, -vvv
to print more information like the prompts or the OpenAI API requests/responses.
ApiPlatformSpell
The ApiPlatformSpell
uses API Platform's to generate the JSON Schema but also to deserialize the JSON result.
You must implement the following methods:
getClass
getPrompt
The following are optional:
isCollection
is false by default, you can return true insteadgetExamples
is empty by default, you can add your examples
You can then use the #[ApiProperty]
attribute to add context to your schema:
Examples
See ./examples/.
All versions of portal with dependencies
composer-runtime-api Version *
guzzlehttp/promises Version ^1.5
nyholm/psr7 Version ^1.5
php-http/cache-plugin Version ^1.0
php-http/logger-plugin Version ^1.0
sourceability/openai-client Version ^0.3.4
symfony/cache Version ^6.0
symfony/console Version ^6.0
symfony/http-client Version ^6.0