{"id":25972855,"url":"https://github.com/grok-php/client","last_synced_at":"2025-03-05T01:17:47.984Z","repository":{"id":276205025,"uuid":"928578109","full_name":"grok-php/client","owner":"grok-php","description":"Grok PHP Client is a robust and community-driven PHP client library for seamless integration with Grok AI API, offering efficient access to advanced AI and data processing capabilities.","archived":false,"fork":false,"pushed_at":"2025-02-22T15:48:50.000Z","size":739,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-02-22T16:33:53.894Z","etag":null,"topics":["ai-api","ai-chatbot","ai-client","ai-developer-tools","ai-integration","ai-powered-applications","ai-sdk","ai-text-generation","generative-ai","grok-ai","machine-learning","php-ai-sdk","rest-api","x-ai","x-ai-grok","x-ai-grok-2"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/grok-php/client","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grok-php.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"thefeqy"}},"created_at":"2025-02-06T21:32:19.000Z","updated_at":"2025-02-22T15:48:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"d0ed03ec-0151-4c4e-bc61-05ef43393100","html_url":"https://github.com/grok-php/client","commit_stats":null,"previous_names":["grok-php/client"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grok-php%2Fclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grok-php%2Fclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grok-php%2Fclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grok-php%2Fclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grok-php","download_url":"https://codeload.github.com/grok-php/client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241945530,"owners_count":20046870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai-api","ai-chatbot","ai-client","ai-developer-tools","ai-integration","ai-powered-applications","ai-sdk","ai-text-generation","generative-ai","grok-ai","machine-learning","php-ai-sdk","rest-api","x-ai","x-ai-grok","x-ai-grok-2"],"created_at":"2025-03-05T01:17:47.415Z","updated_at":"2025-03-05T01:17:47.971Z","avatar_url":"https://github.com/grok-php.png","language":"PHP","readme":"# 🧠 Grok PHP Client\n\n![Grok PHP Client](assets/images/grok-client.png)\n\n**A lightweight, framework-agnostic PHP client for interacting with Grok AI APIs.**  \nSupports **PHP 8.2+**, built with **OOP best practices**, and **fully type-safe**.\n\n[![Latest Version](https://img.shields.io/packagist/v/grok-php/client)](https://packagist.org/packages/grok-php/client)\n[![PHP Version](https://img.shields.io/badge/PHP-8.2%2B-blue)](https://php.net)\n[![Total Downloads](https://img.shields.io/packagist/dt/grok-php/client)](https://packagist.org/packages/grok-php/client)\n![GitHub Workflow Status](https://github.com/grok-php/client/actions/workflows/run-tests.yml/badge.svg)\n[![License](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE)\n\n---\n\n## 📖 Table of Contents\n- [Features](#features)\n- [Installation](#installation)\n- [Quick Start](#quick-start)\n  - [Basic Usage](#basic-usage)\n  - [Vision Analysis](#vision-analysis-image-recognition)\n  - [Advanced Configuration](#advanced-configuration)\n- [Available Grok AI Models](#available-grok-ai-models)\n- [Streaming Responses](#streaming-responses)\n- [Error Handling](#error-handling)\n- [Testing](#testing)\n- [Security](#security)\n- [Contributing](#contributing)\n- [License](#license)\n\n---\n\n## **Features**\n\n![Grok PHP Client Demo](assets/images/demo.gif)\n\n- **Easy Integration** – Seamlessly connects with Grok AI APIs.  \n- **Modern PHP Features** – Utilizes PHP 8.2+ features like enums and traits.  \n- **Framework Agnostic** – Works with any PHP project, CLI scripts, or web applications.  \n- **Streaming Support** – Built-in support for real-time responses.  \n- **Lightweight \u0026 Efficient** – Optimized with PSR-4 autoloading and minimal dependencies.\n\n---\n\n## **Installation**\n\nInstall via Composer:\n```sh\ncomposer require grok-php/client\n```\n\n### **Requirements:**\n- PHP 8.2+\n- Composer 2.0+\n\n---\n\n## **Quick Start**\n\n### **Basic Usage**\n\n```php\nuse GrokPHP\\Client\\Clients\\GrokClient;\nuse GrokPHP\\Client\\Config\\GrokConfig;\nuse GrokPHP\\Client\\Config\\ChatOptions;\nuse GrokPHP\\Client\\Enums\\Model;\n\n// Initialize the client\n$config = new GrokConfig('your-api-key');\n$client = new GrokClient($config);\n\n// Define messages\n$messages = [\n    ['role' =\u003e 'system', 'content' =\u003e 'You are an AI assistant.'],\n    ['role' =\u003e 'user', 'content' =\u003e 'Tell me a joke!']\n];\n\n// Call API\n$options = new ChatOptions(model: Model::GROK_2, temperature: 0.7, stream: false);\n$response = $client-\u003echat($messages, $options);\n\necho \"AI Response: \" . $response['choices'][0]['message']['content'];\n```\n\n### **Defaults Used:**\n- Model: `grok-2`\n- Temperature: `0.7`\n- Streaming: `false`\n\n---\n\n### Vision Analysis (Image Recognition)\nThe **Vision API** allows you to send images for analysis using **Grok-2-Vision** models.\n\n```php\nuse GrokPHP\\Client\\Clients\\GrokClient;\nuse GrokPHP\\Client\\Config\\GrokConfig;\n\n// Initialize the client\n$config = new GrokConfig('your-api-key');\n$client = new GrokClient($config);\n\n// Use the Vision API to analyze an image\n$response = $client-\u003evision()-\u003eanalyze('https://example.com/image.jpg', 'Describe this image.');\n\necho \"Vision Response: \" . $response['choices'][0]['message']['content'];\n```\n\n#### Supported Models for Vision\n| Model Enum                  | API Model Name       | Description                      |\n|-----------------------------|----------------------|----------------------------------|\n| `Model::GROK_2_VISION`        | grok-2-vision        | Base Vision Model               |\n| `Model::GROK_2_VISION_LATEST` | grok-2-vision-latest | Latest Vision Model             |\n| `Model::GROK_2_VISION_1212`   | grok-2-vision-1212   | Default model for image analysis |\n\n**Note:** If you attempt to use an **unsupported model** for vision, an exception will be thrown.\n\n---\n### **Advanced Configuration**\n\n```php\nuse GrokPHP\\Client\\Clients\\GrokClient;\nuse GrokPHP\\Client\\Config\\GrokConfig;\nuse GrokPHP\\Client\\Config\\ChatOptions;\nuse GrokPHP\\Client\\Enums\\Model;\n\n// Load API key from environment\n$apiKey = getenv('GROK_API_KEY');\n\n$config = new GrokConfig($apiKey);\n$client = new GrokClient($config);\n\n// Define messages\n$messages = [\n    ['role' =\u003e 'system', 'content' =\u003e 'You are a helpful assistant.'],\n    ['role' =\u003e 'user', 'content' =\u003e 'How do black holes form?']\n];\n\n// Custom API settings\n$options = new ChatOptions(\n    model: Model::GROK_2_LATEST,\n    temperature: 1.2, \n    stream: false\n);\n\n$response = $client-\u003echat($messages, $options);\necho \"AI Says: \" . $response['choices'][0]['message']['content'];\n```\n\n---\n\n## **Available Grok AI Models**\n\nGrok AI offers multiple models optimized for different use cases.  \nThese models are available in the `Model` enum inside our package:  \n📄 `src/Enums/Model.php`\n\n| Model Enum                   | API Model Name       | Description                                         |\n|------------------------------|----------------------|-----------------------------------------------------|\n| `Model::GROK_VISION_BETA`     | grok-vision-beta     | Experimental vision-enabled model                   |\n| `Model::GROK_2_VISION`        | grok-2-vision        | Advanced multi-modal vision model                   |\n| `Model::GROK_2_VISION_LATEST` | grok-2-vision-latest | Latest iteration of Grok vision models              |\n| `Model::GROK_2_VISION_1212`   | grok-2-vision-1212   | Enhanced vision model with performance improvements |\n| `Model::GROK_2_1212`          | grok-2-1212          | Optimized chat model                                |\n| `Model::GROK_2`               | grok-2               | Default general-purpose Grok model                  |\n| `Model::GROK_2_LATEST`        | grok-2-latest        | Latest iteration of Grok-2                          |\n| `Model::GROK_BETA`            | grok-beta            | Experimental beta model                             |\n\n#### **Default model used:** `Model::GROK_2`\n\n---\n\n## **Streaming Responses**\n\nThe Grok API supports streaming responses for real-time interaction.  \nEnable it by setting `stream: true`:\n\n```php\n$options = new ChatOptions(model: Model::GROK_2, temperature: 0.7, stream: true);\n$response = $client-\u003echat($messages, $options);\n```\nStreaming can be useful for chatbots, real-time applications, and CLI assistants.\n\n---\n\n## **Error Handling**\n\nThis package includes built-in error handling with a dedicated exception class.  \nCommon errors and their messages:\n\n| Error Type         | HTTP Code | Message |\n|--------------------|----------|-------------------------------------------|\n| `Invalid API Key` | 400      | No API key provided. Specify your API key. |\n| `Invalid Request` | 400      | Client specified an invalid argument. |\n| `Invalid Role`    | 422      | Unknown role variant provided in messages. |\n\nExample of handling exceptions:\n\n```php\nuse GrokPHP\\Client\\Exceptions\\GrokException;\n\ntry {\n    $response = $client-\u003echat($messages, $options);\n} catch (GrokException $e) {\n    echo \"Error: \" . $e-\u003egetMessage();\n}\n```\n\n---\n## **Testing**\n\nTo run PHPUnit tests, you need to set up your API key. Follow these steps:\n\n1. **Copy the default PHPUnit configuration file:**\n```sh\ncp phpunit.xml.dist phpunit.xml\n```\n\n2. **Update the API key in `phpunit.xml`:**\nOpen the file and replace `your-grok-api-key-here` with your actual API key:\n```xml\n\u003cphp\u003e\n   \u003cenv name=\"GROK_API_KEY\" value=\"your-grok-api-key-here\"/\u003e\n\u003c/php\u003e\n```\n\n3. **Obtain an API Key:**  \nIf you don’t have an API key, sign up at [Grok AI](https://x.ai/api/) and create one.\n\n4. **Run the tests with PHPUnit:**\n```sh\ncomposer test\n```\nOr run PHPUnit manually:\n```sh\nvendor/bin/phpunit\n```\n---\n\n## **Security**\n\nIf you discover a security vulnerability, please report it via email:  \n📩 [thefeqy@gmail.com](mailto:thefeqy@gmail.com)\n\n---\n\n## **Contributing**\n\nWant to improve this package? Check out [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines.\n\n---\n\n## **License**\n\nThis package is open-source software licensed under the [MIT License](LICENSE).\n","funding_links":["https://github.com/sponsors/thefeqy"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrok-php%2Fclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrok-php%2Fclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrok-php%2Fclient/lists"}