cognesy/instructor-php
Unified LLM API, structured data outputs with LLMs, and agent SDK - in PHP
What it solves
Instructor for PHP 解決了手動解析混亂的 LLM 輸出(例如 JSON 或原始文本)的需求,透過將其轉換為具備型別與驗證功能的 PHP 數據對象。它透過提供一種結構化的方式來提取資訊,並確保數據在應用程式中使用之前符合特定的驗證規則,從而降低了 LLM 整合的脆弱性。
How it works
此函式庫使用「回應模型」(一個帶有型別提示的純 PHP 類別)來定義所需的數據結構。它利用 LLM 的能力——例如原生 JSON 生成、JSON Schema 或工具調用(tool calling)——來強制模型遵循該結構。如果輸出結果未通過驗證(使用 Symfony validation),Instructor 可以自動重新嘗試請求,並向 LLM 提供錯誤細節,以便模型可以自我修正其回應。
Who it’s for
想要在應用程式中整合 LLM,但不想為解析、驗證和錯誤處理編寫大量冗餘代碼的 PHP 開發者,以及需要使用統一 API 在不同 LLM 提供商(如 OpenAI, Anthropic, 或 Ollama)之間切換的開發者。 \n## Highlights
- Structured Extraction: Conversions directly into PHP classes.
- Automated Retries: Self-corrects invalid responses by feeding validation errors back to the LLM.
- Unified Inference: Through the Polyglot component, it provides a single API for 20+ LLM providers.
- Multi-Mode Output: Supports various extraction methods including JSON, JSON Schema, Tool calling, and Markdown-JSON.
- Framework Agnostic: Works standalone or with first-party integrations for Laravel and Symfony.
- Agent SDK: Includes a lightweight SDK for building tool-using agents with state management and lifecycle hooks."},