Resource.php 5.6 KB
<?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
                );
        }
    }





}