89 lines
1.9 KiB
PHP
89 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
|
||
|
|
class LoveGPTService
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 问题
|
||
|
|
* @var
|
||
|
|
*/
|
||
|
|
private $question;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 会话id
|
||
|
|
* @var
|
||
|
|
*/
|
||
|
|
private $session_id;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 指定模型(可不传) gpt-3.5-turbo | gpt-4
|
||
|
|
* @var
|
||
|
|
*/
|
||
|
|
private $model;
|
||
|
|
const DEFAULT_MODEL = 'gpt-3.5-turbo';//默认模型
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 上下文数据(可不传) json数组 [{"role":"user","content":"在吗?"},{"role":"assistant","content":"在的,我是小恋"}]
|
||
|
|
* @var
|
||
|
|
*/
|
||
|
|
private $context_msg;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 类型(可不传) love 小恋 cherub 小天使
|
||
|
|
* @var
|
||
|
|
*/
|
||
|
|
private $gpt_type;
|
||
|
|
const DEFAULT_GPT_TYPE = 'love';//默认类型
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return mixed
|
||
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
||
|
|
*/
|
||
|
|
public function chat()
|
||
|
|
{
|
||
|
|
$client = new Client(['timeout' => 60]);
|
||
|
|
$data = [
|
||
|
|
'question' => $this->question,
|
||
|
|
'sessionId' => $this->session_id,
|
||
|
|
'model' => $this->model ?: self::DEFAULT_MODEL,
|
||
|
|
'contextMsg' => $this->context_msg,
|
||
|
|
'gptType' => $this->gpt_type ?: self::DEFAULT_GPT_TYPE
|
||
|
|
];
|
||
|
|
$res = $client->request('POST', config('love_gpt.service_url'), ['form_params' => $data]);
|
||
|
|
return json_decode($res->getBody()->getContents(), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setQuestion($value): LoveGPTService
|
||
|
|
{
|
||
|
|
$this->question = $value;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setSessionId($value): LoveGPTService
|
||
|
|
{
|
||
|
|
$this->session_id = $value;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setModel($value): LoveGPTService
|
||
|
|
{
|
||
|
|
$this->model = $value;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setContextMsg($value): LoveGPTService
|
||
|
|
{
|
||
|
|
$this->context_msg = $value;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function setGptType($value): LoveGPTService
|
||
|
|
{
|
||
|
|
$this->gpt_type = $value;
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
}
|