opctim/chatgpt-schema-generator
Composer 安装命令:
composer require opctim/chatgpt-schema-generator
包简介
This bundle creates a JSON schema from your classes or DTOs to be used with the ChatGPT API so that you get back what you expect.
README 文档
README
Easily generate JSON Schemas for OpenAI ChatGPT prompts using your PHP DTOs!
This package allows you to dynamically create JSON Schemas based on your DTO (Data Transfer Object) classes. These schemas can be passed to OpenAI’s ChatGPT API to ensure consistent, structured, and strongly-typed JSON responses. Ideal for developers who want precise control over ChatGPT outputs in their applications.
Key Features
- Seamless Schema Generation: Automatically generate JSON Schemas from your PHP DTO classes.
- Enum Support: Define strict enums in your DTO, and ChatGPT will adhere to your choices.
- Integration Ready: Easily pass the generated schemas to OpenAI APIs for structured responses.
- Serialization Support: Deserialize the JSON response back into your DTO with ease.
- Clean and Reusable Code: Write less boilerplate and focus on building smarter applications.
Requirements
- PHP >= 8.3
Installation
composer require opctim/chatgpt-schema-generator
Usage
<?php use Opctim\ChatGpt\SchemaGenerator\Generator; use Opctim\ChatGpt\SchemaGenerator\Tests\Fixtures\Dto\User; // Generate the schema based on your DTO $schema = $this->generator->generateSchema(User::class); // Specify the schema (you can play with this here: https://platform.openai.com/playground/chat?models=gpt-4o) $responseFormat = [ 'type' => 'json_schema', 'json_schema' => $schema->getSchema() ]; $prompt = 'Generate an example user and return based on the input schema with the name ' . $schema->getName(); $model = 'gpt-4o'; $temperature = 0.5; $answer = $myChatClient->chat($model, $temperature, $prompt, $responseFormat); // Deserialize from the response :) $user = $serializer->deserialize($answer, User::class, 'json'); // Done, ready to use your example user object!
Excluding Properties & Classes
You can either replace properties:
<?php declare(strict_types=1); use Opctim\ChatGpt\SchemaGenerator\Attribute\Excluded; class User { #[Excluded] private int $id; /** * This will be ignored too! * * @internal */ private int $someNumber; // [...] }
Or the whole class:
<?php declare(strict_types=1); use Opctim\ChatGpt\SchemaGenerator\Attribute\Excluded; #[Excluded] class User { // [...] }
Union Types
Union types will be returned as oneOf in the JSON schema.
If your class has a property that looks like this:
<?php declare(strict_types=1); use Opctim\ChatGpt\SchemaGenerator\Attribute\Excluded; class User { private Address|Account $addressOrAccount; }
The result will be:
{
"name": "User",
"strict": false,
"schema": {
"type": "object",
"properties": {
"addressOrAccount": {
"oneOf": [
{
"type": "object",
"properties": {
"street": {
"description": "Street name.",
"type": "string"
},
"city": {
"description": "City name.",
"type": "string"
},
"postalCode": {
"description": "Postal code.",
"type": "string"
}
},
"required": [
"street",
"city",
"postalCode"
]
},
{
"type": "object",
"properties": {
"bankName": {
"description": "The name of the bank.",
"type": "string"
}
},
"required": [
"bankName"
]
}
]
}
},
"required": [
"addressOrAccount"
]
}
}
Schema output example
<?php use Opctim\ChatGpt\SchemaGenerator\Generator; use Opctim\ChatGpt\SchemaGenerator\Tests\Fixtures\Dto\User; $schema = $this->generator->generateSchema(User::class); $schema->getName(); // Returns the name of the schema, to be referenced in your custom ChatGPT prompt $schema->getSchema(); // Returns the schema as array $schema['name']; // The schema can also be accessed as an array $schema->getJson(); // Returns the same as (string)$schema echo (string)$schema;
Result
{
"name": "User",
"strict": false,
"schema": {
"type": "object",
"properties": {
"role": {
"type": "string",
"enum": [
"admin",
"editor",
"viewer"
],
"description": "User role."
},
"address": {
"type": "object",
"properties": {
"street": {
"description": "Street name.",
"type": "string"
},
"city": {
"description": "City name.",
"type": "string"
},
"postalCode": {
"description": "Postal code.",
"type": "string"
}
},
"required": [
"street",
"city",
"postalCode"
]
},
"tags": {
"description": "List of tags for the user.",
"type": "array",
"items": {
"type": "string"
}
},
"previousAddresses": {
"description": "List of addresses the user has lived in.",
"type": "array",
"items": {
"type": "object",
"properties": {
"street": {
"description": "Street name.",
"type": "string"
},
"city": {
"description": "City name.",
"type": "string"
},
"postalCode": {
"description": "Postal code.",
"type": "string"
}
},
"required": [
"street",
"city",
"postalCode"
]
}
},
"addressOrAccount": {
"oneOf": [
{
"type": "object",
"properties": {
"street": {
"description": "Street name.",
"type": "string"
},
"city": {
"description": "City name.",
"type": "string"
},
"postalCode": {
"description": "Postal code.",
"type": "string"
}
},
"required": [
"street",
"city",
"postalCode"
]
},
{
"type": "object",
"properties": {
"bankName": {
"description": "The name of the bank.",
"type": "string"
}
},
"required": [
"bankName"
]
}
]
}
},
"required": [
"role",
"address",
"tags",
"previousAddresses",
"addressOrAccount"
]
}
}
Tests
Tests are located inside the tests/ folder and can be run with vendor/bin/phpunit:
composer install vendor/bin/phpunit
opctim/chatgpt-schema-generator 适用场景与选型建议
opctim/chatgpt-schema-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 opctim/chatgpt-schema-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 opctim/chatgpt-schema-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-17