shelfwood/lmstudio-php
Composer 安装命令:
composer require shelfwood/lmstudio-php
包简介
A PHP package for integrating with LMStudio's local API
README 文档
README
A PHP client for interacting with LMStudio and OpenAI-compatible APIs.
Installation
composer require shelfwood/lmstudio
Basic Usage
<?php require 'vendor/autoload.php'; use Shelfwood\LMStudio\LMStudioFactory; // Assuming LMSTUDIO_API_BASE is set in your environment or .env $factory = new LMStudioFactory(); // --- Non-Streaming Chat --- $conversation = $factory->createConversation('your-model-id'); $conversation->addSystemMessage('You are helpful.'); $conversation->addUserMessage('What is the capital of Spain?'); $response = $conversation->getResponse(); // Handles the API call echo "Assistant: {$response}\n"; // --- Streaming Chat --- $streamingConversation = $factory->createStreamingConversation('your-model-id'); $streamingConversation->addSystemMessage('You are helpful.'); $streamingConversation->addUserMessage('Explain black holes simply.'); // Optional: Add listeners to observe the stream $handler = $streamingConversation->streamingHandler; if ($handler) { $handler->on('stream_content', fn(string $content) => print($content)); $handler->on('stream_end', fn() => print("\n")); } // handleStreamingTurn manages the entire process $finalResponse = $streamingConversation->handleStreamingTurn(); echo "\nFinal Response (after stream): {$finalResponse}\n";
Usage with Tools
Tools allow the model to call predefined PHP functions.
<?php require 'vendor/autoload.php'; use Shelfwood\LMStudio\LMStudioFactory; $factory = new LMStudioFactory(); // --- Non-Streaming Chat with Tools --- $conversationWithTools = $factory->createConversation('your-model-id'); // Register tools (manually or load from config service) $toolRegistry = $conversationWithTools->toolRegistry; $toolRegistry->registerTool( 'calculate', fn(string $expression) => ['result' => eval("return {$expression};")], // Basic eval - CAUTION in production! [ 'type' => 'object', 'properties' => [ 'expression' => ['type' => 'string', 'description' => 'Math expression'] ], 'required' => ['expression'] ], 'Calculate a math expression.' ); $conversationWithTools->addUserMessage('What is 5 * 8?'); // getResponse handles the tool call automatically if the model decides to use it $responseWithTools = $conversationWithTools->getResponse(); echo "Assistant (Tools): {$responseWithTools}\n"; // --- Streaming Chat with Tools --- $streamingConvWithTools = $factory->createStreamingConversation('your-model-id'); // Register tools (same as above) $toolRegistryStream = $streamingConvWithTools->toolRegistry; $toolRegistryStream->registerTool('calculate', /* ... same definition ... */ ); $streamingConvWithTools->addUserMessage('Calculate 100 / 4 please.'); // Optional: Listen for tool events $handlerTools = $streamingConvWithTools->streamingHandler; if ($handlerTools) { $handlerTools->on('stream_content', fn(string $content) => print($content)); $handlerTools->on('stream_tool_call', fn($toolCall) => print("\n[Tool Call: {$toolCall->name}]\n")); $handlerTools->on('stream_end', fn() => print("\n")); } $streamingConvWithTools->eventHandler->on('tool_executed', fn($id, $name, $result) => print("[Tool Executed: {$name} -> ".json_encode($result)."]\n")); // handleStreamingTurn manages the stream, tool execution, and final response $finalResponseToolsStream = $streamingConvWithTools->handleStreamingTurn(); echo "\nFinal Response (Tools, Stream): {$finalResponseToolsStream}\n";
Console Commands
The library provides commands for interacting with the API via the console (useful for testing and demonstration).
Setup (if using Laravel):
- Publish the configuration:
php artisan vendor:publish --tag="lmstudio-config" - Set your
LMSTUDIO_API_BASEand optionallyLMSTUDIO_DEFAULT_MODELin your.envfile.
Available Commands:
php <your_console_runner> sequence: Runs a predefined sequence of API calls (list models, chat, streaming chat, tools) to demonstrate functionality.--model=<model_id>: Specify a model to use.
php <your_console_runner> chat: Starts an interactive, non-streaming chat session in your terminal. Supports tool use.--model=<model_id>: Specify a model to use.--system="Your prompt": Set a custom system prompt.- Type
/quitto exit the chat.
Replace <your_console_runner> with artisan for Laravel or your specific entry point (e.g., bin/lmstudio if you set up a custom runner).
License
MIT
shelfwood/lmstudio-php 适用场景与选型建议
shelfwood/lmstudio-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 shelfwood/lmstudio-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shelfwood/lmstudio-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 62
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-24