快速入门 PHP 注意 1. php版本要求8.3.13以上; 2. 安装composer组件; 运行以下命令: composer require openaiphp/client composer dumpautoload 文本生成 java withApiKey($appkey) >withBaseUri($baseUrl) >make(); $response $client>chat()>create([ 'model' > $modelId, 'messages' > [ ['role' > 'user', 'content' > $prompt], ], ]); foreach ($response>choices as $result) { echo $result>message>content; } ?> 图像理解 java $modelId, 'messages' > [ [ 'role' > 'user', 'content' > [ [ 'type' > 'text', 'text' > '描述下这张图' ], [ 'type' > 'imageurl', 'imageurl' > [ 'url' > ' ] ] ] ] ], 'streamoptions' > [ 'includeusage' > true ], 'paralleltoolcalls' > true, 'stream' > false ]; // 初始化 cURL $ch curlinit(); // 设置 cURL 选项 curlsetopt($ch, CURLOPTURL, $url); curlsetopt($ch, CURLOPTRETURNTRANSFER, true); curlsetopt($ch, CURLOPTPOST, true); curlsetopt($ch, CURLOPTPOSTFIELDS, jsonencode($payload)); curlsetopt($ch, CURLOPTHTTPHEADER, [ 'Authorization: Bearer ' . $apiKey, 'ContentType: application/json' ]); // 禁用 SSL 验证 (仅用于开发环境) curlsetopt($ch, CURLOPTSSLVERIFYPEER, false); curlsetopt($ch, CURLOPTSSLVERIFYHOST, false); // 执行请求 $response curlexec($ch); $httpCode curlgetinfo($ch, CURLINFOHTTPCODE); // 检查错误 if (curlerrno($ch)) { echo '请求失败: ' . curlerror($ch); } else { if ($httpCode > 200 && $httpCode Image文本生图 java 'xirangmodelid', // 替换为实际的modelId 'prompt' > $prompt, 'n' > 1, 'size' > $size ]; // 使用 cURL 发送请求 $ch curlinit(); curlsetopt($ch, CURLOPTURL, $url); curlsetopt($ch, CURLOPTPOST, true); curlsetopt($ch, CURLOPTRETURNTRANSFER, true); curlsetopt($ch, CURLOPTHTTPHEADER, [ 'Authorization: Bearer ' . $apiKey, 'ContentType: application/json' ]); curlsetopt($ch, CURLOPTPOSTFIELDS, jsonencode($data)); // 关闭 SSL 验证 (仅用于测试环境) curlsetopt($ch, CURLOPTSSLVERIFYPEER, false); curlsetopt($ch, CURLOPTSSLVERIFYHOST, false); $response curlexec($ch); // 检查 cURL 错误 if (curlerrno($ch)) { $errorMsg curlerror($ch); curlclose($ch); throw new Exception("cURL请求失败: " . $errorMsg); } $httpCode curlgetinfo($ch, CURLINFOHTTPCODE); curlclose($ch); // 检查HTTP状态码 if ($httpCode > 400) { throw new Exception("API返回错误状态码: $httpCode"); } $responseData jsondecode($response, true); // 检查API错误 if (isset($responseData['error'])) { throw new Exception('API错误: ' . $responseData['error']['message']); } if (empty($responseData['data'][0]['b64json'])) { throw new Exception('未找到图像数据'); } // 获取Base64图像数据 $imageData base64decode($responseData['data'][0]['b64json']); // 创建安全文件名 $safePrompt pregreplace('/[^azAZ09]/', '', substr($prompt, 0, 50)); $filename $safePrompt . '' . time() . '.png'; $filepath $outputDir . '/' . $filename; // 保存图像 if (!fileputcontents($filepath, $imageData)) { throw new Exception("保存文件失败: $filepath"); } return $filepath; } // 使用示例 try { $apiKey 'yourappkey'; // 替换为实际的app key $imagePath generateImage('一只戴帽子的可爱小海獭', $apiKey); echo "✅ 图像已保存至: $imagePathn"; } catch (Exception $e) { echo "❌ 错误: " . $e>getMessage() . "n"; exit(1); } embedding文本向量化 java withApiKey($appkey) >withBaseUri($baseUrl) >make(); // 调用Embedding API $response $client>embeddings()>create([ 'model' > $modelId, 'input' > $text, // 支持字符串或字符串数组 ]); // 处理结果 $embedding $response>embeddings[0]>embedding; // 获取第一个文本的嵌入向量 $tokenUsage $response>usage>totalTokens; // 获取使用的令牌数 // 打印结果 echo "嵌入向量维度: " . count($embedding) . "n"; echo "前5个向量值: n"; printr(arrayslice($embedding, 0, 5)); // 显示前5个值 echo "n使用令牌数: " . $tokenUsage . "n"; ?> rerank 重排序 java 'Python教程', 'documents' > ['Python基础语法', 'Python高级特性', 'Java编程入门'], 'model' > $modelId, 'topn' > 2 ]; // 准备请求 $options [ 'http' > [ 'method' > 'POST', 'header' > "Authorization: Bearer $apiKeyrn" . "ContentType: application/jsonrn", 'content' > jsonencode($data), 'timeout' > 10 // 超时时间 ] ]; // 发送请求 $context streamcontextcreate($options); $response filegetcontents($apiUrl, false, $context); if ($response false) { die("请求失败"); } // 解析结果 $result jsondecode($response, true); // 打印结果 echo "查询: '{$data['query']}'n"; echo "返回结果:n"; if (isset($result['results'])) { foreach ($result['results'] as $i > $item) { $docIndex $item['index']; $docText $data['documents'][$docIndex]; printf("%d. [得分: %.4f] %sn", $i + 1, $item['relevancescore'], $docText ); } } else { echo "无效的响应格式n"; printr($result); } ?>