Resource.php
5.6 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace App\Helper;
/**
* api结果处理
* @author:dc
* @time 2023/12/16 14:56
* Class Resource
* @package GlobalSo\Tool\Gpt
*/
abstract class Resource {
/**
* 函数
* @var array
*/
protected $func = [];
/**
* 是否是调试模式
* @var bool
*/
public static $debugInfo = false;
/**
* 获取内容body所有内容
* @return string
*/
abstract public function getBody(): string;
/**
* 获取数据的状态
* @return int
*/
abstract public function getCode(): int;
/**
* 验证code 默认200
* @param int $code
* @return bool
* @time 2023/12/16 16:45
*/
public function checkCode(int $code = 200):bool {
return $this->getCode() === $code;
}
/**
* 获取内容 数据
* @return mixed
* @author:dc
* @time 2024/1/2 15:56
*/
abstract public function getData();
/**
* 获取错误消息
* @return string
*/
abstract public function getMessage(): string;
/**
* 是否是函数
* @return bool
* @author:dc
* @time 2024/1/10 12:35
*/
public function isFun(){
if($this->getBodyFunc()){
return true;
}
return false;
}
/**
* @return array|mixed
* @author:dc
* @time 2024/4/9 15:55
*/
public function getBodyFunc(){
$json = @json_decode($this->getBody(),1);
if(is_array($json) && isset($json['tool_calls'])){
return $json['tool_calls'];
}
return [];
}
/**
* 这个是提交了多少token
* @return int
* @author:dc
* @time 2023/12/18 13:37
*/
public function getPromptToken():int{
// 这个是老版本
if(isset($this->getUsage()['prompt_tokens'])){
return (int) ($this->getUsage()['prompt_tokens']??0);
}
// 这个是新版本
$num = 0;
foreach ($this->getUsage() as $item){
$num += (int) ($item['prompt_tokens']??0);
}
return $num;
}
/**
* 这个是吐出了多少token
* @return int
* @author:dc
* @time 2023/12/18 13:37
*/
public function getCompletionToken():int{
// 这个是老版本
if(isset($this->getUsage()['completion_tokens'])){
return (int) ($this->getUsage()['completion_tokens']??0);
}
// 这个是新版本
$num = 0;
foreach ($this->getUsage() as $item){
$num += (int) ($item['completion_tokens']??0);
}
return $num;
}
/**
* 获取函数
* @return array|mixed
*/
public function getFunc($call=null,...$params)
{
if(!$this->func){
$this->func = $this->getBodyFunc();
}
// {"code":200,"func":{"name":"taocan","arguments":{"attribute":"\u7528\u91cf","usetime":"\u4eca\u5929"}}}
$function = $this->func;
if(!empty($this->func) && is_array($this->func)) {
if (!isset($this->func['name'])) {
$this->func = [$function[0]];
}
}
$result = $this->getFuncAll($call,...$params);
$this->func = $function;
return array_values($result)[0]??null;
}
/**
* 获取所有的函数体
* @param null $call
* @param mixed ...$params
* @return array|false|mixed
* @author:dc
* @time 2024/3/11 14:32
*/
public function getFuncAll($call=null,...$params)
{
if(!$this->func){
$this->func = $this->getBodyFunc();
}
// {"code":200,"func":{"name":"taocan","arguments":{"attribute":"\u7528\u91cf","usetime":"\u4eca\u5929"}}}
$function = [];
if(!empty($this->func) && is_array($this->func)){
$function = $this->func;
}
if($call){
if($function){
// 是否是老版本
if(!empty($function['name'])){
// 整理参数
return [$function['name']=> $this->callFunc(
$call,
$function['name'],
$params,
$function['arguments']??[]
)];
}else{
// 循环 函数
$result = [];
foreach ($function as $func){
$result[$func['name']] = $this->callFunc(
$call,
$func['name'],
$params,
$func['arguments']??[]
);
}
return $result;
}
}
return [];
}
return $function;
}
/**
* call 回调
* @param $call
* @param $funcName
* @param $params
* @return false|mixed
* @author:dc
* @time 2024/3/11 11:35
*/
private function callFunc($call,$funcName,$params,$attr) {
$params[] = $attr;
// 匿名函数
if($call instanceof \Closure){
return $call($funcName, ...$params);
}
// 类名称
elseif ($call){
// 定义了类
if(is_object($call) || class_exists($call)){
// 掉用类
return call_user_func(
[$call,$funcName]
,...$params
);
}else
return call_user_func(
[$call,$funcName]
,...$params
);
}
}
}