• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

php使用chatGPT生成东西做记录

武飞扬头像
小黑雷
帮助1

好久没写了,这么长时间都去坐一些自己感兴趣的事情去了。

之前使用chatgpt-3,效果一直不咋好,这里我们来说说各个版本区别

gpt-3收费成本可以接受,生成的内容对话有点不太聪明的样子

git-3.5-turbo收费相对来说低,生成文本质量还是蛮高的,虽然有可能存在一点废话,但是不影响

git-4对不起用不起哈,等模型训练会不会下带升级之后这个收费较低我在说吧

php调用对话接口

https://api.openai.com/v1/chat/completions

各种三个demo测试

$msg是你需要传入的对话

第二段是屏蔽部分首次进入加入

  1.  
    $data = array(
  2.  
    'model' => 'gpt-3.5-turbo',
  3.  
    'messages' => [
  4.  
    ['role' => 'user', 'content' => $msg],
  5.  
    ],
  6.  
    // 'messages' => array(
  7.  
    // array('role' => 'system', 'content' => '你好,有什么可以帮助您'),
  8.  
    // array('role' => 'user', 'content' => '生成介绍API文章')
  9.  
    // ),
  10.  
    //'max_tokens' => 1000,
  11.  
    // 'model' => 'gpt-3.5-turbo',
  12.  
    //'prompt' => '生成关于ai绘画介绍的详细文章,讲述所涉及到知识点',
  13.  
    );

再就是curl

请求了这种直接使用chatGPT生成的代码改动一下就可以

  1.  
    $ch = curl_init($url);
  2.  
    curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
  3.  
     
  4.  
    curl_setopt($ch, CURLOPT_PROXY, env('gpt.proxy')); //代理服务器地址
  5.  
     
  6.  
    curl_setopt($ch, CURLOPT_PROXYPORT, env('gpt.proxyport')); //代理服务器端口
  7.  
    if(!empty(env('gpt.proxyuserpwd'))){
  8.  
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, env('gpt.proxyuserpwd')); //http代理认证帐号,username:password的格式
  9.  
    }
  10.  
     
  11.  
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
  12.  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  13.  
    curl_setopt($ch, CURLOPT_POST, true);
  14.  
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  15.  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  16.  
    'Content-Type: application/json',
  17.  
    'Authorization: Bearer ' . $apiKey
  18.  
    ));
  19.  
     
  20.  
    $result = curl_exec($ch);
  21.  
     
  22.  
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  23.  
     
  24.  
     
  25.  
    curl_close($ch);
  26.  
    if ($responseCode == 200 || $result === false) {
  27.  
    $response = json_decode($result, true);
  28.  
    // dd($response);
  29.  
    Log::info("chat GPT:---------------------------");
  30.  
    Log::info("内容:".$result);
  31.  
    // Log::info("status:".$response['choices'][0]['finish_reason']);
  32.  
    // print_r($response['choices']);
  33.  
     
  34.  
    //Log::info(htmlspecialchars($html));
  35.  
    return $response;
  36.  
    //$generatedText = $response['choices'][0]['text'];
  37.  
    // print_r($generatedText);
  38.  
    } else {
  39.  
    $response = json_decode($result, true);
  40.  
    if(is_array($response)){
  41.  
    return "Error sending request: " . $result;
  42.  
    return $response['error']['type'].":".$response['error']['message'];
  43.  
    }else{
  44.  
    return "Error sending request: " . curl_error($ch);
  45.  
    }
  46.  
    // 处理请求错误
  47.  
     
  48.  
    }
学新通

当然各种不同接口类型返回数据格式不一样看自己需求

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbhegc
系列文章
更多 icon
同类精品
更多 icon
继续加载