ketchalegend / laravel-together-ai
一个用于集成Together AI的Laravel包
v1.6.0
2024-09-08 21:09 UTC
Requires
- php: ^7.3|^8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.0|^10.0
README
本包为您的Laravel应用程序提供与Together AI API的简单集成。
安装
通过Composer安装包
composer require ketchalegend/laravel-together-ai
配置
发布配置文件
php artisan vendor:publish --provider="ketchalegend\LaravelTogetherAI\TogetherAIServiceProvider" --tag="together-ai-config"
然后,将您的Together AI API密钥添加到.env
文件中
TOGETHER_AI_API_KEY=your-api-key-here
使用方法
以下是使用Laravel Together AI包的各种示例
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!'] ] ]);
这些示例演示了
- 基本聊天完成
- 使用自定义参数聊天
- 系统消息和助手消息的多轮对话
- 流式响应
- 函数调用
- 用户消息的简化输入
- 错误处理
请记得在您的应用程序中适当地处理API响应和错误。实际响应的结构可能因Together AI API版本和特定模型而异。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。