Download the PHP package cognesy/instructor-php without Composer

On this page you can find all versions of the php package cognesy/instructor-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package instructor-php

Instructor for PHP

Structured data extraction in PHP, powered by LLMs. Designed for simplicity, transparency, and control.

What is Instructor?

Instructor is a library that allows you to extract structured, validated data from multiple types of inputs: text, images or OpenAI style chat sequence arrays. It is powered by Large Language Models (LLMs).

Instructor simplifies LLM integration in PHP projects. It handles the complexity of extracting structured data from LLM outputs, so you can focus on building your application logic and iterate faster.

Instructor for PHP is inspired by the Instructor library for Python created by Jason Liu.

Here's a simple CLI demo app using Instructor to extract structured data from text:

Feature highlights

Core features

Flexible inputs

Customization

Sync and streaming support

Observability

Support for multiple LLMs / API providers

Other capabilities

Documentation and examples

Instructor in Other Languages

Check out implementations in other languages below:

If you want to port Instructor to another language, please reach out to us on Twitter we'd love to help you get started!

How Instructor Enhances Your Workflow

Instructor introduces three key enhancements compared to direct API usage.

Response Model

You just specify a PHP class to extract data into via the 'magic' of LLM chat completion. And that's it.

Instructor reduces brittleness of the code extracting the information from textual data by leveraging structured LLM responses.

Instructor helps you write simpler, easier to understand code - you no longer have to define lengthy function call definitions or write code for assigning returned JSON into target data objects.

Validation

Response model generated by LLM can be automatically validated, following set of rules. Currently, Instructor supports only Symfony validation.

You can also provide a context object to use enhanced validator capabilities.

Max Retries

You can set the number of retry attempts for requests.

Instructor will repeat requests in case of validation or deserialization error up to the specified number of times, trying to get a valid response from LLM.

Get Started

Installing Instructor is simple. Run following command in your terminal, and you're on your way to a smoother data handling experience!

Usage

Basic example

This is a simple example demonstrating how Instructor retrieves structured information from provided text (or chat message sequence).

Response model class is a plain PHP class with typehints specifying the types of fields of the object.

NOTE: Instructor supports classes / objects as response models. In case you want to extract simple types or enums, you need to wrap them in Scalar adapter - see section below: Extracting Scalar Values.

Connecting to various LLM API providers

Instructor allows you to define multiple API connections in llm.php file. This is useful when you want to use different LLMs or API providers in your application.

Default configuration is located in /config/llm.php in the root directory of Instructor codebase. It contains a set of predefined connections to all LLM APIs supported out-of-the-box by Instructor.

Config file defines connections to LLM APIs and their parameters. It also specifies the default connection to be used when calling Instructor without specifying the client connection.

To customize the available connections you can either modify existing entries or add your own.

Connecting to LLM API via predefined connection is as simple as calling withClient method with the connection name.

You can change the location of the configuration files for Instructor to use via INSTRUCTOR_CONFIG_PATH environment variable. You can use copies of the default configuration files as a starting point.

Structured-to-structured processing

Instructor offers a way to use structured data as an input. This is useful when you want to use object data as input and get another object with a result of LLM inference.

The input field of Instructor's respond() and request() methods can be an object, but also an array or just a string.

Validation

Instructor validates results of LLM response against validation rules specified in your data model.

For further details on available validation rules, check Symfony Validation constraints.

Max Retries

In case maxRetries parameter is provided and LLM response does not meet validation criteria, Instructor will make subsequent inference attempts until results meet the requirements or maxRetries is reached.

Instructor uses validation errors to inform LLM on the problems identified in the response, so that LLM can try self-correcting in the next attempt.

Alternative ways to call Instructor

You can call request() method to set the parameters of the request and then call get() to get the response.

Streaming support

Instructor supports streaming of partial results, allowing you to start processing the data as soon as it is available.

Partial results

You can define onPartialUpdate() callback to receive partial results that can be used to start updating UI before LLM completes the inference.

NOTE: Partial updates are not validated. The response is only validated after it is fully received.

Shortcuts

String as Input

You can provide a string instead of an array of messages. This is useful when you want to extract data from a single block of text and want to keep your code simple.

Extracting Scalar Values

Sometimes we just want to get quick results without defining a class for the response model, especially if we're trying to get a straight, simple answer in a form of string, integer, boolean or float. Instructor provides a simplified API for such cases.

In this example, we're extracting a single integer value from the text. You can also use Scalar::string(), Scalar::boolean() and Scalar::float() to extract other types of values.

Extracting Enum Values

Additionally, you can use Scalar adapter to extract one of the provided options by using Scalar::enum().

Extracting Sequences of Objects

Sequence is a wrapper class that can be used to represent a list of objects to be extracted by Instructor from provided context.

It is usually more convenient not create a dedicated class with a single array property just to handle a list of objects of a given class.

Additional, unique feature of sequences is that they can be streamed per each completed item in a sequence, rather than on any property update.

See more about sequences in the Sequences section.

Specifying Data Model

Type Hints

Use PHP type hints to specify the type of extracted data.

Use nullable types to indicate that given field is optional.

DocBlock type hints

You can also use PHP DocBlock style comments to specify the type of extracted data. This is useful when you want to specify property types for LLM, but can't or don't want to enforce type at the code level.

See PHPDoc documentation for more details on DocBlock website.

Typed Collections / Arrays

PHP currently does not support generics or typehints to specify array element types.

Use PHP DocBlock style comments to specify the type of array elements.

Complex data extraction

Instructor can retrieve complex data structures from text. Your response model can contain nested objects, arrays, and enums.

Dynamic data schemas

If you want to define the shape of data during runtime, you can use Structure class.

Structures allow you to define and modify arbitrary shape of data to be extracted by LLM. Classes may not be the best fit for this purpose, as declaring or changing them during execution is not possible.

With structures, you can define custom data shapes dynamically, for example based on the user input or context of the processing, to specify the information you need LLM to infer from the provided text or chat messages.

Example below demonstrates how to define a structure and use it as a response model:

For more information see Structures section.

Changing LLM model and options

You can specify model and other options that will be passed to OpenAI / LLM endpoint.

Support for language models and API providers

Instructor offers out of the box support for following API providers:

For usage examples, check Hub section or examples directory in the code repository.

Using DocBlocks as Additional Instructions for LLM

You can use PHP DocBlocks (/* /) to provide additional instructions for LLM at class or field level, for example to clarify what you expect or how LLM should process your data.

Instructor extracts PHP DocBlocks comments from class and property defined and includes them in specification of response model sent to LLM.

Using PHP DocBlocks instructions is not required, but sometimes you may want to clarify your intentions to improve LLM's inference results.

Customizing Validation

ValidationMixin

You can use ValidationMixin trait to add ability of easy, custom data object validation.

Validation Callback

Instructor uses Symfony validation component to validate extracted data. You can use #[Assert/Callback] annotation to build fully customized validation logic.

See Symfony docs for more details on how to use Callback constraint.

Internals

Lifecycle

As Instructor for PHP processes your request, it goes through several stages:

  1. Initialize and self-configure (with possible overrides defined by developer).
  2. Analyze classes and properties of the response data model specified by developer.
  3. Encode data model into a schema that can be provided to LLM.
  4. Execute request to LLM using specified messages (content) and response model metadata.
  5. Receive a response from LLM or multiple partial responses (if streaming enabled).
  6. Deserialize response received from LLM into originally requested classes and their properties.
  7. In case response contained incomplete or corrupted data - if errors are encountered, create feedback message for LLM and requests regeneration of the response.
  8. Execute validations defined by developer for the data model - if any of them fail, create feedback message for LLM and requests regeneration of the response.
  9. Repeat the steps 4-8, unless specified limit of retries has been reached or response passes validation

Receiving notification on internal events

Instructor allows you to receive detailed information at every stage of request and response processing via events.

Receiving events can help you to monitor the execution process and makes it easier for a developer to understand and resolve any processing issues.

Response Models

Instructor is able to process several types of input provided as response model, giving you more flexibility on how you interact with the library.

The signature of respond() method of Instructor states the responseModel can be either string, object or array.

Handling string $responseModel value

If string value is provided, it is used as a name of the class of the response model.

Instructor checks if the class exists and analyzes the class & properties type information & doc comments to generate a schema needed to specify LLM response constraints.

The best way to provide the name of the response model class is to use NameOfTheClass::class instead of string, making it possible for IDE to execute type checks, handle refactorings, etc.

Handling object $responseModel value

If object value is provided, it is considered an instance of the response model. Instructor checks the class of the instance, then analyzes it and its property type data to specify LLM response constraints.

Handling array $responseModel value

If array value is provided, it is considered a raw JSON Schema, therefore allowing Instructor to use it directly in LLM requests (after wrapping in appropriate context - e.g. function call).

Instructor requires information on the class of each nested object in your JSON Schema, so it can correctly deserialize the data into appropriate type.

This information is available to Instructor when you are passing $responseModel as a class name or an instance, but it is missing from raw JSON Schema.

Current design uses JSON Schema $comment field on property to overcome this. Instructor expects developer to use $comment field to provide fully qualified name of the target class to be used to deserialize property data of object or enum type.

Response model contracts

Instructor allows you to customize processing of $responseModel value also by looking at the interfaces the class or instance implements:

Additional Notes

PHP ecosystem does not (yet) have a strong equivalent of Pydantic, which is at the core of Instructor for Python.

To provide an essential functionality we needed here Instructor for PHP leverages:

Dependencies

Instructor for PHP is compatible with PHP 8.2 or later and, due to minimal dependencies, should work with any framework of your choice.

Additional dependencies are required for some extras:

TODOs

Contributing

If you want to help, check out some of the issues. All contributions are welcome - code improvements, documentation, bug reports, blog posts / articles, or new cookbooks and application examples.

License

This project is licensed under the terms of the MIT License.

Support

If you have any questions or need help, please reach out to me on Twitter or GitHub.

Contributors

Contributors


All versions of instructor-php with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
ext-fileinfo Version *
ext-simplexml Version *
adbario/php-dot-notation Version ^3.3
aimeos/map Version ^3.8
guzzlehttp/guzzle Version ^7.8
phpdocumentor/reflection-docblock Version ^5.4
phpstan/phpdoc-parser Version ^1.29
psr/event-dispatcher Version ^1.0
psr/log Version ^3.0
symfony/filesystem Version ^7.1
symfony/intl Version ^7.1
symfony/property-access Version ^6.4 || ^7.0
symfony/property-info Version ^6.4 || ^7.0
symfony/serializer Version ^6.4 || ^7.0
symfony/type-info Version ^7.1
symfony/validator Version ^6.4 || ^7.0
vlucas/phpdotenv Version ^5.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package cognesy/instructor-php contains the following files

Loading the files please wait ....