php调用chatgpt的gpt-3.5-turbo-0613的functions笔记

php调用chatgpt的gpt-3.5-turbo-0613的functions笔记

最近,OpenAI更新了ChatGPT,将GPT-3.5 Turbo模型更新为GPT-3.5 Turbo-0613,支持最长16k的tokens,这是一个非常给力的更新。这意味着AI的能力正在不断增强。以前,AI只能写代码片段,理解短篇PDF文档等任务。但是,随着tokens数量的增加,未来AI将能够一次性读取长篇小说、大型项目的源码,并对其进行修改和总结等操作。

800_auto

此外,OpenAI还增加了一个functions的调用功能。通过这个功能,用户可以将prompt和函数的参数及返回结果等信息发送给ChatGPT,让它帮你理解函数的参数并直接调用函数。这个功能非常实用,将改变应用的使用方式。以前,执行用户的指令需要理解用户的意图,容易出错。现在,通过ChatGPT生成函数的参数,执行指令变得更加容易。

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/chat/completions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$_postobj = array("model" => "gpt-3.5-turbo-0613",
    "messages" => array(["role" => "user", "content" => "东京的天气如何?"]
  
    ),
    "functions" => array(
        array(
            "name" => "get_current_weather",
            "description" => "",
            "parameters" => array(
                "type" => "object",
                "properties" => array(
                    "location" => array(
                        "type" => "string",
                        "description" => "城市或省份,例如北京、上海"
                    ),
                    "unit" => array(
                        "type" => "string",
                        "enum" => array("celsius", "fahrenheit")
                    )
                ),
                "required" => array("location")
            )
        )
    )
);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($_postobj, JSON_UNESCAPED_UNICODE));
$certificate = "/data/wwwroot/default/asset/cacert.pem";
curl_setopt($ch, CURLOPT_CAINFO, $certificate);
curl_setopt($ch, CURLOPT_CAPATH, $certificate);
// curl_setopt($ch, CURLOPT_SSLVERSION, 3);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);

// Set the API key as an HTTP header
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer apikey";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send the request and parse the response
$response = curl_exec($ch);
$respons...

点击查看剩余70%

{{collectdata}}

网友评论