apie/ai-instructor
Composer 安装命令:
composer require apie/ai-instructor
包简介
Use a LLM to create an object in PHP similar to Instructor in Python
README 文档
README
ai-instructor
This package is part of the Apie library. The code is maintained in a monorepo, so PR's need to be sent to the monorepo
Documentation
Instructor is a library for Python that works with LLM's to force a specific structure. Wouldn't it be nice if we have the same functionality in PHP? That's what apie/ai-instructor does. Like you have some class in PHP and ask AI to fill it in for you from a chat prompt given by the user:
class MovieReview { public function __construct( public string $name, public string $description, public int $rating ) { } }
Requirements
You need a OpenAI key or a valid ollama service running (in Docker or locally).
Setup
The simplest standalone setup is using any of the static methods in AiInstructor:
use Apie\AiInstructor\AiInstructor; // ollama $instructor = AiInstructor::createForOllama('http://localhost:11434'); // openAI $instructor = AiInstructor::createForOpenAi('api-key'); // custom: $instructor = AiInstructor::createForCustomConfig( 'api-key', 'http://localhost:11434/' ); $result = $instructor->instruct( MovieReview::class, 'tinyllama', 'You are an AI bot that comes up with a movie review for a movie made from the description given by the user. It should follow the format given. If you can not come up with a movie review of the description given by the user, then make a review of a random Hollywood movie.', 'I think the Lord of the Rings movie has dated terrible' ); var_dump($result); // dumps a MovieReview instance.
It would give a response like this:
object(MovieReview)#160 (3) {
["name"]=>
string(21) "The Lord of the Rings"
["description"]=>
string(472) "Once a groundbreaking epic, The Lord of the Rings now feels surprisingly outdated. The ambitious scope and Howard Shore’s majestic score still impress, but the early-2000s CGI and practical-effects limitations often pull you out of Middle-earth. Pacing issues and theatrical dialogue that once felt grand now come across as heavy-handed. While die-hard fans may forgive its age, newcomers might struggle to stay immersed in a story weighed down by its own technical era."
["rating"]=>
int(4)
}
Setup with Apie
You can also set it up with the Apie library. In This case you would need to require apie/apie-bundle for Symfony or apie/laravel-apie for Laravel to setup the key and url in the Laravel/Symfony configuration:
apie: ai: base_url: http://localhost:11434 api_key: 'ignored-for-ollama'
It is recommended to use environment variables for the api key: Symfony:
apie: ai: api_key: '%env(AI_API_KEY)%'
Laravel:
// config/apie.php return [ 'ai' => [ 'api_key' => env('AI_API_KEY'), ] ];
apie/ai-instructor 适用场景与选型建议
apie/ai-instructor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 apie/ai-instructor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 apie/ai-instructor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-21