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-23 17:35:03 +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
public function getResponse()
{
$client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'text/event-stream',
'Cache-Control' => 'no-cache',
'Authorization' => 'Bearer ' . config('services.dashscope.api_key'),
]
]);
$url = config('services.dashscope.api_base') . '/compatible-mode/v1/chat/completions';
$messages = [
[
'role' => 'user',
'content' => "你好",
],
];
$jsonBody = json_encode([
'model' => $this->model,
'messages' => $messages,
'stream' => true
]);
$response = $client->request('POST', $url, [
'body' => $jsonBody,
'stream' => true,
]);
$body = $response->getBody();
// while (!$body->eof()) {
// echo $body->read(1024);
// }
while (!$body->eof()) {
// $body->read(1);
echo $body->read(1);
}
}
2024-07-23 15:22:09 +00:00
}