Usage
Basic usage
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: By default, Instructor looks for OPENAI_API_KEY environment variable to get your API key. You can also provide the API key explicitly when creating the Instructor instance.
!!! note
Currently, Instructor for PHP only supports classes / objects as response models. In case you want to extract simple types or arrays, you need to wrap them in a class (or use Scalar
helper class).
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.
Alternative way to get results
You can call request()
method to initiate Instructor with request data
and then call get()
to get the response.
Structured-to-structured data 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.
Streaming support
Instructor supports streaming of partial results, allowing you to start processing the data as soon as it is available.
Scalar responses
See Scalar responses for more information on how to generate scalar responses with Scalar
adapter class.
Partial responses and streaming
See Streaming and partial updates for more information on how to work with partial updates and streaming.
Extracting arguments for function call
See FunctionCall helper class for more information on how to extract arguments for callable objects.