ketchalegend/laravel-together-ai
最新稳定版本:v1.6.0
Composer 安装命令:
composer require ketchalegend/laravel-together-ai
包简介
A Laravel package for integrating with Together AI
README 文档
README
This package provides an easy integration with the Together AI API for your Laravel application.
Installation
Install the package via Composer:
composer require ketchalegend/laravel-together-ai
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="ketchalegend\LaravelTogetherAI\TogetherAIServiceProvider" --tag="together-ai-config"
Then, add your Together AI API key to your .env file:
TOGETHER_AI_API_KEY=your-api-key-here
Usage
Here are various examples of how to use the Laravel Together AI package:
use ketchalegend\LaravelTogetherAI\Facades\TogetherAI; // Basic chat completion $response = TogetherAI::chat()->create([ 'messages' => [ ['role' => 'user', 'content' => 'Hello, how are you?'] ] ]); // Chat completion with custom parameters $response = TogetherAI::chat()->create([ 'messages' => [ ['role' => 'user', 'content' => 'Explain quantum computing in simple terms.'] ], 'temperature' => 0.8, 'max_tokens' => 150 ]); // Chat with system message and multiple user messages $response = TogetherAI::chat()->create([ 'messages' => [ ['role' => 'system', 'content' => 'You are a helpful assistant specializing in technology.'], ['role' => 'user', 'content' => 'What is cloud computing?'], ['role' => 'assistant', 'content' => 'Cloud computing is a technology that allows users to access and use computing resources over the internet instead of on their local computer.'], ['role' => 'user', 'content' => 'What are its main benefits?'] ] ]); // Streamed chat completion $stream = TogetherAI::chat()->create([ 'messages' => [ ['role' => 'user', 'content' => 'Tell me a short story about a robot.'] ], 'stream' => true ]); foreach ($stream as $chunk) { if (isset($chunk['done']) && $chunk['done']) { echo "Story finished.\n"; break; } echo $chunk['choices'][0]['delta']['content'] ?? ''; } // Chat completion with function calling (if supported by Together AI) $functions = [ [ 'name' => 'get_current_weather', 'description' => 'Get the current weather in a given location', 'parameters' => [ 'type' => 'object', 'properties' => [ 'location' => [ 'type' => 'string', 'description' => 'The city and state, e.g. San Francisco, CA', ], 'unit' => ['type' => 'string', 'enum' => ['celsius', 'fahrenheit']], ], 'required' => ['location'], ], ] ]; $response = TogetherAI::chat()->create([ 'messages' => [ ['role' => 'user', 'content' => 'What\'s the weather like in New York?'] ], 'functions' => $functions, 'function_call' => 'auto' ]); // Handle the response if (isset($response['choices'][0]['function_call'])) { $functionCall = $response['choices'][0]['function_call']; // Process the function call... } else { echo $response['choices'][0]['message']['content']; } // Error handling try { $response = TogetherAI::chat()->create([ 'messages' => [ ['role' => 'user', 'content' => 'Generate a very long response.'] ], 'max_tokens' => 10000 // Assuming this exceeds the API's limit ]); } catch (\Exception $e) { echo "An error occurred: " . $e->getMessage(); } // Using custom headers and base URI $client = TogetherAI::factory() ->withApiKey($newApiKey) ->withBaseUri($newBaseUri) ->withHttpHeader('Custom-Header', 'Custom-Value'); ->make(); $response = $client->chat()->create([ 'messages' => [ ['role' => 'user', 'content' => 'Hello with custom configuration!'] ] ]);
These examples demonstrate:
- Basic chat completion
- Chat with custom parameters
- Multi-turn conversations with system and assistant messages
- Streamed responses
- Function calling
- Simplified input for user messages
- Error handling
Remember to handle API responses and errors appropriately in your application. The actual structure of the response may vary depending on the Together AI API version and the specific model used.
License
The MIT License (MIT). Please see License File for more information.
ketchalegend/laravel-together-ai 适用场景与选型建议
ketchalegend/laravel-together-ai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ketchalegend/laravel-together-ai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ketchalegend/laravel-together-ai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-07