Download the PHP package shanginn/openai-sdk without Composer
On this page you can find all versions of the php package shanginn/openai-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package openai-sdk
OpenAI SDK PHP
A modern, strongly-typed PHP SDK for interacting with the OpenAI API, focusing initially on the Chat Completions endpoint. Built with asynchronous capabilities in mind using amphp/http-client and robust serialization/deserialization via crell/serde.
Features
- Access to the OpenAI Chat Completions API (
/v1/chat/completions). - Strongly-Typed Objects: Uses PHP classes for Requests, Responses, Messages, Tools, and Schemas, providing better IDE autocompletion and type safety.
- Tool Calling: Define and use tools (functions) that the OpenAI models can invoke. Includes automatic deserialization of tool arguments into PHP objects based on class definitions with attributes like
Spiral\JsonSchemaGenerator\Attribute\Field. - JSON Schema Mode: Force the model to output JSON conforming to a specific structure defined by your PHP classes implementing
JsonSchemaInterface, utilizing attributes likeSpiral\JsonSchemaGenerator\Attribute\Fieldfor detailed schema generation. - Image Input: Supports sending images along with text prompts using
UserMessageandImageContentPart(compatible with models like GPT-4o). - Asynchronous Client: Leverages
amphp/http-clientfor non-blocking I/O (though the currentOpenaiClientimplementation buffers the full response). - Serialization: Uses
crell/serdeandsymfony/serializerfor mapping between PHP objects and OpenAI's JSON format. - Simplified Wrapper: Includes an
OpenaiSimpleclass for common use cases like simple text generation, JSON object generation, and tool calling with less boilerplate. - Custom Exceptions: Provides specific exceptions for different API error conditions (e.g.,
OpenaiErrorResponseException,OpenaiNoChoicesException,OpenaiWrongSchemaException).
Installation
Install the package via Composer:
Usage
Simple Text Generation (OpenaiSimple)
This is the easiest way to get a text response for a simple prompt.
Basic Completion (Openai Core Class)
If you need more control over the request parameters or want to handle the response object directly.
Advanced Usage
Tool Calling
Define a tool class implementing ToolInterface (often by extending AbstractTool and using #[OpenaiToolSchema]) and detail its parameters using #[Field] attributes from spiral/json-schema-generator. The SDK will attempt to deserialize the model's arguments into an instance of your tool class.
1. Define the Tool Schema:
2. Call the Tool using OpenaiSimple:
This simplifies the process of forcing a specific tool call and getting the parsed arguments.
3. Call the Tool using Core Openai Class:
This gives you more control over the request and access to the full response, including the tool call ID.
JSON Schema Output
Force the model to generate a JSON object conforming to your predefined PHP class structure. Define the schema class implementing JsonSchemaInterface (often by extending AbstractJsonSchema and using #[OpenaiSchema]) and detail its properties using #[Field] attributes.
1. Define the JSON Schema Class:
2. Generate JSON using OpenaiSimple:
This simplifies getting the deserialized schema object directly.
3. Generate JSON using Core Openai Class:
Provides access to the full response, including the raw JSON string before deserialization.
Image Input
Provide an array of ContentPartInterface objects (TextContentPart, ImageContentPart) to the UserMessage constructor. Requires a vision-capable model like gpt-4o.
Error Handling
The SDK throws specific exceptions found in the Shanginn\Openai\Exceptions namespace for easier error management.
OpenaiErrorResponseException: Wraps anErrorResponseobject returned directly by the OpenAI API (e.g., invalid API key, rate limit exceeded). Access the details via$e->response.OpenaiRefusedResponseException: Thrown byOpenaiSimplewhen the model explicitly refuses to answer (contains arefusalmessage). Access via$e->refusaland$e->response.OpenaiNoChoicesException: Thrown when the API returns a valid response but with an emptychoicesarray. Access the original response via$e->response.OpenaiNoContentException: Thrown byOpenaiSimplewhen a choice exists but has nocontent. Access via$e->response.OpenaiWrongSchemaException: Thrown byOpenaiSimpleor potentially during coreOpenaiprocessing if JSON schema/tool calling was requested, but the response didn't conform as expected (e.g., deserialization failed). Access via$e->response.OpenaiInvalidResponseException: Base class for response validation issues likeNoChoices,NoContent,WrongSchema.OpenaiException: Base exception for all SDK-specific errors.
Dependencies
- amphp/http-client: For asynchronous HTTP requests.
- crell/serde: For robust serialization and deserialization between PHP objects and JSON.
- symfony/serializer: Used alongside
crell/serdefor normalization, particularly handling enums, snake_case, and custom normalizers. - spiral/json-schema-generator: (Recommended) Used internally and via attributes (
#[Field]) to generate detailed JSON Schema definitions from PHP classes for Tool Calling and JSON Schema mode.
Contributing
Contributions are welcome! Please follow these general steps:
- Fork the repository.
- Create a new branch for your feature or bug fix (
git checkout -b feature/my-new-feature). - Make your changes.
- Add tests for your changes.
- Ensure tests pass (
vendor/bin/phpunit). - Ensure code style compliance (e.g., using PHP CS Fixer or Rector with provided config, if any).
- Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin feature/my-new-feature). - Create a new Pull Request.
License
This project is licensed under the MIT License
All versions of openai-sdk with dependencies
symfony/serializer Version ^7
crell/serde Version ^1.3
amphp/http Version ^2.1
amphp/file Version ^3
amphp/http-client Version ^5.3
spiral/json-schema-generator Version ^1.1
symfony/property-access Version ^7.2