AiCommandService.php 1.5 KB
<?php
namespace App\Services;

/**
 * https://gemini.google.com/app
 * Class AiCommandService
 * @package App\Services
 */
class AiCommandService
{
    public $url = 'https://api.cmer.com/v2/chat';

    public $api_key = 'nnLsyr3IhPNsJt5OvTtD9SVCLEixMntg';

    /**
     * @var string gemini 模型
     *
     * gemini-2.0-flash-lite
     * gemini-2.5-pro-preview-05-06
     * gemini-2.5-flash-preview-05-20
     * gemini-2.5-flash-preview-04-17
     */
    public $model = 'gemini-2.5-pro-preview-05-06';

    public $supplier = 'google';

    /**
     * AI排版
     * @param $content
     * @return array
     */
    public function send_layout_design($content){
        $param = [
            'messages'=>[
                ['content'=>$content, 'role'=>'user'],
            ],
            'model'=> $this->model,
            'supplier'=> $this->supplier,
            'security_check'=> false
        ];
        $header = array(
            "Accept: application/json",
            "X-CmerApi-Host: llm-chat.p.cmer.com",
            "apikey: $this->api_key",
            "Content-Type:application/json;charset=utf-8",
        );
        $result = http_post($this->url,json_encode($param,true),$header);
        $data = $result['content'][0]['data'] ?? '';
        $data = trim($data, "\"\n");
        $data = preg_replace('/^```html\s*/', '', $data); // 去除开头
        $data = preg_replace('/\s*```$/', '', $data);     // 去除结尾
        $data = str_replace("\\n", "\n", $data);
        $data = str_replace('\"', '"', $data);
        return ['data'=>$data];
    }

}