| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\service\AIGatewayService;
- use think\Db;
- use think\Exception;
- /**
- * 首页接口
- */
- class Index extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- */
- public function index()
- {
- $this->success('请求成功');
- }
- // 向量引擎配置
- private $baseUrl = "https://api.vectorengine.ai/v1";
- private $apiKey = "sk-P877pnXMk2erRS2an7qEa3Kdb3rIb7JVAWZ39lhA8HeN71gZ"; // 从控制台获取
- private $timeout = 120; // 超时时间(秒),视频生成需要更长
- /**
- * 文生图接口
- * POST /api/index/textToImage
- * 参数: prompt (string) 提示词
- */
- public function textToImage()
- {
- $params = $this->request->param();
- if (empty($params)) {
- $this->error('提示词不能为空');
- }
- $data = [
- "model" => "gemini-3-pro-image-preview", // 可替换为 dall-e-3 等
- "prompt" => $params['prompt'],
- "n" => 1,
- "size" => "1024x1024"
- ];
- $result = $this->requestVectorEngine("/images/generations", $data);
- if ($result['code'] === 0) {
- $this->success('生成成功', $result['data']);
- } else {
- $this->error($result['msg'], $result['data']);
- }
- }
- /**
- * 图生文接口
- * POST /api/index/imageToText
- * 参数: image_url (string) 公网图片URL, prompt (string) 提问指令
- */
- public function imageToText()
- {
- $imageUrl = $this->request->post('image_url');
- $prompt = $this->request->post('prompt', '描述这张图片的内容');
- if (empty($imageUrl)) {
- $this->error('图片URL不能为空');
- }
- $data = [
- "model" => "gpt-4-vision-preview",
- "messages" => [
- [
- "role" => "user",
- "content" => [
- ["type" => "text", "text" => $prompt],
- ["type" => "image_url", "image_url" => ["url" => $imageUrl]]
- ]
- ]
- ],
- "max_tokens" => 1000
- ];
- $result = $this->requestVectorEngine("/chat/completions", $data);
- if ($result['code'] === 0) {
- $this->success('识别成功', $result['data']);
- } else {
- $this->error($result['msg'], $result['data']);
- }
- }
- /**
- * 文生视频接口
- * POST /api/index/textToVideo
- * 参数: prompt (string) 提示词, duration (int) 时长(秒), resolution (string) 分辨率
- */
- public function textToVideo()
- {
- $prompt = $this->request->post('prompt');
- $duration = $this->request->post('duration', 5);
- $resolution = $this->request->post('resolution', '720p');
- if (empty($prompt)) {
- $this->error('提示词不能为空');
- }
- $data = [
- "model" => "kling-1.6", // 可替换为 seedance-2.0 等
- "prompt" => $prompt,
- "duration" => (int)$duration,
- "resolution" => $resolution
- ];
- $result = $this->requestVectorEngine("/videos/generations", $data);
- if ($result['code'] === 0) {
- $this->success('生成成功', $result['data']);
- } else {
- $this->error($result['msg'], $result['data']);
- }
- }
- /**
- * 封装向量引擎通用CURL请求
- */
- private function requestVectorEngine($endpoint, $data)
- {
- $url = $this->baseUrl . $endpoint;
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_HTTPHEADER => [
- "Content-Type: application/json",
- "Authorization: Bearer " . $this->apiKey
- ],
- CURLOPT_SSL_VERIFYPEER => false, // 生产环境建议开启
- CURLOPT_TIMEOUT => $this->timeout
- ]);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $error = curl_error($ch);
- curl_close($ch);
- if ($error) {
- return ['code' => -1, 'msg' => '请求失败: ' . $error, 'data' => null];
- }
- if ($httpCode !== 200) {
- return ['code' => $httpCode, 'msg' => 'API返回异常', 'data' => json_decode($response, true)];
- }
- return ['code' => 0, 'msg' => '成功', 'data' => json_decode($response, true)];
- }
- }
|