amber-laravel/app/LLM/Qwen.php

62 lines
1.3 KiB
PHP
Raw Normal View History

2024-07-23 15:22:09 +00:00
<?php
namespace App\LLM;
2024-07-23 17:35:03 +00:00
use GuzzleHttp\Client;
2024-07-23 15:22:09 +00:00
class Qwen extends Base
{
2024-07-24 08:13:16 +00:00
protected string $model = 'qwen-max';
2024-07-23 15:22:09 +00:00
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
2024-07-23 17:35:03 +00:00
2024-07-24 08:13:16 +00:00
public function getResponse(): void
2024-07-23 17:35:03 +00:00
{
$client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'text/event-stream',
'Cache-Control' => 'no-cache',
2024-07-24 08:13:16 +00:00
'Authorization' => 'Bearer '.config('services.dashscope.api_key'),
],
2024-07-23 17:35:03 +00:00
]);
2024-07-24 08:13:16 +00:00
$url = config('services.dashscope.api_base').'/compatible-mode/v1/chat/completions';
2024-07-23 17:35:03 +00:00
$messages = [
[
'role' => 'user',
2024-07-24 08:13:16 +00:00
'content' => '你好',
2024-07-23 17:35:03 +00:00
],
];
$jsonBody = json_encode([
'model' => $this->model,
'messages' => $messages,
2024-07-24 08:13:16 +00:00
'stream' => true,
2024-07-23 17:35:03 +00:00
]);
$response = $client->request('POST', $url, [
'body' => $jsonBody,
'stream' => true,
]);
$body = $response->getBody();
2024-07-24 08:13:16 +00:00
// while (!$body->eof()) {
// echo $body->read(1024);
// }
2024-07-23 17:35:03 +00:00
2024-07-24 08:13:16 +00:00
while (! $body->eof()) {
// $body->read(1);
2024-07-23 17:35:03 +00:00
echo $body->read(1);
}
}
2024-07-23 15:22:09 +00:00
}