### Instructor PHP: Sync and Streaming Support Source: https://docs.instructorphp.com/index Instructor PHP supports both synchronous (blocking) and streaming (non-blocking) responses. Streaming allows for receiving partial updates as they are generated, improving user experience for longer-running AI tasks. ```php chat->completions->create([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "Write a short story."] ], "response_model" => Story::class ]); echo "Synchronous Story:\n" . $response->story; // Streaming response $stream = $client->chat->completions->stream([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "Write a short story."] ], "response_model" => Story::class ]); echo "Streaming Story:\n"; foreach ($stream as $response) { foreach ($response->choices as $choice) { echo $choice->delta->story ?? ''; } } ?> ``` -------------------------------- ### Instructor PHP: Multiple LLM/API Providers Source: https://docs.instructorphp.com/index Integrate with a wide array of LLM API providers, including OpenAI, Anthropic, Cohere, Azure, Groq, Mistral, Fireworks AI, Ollama, OpenRouter, and Together AI. It also supports using local models via Ollama. ```php "YOUR_AZURE_API_KEY", "endpoint" => "YOUR_AZURE_ENDPOINT", "deployment_id" => "YOUR_AZURE_DEPLOYMENT_ID" ]); // Example: Using Ollama (local models) $client = Ollama::client([ "host" => "http://localhost:11434", "model" => "llama3" ]); // Example: Using Groq $client = Groq::client("YOUR_GROQ_API_KEY"); // Example: Using Mistral AI $client = MistralAI::client("YOUR_MISTRAL_API_KEY"); // Example: Using Fireworks AI $client = FireworksAI::client("YOUR_FIREWORKS_AI_API_KEY"); // Example: Using OpenRouter $client = OpenRouter::client("YOUR_OPENROUTER_API_KEY"); // Example: Using Together AI $client = TogetherAI::client("YOUR_TOGETHER_AI_API_KEY"); // Example: Using Cohere $client = Cohere::client("YOUR_COHERE_API_KEY"); // All clients can be used with response_model for structured output $response = $client->chat->completions->create([ "model" => "some-model-name", "messages" => [ ["role" => "user", "content" => "Hello!"] ], "response_model" => Greeting::class ]); echo $response->greeting; ?> ``` -------------------------------- ### Instructor PHP: Customizable Outputs Source: https://docs.instructorphp.com/index Define your response data models using type-hinted classes, JSON Schema arrays, or dynamically with Structures. Customize prompts, retry prompts, and leverage attributes or PHP DocBlocks for LLM instructions. Implement custom interfaces for schema, deserialization, and transformation. ```php 'object', 'properties' => [ 'name' => ['type' => 'string'], 'age' => ['type' => 'integer'] ], 'required' => ['name', 'age'] ]; // Using Attributes for instructions #[Attribute] class ValidateWithSchema implements SchemaContract { public function getSchema(): array { return $schema; } public function getDeserializer(): ?ResponseDeserializer { return null; } public function getValidator(): ?ValidatorContract { return null; } } // Example usage with custom schema #[ValidateWithSchema] class UserProfile { public string $name; public int $age; } $response = $client->chat->completions->create([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "Create a user profile for John Doe, age 30."] ], "response_model" => UserProfile::class ]); echo "User: " . $response->name . ", Age: " . $response->age; // Customizing prompts and retries $response = $client->chat->completions->create([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "Extract the company name."] ], "response_model" => CompanyName::class, "max_retries" => 3, "retry_prompt" => "Please try again, ensuring the output is only the company name." ]); ?> ``` -------------------------------- ### Instructor PHP: Observability Source: https://docs.instructorphp.com/index Gain detailed insight into the internal processing of the Instructor PHP library through its event system. This allows for monitoring, debugging, and understanding the flow of requests and responses. ```php on('response_received', function (array $eventData) { // $eventData might contain details like: // 'request_id', 'model', 'response_time', 'status_code', 'output_tokens', 'input_tokens' echo "Received response for request ID: " . $eventData['request_id'] . "\n"; // Log or process event data as needed }); // Registering an event listener for 'retry_attempt' $client->on('retry_attempt', function (array $eventData) { // $eventData might contain details like: // 'request_id', 'attempt_number', 'error_message' echo "Retrying request ID: " . $eventData['request_id'] . " (Attempt: " . $eventData['attempt_number'] . ")\n"; }); // Triggering events by making a call $response = $client->chat->completions->create([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "What is the weather today?"] ], "response_model" => Weather::class ]); // The registered listeners will be called automatically during the process. ?> ``` -------------------------------- ### Instructor PHP: Flexible Input Processing Source: https://docs.instructorphp.com/index Instructor PHP supports processing various input data types, including plain text, series of chat messages, and images. It facilitates 'structured-to-structured' processing, allowing users to provide object or array inputs and receive structured inference results. ```php chat->completions->create([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "Explain the concept of recursion."] ], "response_model" => MyExplanation::class ]); echo $response->explanation; // Example: Processing chat messages $response = $client->chat->completions->create([ "model" => "gpt-4", "messages" => [ ["role" => "user", "content" => "What is the capital of France?"], ["role" => "assistant", "content" => "The capital of France is Paris."] ], "response_model" => Location::class ]); echo $response->city; // Example: Processing image input (requires specific model support) // $response = $client->chat->completions->create([ // "model" => "gpt-4-vision-preview", // "messages" => [ // ["role" => "user", "content" => [ // ["type" => "text", "text" => "Describe this image."], // ["type" => "image_url", "image_url" => ["url" => "https://example.com/image.jpg"]] // ]] // ], // "response_model" => ImageDescription::class // ]); // echo $response->description; ?> ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.