封装一个基础的openai请求类
<?php
/**
* Created by PhpStorm
* @Author: wangwin
* @Date: 2024/9/5
* @Time: 15:40
* @Version: 1.0
*/
namespace App\Service\Gpt;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class ChatService
{
protected $client;
protected $apiKey;
protected $baseUri;
protected $model;
/**
* Constructor to initialize the API service.
*
* @param string $apiKey
* @param string $baseUri
*/
public function __construct(string $apiKey, string $baseUri)
{
$this->apiKey = $apiKey;
$this->baseUri = $baseUri;
$this->client = new Client([
'base_uri' => $this->baseUri,
]);
}
/**
* Perform the chat completion request.
*
* @param array $data
* @return array
* @throws \Exception
*/
public function chat(array $data)
{
try {
if ($this->model) {
$data['model'] = $this->model;
}
$response = $this->client->post('chat/completions', [
'headers' => [
'Authorization' => 'Bearer '.$this->apiKey,
'Content-Type' => 'application/json',
],
'json' => $data,
]);
return json_decode($response->getBody()->getContents(), true);
} catch (RequestException $e) {
throw new \Exception('Request failed: '.$e->getMessage());
}
}
/**
* Set the API key dynamically.
*
* @param string $apiKey
* @return $this
*/
public function setApiKey(string $apiKey): self
{
$this->apiKey = $apiKey;
return $this;
}
/**
* Set a new base URI for the API client.
*
* @param string $baseUri
* @return $this
*/
public function setBaseUri(string $baseUri): self
{
$this->baseUri = $baseUri;
$this->client = new Client([
'base_uri' => $this->baseUri,
]);
return $this;
}
/**
* Send a post request using cURL as a fallback.
*
* @param string $url
* @param array $postData
* @param array $headers
* @return mixed
* @throws \Exception
*/
public function sendPostRequest($url, $postData, $headers = [])
{
$ch = curl_init();
$jsonData = json_encode($postData);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new \Exception("cURL Error: $error");
}
curl_close($ch);
return json_decode($response, true);
}
/*php artisan make:command ProcessTransaction
*
* 实例化
* @param string $apiKey
* @param string $baseUri
* @return static
*/
public static function create(string $apiKey = '7dcae406-5eea-4ab8-b6aa-191d50288c57', string $baseUri = 'https://ark.cn-beijing.volces.com/api/v3/'): self
{
return new self($apiKey, $baseUri);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
在线编辑 (opens new window)
上次更新: 2025/02/25, 18:30:54